about summary refs log tree commit diff
path: root/converter/other/bmptopnm.c
blob: 091d948a8e1718ed3e3e63a855a756f0ff5b0f82 (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
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
/*****************************************************************************
                                    bmptopnm.c
******************************************************************************

 Bmptopnm - Converts from a Microsoft Windows or OS/2 .BMP file to a
 PBM, PGM, or PPM file.

 This program was formerly called Bmptoppm (and generated only PPM output).
 The name was changed in March 2002.

 Copyright (C) 1992 by David W. Sanderson.

 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.

 Note: From mid-2003 to mid-2007, this program would crash on any 16
 bit BMP without transparency and no one reported it.  Before that, it
 refused to even try to read a 16 bit BMP.  I conclude that essentially
 nobody is using 16 bit BMP.

*****************************************************************************/
#include <string.h>
#include <limits.h>
#include <assert.h>

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

static xelval const bmpMaxval = 255;
    /* The maxval for intensity values in a BMP image -- either in a
       truecolor raster or in a colormap
    */

enum rowOrder {BOTTOMUP, TOPDOWN};

struct bitPosition {
    /* mask and shift count to describe a set of bits in a binary value.

       Example: if 16 bits are laid out as XRRRRRGGGGGBBBBB then the shift
       count for the R component is 10 and the mask is 0000000000011111.

       A 'mask' of zero denotes absence of any bits; e.g. in the example
       above, the mask for the transparency component is zero because there
       is no transparency component .  'shift' is arbitrary in that case.
    */
    unsigned int shift;
        /* How many bits right you have to shift the value to get the subject
           bits in the least significant bit positions.
        */
    unsigned int mask;
        /* Has one bits in positions where the subject bits are after
           shifting.
        */
};

struct pixelformat {
    /* The format of a pixel representation from the raster.  i.e. which
       bits apply to red, green, blue, and transparency
    */
    struct bitPosition red;
    struct bitPosition blu;
    struct bitPosition grn;
    struct bitPosition trn;

    bool conventionalBgr;
        /* This means that the above bit positions are just the conventional
           BGR format -- one byte Blue, one byte Green, one byte Red,
           no alpha.  Though it's totally redundant with the members above,
           this member speeds up computation:  We've never actually seen
           a BMP file that doesn't use conventional BGR, and it doesn't
           require any masking or shifting at all to interpret.
        */
};

typedef struct {
    /* These are all encodings of floating point */
    unsigned long x;
    unsigned long y;
    unsigned long z;
} cieXyz;

typedef struct {
    cieXyz red;
    cieXyz grn;
    cieXyz blu;
} cieXyzTriple;

struct bmpInfoHeader {
    enum rowOrder rowOrder;
    unsigned int cols;
    unsigned int rows;
    unsigned int cBitCount;
        /* Number of bits in the BMP file that each pixel occupies. */
    enum bmpClass class;
    bool bitFields;
        /* The raster values are arranged in arbitrary bit fields as
           described by the "mask" values in the header, rather than
           fixed formats.
        */
    unsigned int cmapSize;
        /* Size in bytes of the colormap (palette) in the BMP file.

           Zero means there is no colormap.
        */
    unsigned int imageSize;
        /* Size in bytes of the image data.  We only reference this
           when the image is compressed. */
    unsigned short cPlanes;
    BMPCompType compression;
    struct pixelformat pixelformat;
    cieXyzTriple endPoints;
};



struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFileName;
    unsigned int verbose;
};

static const char * ifname;



static void
parseCommandLine(int argc, const 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;   /* Used by OPTENT3 */
    optStruct3 opt;

    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "verbose",     OPT_FLAG,   NULL, &cmdlineP->verbose,   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 */

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

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



static const char er_read[] = "%s: read error";

static int
GetByte(FILE * const fp) {

    int             v;

    if ((v = getc(fp)) == EOF)
        pm_error(er_read, ifname);

    return v;
}



static short
GetShort(FILE * const fp) {

    short           v;

    if (pm_readlittleshort(fp, &v) == -1)
        pm_error(er_read, ifname);

    return v;
}



static short
GetBigShort(FILE * const fp) {

    short           v;

    if (pm_readbigshort(fp, &v) == -1)
        pm_error(er_read, ifname);

    return v;
}



static long
GetLong(FILE * const fp) {

    long v;

    if (pm_readlittlelong(fp, &v) == -1)
        pm_error(er_read, ifname);

    return v;
}



static cieXyz
GetCieXyz(FILE * const ifP) {

    cieXyz retval;

    retval.x = GetLong(ifP);
    retval.y = GetLong(ifP);
    retval.z = GetLong(ifP);

    return retval;
}



static cieXyzTriple
GetCieXyzTriple(FILE *         const ifP) {

    cieXyzTriple retval;

    retval.red = GetCieXyz(ifP);
    retval.grn = GetCieXyz(ifP);
    retval.blu = GetCieXyz(ifP);

    return retval;
}



static struct pixelformat
defaultPixelformat(unsigned int const bitCount) {

    struct pixelformat retval;

    switch (bitCount) {
    case 16:
        /* This layout is sometimes called "RGB555".  A document from
           Microsoft says this is the default (when the "compression"
           field of the header says COMP_BITFIELDS).
        */
        retval.conventionalBgr = FALSE;
        retval.red.shift = 10;
        retval.grn.shift = 5;
        retval.blu.shift = 0;
        retval.trn.shift = 0;
        retval.red.mask = 0x1f;  /* 5 bits */
        retval.grn.mask = 0x1f;  /* 5 bits */
        retval.blu.mask = 0x1f;  /* 5 bits */
        retval.trn.mask = 0;
        break;
    case 24:
    case 32:
        retval.conventionalBgr = TRUE;
        retval.red.shift = 16;
        retval.grn.shift = 8;
        retval.blu.shift = 0;
        retval.trn.shift = 0;
        retval.red.mask = 0xff;  /* 8 bits */
        retval.grn.mask = 0xff;  /* 8 bits */
        retval.blu.mask = 0xff;  /* 8 bits */
        retval.trn.mask = 0;
        break;
    default:
        /* colormapped - masks are undefined */
        break;
    }

    return retval;
}



static void
readOffBytes(FILE * const fp, unsigned int const nbytes) {
/*----------------------------------------------------------------------------
   Read 'nbytes' from file 'fp'.  Abort program if read error.
-----------------------------------------------------------------------------*/
    int i;

    for(i = 0; i < nbytes; ++i) {
        int rc;
        rc = getc(fp);
        if (rc == EOF)
            pm_error(er_read, ifname);
    }
}



static void
bmpReadfileheader(FILE *         const ifP,
                  unsigned int * const bytesReadP,
                  unsigned int * const offBitsP) {

    unsigned long     offBits;

    if (GetByte(ifP) != 'B')
        pm_error("'%s' is not a BMP file.  (It doesn't start with 'BM')",
                 ifname);
    if (GetByte(ifP) != 'M')
        pm_error("'%s' is not a BMP file.  (It doesn't start with 'BM')",
                 ifname);


    /* fileSize = */ GetLong(ifP);  /* This is not always reliable. */
    /* xHotSpot = */ GetShort(ifP);
    /* yHotSpot = */ GetShort(ifP);
    offBits  = GetLong(ifP);

    *offBitsP = offBits;

    assert(BMPlenfileheader() == 14);

    *bytesReadP = 14;
}



static void
readOs2InfoHeaderRest(FILE *                 const ifP,
                      struct bmpInfoHeader * const headerP) {
/*----------------------------------------------------------------------------
   Read the rest of the info header, after its size field, of an OS2 BMP from
   *ifP.

   Add the information from it to *headerP, in particular these members:

     cols
     rows
     rowOrder
     cPlanes
     cBitCount
     cmapSize
     pixelformat
     compression
-----------------------------------------------------------------------------*/
    unsigned short colsField, rowsField;
    unsigned short planesField, bitCountField;

    pm_readlittleshortu(ifP, &colsField);
    if (colsField == 0)
        pm_error("Invalid BMP file: says width is zero");
    else
        headerP->cols = colsField;

    pm_readlittleshortu(ifP, &rowsField);
    if (rowsField == 0)
        pm_error("Invalid BMP file: says height is zero");
    else
        headerP->rows = rowsField;

    headerP->rowOrder = BOTTOMUP;
    pm_readlittleshortu(ifP, &planesField);
    headerP->cPlanes = planesField;
    pm_readlittleshortu(ifP, &bitCountField);
    headerP->cBitCount = bitCountField;
    /* I actually don't know if the OS/2 BMP format allows
       cBitCount > 8 or if it does, what it means, but ppmtobmp
       creates such BMPs, more or less as a byproduct of creating
       the same for Windows BMP, so we interpret cBitCount > 8 the
       same as for Windows.
    */
    if (headerP->cBitCount <= 8)
        headerP->cmapSize = 1 << headerP->cBitCount;
    else if (headerP->cBitCount == 24)
        headerP->cmapSize = 0;
    /* There is a 16 bit truecolor format, but we don't know how the
       bits are divided among red, green, and blue, so we can't handle it.
    */
    else
        pm_error("Unrecognized bits per pixel in OS/2 BMP file header: %d",
                 headerP->cBitCount);

    headerP->pixelformat = defaultPixelformat(headerP->cBitCount);

    headerP->compression = BMPCOMP_RGB;
}



static void
validateCompression(unsigned long const compression,
                    enum rowOrder const rowOrder,
                    unsigned int  const cBitCount) {

    if (compression != BMPCOMP_RGB && compression != BMPCOMP_BITFIELDS &&
        compression != BMPCOMP_RLE4 && compression != BMPCOMP_RLE8)
        pm_error("Input has unknown encoding.  "
                 "Compression type code = %ld.  The only ones we know "
                 "are RGB (%u), BITFIELDS (%u), "
                 "RLE4 (%u), and RLE8 (%u)",
                 compression, BMPCOMP_RGB, BMPCOMP_BITFIELDS,
                 BMPCOMP_RLE4, BMPCOMP_RLE8);

    if ((compression == BMPCOMP_RLE4 || compression == BMPCOMP_RLE8) &&
        rowOrder == TOPDOWN )
        pm_error("Invalid BMP header.  Claims image is top-down and also "
                 "compressed, which is an impossible combination.");

    if ((compression == BMPCOMP_RLE4 && cBitCount !=4 ) ||
        (compression == BMPCOMP_RLE8 && cBitCount !=8 ))
        pm_error("Invalid BMP header.  "
                 "Compression type (%s) disagrees with "
                 "number of bits per pixel (%u).",
                 compression == BMPCOMP_RLE4 ? "RLE4" : "RLE8",
                 cBitCount);
}



static void
readWindowsBasic40ByteInfoHeader(FILE *                 const ifP,
                                 struct bmpInfoHeader * const headerP) {
/*----------------------------------------------------------------------------
   Read from the file stream 'ifP' the basic BMP Info header.  This does
   not include any Info header extension.  The Info header is the data
   that comes after the BMP file header.

   Return the information from the info header as *headerP.
-----------------------------------------------------------------------------*/
    int colorsused;        /* ColorsUsed value from header */
    unsigned short planesField, bitCountField;
    int32_t colsField;

    pm_readlittlelong2(ifP, &colsField);

    if (colsField == 0)
        pm_error("Invalid BMP file: says width is zero");
    else if (colsField < 0)
        pm_error("Invalid BMP file: says width is negative (%d)", colsField);
    else
        headerP->cols = (unsigned int)colsField;

    {
        long const cy = GetLong(ifP);

        if (cy == 0)
            pm_error("Invalid BMP file: says height is zero");
        if (cy < 0) {
            headerP->rowOrder = TOPDOWN;
            headerP->rows = - cy;
        } else {
            headerP->rowOrder = BOTTOMUP;
            headerP->rows = cy;
        }
    }
    pm_readlittleshortu(ifP, &planesField);
    headerP->cPlanes = planesField;
    pm_readlittleshortu(ifP, &bitCountField);
    headerP->cBitCount = bitCountField;
    {
        unsigned long int const compression = GetLong(ifP);

        validateCompression(compression, headerP->rowOrder,
                            headerP->cBitCount);

        headerP->bitFields = (compression == BMPCOMP_BITFIELDS);

        headerP->compression = compression;
    }
    /* And read the rest of the junk in the 40 byte header */
    headerP->imageSize = GetLong(ifP);   /* ImageSize */
    GetLong(ifP);   /* XpixelsPerMeter */
    GetLong(ifP);   /* YpixelsPerMeter */
    colorsused = GetLong(ifP);   /* ColorsUsed */
    /* See comments in bmp.h for info about the definition of the following
       word and its relationship to the color map size (headerP->cmapSize).
    */
    /* colorsimportant = */ GetLong(ifP);  /* ColorsImportant */

    if (headerP->cBitCount <= 8) {
        if (colorsused != 0) {
            if (colorsused > 1 << headerP->cBitCount)
                pm_error("Invalid BMP header.  Says %u bits per pixel, "
                         "but %d colors used",
                         headerP->cBitCount, colorsused);
            else if (colorsused == 1 && headerP->cBitCount == 1) {
                pm_message("Abnormal BMP header.  Says 1 bit per pixel. "
                           "Should have 2 colors, but says only 1 color used. "
                    );
                headerP->cmapSize = colorsused;
        }
            else
                headerP->cmapSize = colorsused;
        } else
            headerP->cmapSize = 1 << headerP->cBitCount;
    } else if (headerP->cBitCount == 24 ||
               headerP->cBitCount == 16 ||
               headerP->cBitCount == 32)
        headerP->cmapSize = 0;
    else
        pm_error("Unrecognized bits per pixel in Windows BMP file header: %d",
                 headerP->cBitCount);
}



static unsigned int
lsbZeroCount(unsigned int const mask)
/*----------------------------------------------------------------------------
   Return the number of consecutive zeroes in the mask 'mask', starting with
   the least significant bit and going up.  E.g. for 0x20, it would be 5.

   Use GCC built-in when available.
-----------------------------------------------------------------------------*/

#if HAVE_GCC_BITCOUNT
{
      return ( mask==0 ? sizeof(mask)*8 : __builtin_ctz(mask) );
}
#else
{
      unsigned int i=0;

      while (((mask >> i) & 0x1) == 0 && i < sizeof(mask)*8)
        ++i;

      return i;

}
#endif


static struct bitPosition
bitPositionFromMask(long const bmpMask) {
    struct bitPosition retval;

    retval.shift = lsbZeroCount(bmpMask);
    retval.mask  = bmpMask >> retval.shift;

    return retval;
}



static void
computeConventionalBgr(struct pixelformat * const fP,
                       unsigned int         const bitCount) {

    switch (bitCount) {
    case 24:
        fP->conventionalBgr =
            fP->red.shift ==  0 && fP->red.mask == 0xFF &&
            fP->grn.shift ==  8 && fP->grn.mask == 0xFF &&
            fP->blu.shift == 16 && fP->blu.mask == 0xFF &&
            fP->trn.mask == 0
            ;
        break;
    case 32:
        fP->conventionalBgr =
            fP->red.shift ==  8  && fP->red.mask == 0xFF &&
            fP->grn.shift == 16  && fP->grn.mask == 0xFF &&
            fP->blu.shift == 24  && fP->blu.mask == 0xFF &&
            fP->trn.mask == 0
            ;
        break;
    default:
        fP->conventionalBgr = FALSE;
    }
}



static void
readV4InfoHeaderExtension(FILE *                 const ifP,
                          struct bmpInfoHeader * const headerP,
                          unsigned int *         const bytesReadP) {

    unsigned long redMsk, grnMsk, bluMsk, trnMsk;

    redMsk = GetLong(ifP);
    grnMsk = GetLong(ifP);
    bluMsk = GetLong(ifP);
    trnMsk = GetLong(ifP);

    if (headerP->bitFields) {
        /* A document from Microsoft says on Windows 95 there is no
           transparency plane and (red, green, blue) must be either
           (5,5,5) or (5,6,5) for 16 bit and (8,8,8) for 32 bit.
           It calls these RGB555, RGB565, RGB888.
        */
        headerP->pixelformat.red = bitPositionFromMask(redMsk);
        headerP->pixelformat.grn = bitPositionFromMask(grnMsk);
        headerP->pixelformat.blu = bitPositionFromMask(bluMsk);
        headerP->pixelformat.trn = bitPositionFromMask(trnMsk);

        computeConventionalBgr(&headerP->pixelformat, headerP->cBitCount);
    } else
        headerP->pixelformat = defaultPixelformat(headerP->cBitCount);

    GetLong(ifP);  /* Color space */

    headerP->endPoints = GetCieXyzTriple(ifP);  /* 36 bytes */

    GetLong(ifP);  /* GammaRed */
    GetLong(ifP);  /* GammaGreen */
    GetLong(ifP);  /* GammaBlue */

    *bytesReadP = 68;
}



static void
readV5InfoHeaderExtension(FILE *                 const ifP,
                          struct bmpInfoHeader * const headerP,
                          unsigned int *         const bytesReadP) {

    GetLong(ifP);  /* Intent */
    GetLong(ifP);  /* ProfileData */
    GetLong(ifP);  /* ProfileSize */
    GetLong(ifP);  /* Reserved */

    *bytesReadP = 16;
}



static void
defaultV4InfoHeaderExtension(struct bmpInfoHeader * const headerP) {

    headerP->pixelformat = defaultPixelformat(headerP->cBitCount);

}



static void
readWindowsInfoHeaderRest(FILE *                 const ifP,
                          unsigned int           const cInfoHeaderSize,
                          struct bmpInfoHeader * const headerP) {
/*----------------------------------------------------------------------------
   Read the rest of the info header, after the length field, of a Windows BMP
   from *ifP.

   'cInfoHeaderSize' is the size of the info header, not counting its size
   field.  Note that besides telling us how much data to read, this also
   implies which of the three major formats the data is in.

   Add the information from it to *headerP, in particular these members:

     cols
     rows
     rowOrder
     cPlanes
     cBitCount
     bitFields
     compression
     imageSize
     cmapSize
     pixelformat
     endPoints
-----------------------------------------------------------------------------*/
    /* There are 3 major formats of Windows
       BMP, identified by the 3 info header lengths.  The original
       one is 40 bytes.  The "V4 header" is 108 bytes and was
       new with Windows 95 and NT 4.0.  The "V5 header" is 124 bytes
       and was new with Windows 98 and Windows 2000.
    */
    unsigned int bytesRead;

    readWindowsBasic40ByteInfoHeader(ifP, headerP);

    bytesRead = 40;

    if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V4) {
        unsigned int v4BytesRead;
        readV4InfoHeaderExtension(ifP, headerP, &v4BytesRead);
        bytesRead += v4BytesRead;

        assert(bytesRead == BMP_HDRLEN_WIN_V4);
    } else
        defaultV4InfoHeaderExtension(headerP);

    if (cInfoHeaderSize >= BMP_HDRLEN_WIN_V5) {
        unsigned int v5BytesRead;
        readV5InfoHeaderExtension(ifP, headerP, &v5BytesRead);
        bytesRead += v5BytesRead;
        assert(bytesRead == BMP_HDRLEN_WIN_V5);
    }

    for (; bytesRead < cInfoHeaderSize;) {
        GetByte(ifP);
        ++bytesRead;
    }

    assert(bytesRead == cInfoHeaderSize);
}



static void
bmpReadinfoheader(FILE *                 const ifP,
                  unsigned int *         const bytesReadP,
                  struct bmpInfoHeader * const headerP,
                  const char **          const errorP) {

    unsigned int const cInfoHeaderSize = GetLong(ifP);

    const char * error;

    BMPdetermineclass(cInfoHeaderSize, &headerP->class, &error);

    if (error) {
        pm_asprintf(errorP, "Cannot determine the class of BMP from the "
                    "info header size %u.  %s", cInfoHeaderSize, error);
        pm_strfree(error);
    } else {
        switch (headerP->class) {
        case BMP_C_WIN_V1:
        case BMP_C_WIN_V2:
        case BMP_C_WIN_V3:
        case BMP_C_WIN_V4:
        case BMP_C_WIN_V5:
            readWindowsInfoHeaderRest(ifP, cInfoHeaderSize, headerP);
            break;
        case BMP_C_OS2_1x:
        case BMP_C_OS2_2x:
            readOs2InfoHeaderRest(ifP, headerP);
            break;
        }
        *errorP = NULL;
        *bytesReadP = cInfoHeaderSize;
    }
    /* Part of our anti-arithmetic overflow strategy is to make sure height
       and width always fit in 16 bits, so they can be multiplied together.
       This shouldn't be a problem, since they come from 16 bit fields in
       the BMP info header.
    */
    assert(headerP->cols < (1<<16));
    assert(headerP->rows < (1<<16));
}



static void
bmpReadColormap(FILE *         const ifP,
                enum bmpClass  const class,
                xel **         const colormapP,
                unsigned int   const cmapSize,
                unsigned int * const bytesReadP) {
/*----------------------------------------------------------------------------
   Read the color map from the present position in the input BMP file
   *ifP.

   The map has 'cmapSize' entries in it.  cmapSize == 0 means there is
   no color map.

   We return a color map as *colormapP.  If there is no color map in the
   BMP, this is just an arbitrary color map.

   'class' is the class of BMP image - Windows or OS/2.
-----------------------------------------------------------------------------*/
    xel * const colormap = pnm_allocrow(MAX(1, cmapSize));

    unsigned int i;
    unsigned int bytesRead;

    for (i = 0, bytesRead = 0; i < cmapSize; ++i) {
        /* There is a document that says the bytes are ordered R,G,B,Z,
           but in practice it appears to be the following instead:
        */
        unsigned int const b = GetByte(ifP);
        unsigned int const g = GetByte(ifP);
        unsigned int const r = GetByte(ifP);

        unsigned int j;

        PNM_ASSIGN(colormap[i], r, g, b);

        bytesRead += 3;

        for (j = 3; j < BMPlenrgb(class); ++j) {
            GetByte(ifP);
            bytesRead += 1;
        }
    }

    *colormapP  = colormap;
    *bytesReadP = bytesRead;
}



static void
extractBitFields(unsigned int       const rasterval,
                 struct pixelformat const pixelformat,
                 pixval             const maxval,
                 pixval *           const rP,
                 pixval *           const gP,
                 pixval *           const bP,
                 pixval *           const aP) {

    unsigned int const rbits =
        (rasterval >> pixelformat.red.shift) & pixelformat.red.mask;
    unsigned int const gbits =
        (rasterval >> pixelformat.grn.shift) & pixelformat.grn.mask;
    unsigned int const bbits =
        (rasterval >> pixelformat.blu.shift) & pixelformat.blu.mask;
    unsigned int const abits =
        (rasterval >> pixelformat.trn.shift) & pixelformat.trn.mask;

    *rP = pixelformat.red.mask > 0 ?
        (unsigned int) rbits * maxval / pixelformat.red.mask : 0;
    *gP = pixelformat.grn.mask > 0 ?
        (unsigned int) gbits * maxval / pixelformat.grn.mask : 0;
    *bP = pixelformat.blu.mask > 0 ?
        (unsigned int) bbits * maxval / pixelformat.blu.mask : 0;
    *aP = pixelformat.trn.mask > 0 ?
        (unsigned int) abits * maxval / pixelformat.trn.mask : 0;
}



static void
convertRow16(unsigned char      const bmprow[],
             xel                      xelrow[],
             int                const cols,
             struct pixelformat const pixelformat) {
    /* It's truecolor.  */

    unsigned int col;
    unsigned int cursor;
    cursor = 0;
    for (col=0; col < cols; ++col) {
        unsigned short const rasterval = (unsigned short)
            bmprow[cursor+1] << 8 | bmprow[cursor+0];

        pixval r, g, b, a;

        extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);

        PNM_ASSIGN(xelrow[col], r, g, b);

        cursor += 2;
    }
}



static void
convertRow24(unsigned char      const bmprow[],
             xel                      xelrow[],
             int                const cols,
             struct pixelformat const pixelformat) {

    /* It's truecolor */
    /* There is a document that gives a much different format for
       24 bit BMPs.  But this seems to be the de facto standard, and is,
       with a little ambiguity and contradiction resolved, defined in the
       Microsoft BMP spec.
    */

    unsigned int col;
    unsigned int cursor;

    cursor = 0;
    for (col = 0; col < cols; ++col) {
        pixval r, g, b, a;

        if (pixelformat.conventionalBgr) {
            r = bmprow[cursor+2];
            g = bmprow[cursor+1];
            b = bmprow[cursor+0];
            a = 0;
        } else {
            unsigned int const rasterval =
                (bmprow[cursor+0] << 16) +
                (bmprow[cursor+1] << 8) +
                (bmprow[cursor+2] << 0);

            extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);
        }
        PNM_ASSIGN(xelrow[col], r, g, b);
        cursor += 3;
    }
}



static void
convertRow32(unsigned char      const bmprow[],
             xel                      xelrow[],
             int                const cols,
             struct pixelformat const pixelformat) {

    /* It's truecolor */

    unsigned int col;
    unsigned int cursor;
    cursor = 0;
    for (col = 0; col < cols; ++col) {
        pixval r, g, b, a;

        if (pixelformat.conventionalBgr) {
            /* bmprow[cursor+3] is just padding */
            r = bmprow[cursor+2];
            g = bmprow[cursor+1];
            b = bmprow[cursor+0];
            a = 0;
        } else {
            unsigned int const rasterval =
                (bmprow[cursor+0] << 24) +
                (bmprow[cursor+1] << 16) +
                (bmprow[cursor+2] << 8) +
                (bmprow[cursor+3] << 0);

            extractBitFields(rasterval, pixelformat, 255, &r, &g, &b, &a);
        }

        PNM_ASSIGN(xelrow[col],
                   bmprow[cursor+2], bmprow[cursor+1], bmprow[cursor+0]);
        cursor += 4;
    }
}



static void
validateIndex(unsigned int const index,
              unsigned int const cmapSize ) {

    if (index >= cmapSize)
        pm_error("Error: invalid index to color palette.");
}



static void
convertRow(unsigned char      const bmprow[],
           xel                      xelrow[],
           int                const cols,
           unsigned int       const cBitCount,
           struct pixelformat const pixelformat,
           xel                const colormap[],
           unsigned int       const cmapSize
           ) {
/*----------------------------------------------------------------------------
   Convert a row in raw BMP raster format bmprow[] to a row of xels xelrow[].

   Use maxval 255 for the output xels.

   The BMP image has 'cBitCount' bits per pixel.

   If the image is colormapped, colormap[] is the colormap
   (colormap[i] is the color with color index i).
-----------------------------------------------------------------------------*/
    if (cBitCount == 24)
        convertRow24(bmprow, xelrow, cols, pixelformat);
    else if (cBitCount == 16)
        convertRow16(bmprow, xelrow, cols, pixelformat);
    else if (cBitCount == 32)
        convertRow32(bmprow, xelrow, cols, pixelformat);
    else if (cBitCount == 8) {
        /* It's a whole byte colormap index */
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            unsigned int const index = bmprow[col];
            validateIndex(index, cmapSize);
            xelrow[col] = colormap[index];
    }
    } else if (cBitCount == 1 || cBitCount == 2 || cBitCount == 4) {
        /* It's a bit field color index */
        unsigned char const mask = ( 1 << cBitCount ) - 1;

        unsigned int col;

        for (col = 0; col < cols; ++col) {
            unsigned int const cursor = (col*cBitCount)/8;
            unsigned int const shift = 8 - ((col*cBitCount) % 8) - cBitCount;
            unsigned int const index =
                (bmprow[cursor] & (mask << shift)) >> shift;
            validateIndex(index, cmapSize);
            xelrow[col] = colormap[index];
        }
    } else {
        /* Every possible BMP bits per pixel is handled above */
        assert(false);
    }
}



static unsigned char **
allocBmpRaster(unsigned int const rows,
               unsigned int const bytesPerRow) {

    unsigned int const storageSize =
        rows * sizeof(unsigned char *) + rows * bytesPerRow;
    unsigned char ** bmpRaster;
    unsigned int row;
    unsigned char * startOfRows;

    /* The raster array consists of an array of pointers to the rows
       followed by the rows of bytes, in a single allocated chunk of storage.
    */

    if (UINT_MAX / (bytesPerRow + sizeof(unsigned char *)) < rows)
        pm_error("raster is ridiculously large.");

    bmpRaster = (unsigned char **) malloc(storageSize);

    if (bmpRaster == NULL)
        pm_error("Unable to allocate %u bytes for the BMP raster\n",
                 storageSize);

    startOfRows = (unsigned char *)(bmpRaster + rows);

    for (row = 0; row < rows; ++row)
        bmpRaster[row] = startOfRows + row * bytesPerRow;

    return bmpRaster;
}



static void
readrow(FILE *           const ifP,
        unsigned int     const row,
        unsigned int     const bytesPerRow,
        unsigned char ** const bmpRaster,
        unsigned int *   const bytesReadP) {

    size_t bytesRead;

    assert(bytesPerRow > 0);

    bytesRead = fread(bmpRaster[row], 1, bytesPerRow, ifP);

    if (bytesRead < bytesPerRow) {
        if (feof(ifP))
            pm_error("End of file reading row %u of BMP raster.", row);
        else
            pm_error("Error reading BMP raster.  Errno=%d (%s)",
                     errno, strerror(errno));
    }
    *bytesReadP += bytesRead;
}



static void
nybbleAlign(unsigned char * const bytes,
            unsigned int    const nybbleCt){
/*----------------------------------------------------------------------------
  Shift the 'nybbleCt' nybbles of bytes[], after the first byte, one nybble
  toward the left, with the first of those nybble shifting into the right half
  of the first byte.  Leave the left half of the first byte alone.

  Example:

  (Numbers in hex, 8 nybbles)
            5? 13 7E 89 A1
   becomes  51 37 E8 9A 10
-----------------------------------------------------------------------------*/
    unsigned int const fullByteCt = (nybbleCt + 1) / 2;
    unsigned int i;

    bytes[0] >>= 4;

    for (i = 0; i < fullByteCt; ++i)
        bytes[i] = bytes[i] << 4 | bytes[i+1] >> 4;

    if (nybbleCt % 2 == 0) {
        /* There is a final right nybble.  Shift it. */
        bytes[fullByteCt] <<= 4;
    }
}



enum rleStatus { ABS_MODE, ENC_MODE, END_OF_ROW, END_OF_BMP, DELTA };

static enum rleStatus
readRLEcode(FILE *          const ifP,
            unsigned int *  const cntP,
            unsigned char * const codeP) {

    unsigned short s;
    enum rleStatus retval;

    s = GetBigShort(ifP);

    if      (s == 0) retval = END_OF_ROW;
    else if (s == 1) retval = END_OF_BMP;
    else if (s == 2) retval = DELTA;
    else if (s < 256) {
        if (cntP)
            *cntP = s & 0xff;
        retval = ABS_MODE;
    } else {
        if (cntP && codeP) {
            *cntP  = (s >> 8) & 0xff;
            *codeP = s & 0xff;
        }
        retval = ENC_MODE;
    }
    return retval;
}



static void
readrowRLE(FILE *           const ifP,
           unsigned int     const row,
           unsigned int     const cols,
           bool             const lastrow,
           BMPCompType      const compression,
           unsigned char ** const bmpRaster,
           unsigned int  *  const bytesReadP) {

    bool const rle4 = (compression == BMPCOMP_RLE4);
    int  const pixelsPerRowMargin = rle4 ? cols % 2 : 0;

    char const err_decode[] =
        "Error while decoding compressed BMP image.  "
        "%s.  Row: %u  Pixel: %u" ;

    unsigned int totalBytesRead;
    unsigned int pixelsRead;

    /* There are RLE4 images with rows coded up to the byte boundary,
       resulting in each row one pixel larger than the column length
       stated in the BMP info header (header.cols) when the column length
       is odd.

       pixelsPerRowMargin is a "wart" to provide for this case.
    */

    totalBytesRead = 0;  /* Initial value */
    pixelsRead = 0;      /* Initial value */

    while (true) {
        unsigned int n;
            /* decompressed bytes already read; current write point */
        unsigned int cnt;
        unsigned char code;

        n = rle4 ? (pixelsRead + 1) / 2 : pixelsRead;

        switch (readRLEcode(ifP, &cnt, &code)) {
        case ENC_MODE: {
            unsigned int const byteCnt = rle4 ? (cnt + 1) /2 : cnt;
            unsigned int i;

            if (pixelsRead + cnt > cols + pixelsPerRowMargin)
                pm_error(err_decode,  "Too many pixels in encoded mode",
                         row, pixelsRead );

            for (i = 0; i < byteCnt; ++i)
                bmpRaster[row][n+i] = code;

            if (rle4 && pixelsRead % 2 == 1)
                /* previous read ended odd */
                nybbleAlign(&bmpRaster[row][n-1], cnt);

            pixelsRead += cnt;
            totalBytesRead += 2;
        } break;

        case ABS_MODE: {
            unsigned int cmpBytesRead; /* compressed bytes read */
            /* align read-end to 16 bit boundary */
            unsigned int const bytesToRead =
                rle4 ? (cnt + 3) / 4 * 2 : (cnt + 1) / 2 * 2;

            if (pixelsRead + cnt > cols + pixelsPerRowMargin)
                pm_error(err_decode,  "Too many pixels in absolute mode",
                         row, pixelsRead);

            cmpBytesRead = fread(&bmpRaster[row][n],
                                 sizeof(char), bytesToRead, ifP);

            if (cmpBytesRead < bytesToRead) {
                if (feof(ifP))
                    pm_error("End of file reading row %u "
                             "of compressed BMP raster.", row);
                else
                    pm_error("Error reading BMP raster.  Errno=%d (%s)",
                             errno, strerror(errno));
            }
            if (rle4 && pixelsRead % 2 == 1) /* previous read ended odd */
                nybbleAlign(&bmpRaster[row][n-1], cnt);

            pixelsRead += cnt;
            totalBytesRead += cmpBytesRead + 2;
        } break;

        case END_OF_ROW: {
            if (cols == pixelsRead ||
                cols + pixelsPerRowMargin == pixelsRead) {
                if (!lastrow) {
                    *bytesReadP += totalBytesRead + 2;
                    return;
                } else if (readRLEcode(ifP, NULL, NULL) == END_OF_BMP) {
                    *bytesReadP += totalBytesRead +4;
                    return;
                } else
                    /* lastrow and END_OF_BITMAP not detected */
                    pm_error(err_decode,  "End of bitmap not marked",
                             row, pixelsRead );
            } else
                pm_error(err_decode,  "Premature end of row",
                         row, pixelsRead);
        } break;

        case END_OF_BMP: {
            if (lastrow && (cols == pixelsRead ||
                            cols + pixelsPerRowMargin == pixelsRead)){
                *bytesReadP += totalBytesRead + 2;
                return;
            } else
                pm_error(err_decode,  "Premature end of bitmap",
                         row, pixelsRead );
            /* Windows programs do not reject premature end of bitmap.
               Rather, they set the remaining pixels of the raster to
               an arbitrary value.  In practice, images with incomplete
               bitmaps are rare.
            */
        } break;

        case DELTA: {
            /* Delta means "move the point (col,row) by the amount given
               in the next two bytes."  Like premature end of bitmap, the
               official specs do not specify what value the skipped pixels
               should be set to.  Judging from Windows utilities, there is
               no consensus within Microsoft either.
            */
            pm_error(err_decode,
                     "Delta code in compressed BMP image.  "
                     "This program does not process deltas",
                     row, pixelsRead);

        } break;

        default:
            pm_error("Internal error processing RLE code in row %u", row);
        }
   }
}



static void
bmpReadraster(FILE *            const ifP,
              unsigned int      const cols,
              unsigned int      const rows,
              enum rowOrder     const rowOrder,
              unsigned int      const cBitCount,
              BMPCompType       const compression,
              unsigned char *** const bmpRasterP,
              unsigned int *    const bytesReadP) {
/*----------------------------------------------------------------------------
   Read the raster from the BMP file on *ifP (which is positioned to the
   raster).  The raster is 'rows' rows of 'cols' columns, 'cBitCount' bits per
   pixel, with rows in order 'rowOrder'.

   Return the raster in a newly malloced 2-dimensional array and return
   a pointer to that array as *bmpRasterP.

   Leave the input file positioned immediately after the raster and return
   as *bytesReadP the number of bytes we read from the file (i.e. the number
   of bytes in the raster portion of the file).
-----------------------------------------------------------------------------*/
    unsigned int const bytesPerRow =
        (compression == BMPCOMP_RLE4) ? cols / 2 + 2 :
        (compression == BMPCOMP_RLE8) ? cols + 1 :
        ((cols * cBitCount + 31) / 32) * 4;
        /* A BMP raster row is a multiple of 4 bytes, padded on the right
           with don't cares.
        */
    unsigned char ** bmpRaster;

    assert(cols < (1<<16));
    assert(bytesPerRow < (1<<16));

    bmpRaster = allocBmpRaster(rows, bytesPerRow);

    *bytesReadP = 0;

    /* row order BOTTOMUP is by far the most common case - the bottom
       line is first in the file, the top line last.

       We have never actually seen TOPDOWN, except in a Microsoft spec
    */

    switch(compression){
    case BMPCOMP_RGB:
    case BMPCOMP_BITFIELDS: {
        unsigned int i;
        for (i = 0; i < rows; ++i)
            readrow(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                    bytesPerRow, bmpRaster, bytesReadP);
    } break;
    case BMPCOMP_RLE4:
    case BMPCOMP_RLE8: {
        unsigned int i;
        /* Read all rows except last */
        assert(rows >= 1);
        for (i = 0; i < rows - 1; ++i){
            readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                       cols, FALSE, compression, bmpRaster, bytesReadP);
        }
        /* Read last row */
        readrowRLE(ifP, rowOrder == TOPDOWN ? i : rows - i - 1,
                   cols, TRUE,  compression, bmpRaster, bytesReadP);
    } break;
    case BMPCOMP_JPEG:
        pm_error("BMP file uses JPEG compression.  We don't know how to "
                 "interpret that.");
        break;
    case BMPCOMP_PNG:
        pm_error("BMP file uses PNG compression.  We don't know how to "
                 "interpret that.");
        break;
    }
    *bmpRasterP = bmpRaster;
}



static void
reportHeader(struct bmpInfoHeader const header,
             unsigned int         const offBits,
             bool                 const verbose) {

    if (verbose) {
        pm_message("BMP image header says:");
        pm_message("  Class of BMP: %s", BMPClassName(header.class));
        pm_message("  Width: %d pixels", header.cols);
        pm_message("  Height: %d pixels", header.rows);
        pm_message("  Depth: %d planes", header.cPlanes);
        pm_message("  Row order: %s",
                   header.rowOrder == BOTTOMUP ? "bottom up" : "top down");
        pm_message("  Byte offset of raster within file: %u", offBits);
        pm_message("  Bits per pixel in raster: %u", header.cBitCount);
        pm_message("  Compression: %s", BMPCompTypeName(header.compression));
        pm_message("  Colors in color map: %u", header.cmapSize);
    } else {
        pm_message("%s BMP, %ux%ux%u",
                   BMPClassName(header.class),
                   header.cols,
                   header.rows,
                   header.cBitCount);
    }
}



static void
validateCPlanes(unsigned short const cPlanes) {

    if (cPlanes != 1)
        pm_error("Error: invalid planes value in BMP header.  Must be 1");
}



static void
analyzeColors(xel          const colormap[],
              unsigned int const cmapSize,
              xelval       const maxval,
              bool *       const grayPresentP,
              bool *       const colorPresentP) {

    if (cmapSize == 0) {
        /* No colormap, and we're not about to search the entire raster,
           so we just assume it's full color
        */
        *colorPresentP = TRUE;
        *grayPresentP = TRUE;
    } else {
        unsigned int i;

        *colorPresentP = FALSE;  /* initial assumption */
        *grayPresentP = FALSE;   /* initial assumption */
        for (i = 0; i < cmapSize; ++i) {
            if (PPM_ISGRAY(colormap[i])) {
                if (PPM_GETR(colormap[i]) != 0 &&
                    PPM_GETR(colormap[i]) != maxval)
                    *grayPresentP = TRUE;
            } else
                *colorPresentP = TRUE;
        }
    }
}



static void
warnIfOffBitsWrong(struct bmpInfoHeader const bmpHeader,
                   unsigned int         const offBits) {

    if (offBits != BMPoffbits(bmpHeader.class, bmpHeader.cBitCount,
                              bmpHeader.cmapSize)) {

        pm_message("warning: the BMP header says the raster starts "
                   "at offset %u bytes into the file (offbits), "
                   "but that there are %u bytes of information before "
                   "the raster.  This inconsistency probably means the "
                   "input file is not a legal BMP file and is unusable.",
                   offBits,
                   BMPoffbits(bmpHeader.class, bmpHeader.cBitCount,
                              bmpHeader.cmapSize));
    }
}



static void
readColorMap(FILE *               const ifP,
             struct bmpInfoHeader const bmpHeader,
             xel **               const colorMapP,
             unsigned int *       const posP) {

    unsigned int bytesRead;

    bmpReadColormap(ifP, bmpHeader.class,
                    colorMapP, bmpHeader.cmapSize, &bytesRead);

    *posP += bytesRead;
}



static void
readRaster(FILE *               const ifP,
           struct bmpInfoHeader const bmpHeader,
           unsigned char ***    const bmpRasterP,
           unsigned int *       const posP) {

    unsigned int bytesRead;

    bmpReadraster(ifP, bmpHeader.cols, bmpHeader.rows, bmpHeader.rowOrder,
                  bmpHeader.cBitCount, bmpHeader.compression,
                  bmpRasterP, &bytesRead);

    *posP += bytesRead;
}



static bool
isValidBmpBpp(unsigned int const cBitCount) {

    switch (cBitCount) {
    case 1:
    case 2:
    case 4:
    case 8:
    case 16:
    case 24:
    case 32:
        return true;
    default:
        return false;
    }
}



static void
readBmp(FILE *               const ifP,
        unsigned char ***    const bmpRasterP,
        unsigned int *       const colsP,
        unsigned int *       const rowsP,
        bool *               const grayPresentP,
        bool *               const colorPresentP,
        unsigned int *       const cBitCountP,
        struct pixelformat * const pixelformatP,
        xel **               const colormapP,
        unsigned int *       const cmapSizeP,
        bool                 const verbose) {

    xel * colormap;  /* malloc'ed */
    unsigned int pos;
        /* Current byte position in the BMP file */

    /* The following are all information from the BMP headers */

    unsigned int offBits;
        /* Byte offset into file of raster */
    struct bmpInfoHeader bmpHeader;

    pos = 0;  /* Starting at the beginning ... */
    {
        unsigned int bytesRead;
        bmpReadfileheader(ifP, &bytesRead, &offBits);
        pos += bytesRead;
    }
    {
        unsigned int bytesRead;
        const char * error;
        bmpReadinfoheader(ifP, &bytesRead, &bmpHeader, &error);
        if (error)
            pm_error("Failed to read the BMP info header.  Image may "
                     "not be a valid BMP.  %s", error);

        if (verbose)
            pm_message("Read %u bytes of header", bytesRead);
        pos += bytesRead;
    }

    reportHeader(bmpHeader, offBits, verbose);

    validateCPlanes(bmpHeader.cPlanes);

    warnIfOffBitsWrong(bmpHeader, offBits);

    readColorMap(ifP, bmpHeader, &colormap, &pos);

    analyzeColors(colormap, bmpHeader.cmapSize, bmpMaxval,
                  grayPresentP, colorPresentP);

    readOffBytes(ifP, offBits - pos);

    pos = offBits;

    readRaster(ifP, bmpHeader, bmpRasterP, &pos);

    if (fgetc(ifP) != EOF)
        pm_message("warning: some image data remains unread.");

    if (!isValidBmpBpp(bmpHeader.cBitCount))
        pm_error("Invalid BMP image: 'cBitCount' field of header "
                 "(number of bits for each pixel in raster) is %u",
                 bmpHeader.cBitCount);

    *cBitCountP   = bmpHeader.cBitCount;

    *colsP        = bmpHeader.cols;
    *rowsP        = bmpHeader.rows;
    *pixelformatP = bmpHeader.pixelformat;
    *colormapP    = colormap;
    *cmapSizeP    = bmpHeader.cmapSize;
}



static void
writeRasterGen(unsigned char **   const bmpRaster,
               unsigned int       const cols,
               unsigned int       const rows,
               int                const format,
               unsigned int       const cBitCount,
               struct pixelformat const pixelformat,
               xel                const colormap[],
               unsigned int       const cmapSize) {
/*----------------------------------------------------------------------------
  Write the PNM raster to Standard Output, corresponding to the raw BMP
  raster bmpRaster.  Write the raster assuming the PNM image has
  dimensions 'cols' by 'rows' and format 'format', with maxval 255.

  The BMP image has 'cBitCount' bits per pixel, arranged in format
  'pixelformat'.

  If the image is colormapped, colormap[] is the colormap
  (colormap[i] is the color with color index i).

  writeRasterPbm() is faster for a PBM image.
-----------------------------------------------------------------------------*/
    xel * xelrow;
    unsigned int row;

    xelrow = pnm_allocrow(cols);

    for (row = 0; row < rows; ++row) {
        convertRow(bmpRaster[row], xelrow, cols, cBitCount, pixelformat,
                   colormap, cmapSize);
        pnm_writepnmrow(stdout, xelrow, cols, bmpMaxval, format, FALSE);
    }
    pnm_freerow(xelrow);
}



static void
writeRasterPbm(unsigned char ** const bmpRaster,
               unsigned int     const cols,
               unsigned int     const rows,
               xel              const colormap[]) {
/*----------------------------------------------------------------------------
  Write the PBM raster to Standard Output corresponding to the raw BMP
  raster bmpRaster.  Write the raster assuming the PBM image has
  dimensions 'cols' by 'rows'.

  The BMP image has 'cBitCount' bits per pixel, arranged in format
  'pixelformat'.

  The image must be colormapped; colormap[] is the colormap
  (colormap[i] is the color with color index i).  We cannot handle the
  abnormal case in which colormap[0] and colormap[1] have the same
  value (i.e. both white or both black.)

  We destroy *bmpRaster as a side effect.
-----------------------------------------------------------------------------*/
    unsigned int const colCharCt = pbm_packed_bytes(cols);

    unsigned int row;
    enum colorFormat {BlackWhite, WhiteBlack};
    enum colorFormat colorformat;

    if (PPM_GETR(colormap[0]) > 0)
        colorformat = WhiteBlack;
    else
        colorformat = BlackWhite;

    for (row = 0; row < rows; ++row){
        unsigned char * const bitrow = bmpRaster[row];

        if (colorformat == BlackWhite) {
            unsigned int i;
            for (i = 0; i < colCharCt; ++i)
                bitrow[i] = ~bitrow[i]; /* flip all pixels */
        }

        pbm_cleanrowend_packed(bitrow, cols);
        pbm_writepbmrow_packed(stdout, bitrow, cols, FALSE);
    }
}



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

    struct cmdlineInfo cmdline;
    FILE * ifP;
    int outputType;

    bool grayPresent, colorPresent;
        /* These tell whether the image contains shades of gray other than
           black and white and whether it has colors other than black, white,
           and gray.
        */
    unsigned int cols, rows;
    unsigned char ** bmpRaster;
        /* The raster part of the BMP image, as a row x column array, with
           each element being a raw byte from the BMP raster.  Note that
           bmpRaster[0] is really Row 0 -- the top row of the image, even
           though the bottom row comes first in the BMP format.
        */
    unsigned int cBitCount;
        /* Number of bits in BMP raster for each pixel */
    struct pixelformat pixelformat;
        /* Format of the raster bits for a single pixel */
    xel * colormap;
        /* Malloc'ed colormap (palette) from the BMP.  Contents of map
           undefined if not a colormapped BMP.
         */
    unsigned int cmapSize;
        /* Number of colormap entries.  From BMP header.  Note that a file may
           be 8 bits per pixel but have fewer than 256 colors.  In the 1 bit
           per pixel case, there should be 2 entries according to the official
           specification, but we allow files with just 1.
        */

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);
    if (streq(cmdline.inputFileName, "-"))
        ifname = "Standard Input";
    else
        ifname = cmdline.inputFileName;

    readBmp(ifP, &bmpRaster, &cols, &rows, &grayPresent, &colorPresent,
            &cBitCount, &pixelformat, &colormap, &cmapSize,
            cmdline.verbose);
    pm_close(ifP);

    if (colorPresent) {
        outputType = PPM_TYPE;
        pm_message("WRITING PPM IMAGE");
    } else if (grayPresent) {
        outputType = PGM_TYPE;
        pm_message("WRITING PGM IMAGE");
    } else {
        outputType = PBM_TYPE;
        pm_message("WRITING PBM IMAGE");
    }

    if (outputType == PBM_TYPE  && cBitCount == 1){
        pbm_writepbminit(stdout, cols, rows, FALSE);
        writeRasterPbm(bmpRaster, cols, rows, colormap);
    } else {
        pnm_writepnminit(stdout, cols, rows, bmpMaxval, outputType, FALSE);
        writeRasterGen(bmpRaster, cols, rows, outputType, cBitCount,
                       pixelformat, colormap, cmapSize);
    }
    free(colormap);
    free(bmpRaster);

    return 0;
}