about summary refs log tree commit diff
path: root/converter/other/giftopnm.c
blob: a79a0bbea6b2d0dfacff237eccb57520dbe1a62f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
/* +-------------------------------------------------------------------+ */
/* | Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)    | */
/* |   Permission to use, copy, modify, and distribute this software   | */
/* |   and its documentation for any purpose and without fee is hereby | */
/* |   granted, provided that the above copyright notice appear in all | */
/* |   copies and that both that copyright notice and this permission  | */
/* |   notice appear in supporting documentation.  This software is    | */
/* |   provided "as is" without express or implied warranty.           | */
/* +-------------------------------------------------------------------+ */

/* There is a copy of the GIF89 specification, as defined by its
   inventor, Compuserve, in 1989, at http://members.aol.com/royalef/gif89a.txt

   This covers the high level format, but does not cover how the "data"
   contents of a GIF image represent the raster of color table indices.
   An appendix describes extensions to Lempel-Ziv that GIF makes (variable
   length compression codes and the clear and end codes), but does not
   describe the Lempel-Ziv base.
*/

#define _BSD_SOURCE   /* Make sure strcasecmp() is in string.h */

#include <string.h>
#include <assert.h>

#include "mallocvar.h"
#include "nstring.h"
#include "shhopt.h"
#include "pnm.h"

#define GIFMAXVAL 255
#define MAXCOLORMAPSIZE 256

#define CM_RED 0
#define CM_GRN 1
#define CM_BLU 2

#define MAX_LZW_BITS  12

#define INTERLACE      0x40
#define LOCALCOLORMAP  0x80
#define BitSet(byte, bit)      (((byte) & (bit)) == (bit))

static __inline__ bool
ReadOK(FILE *          const fileP,
       unsigned char * const buffer,
       size_t          const len) {

    size_t bytesRead;

    bytesRead = fread(buffer, len, 1, fileP);

    return (bytesRead != 0);
}



static void
readFile(FILE *          const ifP,
         unsigned char * const buffer,
         size_t          const len,
         const char **   const errorP) {

    size_t bytesRead;

    bytesRead = fread(buffer, len, 1, ifP);

    if (bytesRead == len)
        *errorP = NULL;
    else {
        if (ferror(ifP))
            asprintfN(errorP, "Error reading file.  errno=%d (%s)",
                      errno, strerror(errno));
        else if (feof(ifP))
            asprintfN(errorP, "End of file encountered");
        else
            asprintfN(errorP, "Short read -- %u bytes of %u", bytesRead, len);
    }
}



#define LM_to_uint(a,b)                        (((b)<<8)|(a))

static int const maxnum_lzwCode = (1<<MAX_LZW_BITS);

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * input_filespec;  /* Filespecs of input files */
    unsigned int verbose;    /* -verbose option */
    unsigned int comments;   /* -comments option */
    bool all_images;  /* He wants all the images */
    unsigned int image_no;
        /* image number he wants from input, starting at 0.  Undefined
           if all_images is TRUE
        */
    const char * alpha_filename;
    unsigned int quitearly;
    unsigned int repair;
};



static void
parseCommandLine(int argc, char ** argv,
                 struct cmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to optParseOptions3 on how to parse our options.
         */
    
    optStruct3 opt;

    unsigned int alphaSpec, imageSpec;
    const char * image;

    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);
    
    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "verbose",     OPT_FLAG, NULL, 
            &cmdlineP->verbose,         0);
    OPTENT3(0, "comments",    OPT_FLAG, NULL,
            &cmdlineP->comments,        0);
    OPTENT3(0, "quitearly",   OPT_FLAG, NULL,
            &cmdlineP->quitearly,       0);
    OPTENT3(0, "repair",      OPT_FLAG, NULL,
            &cmdlineP->repair,          0);
    OPTENT3(0, "image",       OPT_STRING, &image,
            &imageSpec,                 0);
    OPTENT3(0, "alphaout",    OPT_STRING, &cmdlineP->alpha_filename, 
            &alphaSpec,                 0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;  /* We have no parms that are negative numbers */

    optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (!imageSpec) {
        cmdlineP->image_no = 0;
        cmdlineP->all_images = FALSE;
    } else {
        if (strcasecmp(image, "all") == 0) 
            cmdlineP->all_images = TRUE;
        else {
            char * tailptr;

            long const imageNo = strtol(image, &tailptr, 10);

            if (*tailptr != '\0')
                pm_error("Invalid value for '-image' option.  Must be either "
                         "a number or 'all'.  You specified '%s'", image);
            else if (imageNo < 0)
                pm_error("Invalid value for '-image' option.  Must be "
                         "positive.  You specified %ld", imageNo);
            else if (imageNo == 0)
                pm_error("Invalid value for 'image' option.  You specified "
                         "zero.  The first image is 1.");

            cmdlineP->all_images = FALSE;
            cmdlineP->image_no = (unsigned int) imageNo - 1;
        }
    }
    
    if (argc-1 == 0) 
        cmdlineP->input_filespec = "-";
    else if (argc-1 != 1)
        pm_error("Program takes zero or one argument (filename).  You "
                 "specified %d", argc-1);
    else
        cmdlineP->input_filespec = argv[1];

    if (!alphaSpec) 
        cmdlineP->alpha_filename = NULL;
}


typedef unsigned char gifColorMap[3][MAXCOLORMAPSIZE];

struct gifScreen {
    unsigned int    Width;
    unsigned int    Height;
    gifColorMap     ColorMap;
    unsigned int    ColorMapSize;
        /* Number of colors in the color map. */
    unsigned int    ColorResolution;
    unsigned int    Background;
    unsigned int    AspectRatio;
        /* Aspect ratio of each pixel, times 64, minus 15.  (i.e. 1 => 1:4).
           But Zero means 1:1.
        */
    int      hasGray;  
        /* Boolean: global colormap has at least one gray color
           (not counting black and white) 
        */
    int      hasColor;
        /* Boolean: global colormap has at least one non-gray,
           non-black, non-white color 
        */
};

struct gif89 {
       int     transparent;
       int     delayTime;
       int     inputFlag;
       int     disposal;
};

static void
initGif89(struct gif89 * const gif89P) {
    gif89P->transparent = -1;
    gif89P->delayTime = -1;
    gif89P->inputFlag = -1;
    gif89P->disposal = -1;
}       


static int verbose;
int    showComment;



static void
readColorMap(FILE *ifP, const int colormapsize, 
             unsigned char colormap[3][MAXCOLORMAPSIZE],
             int *hasGrayP, int * const hasColorP) {

    int             i;
    unsigned char   rgb[3];

    assert(colormapsize <= MAXCOLORMAPSIZE);

    *hasGrayP = FALSE;  /* initial assumption */
    *hasColorP = FALSE;  /* initial assumption */

    for (i = 0; i < colormapsize; ++i) {
        if (! ReadOK(ifP, rgb, sizeof(rgb)))
            pm_error("Unable to read Color %d from colormap", i);

        colormap[CM_RED][i] = rgb[0] ;
        colormap[CM_GRN][i] = rgb[1] ;
        colormap[CM_BLU][i] = rgb[2] ;

        if (rgb[0] == rgb[1] && rgb[1] == rgb[2]) {
            if (rgb[0] != 0 && rgb[0] != GIFMAXVAL)
                *hasGrayP = TRUE;
        } else
            *hasColorP = TRUE;
    }
}



static bool zeroDataBlock = FALSE;
    /* the most recently read DataBlock was an EOD marker, i.e. had
       zero length
    */

static void
getDataBlock(FILE *          const ifP, 
             unsigned char * const buf, 
             bool *          const eofP,
             unsigned int *  const lengthP,
             const char **   const errorP) {
/*----------------------------------------------------------------------------
   Read a DataBlock from file 'ifP', return it at 'buf'.

   The first byte of the datablock is the length, in pure binary, of the
   rest of the datablock.  We return the data portion (not the length byte)
   of the datablock at 'buf', and its length as *lengthP.

   Except that if we hit EOF or have an I/O error reading the first
   byte (size field) of the DataBlock, we return *eofP == TRUE and
   *lengthP == 0.

   We return *eofP == FALSE if we don't hit EOF or have an I/O error.

   If we hit EOF or have an I/O error reading the data portion of the
   DataBlock, we exit the program with pm_error().
-----------------------------------------------------------------------------*/
    unsigned char count;
    bool successfulRead;
    
    long const pos = ftell(ifP);
    successfulRead = ReadOK(ifP, &count, 1);
    if (!successfulRead) {
        pm_message("EOF or error in reading DataBlock size from file" );
        *errorP = FALSE;
        *eofP = TRUE;
        *lengthP = 0;
    } else {
        if (verbose)
            pm_message("%d byte block at Position %ld", count, pos);
        *eofP = FALSE;
        *lengthP = count;

        if (count == 0) {
            *errorP = NULL;
            zeroDataBlock = TRUE;
        } else {
            bool successfulRead;

            zeroDataBlock = FALSE;
            successfulRead = ReadOK(ifP, buf, count); 

            if (successfulRead) 
                *errorP = NULL;
            else
                asprintfN(errorP,
                          "EOF or error reading data portion of %u byte "
                          "DataBlock from file", count);
        }
    }
}



static void
readThroughEod(FILE * const ifP) {
/*----------------------------------------------------------------------------
  Read the file 'ifP' through the next EOD marker.  An EOD marker is a
  a zero length data block.

  If there is no EOD marker between the present file position and EOF,
  we read to EOF and issue warning message about a missing EOD marker.
-----------------------------------------------------------------------------*/
    unsigned char buf[260];
    bool eod;

    eod = FALSE;  /* initial value */
    while (!eod) {
        bool eof;
        unsigned int count;
        const char * error;

        getDataBlock(ifP, buf, &eof, &count, &error);
        if (error || eof)
            pm_message("EOF encountered before EOD marker.  The GIF "
                       "file is malformed, but we are proceeding "
                       "anyway as if an EOD marker were at the end "
                       "of the file.");
        if (error || eof || count == 0)
            eod = TRUE;
    }
}



static void
doCommentExtension(FILE * const ifP) {
/*----------------------------------------------------------------------------
   Read the rest of a comment extension from the input file 'ifP' and handle
   it.
   
   We ought to deal with the possibility that the comment is not text.  I.e.
   it could have nonprintable characters or embedded nulls.  I don't know if
   the GIF spec requires regular text or not.
-----------------------------------------------------------------------------*/
    char buf[255+1];
    unsigned int blocklen;  
    bool done;

    done = FALSE;
    while (!done) {
        bool eof;
        const char * error;
        getDataBlock(ifP, (unsigned char*) buf, &eof, &blocklen, &error); 
        if (error)
            pm_error("Error reading a data block in a comment extension.  %s",
                     error);
        if (blocklen == 0 || eof)
            done = TRUE;
        else {
            buf[blocklen] = '\0';
            if (showComment) {
                pm_message("gif comment: %s", buf);
            }
        }
    }
}



static void 
doGraphicControlExtension(FILE *         const ifP,
                          struct gif89 * const gif89P) {

    bool eof;
    unsigned int length;
    static unsigned char buf[256];
    const char * error;

    getDataBlock(ifP, buf, &eof, &length, &error);
    if (error)
        pm_error("Error reading 1st data block of Graphic Control Extension");
    if (eof)
        pm_error("EOF/error encountered reading "
                 "1st DataBlock of Graphic Control Extension.");
    else if (length < 4) 
        pm_error("graphic control extension 1st DataBlock too short.  "
                 "It must be at least 4 bytes; it is %d bytes.",
                 length);
    else {
        gif89P->disposal = (buf[0] >> 2) & 0x7;
        gif89P->inputFlag = (buf[0] >> 1) & 0x1;
        gif89P->delayTime = LM_to_uint(buf[1],buf[2]);
        if ((buf[0] & 0x1) != 0)
            gif89P->transparent = buf[3];
        readThroughEod(ifP);
    }
}



static void
doExtension(FILE * const ifP, int const label, struct gif89 * const gif89P) {
    const char * str;
    
    switch (label) {
    case 0x01:              /* Plain Text Extension */
        str = "Plain Text";
#ifdef notdef
        GetDataBlock(ifP, (unsigned char*) buf, &eof, &length);
        
        lpos   = LM_to_uint(buf[0], buf[1]);
        tpos   = LM_to_uint(buf[2], buf[3]);
        width  = LM_to_uint(buf[4], buf[5]);
        height = LM_to_uint(buf[6], buf[7]);
        cellw  = buf[8];
        cellh  = buf[9];
        foreground = buf[10];
        background = buf[11];
        
        while (GetDataBlock(ifP, (unsigned char*) buf) != 0) {
            PPM_ASSIGN(xels[ypos][xpos],
                       cmap[CM_RED][v],
                       cmap[CM_GRN][v],
                       cmap[CM_BLU][v]);
            ++index;
        }
#else
        readThroughEod(ifP);
#endif
        break;
    case 0xff:              /* Application Extension */
        str = "Application";
        readThroughEod(ifP);
        break;
    case 0xfe:              /* Comment Extension */
        str = "Comment";
        doCommentExtension(ifP);
        break;
    case 0xf9:              /* Graphic Control Extension */
        str = "Graphic Control";
        doGraphicControlExtension(ifP, gif89P);
        break;
    default: {
        static char buf[256];
        str = buf;
        sprintf(buf, "UNKNOWN (0x%02x)", label);
        pm_message("Ignoring unrecognized extension (type 0x%02x)", label);
        readThroughEod(ifP);
        }
        break;
    }
    if (verbose)
        pm_message(" got a '%s' extension", str );
}



struct getCodeState {
    unsigned char buf[280];
        /* This is the buffer through which we read the data from the 
           stream.  We must buffer it because we have to read whole data
           blocks at a time, but our client wants one code at a time.
           The buffer typically contains the contents of one data block
           plus two bytes from the previous data block.
        */
    int bufCount;
        /* This is the number of bytes of contents in buf[]. */
    int curbit;
        /* The bit number (starting at 0) within buf[] of the next bit
           to be returned.  If the next bit to be returned is not yet in
           buf[] (we've already returned everything in there), this points
           one beyond the end of the buffer contents.
        */
    bool streamExhausted;
        /* The last time we read from the input stream, we got an EOD marker
           or EOF or an error that prevents further reading of the stream.
        */
};



static void
getAnotherBlock(FILE *                const ifP, 
                struct getCodeState * const gsP,
                const char **         const errorP) {

    unsigned int count;
    unsigned int assumed_count;
    bool eof;

    /* Shift buffer down so last two bytes are now the
       first two bytes.  Shift 'curbit' cursor, which must
       be somewhere in or immediately after those two
       bytes, accordingly.
    */
    gsP->buf[0] = gsP->buf[gsP->bufCount-2];
    gsP->buf[1] = gsP->buf[gsP->bufCount-1];

    gsP->curbit -= (gsP->bufCount-2)*8;
    gsP->bufCount = 2;
        
    /* Add the next block to the buffer */
    getDataBlock(ifP, &gsP->buf[gsP->bufCount], &eof, &count, errorP);
    if (*errorP)
        gsP->streamExhausted = TRUE;
    else {
        if (eof) {
            pm_message("EOF encountered in image "
                       "before EOD marker.  The GIF "
                       "file is malformed, but we are proceeding "
                       "anyway as if an EOD marker were at the end "
                       "of the file.");
            assumed_count = 0;
        } else
            assumed_count = count;

        gsP->streamExhausted = (assumed_count == 0);
        
        gsP->bufCount += assumed_count;
    }
}



static struct getCodeState getCodeState;

static void
getCode_init(struct getCodeState * const getCodeStateP) {
    
    /* Fake a previous data block */
    getCodeStateP->buf[0] = 0;
    getCodeStateP->buf[1] = 0;
    getCodeStateP->bufCount = 2;
    getCodeStateP->curbit = getCodeStateP->bufCount * 8;
    getCodeStateP->streamExhausted = FALSE;
}



static void
getCode_get(struct getCodeState * const gsP,
            FILE *                const ifP, 
            int                   const codeSize,
            bool *                const eofP,
            unsigned int *        const codeP,
            const char **         const errorP) {
/*----------------------------------------------------------------------------
  Read and return the next lzw code from the file *ifP.

  'codeSize' is the number of bits in the code we are to get.

  Return *eofP == TRUE iff we hit the end of the stream.  That means a legal
  end of stream, marked by an EOD marker, not just end of file.  An end of
  file in the middle of the GIF stream is an error.

  If there are bits left in the stream, but not 'codeSize' of them, we
  call that a success with *eofP == TRUE.

  Return the code read (assuming *eofP == FALSE and *errorP == NULL)
  as *codeP.
-----------------------------------------------------------------------------*/
    if ((gsP->curbit+codeSize) > gsP->bufCount*8 && !gsP->streamExhausted) 
        /* Not enough left in buffer to satisfy request.  Get the next
           data block into the buffer.
        */
        getAnotherBlock(ifP, gsP, errorP);
    else
        *errorP = NULL;

    if (!*errorP) {
        if ((gsP->curbit+codeSize) > gsP->bufCount*8) {
            /* The buffer still doesn't have enough bits in it; that means
               there were no data blocks left to read.
            */
            *eofP = TRUE;

            {
                int const bitsUnused = gsP->bufCount * 8 - gsP->curbit;
                if (bitsUnused > 0)
                    pm_message("Stream ends with a partial code "
                               "(%d bits left in file; "
                               "expected a %d bit code).  Ignoring.",
                               bitsUnused, codeSize);
            }
        } else {
            int i, j;
            unsigned int code;
            unsigned char * const buf = gsP->buf;
            
            code = 0;  /* initial value */
            for (i = gsP->curbit, j = 0; j < codeSize; ++i, ++j)
                code |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
            gsP->curbit += codeSize;
            *eofP = FALSE;
            *codeP = code;
        }
    }
}




struct stack {
    /* Stack grows from low addresses to high addresses */
    int * stack;  /* malloc'ed array */
    int * sp;     /* stack pointer */
    int * top;    /* next word above top of stack */
};



static void 
initStack(struct stack * const stackP, unsigned int const size) {

    MALLOCARRAY(stackP->stack, size);
    if (stackP->stack == NULL)
        pm_error("Unable to allocate %d-word stack.", size);
    stackP->sp = stackP->stack;
    stackP->top = stackP->stack + size;
}



static void
pushStack(struct stack * const stackP, int const value) {

    if (stackP->sp >= stackP->top)
        pm_error("stack overflow");

    *(stackP->sp++) = value;
}



static bool
stackIsEmpty(const struct stack * const stackP) {
    return stackP->sp == stackP->stack;
}



static int
popStack(struct stack * const stackP) {

    if (stackP->sp <= stackP->stack)
        pm_error("stack underflow");
    
    return *(--stackP->sp);
}



static void
termStack(struct stack * const stackP) {
    free(stackP->stack);
    stackP->stack = NULL;
}

    

/*----------------------------------------------------------------------------
   Some notes on LZW.

   LZW is an extension of Limpel-Ziv.  The two extensions are:

     1) in Limpel-Ziv, codes are all the same number of bits.  In
        LZW, they start out small and increase as the stream progresses.

     2) LZW has a clear code that resets the string table and code
        size.

   The LZW code space is allocated as follows:

   The true data elements are dataWidth bits wide, so the maximum
   value of a true data element is 2**dataWidth-1.  We call that
   max_dataVal.  The first byte in the stream tells you what dataWidth
   is.

   LZW codes 0 - max_dataVal are direct codes.  Each one represents
   the true data element whose value is that of the LZW code itself.
   No decompression is required.

   max_dataVal + 1 and up are compression codes.  They encode
   true data elements:

   max_dataVal + 1 is the clear code.
         
   max_dataVal + 2 is the end code.

   max_dataVal + 3 and up are string codes.  Each string code 
   represents a string of true data elements.  The translation from a
   string code to the string of true data elements varies as the stream
   progresses.  In the beginning and after every clear code, the
   translation table is empty, so no string codes are valid.  As the
   stream progresses, the table gets filled and more string codes 
   become valid.

-----------------------------------------------------------------------------*/


struct decompressor {
    struct stack stack;
    int      fresh;
        /* The stream is right after a clear code or at the very beginning */
    int      codeSize;
        /* The current code size -- each LZW code in this part of the image
           is this many bits.  Ergo, we read this many bits at a time from
           the stream.
        */
    int      maxnum_code;
        /* The maximum number of LZW codes that can be represented with the 
           current code size.  (1 << codeSize)
        */
    int      next_tableSlot;
        /* Index in the code translation table of the next free entry */
    unsigned int firstcode;
        /* This is always a true data element code */
    int      prevcode;
        /* The code just before, in the image, the one we're processing now */
    int      table[2][(1 << MAX_LZW_BITS)];

    /* The following are constant for the life of the decompressor */
    FILE * ifP;
    int init_codeSize;
    int max_dataVal;
    int clear_code;
    int end_code; 
};



static void
resetDecompressor(struct decompressor * const decompP) {

    decompP->codeSize = decompP->init_codeSize+1;
    decompP->maxnum_code = 1 << decompP->codeSize;
    decompP->next_tableSlot = decompP->max_dataVal + 3;
    decompP->fresh = 1;
}



static void
lzwInit(struct decompressor * const decompP, 
        FILE *                const ifP,
        int                   const init_codeSize) {

    if (verbose)
        pm_message("Image says the initial compression code size is "
                   "%d bits", 
                   init_codeSize);
    
    decompP->ifP = ifP;
    decompP->init_codeSize = init_codeSize;

    assert(decompP->init_codeSize < sizeof(decompP->max_dataVal) * 8);

    decompP->max_dataVal = (1 << init_codeSize) - 1;
    decompP->clear_code = decompP->max_dataVal + 1;
    decompP->end_code = decompP->max_dataVal + 2;

    if (verbose)
        pm_message("Initial code size is %u bits; clear code = 0x%x, "
                   "end code = 0x%x",
                   decompP->init_codeSize, 
                   decompP->clear_code, decompP->end_code);
    
    /* The entries in the translation table for true data codes are
       constant throughout the stream.  We set them now and they never
       change.
    */
    {
        unsigned int i;
        for (i = 0; i <= decompP->max_dataVal; ++i) {
            decompP->table[0][i] = 0;
            decompP->table[1][i] = i;
        }
    }
    resetDecompressor(decompP);

    getCode_init(&getCodeState);
    
    decompP->fresh = TRUE;
    
    initStack(&decompP->stack, maxnum_lzwCode * 2);
}



static void
lzwTerm(struct decompressor * const decompP) {

    termStack(&decompP->stack);
}



static void
expandCodeOntoStack(struct decompressor * const decompP,
                    int                   const incode,
                    const char **         const errorP) {
/*----------------------------------------------------------------------------
   'incode' is an LZW string code.  It represents a string of true data
   elements, as defined by the string translation table in *decompP.

   Expand the code to a string of LZW direct codes and push them onto the
   stack such that the leftmost code is on top.

   Also add to the translation table where appropriate.

   Iff the translation table contains a cycle (which means the LZW stream
   from which it was built is invalid), fail (return text explanation
   as *errorP).
-----------------------------------------------------------------------------*/
    int code;
    const char * error;

    error = NULL; /* Initial value */

    if (incode < decompP->next_tableSlot) 
        code = incode;
    else {
        /* It's a code that isn't in our translation table yet */
        pushStack(&decompP->stack, decompP->firstcode);
        code = decompP->prevcode;
    }

    {
        /* Get the whole string that this compression code
           represents and push it onto the code stack so the
           leftmost code is on top.  Set decompP->firstcode to the
           first (leftmost) code in that string.
        */

        unsigned int stringCount;
        stringCount = 0;

        while (code > decompP->max_dataVal && !error) {
            if (stringCount > maxnum_lzwCode) {
                asprintfN(&error,
                          "Error in GIF image: contains LZW string loop");
            } else {
                ++stringCount;
                pushStack(&decompP->stack, decompP->table[1][code]);
                code = decompP->table[0][code];
            }
        }
        decompP->firstcode = decompP->table[1][code];
        pushStack(&decompP->stack, decompP->firstcode);
    }

    if (decompP->next_tableSlot < maxnum_lzwCode) {
        decompP->table[0][decompP->next_tableSlot] = decompP->prevcode;
        decompP->table[1][decompP->next_tableSlot] = decompP->firstcode;
        ++decompP->next_tableSlot;
        if (decompP->next_tableSlot >= decompP->maxnum_code) {
            /* We've used up all the codes of the current code size.
               Future codes in the stream will have codes one bit longer.
               But there's an exception if we're already at the LZW
               maximum, in which case the codes will simply continue
               the same size.
            */
            if (decompP->codeSize < MAX_LZW_BITS) {
                ++decompP->codeSize;
                decompP->maxnum_code = 1 << decompP->codeSize;
            }
        }
    }

    *errorP = error;

    decompP->prevcode = incode;
}



static void
lzwReadByteFresh(struct getCodeState * const getCodeStateP,
                 struct decompressor * const decompP,
                 bool *                const endOfImageP,
                 unsigned int *        const dataReadP,
                 const char **         const errorP) {
                     
    /* Read off all initial clear codes, read the first non-clear code,
       and return it.  There are no strings in the table yet, so the next
       code must be a direct true data code.
    */
    bool eof;
    do {
        getCode_get(getCodeStateP, decompP->ifP, decompP->codeSize,
                    &eof, &decompP->firstcode, errorP);
        decompP->prevcode = decompP->firstcode;
    } while (decompP->firstcode == decompP->clear_code && !*errorP && !eof);

    if (!*errorP) {
        if (eof)
            *endOfImageP = TRUE;
        else if (decompP->firstcode == decompP->end_code) {
            if (!zeroDataBlock)
                readThroughEod(decompP->ifP);
            *endOfImageP = TRUE;
        } else {
            *endOfImageP = FALSE;
            *dataReadP = decompP->firstcode;
        }
    }
}



static void
lzwReadByte(struct decompressor * const decompP,
            unsigned int *        const dataReadP,
            bool *                const endOfImageP,
            const char **         const errorP) {
/*----------------------------------------------------------------------------
  Return the next data element of the decompressed image.  In the context
  of a GIF, a data element is the color table index of one pixel.

  We read and return the next byte of the decompressed image.

  If we can't, because the stream is too corrupted to make sense out of
  it or the stream ends, we fail (return text description of why as
  *errorP).

  We forgive the case that the "end" code is the end of the stream --
  not followed by an EOD marker (zero length DataBlock).

  Iff we can't read a byte because we've hit the end of the image,
  we return *endOfImageP = true.
-----------------------------------------------------------------------------*/
    if (!stackIsEmpty(&decompP->stack)) {
        *errorP = NULL;
        *endOfImageP = FALSE;
        *dataReadP = popStack(&decompP->stack);
    } else if (decompP->fresh) {
        decompP->fresh = FALSE;

        lzwReadByteFresh(&getCodeState, decompP, endOfImageP, dataReadP,
                         errorP);
    } else {
        unsigned int code;
        bool eof;
        getCode_get(&getCodeState, decompP->ifP, decompP->codeSize,
                    &eof, &code, errorP);
        if (!*errorP) {
            if (eof)
                asprintfN(errorP,
                          "Premature end of file; no proper GIF closing");
            else {
                if (code == decompP->clear_code) {
                    resetDecompressor(decompP);
                    lzwReadByte(decompP, dataReadP, endOfImageP, errorP);
                } else {
                    if (code == decompP->end_code) {
                        if (!zeroDataBlock)
                            readThroughEod(decompP->ifP);
                        *endOfImageP = TRUE;
                        *errorP = NULL;
                    } else {
                        expandCodeOntoStack(decompP, code, errorP);
                        if (!*errorP)
                            *dataReadP = popStack(&decompP->stack);
                    }
                }
            }
        }
    }
}



enum pass {MULT8PLUS0, MULT8PLUS4, MULT4PLUS2, MULT2PLUS1};

static void
bumpRowInterlace(unsigned int * const rowP,
                 unsigned int   const rows,
                 enum pass *    const passP) {
/*----------------------------------------------------------------------------
   Move *pixelCursorP to the next row in the interlace pattern.
-----------------------------------------------------------------------------*/
    /* There are 4 passes:
       MULT8PLUS0: Rows 8, 16, 24, 32, etc.
       MULT8PLUS4: Rows 4, 12, 20, 28, etc.
       MULT4PLUS2: Rows 2, 6, 10, 14, etc.
       MULT2PLUS1: Rows 1, 3, 5, 7, 9, etc.
    */
    
    switch (*passP) {
    case MULT8PLUS0:
        *rowP += 8;
        break;
    case MULT8PLUS4:
        *rowP += 8;
        break;
    case MULT4PLUS2:
        *rowP += 4;
        break;
    case MULT2PLUS1:
        *rowP += 2;
        break;
    }
    /* Set the proper pass for the next read.  Note that if there are
       more than 4 rows, the sequence of passes is sequential, but
       when there are fewer than 4, we may skip e.g. from MULT8PLUS0
       to MULT4PLUS2.
    */
    while (*rowP >= rows && *passP != MULT2PLUS1) {
        switch (*passP) {
        case MULT8PLUS0:
            *passP = MULT8PLUS4;
            *rowP = 4;
            break;
        case MULT8PLUS4:
            *passP = MULT4PLUS2;
            *rowP = 2;
            break;
        case MULT4PLUS2:
            *passP = MULT2PLUS1;
            *rowP = 1;
            break;
        case MULT2PLUS1:
            /* We've read the entire image */
            break;
        }
    }
}



struct pnmBuffer {
    xel ** xels;
    unsigned int col;
    unsigned int row;
};

static void
addPixelToRaster(unsigned int       const cmapIndex,
                 struct pnmBuffer * const pnmBufferP,
                 unsigned int       const cols,
                 unsigned int       const rows,
                 gifColorMap              cmap, 
                 unsigned int       const cmapSize,
                 bool               const interlace,
                 int                const transparentIndex,
                 bit **             const alphabits,
                 enum pass *        const passP) {

    if (cmapIndex >= cmapSize)
        pm_error("Invalid color index %u in an image that has only "
                 "%u colors in the color map.", cmapIndex, cmapSize);
    
    assert(cmapIndex < MAXCOLORMAPSIZE);
    
    PPM_ASSIGN(pnmBufferP->xels[pnmBufferP->row][pnmBufferP->col], 
               cmap[CM_RED][cmapIndex],
               cmap[CM_GRN][cmapIndex],
               cmap[CM_BLU][cmapIndex]);
    
    if (alphabits) 
        alphabits[pnmBufferP->row][pnmBufferP->col] =
            (cmapIndex == transparentIndex) ? PBM_BLACK : PBM_WHITE;
    
    ++pnmBufferP->col;
    if (pnmBufferP->col == cols) {
        pnmBufferP->col = 0;
        if (interlace)
            bumpRowInterlace(&pnmBufferP->row, rows, passP);
        else
            ++pnmBufferP->row;
    }
}



static void
verifyPixelRead(bool          const endOfImage,
                const char *  const readError,
                unsigned int  const cols,
                unsigned int  const rows,
                unsigned int  const failedRowNum,
                const char ** const errorP) {

    if (readError)
        *errorP = strdup(readError);
    else {
        if (endOfImage)
            asprintfN(errorP,
                      "Error in GIF image: Not enough raster data to fill "
                      "%u x %u dimensions.  Ran out of raster data in "
                      "row %u.  The image has proper ending sequence, so "
                      "this is not just a truncated file.",
                      cols, rows, failedRowNum);
        else
            *errorP = NULL;
    }
}



static void
readRaster(struct decompressor * const decompP,
           xel **                const xels, 
           unsigned int          const cols,
           unsigned int          const rows,
           gifColorMap                 cmap, 
           unsigned int          const cmapSize,
           bool                  const interlace,
           int                   const transparentIndex,
           bit **                const alphabits,
           bool                  const tolerateBadInput) {
                   
    struct pnmBuffer pnmBuffer;
    enum pass pass;
    bool fillingMissingPixels;

    pass = MULT8PLUS0;
    pnmBuffer.xels = xels;
    pnmBuffer.col  = 0;
    pnmBuffer.row  = 0;
    fillingMissingPixels = false;  /* initial value */

    while (pnmBuffer.row < rows) {
        unsigned int colorIndex;

        if (fillingMissingPixels)
            colorIndex = 0;
        else {
            const char * error;

            const char * readError;
            unsigned int readColorIndex;
            bool endOfImage;

            lzwReadByte(decompP, &readColorIndex, &endOfImage, &readError);

            verifyPixelRead(endOfImage, readError, cols, rows, pnmBuffer.row,
                            &error);

            if (readError)
                strfree(readError);

            if (error) {
                if (tolerateBadInput) {
                    pm_message("WARNING: %s.  "
                               "Filling bottom %u rows with arbitrary color",
                               error, rows - pnmBuffer.row);
                    fillingMissingPixels = true;
                } else
                    pm_error("Unable to read input image.  %s.  Use the "
                             "-repair option to try to salvage some of "
                             "the image",
                             error);

                colorIndex = 0;
            } else
                colorIndex = readColorIndex;
        }
        addPixelToRaster(colorIndex, &pnmBuffer, cols, rows, cmap, cmapSize,
                         interlace, transparentIndex, alphabits, &pass);
    }
}



static void
skipExtraneousData(struct decompressor * const decompP) {

    unsigned int byteRead;
    bool endOfImage;
    const char * error;

    lzwReadByte(decompP, &byteRead, &endOfImage, &error);

    if (error)
        strfree(error);
    else if (!endOfImage) {
        pm_message("Extraneous data at end of image.  "
                   "Skipped to end of image");

        while (!endOfImage && !error)
            lzwReadByte(decompP, &byteRead, &endOfImage, &error);

        if (error) {
            pm_message("Error encountered skipping to end of image: %s",
                       error);
            strfree(error);
        }
    }
}



static void
readImageData(FILE *       const ifP, 
              xel **       const xels, 
              unsigned int const cols,
              unsigned int const rows,
              gifColorMap        cmap, 
              unsigned int const cmapSize,
              bool         const interlace,
              int          const transparentIndex,
              bit **       const alphabits,
              bool         const tolerateBadInput) {

    unsigned char lzwMinCodeSize;      
    struct decompressor decomp;
    bool gotMinCodeSize;

    gotMinCodeSize =  ReadOK(ifP, &lzwMinCodeSize, 1);
    if (!gotMinCodeSize)
        pm_error("GIF stream ends (or read error) "
                 "right after an image separator; no "
                 "image data follows.");

    if (lzwMinCodeSize > MAX_LZW_BITS)
        pm_error("Invalid minimum code size value in image data: %u.  "
                 "Maximum allowable code size in GIF is %u", 
                 lzwMinCodeSize, MAX_LZW_BITS);

    lzwInit(&decomp, ifP, lzwMinCodeSize);

    readRaster(&decomp, xels, cols, rows, cmap, cmapSize, interlace,
               transparentIndex, alphabits, tolerateBadInput);

    skipExtraneousData(&decomp);

    lzwTerm(&decomp);
}



static void
writePnm(FILE * const outfileP,
         xel ** const xels, 
         int    const cols,
         int    const rows,
         int    const hasGray,
         int    const hasColor) {
/*----------------------------------------------------------------------------
   Write a PNM image to the current position of file *outfileP with
   dimensions 'cols' x 'rows' and raster 'xels'.
   
   Make it PBM, PGM, or PBM according to 'hasGray' and 'hasColor'.
-----------------------------------------------------------------------------*/
    int format;
    const char * formatName;
           
    if (hasColor) {
        format = PPM_FORMAT;
        formatName = "PPM";
    } else if (hasGray) {
        format = PGM_FORMAT;
        formatName = "PGM";
    } else {
        format = PBM_FORMAT;
        formatName = "PBM";
    }
    if (verbose) 
        pm_message("writing a %s file", formatName);
    
    if (outfileP) 
        pnm_writepnm(outfileP, xels, cols, rows,
                     (xelval) GIFMAXVAL, format, FALSE);
}



static void
transparencyMessage(int const transparentIndex, 
                    gifColorMap cmap) {
/*----------------------------------------------------------------------------
   If user wants verbose output, tell him that the color with index
   'transparentIndex' is supposed to be a transparent background color.
   
   If transparentIndex == -1, tell him there is no transparent background
   color.
-----------------------------------------------------------------------------*/
    if (verbose) {
        if (transparentIndex == -1)
            pm_message("no transparency");
        else
            pm_message("transparent background color: rgb:%02x/%02x/%02x "
                       "Index %d",
                       cmap[CM_RED][transparentIndex],
                       cmap[CM_GRN][transparentIndex],
                       cmap[CM_BLU][transparentIndex],
                       transparentIndex
                );
    }
}

static void
readGifHeader(FILE * const gifFile, struct gifScreen * const gifScreenP) {
/*----------------------------------------------------------------------------
   Read the GIF stream header off the file gifFile, which is present
   positioned to the beginning of a GIF stream.  Return the info from it
   as *gifScreenP.
-----------------------------------------------------------------------------*/
    unsigned char   buf[16];
    char     version[4];


    if (! ReadOK(gifFile,buf,6))
        pm_error("error reading magic number" );
    
    if (strncmp((char *)buf,"GIF",3) != 0)
        pm_error("File does not contain a GIF stream.  It does not start "
                 "with 'GIF'.");
    
    strncpy(version, (char *)buf + 3, 3);
    version[3] = '\0';
    
    if (verbose)
        pm_message("GIF format version is '%s'", version);
    
    if ((!streq(version, "87a")) && (!streq(version, "89a")))
        pm_error("bad version number, not '87a' or '89a'" );
    
    if (! ReadOK(gifFile,buf,7))
        pm_error("failed to read screen descriptor" );
    
    gifScreenP->Width           = LM_to_uint(buf[0],buf[1]);
    gifScreenP->Height          = LM_to_uint(buf[2],buf[3]);
    gifScreenP->ColorMapSize    = 1 << ((buf[4] & 0x07) + 1);
    gifScreenP->ColorResolution = (buf[4] & 0x70 >> 3) + 1;
    gifScreenP->Background      = buf[5];
    gifScreenP->AspectRatio     = buf[6];

    if (verbose) {
        pm_message("GIF Width = %d GIF Height = %d "
                   "Pixel aspect ratio = %d (%f:1)",
                   gifScreenP->Width, gifScreenP->Height, 
                   gifScreenP->AspectRatio, 
                   gifScreenP->AspectRatio == 0 ? 
                   1 : (gifScreenP->AspectRatio + 15) / 64.0);
        pm_message("Colors = %d   Color Resolution = %d",
                   gifScreenP->ColorMapSize, gifScreenP->ColorResolution);
    }           
    if (BitSet(buf[4], LOCALCOLORMAP)) {    /* Global Colormap */
        readColorMap(gifFile, gifScreenP->ColorMapSize, gifScreenP->ColorMap,
                     &gifScreenP->hasGray, &gifScreenP->hasColor);
        if (verbose) {
            pm_message("Color map %s grays, %s colors", 
                       gifScreenP->hasGray ? "contains" : "doesn't contain",
                       gifScreenP->hasColor ? "contains" : "doesn't contain");
        }
    }
    
    if (gifScreenP->AspectRatio != 0 && gifScreenP->AspectRatio != 49) {
        float   r;
        r = ( (float) gifScreenP->AspectRatio + 15.0 ) / 64.0;
        pm_message("warning - input pixels are not square, "
                   "but we are rendering them as square pixels "
                   "in the output.  "
                   "To fix the output, run it through "
                   "'pnmscale -%cscale %g'",
                   r < 1.0 ? 'x' : 'y',
                   r < 1.0 ? 1.0 / r : r );
    }
}



static void
readExtensions(FILE*          const ifP, 
               struct gif89 * const gif89P,
               bool *         const eodP,
               const char **  const errorP) {
/*----------------------------------------------------------------------------
   Read extension blocks from the GIF stream to which the file *ifP is
   positioned.  Read up through the image separator that begins the
   next image or GIF stream terminator.

   If we encounter EOD (end of GIF stream) before we find an image 
   separator, we return *eodP == TRUE.  Else *eodP == FALSE.

   If we hit end of file before an EOD marker, we fail.
-----------------------------------------------------------------------------*/
    bool imageStart;
    bool eod;

    *errorP = NULL;  /* initial value */

    eod = FALSE;
    imageStart = FALSE;

    /* Read the image descriptor */
    while (!imageStart && !eod && !*errorP) {
        unsigned char c;
        const char * error;

        readFile(ifP, &c, 1, &error);

        if (error) {
            asprintfN(errorP, "File read error where start of image "
                      "descriptor or end of GIF expected.  %s",
                      error);
            strfree(error);
        } else {
            if (c == ';') {         /* GIF terminator */
                eod = TRUE;
            } else if (c == '!') {         /* Extension */
                unsigned char functionCode;
                const char * error;

                readFile(ifP, &functionCode, 1, &error);

                if (error) {
                    asprintfN(errorP, "Failed to read function code "
                              "of GIF extension (immediately after the '!' "
                              "extension delimiter) from input.  %s", error);
                    strfree(error);
                } else {
                    doExtension(ifP, functionCode, gif89P);
                }
            } else if (c == ',') 
                imageStart = TRUE;
            else 
                pm_message("bogus character 0x%02x, ignoring", (int)c);
        }
    }
    *eodP = eod;
}



static void
reportImageInfo(unsigned int const cols,
                unsigned int const rows,
                bool         const useGlobalColormap,
                unsigned int const localColorMapSize,
                bool         const interlaced) {

    pm_message("reading %u by %u%s GIF image",
               cols, rows, interlaced ? " interlaced" : "" );

    if (useGlobalColormap)
        pm_message("  Uses global colormap");
    else
        pm_message("  Uses local colormap of %u colors", localColorMapSize);
}



static void
convertImage(FILE *           const ifP, 
             bool             const skipIt, 
             FILE *           const imageout_file, 
             FILE *           const alphafile, 
             struct gifScreen       gifScreen,
             struct gif89     const gif89,
             bool             const tolerateBadInput) {
/*----------------------------------------------------------------------------
   Read a single GIF image from the current position of file 'ifP'.

   If 'skipIt' is TRUE, don't do anything else.  Otherwise, write the
   image to the current position of files 'imageout_file' and 'alphafile'.
   If 'alphafile' is NULL, though, don't write any alpha information.
-----------------------------------------------------------------------------*/
    unsigned char buf[16];
    bool useGlobalColormap;
    xel **xels;  /* The image raster, in libpnm format */
    bit **alphabits;  
        /* The image alpha mask, in libpbm format.  NULL if we aren't computing
           an alpha mask.
        */
    unsigned int cols, rows;  /* Dimensions of the image */
    gifColorMap localColorMap;
    unsigned int localColorMapSize;
    bool interlaced;

    if (! ReadOK(ifP,buf,9))
        pm_error("couldn't read left/top/width/height");

    useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
    localColorMapSize = 1u << ((buf[8] & 0x07) + 1);
    cols = LM_to_uint(buf[4], buf[5]);
    rows = LM_to_uint(buf[6], buf[7]);
    interlaced = !!BitSet(buf[8], INTERLACE);

    if (verbose)
        reportImageInfo(cols, rows, useGlobalColormap, localColorMapSize,
                        interlaced);
        
    xels = pnm_allocarray(cols, rows);
    if (!xels)
        pm_error("couldn't alloc space for image" );

    if (alphafile) {
        alphabits = pbm_allocarray(cols, rows);
        if (!alphabits)
            pm_error("couldn't alloc space for alpha image" );
    } else
        alphabits = NULL;
    
    if (!useGlobalColormap) {
        int hasGray, hasColor;

        readColorMap(ifP, localColorMapSize, localColorMap, 
                     &hasGray, &hasColor);
        transparencyMessage(gif89.transparent, localColorMap);
        readImageData(ifP, xels, cols, rows, localColorMap, localColorMapSize,
                      interlaced, gif89.transparent, alphabits,
                      tolerateBadInput);
        if (!skipIt) {
            writePnm(imageout_file, xels, cols, rows,
                     hasGray, hasColor);
        }
    } else {
        transparencyMessage(gif89.transparent, gifScreen.ColorMap);
        readImageData(ifP, xels, cols, rows, 
                      gifScreen.ColorMap, gifScreen.ColorMapSize,
                      interlaced, gif89.transparent, alphabits,
                      tolerateBadInput);
        if (!skipIt) {
            writePnm(imageout_file, xels, cols, rows,
                     gifScreen.hasGray, gifScreen.hasColor);
        }
    }

    if (!skipIt && alphafile && alphabits)
        pbm_writepbm(alphafile, alphabits, cols, rows, FALSE);

    pnm_freearray(xels, rows);
    if (alphabits)
        pbm_freearray(alphabits, rows);
}



static void
disposeOfReadExtensionsError(const char * const error,
                             bool         const tolerateBadInput,
                             unsigned int const imageSeq,
                             bool *       const eodP) {
    if (error) {
        if (tolerateBadInput)
            pm_message("Error accessing Image %u of stream; no further "
                       "images can be accessed.  %s",
                       imageSeq, error);
        else
            pm_error("Error accessing Image %u of stream.  %s",
                     imageSeq, error);
        strfree(error);
        *eodP = TRUE;
    }
}



static void
convertImages(FILE * const ifP, 
              bool   const allImages,
              int    const requestedImageSeq, 
              bool   const drainStream,
              FILE * const imageout_file, 
              FILE * const alphafile,
              bool   const tolerateBadInput) {
/*----------------------------------------------------------------------------
   Read a GIF stream from file 'ifP' and write one or more images from
   it as PNM images to file 'imageout_file'.  If the images have transparency
   and 'alphafile' is non-NULL, write PGM alpha masks to file 'alphafile'.

   'allImages' means Caller wants all the images in the stream.  

   'requestedImageSeq' is meaningful only when 'allImages' is FALSE.  It 
   is the sequence number of the one image Caller wants from the stream,
   with the first image being 0.

   'drainInput' means to read the entire GIF stream, even after
   reading the image Caller asked for.  We read the stream, not just
   the file it's in, so we still recognize certain errors in the GIF
   format in the tail of the stream and there may yet be more stuff in
   the file when we return.
-----------------------------------------------------------------------------*/
    int imageSeq;
        /* Sequence within GIF stream of image we are currently processing.
           First is 0.
        */
    struct gifScreen gifScreen;
    struct gif89 gif89;
    bool eod;
        /* We've read through the GIF terminator character */

    initGif89(&gif89);

    readGifHeader(ifP, &gifScreen);

    for (imageSeq = 0, eod = FALSE;
         !eod && (imageSeq <= requestedImageSeq || allImages || drainStream);
         ++imageSeq) {

        const char * error;

        readExtensions(ifP, &gif89, &eod, &error);

        disposeOfReadExtensionsError(error, tolerateBadInput, imageSeq, &eod);

        if (eod) {
            /* GIF stream ends before image with sequence imageSeq */
            if (!allImages && (imageSeq <= requestedImageSeq))
                pm_error("You requested Image %d, but "
                         "only %d image%s found in GIF stream",
                         requestedImageSeq + 1,
                         imageSeq, imageSeq > 1 ? "s" : "");
        } else {
            if (verbose)
                pm_message("Reading Image Sequence %d", imageSeq);
            convertImage(ifP, !allImages && (imageSeq != requestedImageSeq), 
                         imageout_file, alphafile, gifScreen, gif89,
                         tolerateBadInput);
        }
    }
}



int
main(int argc, char **argv) {

    struct cmdlineInfo cmdline;
    FILE *ifP;
    FILE *alpha_file, *imageout_file;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);
    verbose = cmdline.verbose;
    showComment = cmdline.comments;
   
    ifP = pm_openr(cmdline.input_filespec);

    if (cmdline.alpha_filename == NULL)
        alpha_file = NULL;
    else
        alpha_file = pm_openw(cmdline.alpha_filename);

    if (alpha_file && streq(cmdline.alpha_filename, "-"))
        imageout_file = NULL;
    else
        imageout_file = stdout;

    convertImages(ifP, cmdline.all_images, cmdline.image_no, 
                  !cmdline.quitearly, imageout_file, alpha_file,
                  cmdline.repair);

    pm_close(ifP);
    if (imageout_file != NULL) 
        pm_close(imageout_file);
    if (alpha_file != NULL)
        pm_close(alpha_file);

    return 0;
}