about summary refs log tree commit diff
path: root/converter/other/winicontopam.c
blob: f6f89e1146d22c36f91679adff5f85233981d819 (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
/*=============================================================================
                             winicontopam
===============================================================================
  Convert from Windows icon format to PAM
=============================================================================*/

/*
  Here are some references for the Windows icon format:

  ICO (file format) - Wikipedia
  https://en.wikipedia.org/wiki/ICO_(file_format)

  ICO - Just Solve the File Format Problem
  http://fileformats.archiveteam.org/wiki/ICO
  (Has links to example icon file collections)

  GFF Format Summary: Microsoft Windows Cursor and Icon
  https://web.archive.org/web/20050421161512/http:/www.oreilly.com/www/centers/gff/formats/miccur/index.htm


*/

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

#include "netpbm/pm_config.h"
#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "netpbm/nstring.h"
#include "netpbm/shhopt.h"
#include "netpbm/pam.h"
#include "netpbm/pm_system.h"

#include "winicon.h"

#define RED   0
#define GRN   1
#define BLU   2
#define ALPHA 3
#define CHANNEL_CHARS "RGBA"



static bool verbose;



struct CmdlineInfo {

    const char * inputFileName;
    unsigned int allimages;
    unsigned int imageSpec;
    unsigned int image;
    unsigned int andmasks;
    unsigned int headerdump;
    unsigned int verbose;
};



static void
parseCommandLine(int argc, const char **argv,
                 struct CmdlineInfo * const cmdlineP) {

    optEntry *   option_def;
    unsigned int option_def_index;
    optStruct3   opt3;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;

    OPTENT3(0, "allimages",   OPT_FLAG,   NULL,
            &cmdlineP->allimages,         0);
    OPTENT3(0, "image",     OPT_UINT,   &cmdlineP->image,
            &cmdlineP->imageSpec,         0);
    OPTENT3(0, "andmasks",  OPT_FLAG,   NULL,
            &cmdlineP->andmasks,          0);
    OPTENT3(0, "headerdump",   OPT_FLAG,   NULL,
            &cmdlineP->headerdump,        0);
    OPTENT3(0, "verbose",   OPT_FLAG,   NULL,
            &cmdlineP->verbose,           0);

    opt3.opt_table     = option_def;
    opt3.short_allowed = false;
    opt3.allowNegNum   = false;

    pm_optParseOptions3(&argc, (char **)argv, opt3, sizeof(opt3), 0);

    if (cmdlineP->allimages && cmdlineP->imageSpec)
        pm_error("You cannot specify both -allimages and -image");

    if (argc-1 < 1)
        cmdlineP->inputFileName = "-";
    else {
        cmdlineP->inputFileName = argv[1];

        if (argc-1 > 1)
            pm_error("Too many arguments.  The only possible "
                     "non-option argument is the input file name");
    }

    free(option_def);
}



static unsigned char const pngSignature[] = PNG_SIGNATURE;



struct File {

    FILE *       fileP;
    const char * name;
    pm_filepos   pos;

};



static uint32_t
u8_le(const unsigned char * const buf,
      size_t                const offset) {

    return buf[offset + 0];
}



static uint32_t
u16_le(const unsigned char * const buf,
       size_t                const offset) {

    return
        ((uint32_t)buf[offset + 0] << 0) +
        ((uint32_t)buf[offset + 1] << 8);
}



static uint32_t
u32_le(const unsigned char * const buf,
       size_t                const offset) {

    return
        ((uint32_t)buf[offset + 0] <<  0) +
        ((uint32_t)buf[offset + 1] <<  8) +
        ((uint32_t)buf[offset + 2] << 16) +
        ((uint32_t)buf[offset + 3] << 24);
}



static uint32_t
s32_le(const unsigned char * const buf,
       size_t                const offset) {

    return
        ((uint32_t)buf[offset + 0] <<  0) +
        ((uint32_t)buf[offset + 1] <<  8) +
        ((uint32_t)buf[offset + 2] << 16) +
        ((uint32_t)buf[offset + 3] << 24);
}



static uint32_t
u8_be(const unsigned char * const buf,
      size_t                const offset) {

    return buf[offset + 0];
}



static uint32_t
u32_be(const unsigned char * const buf,
       size_t                const offset) {

    return
        ((uint32_t)buf[offset + 0] << 24) +
        ((uint32_t)buf[offset + 1] << 16) +
        ((uint32_t)buf[offset + 2] <<  8) +
        ((uint32_t)buf[offset + 3] <<  0);
}



static uint32_t
u32_xx(const unsigned char * const buf,
       size_t                const offset) {

    uint32_t u32;

    ((uint8_t*) &u32)[0] = buf[offset + 0];
    ((uint8_t*) &u32)[1] = buf[offset + 1];
    ((uint8_t*) &u32)[2] = buf[offset + 2];
    ((uint8_t*) &u32)[3] = buf[offset + 3];

    return (u32);
}



#ifndef LITERAL_FN_DEF_MATCH
static qsort_comparison_fn cmpfn;
#endif

static int
cmpfn(const void * const aP,
      const void * const bP) {

    const struct IconDirEntry * const dirEntryAP = aP;
    const struct IconDirEntry * const dirEntryBP = bP;

    if (dirEntryAP->offset < dirEntryBP->offset)
        return -1;
    else if (dirEntryAP->offset > dirEntryBP->offset)
        return +1;
    else
        return 0;
}



static void
dumpIconDir(const struct IconDir * const dirP) {

    unsigned int i;

    pm_message("Type: %u", dirP->type);
    pm_message("Icon directory has %u images:", dirP->count);

    for (i = 0; i < dirP->count; ++i) {
        const struct IconDirEntry * const dirEntryP = &dirP->entries[i];

        pm_message("width: %u", dirEntryP->width);
        pm_message("height: %u", dirEntryP->height);
        pm_message("color count: %u", dirEntryP->color_count);
        pm_message("# color planes: %u", dirEntryP->color_planes);
        pm_message("bits per pixel: %u", dirEntryP->bits_per_pixel);
        pm_message("offset in file of image: %u", dirEntryP->offset);
        pm_message("size of image: %u", dirEntryP->size);
        pm_message("zero field: %u", dirEntryP->zero);
    }
}



static struct IconDir *
readIconDir(struct File * const fP,
            bool          const needHeaderDump) {

    struct IconDir head;
    struct IconDir * dirP;
    uint32_t  imageIndex; /* more bits than dir.count */

    pm_readlittleshortu(fP->fileP, &head.zero);
    pm_readlittleshortu(fP->fileP, &head.type);
    pm_readlittleshortu(fP->fileP, &head.count);
    fP->pos += 6;

    if (head.zero != 0 || head.type != ICONDIR_TYPE_ICO)
        pm_error("Not a valid windows icon file");

    MALLOCVAR(dirP);

    if (dirP == NULL)
        pm_error("Couldn't allocate memory for Icon directory");

    MALLOCARRAY(dirP->entries, head.count);

    if (dirP->entries == NULL)
        pm_error("Could not allocate memory for %u entries in icon directory",
                 head.count);

    dirP->zero           = head.zero;
    dirP->type           = head.type;
    dirP->count          = head.count;
    dirP->entriesAllocCt = head.count;

    for (imageIndex = 0; imageIndex < head.count; ++imageIndex) {
        struct IconDirEntry * const dirEntryP = &dirP->entries[imageIndex];

        unsigned char widthField, heightField;

        unsigned long ul;

        pm_readcharu(fP->fileP, &widthField);
        dirEntryP->width  = (widthField == 0 ? 256 : widthField);

        pm_readcharu(fP->fileP, &heightField);
        dirEntryP->height = (heightField == 0 ? 256 : heightField);

        pm_readcharu(fP->fileP, &dirEntryP->color_count);

        pm_readcharu(fP->fileP, &dirEntryP->zero);

        pm_readlittleshortu(fP->fileP, &dirEntryP->color_planes);

        pm_readlittleshortu(fP->fileP, &dirEntryP->bits_per_pixel);

        pm_readlittlelongu(fP->fileP, &ul); dirEntryP->size = ul;

        pm_readlittlelongu(fP->fileP, &ul); dirEntryP->offset = ul;

        fP->pos += 16;

        dirEntryP->index = imageIndex;
    }

    /* The following is paranoia code only:

       I've never seen a windows icon file in the wild with having the entries
       in the directory stored in a different order than the images
       themselves.  However, the file format allows for it ...
     */
    qsort(dirP->entries, dirP->count, sizeof(struct IconDirEntry), cmpfn);

    if (verbose) {
        pm_message("%s icon directory (%u image%s):",
                   fP->name,
                   dirP->count, dirP->count == 1 ? "" : "s");

        for (imageIndex = 0; imageIndex < dirP->count; ++imageIndex) {
            const struct IconDirEntry * const dirEntryP =
                &dirP->entries[imageIndex];

            uint32_t colorCt;

            if (dirEntryP->bits_per_pixel == 0)
                colorCt = 0;
            else if (dirEntryP->bits_per_pixel >= 32)
                colorCt = 1u << 24;
            else
                colorCt = 1u << dirEntryP->bits_per_pixel;

            if (dirEntryP->color_count != 0 &&
                colorCt > dirEntryP->color_count) {
                colorCt = dirEntryP->color_count;
            }
            pm_message ("%5u: %3u x %3u, %8u colors, %5u bytes",
                        dirEntryP->index,
                        dirEntryP->width,
                        dirEntryP->height,
                        colorCt,
                        dirEntryP->size);
        }
    }

    if (needHeaderDump)
        dumpIconDir(dirP);

    return dirP;
}



static void
freeIconDir(struct IconDir * const dirP) {

    free(dirP->entries);
    free(dirP);
}



static const unsigned char *
readImage(struct File *         const fP,
          struct IconDirEntry * const dirEntryP) {

    size_t rc;
    unsigned char * image;
    uint32_t skippedCt;

    /*  Don't try to read an image that is smaller than the
        BITMAPINFOHEADER of BMP images (40 bytes).

        PNG compressed images can't be smaller than that either, as the
        PNG header plus the mandantory IHDR and IEND chunks already take
        8 + 25 + 12 = 35 bytes, and there is to be a IDAT chunk too.
     */
    if (dirEntryP->size < 40) {
        pm_error("image %2u: format violation: too small as an image.",
                  dirEntryP->index);
    }
    if ((pm_filepos) dirEntryP->offset < fP->pos)
        pm_error("image %2u: format violation: invalid offset.",
                 dirEntryP->index);

    /* The following is paranoia code only:

       I've never seen a windows icon file in the wild with gaps between
       the images, but the file format allows for it, and Microsoft
       expects the user to fseek() to the start of each image.
     */
    skippedCt = 0;

    while ((pm_filepos) dirEntryP->offset > fP->pos) {
        if (getc(fP->fileP) == EOF) {
            pm_error("seeking to image %u: unexpected EOF", dirEntryP->index);
        }
        ++fP->pos;
        ++skippedCt;
    }

    /*  The additional four bytes are for purify and friends, as the
        routines reading BMP XOR and AND masks might read (but not
        evaluate) some bytes beyond the image data.
     */
    image = malloc(dirEntryP->size + sizeof(uint32_t));
    if (image == NULL)
        pm_error("out of memory.");

    rc = fread (image, 1, dirEntryP->size, fP->fileP);
    if (rc != dirEntryP->size) {
        pm_error("reading image %2u: unexpected EOF", dirEntryP->index);
    }
    fP->pos += dirEntryP->size;

    return image;
}



static uint8_t
getIdx1(const unsigned char * const bitmap,
        uint32_t              const offset,
        int16_t               const col) {

    return u8_le(bitmap, offset + (col >> 3)) >> (7 - (col & 0x07)) & 0x1;
}



static uint8_t
getIdx4(const unsigned char * const bitmap,
        uint32_t              const offset,
        int16_t               const col) {

    if ((col & 1) == 0x0000)
        return u8_le(bitmap, offset + (col >> 1)) >> 4 & 0x0F;
    else
        return u8_le(bitmap, offset + (col >> 1)) >> 0 & 0x0F;
}



static uint8_t
getIdx8(const unsigned char * const bitmap,
        uint32_t              const offset,
        int16_t               const col) {

    return u8_le(bitmap, offset + col);
}



typedef unsigned char PaletteEntry[4];



static void
dumpPalette(const PaletteEntry * const palette,
            unsigned int         const colorCt) {

    unsigned int i;

    for (i = 0; i < colorCt; ++i) {
        pm_message("Color %u: (%u, %u, %u)",
                   i, palette[i][2], palette[i][1], palette[i][0]);
    }
}



static void
readXorPalette(struct BitmapInfoHeader * const hdrP,
               const unsigned char *     const bitmap,
               uint32_t                  const maxSize,
               tuple **                  const tuples,
               uint16_t                  const index,
               bool                      const needHeaderDump,
               uint32_t *                const bytesConsumedP) {

    uint32_t paletteSize;

    int16_t     row;
    const PaletteEntry * palette;
    uint32_t    truncatedXorSize;
    uint32_t    bytesConsumed;
    uint32_t    bytesPerRow;
    const unsigned char * bitmapCursor;
    uint32_t sizeRemaining;

    uint8_t (*getIdx) (const unsigned char * bitmap,
                       uint32_t rowOffset,
                       int16_t col);

    if (hdrP->compression_method != BI_RGB)
        pm_error("image %2u: invalid compression method %u.",
                 index, hdrP->compression_method);

    assert(hdrP->bits_per_pixel < 16);

    switch (hdrP->bits_per_pixel) {
    case 1:
        if (hdrP->colors_in_palette == 0)
            hdrP->colors_in_palette = 2;
        getIdx = getIdx1;
        break;

    case 4:
        if (hdrP->colors_in_palette == 0)
            hdrP->colors_in_palette = 16;
        getIdx = getIdx4;
        break;

    case 8:
        if (hdrP->colors_in_palette == 0)
            hdrP->colors_in_palette = 256;
        getIdx = getIdx8;
        break;

    default:
        pm_error("image %2u: "
                 "bits per pixel is a value we don't understand: %u",
                  index, hdrP->bits_per_pixel);
        getIdx = NULL;
    }

    bitmapCursor = &bitmap[0];  /* initial value */
    sizeRemaining = maxSize;    /* initial value */
    bytesConsumed = 0;          /* initial value */

    paletteSize = hdrP->colors_in_palette * 4;

    if (sizeRemaining < paletteSize)
        pm_error("image %2u: "
                 "reading palette: image truncated.", index);

    palette = (const PaletteEntry *) bitmapCursor;

    if (needHeaderDump)
        dumpPalette(palette, hdrP->colors_in_palette);

    bitmapCursor  += paletteSize;
    sizeRemaining -= paletteSize;
    bytesConsumed += paletteSize;

    {
        uint32_t const xorSize = (uint32_t)
            (((hdrP->bits_per_pixel * hdrP->bm_width + 31) / 32)
             * 4 * hdrP->bm_height / 2);

        if (sizeRemaining < xorSize) {
            pm_message("image %2u: "
                       "reading XOR mask: image truncated.", index);
            truncatedXorSize = sizeRemaining;
        } else
            truncatedXorSize = xorSize;
    }

    bytesPerRow = ((hdrP->bits_per_pixel * hdrP->bm_width + 31) / 32) * 4;

    for (row = 0; hdrP->bm_height / 2 > row; ++row) {
        uint32_t rowOffset;

        if (hdrP->top_down)
            rowOffset = row * bytesPerRow;
        else
            rowOffset = (hdrP->bm_height / 2 - row - 1) * bytesPerRow;

        if (rowOffset + bytesPerRow <= truncatedXorSize) {
            int16_t col;
            for (col = 0; hdrP->bm_width > col; ++col) {
                uint8_t const idx = getIdx(bitmapCursor, rowOffset, col);

                if (idx > hdrP->colors_in_palette)
                    pm_error("invalid palette index in row %u, column %u.",
                             row, col);

                /*  The palette is an array of little-endian 32-bit values,
                    where the RGB value is encoded as follows:

                    red:   bits 2^16..2^23
                    green: bits 2^8 ..2^15
                    blue:  bits 2^0 ..2^7
                 */
                tuples[row][col][PAM_RED_PLANE] = palette[idx][2];
                tuples[row][col][PAM_GRN_PLANE] = palette[idx][1];
                tuples[row][col][PAM_BLU_PLANE] = palette[idx][0];
            }
        }
    }

    bitmapCursor  += truncatedXorSize;
    sizeRemaining -= truncatedXorSize;
    bytesConsumed += truncatedXorSize;

    *bytesConsumedP = bytesConsumed;
}



static void
readXorBitfields(struct BitmapInfoHeader * const hdrP,
                 const unsigned char *     const bitmap,
                 uint32_t                  const maxSize,
                 tuple **                  const tuples,
                 uint16_t                  const index,
                 bool *                    const haveAlphaP,
                 uint32_t *                const bytesConsumedP) {
/*----------------------------------------------------------------------------
   Return as *haveAlphaP whether the Xor mask indicates the pixels are
   anything but fully opaque.
-----------------------------------------------------------------------------*/
    uint32_t   bitfields[4];
    uint8_t    shift    [4];
    sample     maxval   [4];

    int16_t      row;
    uint32_t     bytesConsumed;
    uint32_t     bytesPerSample;
    uint32_t     bytesPerRow;
    const unsigned char * bitmapCursor;
    uint32_t     sizeRemaining;
    uint32_t     truncatedXorSize;

    static uint8_t alphas [256];
    bool         allOpaque;
    bool         allTransparent;

    bytesConsumed = 0;

    if (hdrP->compression_method != BI_RGB
        && hdrP->compression_method != BI_BITFIELDS)
        pm_error("image %2u: invalid compression method %u.",
                 index, hdrP->compression_method);

    assert(hdrP->bits_per_pixel >= 16);

    switch (hdrP->bits_per_pixel) {
    case 16:
        bytesPerSample = 2;
        bitfields[RED]   = 0x7C00;
        bitfields[GRN]   = 0x03E0;
        bitfields[BLU]   = 0x001F;
        bitfields[ALPHA] = 0x0000;
        break;

    case 24:
        bytesPerSample = 3;
        bitfields[RED]   = 0xFF0000;
        bitfields[GRN]   = 0x00FF00;
        bitfields[BLU]   = 0x0000FF;
        bitfields[ALPHA] = 0x000000;
        break;

    case 32:
        bytesPerSample = 4;
        bitfields[RED]   = 0x00FF0000;
        bitfields[GRN]   = 0x0000FF00;
        bitfields[BLU]   = 0x000000FF;
        bitfields[ALPHA] = 0xFF000000;
        break;

    default:
        pm_error("image %2u: bits per pixel is one we don't understand: %u.",
                 index, hdrP->bits_per_pixel);
        bytesPerSample = 0;
    }

    bitmapCursor = &bitmap[0]; /* initial value */
    sizeRemaining = maxSize;  /* initial value */

    /*  read bit fields from image data  */
    if (hdrP->compression_method == BI_BITFIELDS) {
        if (sizeRemaining < 12)
            pm_error("image %2u: "
                     "reading bit fields: image truncated.", index);

        bitfields[RED]   = u32_le(bitmapCursor, 0);
        bitfields[GRN]   = u32_le(bitmapCursor, 4);
        bitfields[BLU]   = u32_le(bitmapCursor, 8);
        bitfields[ALPHA] = 0;

        bitmapCursor  += 12;
        sizeRemaining -= 12;
        bytesConsumed += 12;
    }

    /*  determine shift and maxval from bit field for each channel */
    {
        unsigned int i;

        for (i = 0; 4 > i; ++i) {
            if (bitfields[i] == 0) {
                maxval[i] = 1;
                shift [i] = 0;
            } else {
                unsigned int j;

                maxval[i] = bitfields[i];

                for (j = 0; 32 > j; ++j) {
                    if ((maxval[i] & 0x1) != 0)
                        break;
                    maxval[i] >>= 1;
                }
                shift[i] = j;
            }

        }
    }

    /*  read the XOR mask */
    {
        uint32_t const xorSize = (uint32_t)
            (((hdrP->bits_per_pixel * hdrP->bm_width + 31) / 32)
             * 4 * hdrP->bm_height / 2);

        if (sizeRemaining < xorSize) {
            pm_message("image %2u: "
                       "reading XOR mask: image truncated.", index);
            truncatedXorSize = sizeRemaining;
        } else
            truncatedXorSize = xorSize;
    }

    bytesPerRow = ((hdrP->bits_per_pixel * hdrP->bm_width + 31) / 32) * 4;
    MEMSZERO(alphas);

    for (row = 0, allOpaque = true, allTransparent = true;
         hdrP->bm_height / 2 > row;
         ++row) {

        uint32_t offset;

        if (hdrP->top_down)
            offset = row * bytesPerRow;
        else
            offset = (hdrP->bm_height / 2 - row - 1) * bytesPerRow;

        if (offset + bytesPerRow <= truncatedXorSize) {
            unsigned int col;
            for (col = 0; col < hdrP->bm_width; ++col) {
                uint32_t const pixel = u32_le(bitmapCursor, offset);
                offset += bytesPerSample;

                tuples[row][col][PAM_RED_PLANE] =
                    pnm_scalesample((pixel & bitfields[RED]) >> shift[RED],
                                    maxval[RED], 255);
                tuples[row][col][PAM_GRN_PLANE] =
                    pnm_scalesample((pixel & bitfields[GRN]) >> shift[GRN],
                                    maxval[GRN], 255);
                tuples [row][col][PAM_BLU_PLANE]
                    = pnm_scalesample((pixel & bitfields[BLU]) >> shift[BLU],
                                      maxval[BLU], 255);

                if (bitfields [ALPHA] != 0) {
                    tuples[row][col][PAM_TRN_PLANE]
                        = pnm_scalesample(
                            (pixel & bitfields[ALPHA]) >> shift[ALPHA],
                            maxval[ALPHA], 255);

                    if (tuples[row][col][PAM_TRN_PLANE] != 0)
                        allTransparent = false;

                    if (tuples [row][col][PAM_TRN_PLANE] != 255)
                        allOpaque = false;

                    alphas[tuples[row][col][PAM_TRN_PLANE]] = !0;
                }
            }
        }
    }

    bitmapCursor  += truncatedXorSize;
    sizeRemaining -= truncatedXorSize;
    bytesConsumed += truncatedXorSize;

    /*  A fully transparent alpha channel (all zero) in XOR mask is
        defined to be void by Microsoft.
    */
    if (verbose) {
        if (allTransparent)
            pm_message("image %2u: All pixels are nominally coded in the "
                       "transparency map as fully transparent, "
                       "which is defined by the format to mean they "
                       "are all opaque", index);
        if (allOpaque)
            pm_message("image %2u: All pixels are fully opaque "
                       "in the transparency map", index);
    }
    *haveAlphaP = !allTransparent && !allOpaque;

    if (!allTransparent && ! allOpaque && verbose) {
        unsigned int i;
        unsigned int c;

        for (i = 0, c = 0; 256 > i; ++i) {
            if (alphas[i] != 0)
                ++c;
        }
        pm_message("image %2u: %u distinct transparency value%s",
                   index, c, (c == 1) ? "": "s");
    }
    *bytesConsumedP = bytesConsumed;
}



static void
readAnd(struct BitmapInfoHeader * const hdrP,
        const unsigned char *     const bitmap,
        uint32_t                  const maxSize,
        tuple **                  const tuples,
        uint16_t                  const index,
        unsigned int              const plane,
        sample                    const maxval) {
/*----------------------------------------------------------------------------
  Fill in plane 'plane' of the tuple array 'tuples' according to the and
  mask of a Windows icon.  Where the and mask for a pixel is 1, set the
  plane to 'maxval'; where it is zero, set it to zero.

  'bitmap' is the and mask, in a format describe dby *hdrP.

  'index' is the position of the icon in question in the Windows icon file --
  the first image in the file is 0, second is 1, etc.
-----------------------------------------------------------------------------*/
    int16_t  row;
    uint32_t bytesConsumed;
    uint32_t bytesPerRow;
    uint32_t sizeRemaining;
    uint32_t truncatedAndSize;

    sizeRemaining = maxSize;  /* initial value */
    bytesConsumed = 0;  /* initial value */

    {
        uint32_t const andSize = (uint32_t)
            (((1 * hdrP->bm_width + 31) / 32) * 4 * hdrP->bm_height / 2);

        if (sizeRemaining < andSize) {
            pm_message ("image %2u: "
                        "Input image ends %u bytes into the %u-byte "
                        "AND mask.  Implying remainder of mask",
                        index, sizeRemaining, andSize);
            truncatedAndSize = sizeRemaining;
        } else
            truncatedAndSize = andSize;
    }

    bytesPerRow = ((1 * hdrP->bm_width + 31) / 32) * 4;

    for (row = 0; row < hdrP->bm_height / 2; ++row) {
        uint32_t offset;

        if (hdrP->top_down)
            offset = row * bytesPerRow;
        else
            offset = (hdrP->bm_height / 2 - row - 1) * bytesPerRow;

        if (offset + bytesPerRow <= sizeRemaining) {
            unsigned int col;

            for (col = 0; col < hdrP->bm_width; ++col) {
                tuples[row][col][plane] =
                    ((u8_le(bitmap, offset + col/8)
                      & (1 << (7 - (col & 0x7)))) == 0x00) ?
                    maxval : 0
                ;
            }
        }
    }
    sizeRemaining -= truncatedAndSize;
    bytesConsumed += truncatedAndSize;
}



static void
dumpBmpHeader(struct BitmapInfoHeader const hdr,
              unsigned int            const imageIndex) {

    pm_message("BMP header for Image %u:", imageIndex);

    pm_message("header size: %u", hdr.header_size);
    pm_message("bitmap width: %u", hdr.bm_width);
    pm_message("bitmap height * 2: %u", hdr.bm_height);
    pm_message("row order: %s", hdr.top_down ? "top down" : "bottom up");
    pm_message("# color planes: %u", hdr.color_planes);
    pm_message("bits per pixel: %u", hdr.bits_per_pixel);
    pm_message("image size: %u", hdr.image_size);
    pm_message("horizontal resolution: %u", hdr.horizontal_resolution);
    pm_message("vertical resolution: %u", hdr.vertical_resolution);
    pm_message("# colors in palette: %u", hdr.colors_in_palette);
    pm_message("# important colors: %u", hdr.important_colors);
}



static void
readBmpHeader(const unsigned char *     const image,
              uint32_t                  const size,
              unsigned int              const imageIndex,
              bool                      const needHeaderDump,
              struct BitmapInfoHeader * const hdrP) {

    /*  BITMAPINFOHEADER structure */

    if (size < 40)
        pm_error("image %2u: reading BITMAPINFOHEADER: not enough data.",
                 imageIndex);

    hdrP->header_size           = u32_le(image,  0);
    hdrP->bm_width              = s32_le(image,  4);
    hdrP->bm_height             = s32_le(image,  8);
    hdrP->color_planes          = u16_le(image, 12);
    hdrP->bits_per_pixel        = u16_le(image, 14);
    hdrP->compression_method    = u32_le(image, 16);
    hdrP->image_size            = u32_le(image, 20);
    hdrP->horizontal_resolution = s32_le(image, 24);
    hdrP->vertical_resolution   = s32_le(image, 28);
    hdrP->colors_in_palette     = u32_le(image, 32);
    hdrP->important_colors      = u32_le(image, 36);

    if (hdrP->bm_height > 0) {
        hdrP->top_down = false;
    } else {
        hdrP->top_down   = true;
        hdrP->bm_height *= -1;
    }

    if (hdrP->header_size < 36
        || hdrP->bm_width == 0 || hdrP->bm_height == 0
        || (hdrP->bm_height & 1) != 0x0000) {
        pm_error("image %2u: format violation: invalid BMP header.",
                 imageIndex);
    }

    if (needHeaderDump)
        dumpBmpHeader(*hdrP, imageIndex);
}



static void
readXorMask(struct BitmapInfoHeader * const hdrP,
            const unsigned char *     const imageCursor,
            uint32_t                  const imageSize,
            tuple **                  const tuples,
            uint16_t                  const index,
            bool                      const needHeaderDump,
            bool *                    const haveAlphaP,
            uint32_t *                const bytesConsumedP) {
/*----------------------------------------------------------------------------
   Read the so-called XOR mask (for non-monochrome images, this is the
   color pixmap, which may include transparency).

   Return the pixels as 'tuples', an array whose width and height are
   given by *hdrP and depth is 4.

   Return as *haveAlphaP whether the Xor mask indicates the pixels are
   anything but fully opaque.
-----------------------------------------------------------------------------*/
    /*  preset the PAM with fully opaque black (just in case the image
        is truncated and not all pixels are filled in below).
    */
    {
        unsigned int row;

        for (row = 0; row < hdrP->bm_height / 2; ++row) {
            unsigned int col;
            for (col = 0; col < hdrP->bm_width; ++col) {
                tuples[row][col][PAM_RED_PLANE] = 0;
                tuples[row][col][PAM_GRN_PLANE] = 0;
                tuples[row][col][PAM_BLU_PLANE] = 0;
                tuples[row][col][PAM_TRN_PLANE] = 255;
            }
        }
    }

    if (hdrP->bits_per_pixel < 16) {
        readXorPalette(hdrP, imageCursor, imageSize, tuples, index,
                       needHeaderDump,
                       bytesConsumedP);
        *haveAlphaP = false;
    } else
        readXorBitfields(hdrP, imageCursor, imageSize, tuples, index,
                         haveAlphaP, bytesConsumedP);
}



static void
reportImage(unsigned int            const imageIndex,
            struct BitmapInfoHeader const hdr,
            bool                    const haveAlpha) {

    const char * const style =
        haveAlpha ? "RGB +alpha" :
        hdr.bits_per_pixel < 16 ? "RGB/palette" :
        "RGB"
        ;

    pm_message("image %2u: "
               "BMP %3u x %3u x %2u (%s)",
               imageIndex,
               hdr.bm_width, hdr.bm_height / 2, hdr.bits_per_pixel,
               style);
}



static void
convertBmp(const unsigned char * const image,
           FILE *                const ofP,
           struct IconDirEntry * const dirEntryP,
           bool                  const needHeaderDump,
           bool                  const wantAndMaskPlane) {

    struct BitmapInfoHeader hdr;
    uint32_t                offset;
    bool                    haveAlpha;
    uint32_t                xorByteCt;

    struct pam outpam;
    tuple **   tuples;

    readBmpHeader(image, dirEntryP->size, dirEntryP->index, needHeaderDump,
                  &hdr);

    offset = hdr.header_size;  /* Start after header */

    if ((dirEntryP->width != hdr.bm_width)
        || (dirEntryP->height != hdr.bm_height / 2)) {
        pm_message("image %2u: "
                   "mismatch in header and image dimensions "
                   "(%u x %u vs. %u x %u)",
                   dirEntryP->index,
                   dirEntryP->width,
                   dirEntryP->height,
                   hdr.bm_width,
                   hdr.bm_height / 2);
    }

    if ((dirEntryP->bits_per_pixel != 0)
        && (dirEntryP->bits_per_pixel != hdr.bits_per_pixel)) {
        pm_message("image %2u "
                   "mismatch in header and image bpp value"
                   "(%u vs. %u)",
                   dirEntryP->index,
                   dirEntryP->bits_per_pixel,
                   hdr.bits_per_pixel);
    }

    outpam.size   = sizeof(struct pam);
    outpam.len    = PAM_STRUCT_SIZE(allocation_depth);
    outpam.file   = ofP;
    outpam.format = PAM_FORMAT;
    outpam.width  = hdr.bm_width;
    outpam.height = hdr.bm_height / 2;
    outpam.maxval = 255;
    outpam.allocation_depth = 5;
    outpam.depth  = 0;
        /* Just for tuple array allocation; we set the value for the actual
           output image below.
        */

    tuples = pnm_allocpamarray(&outpam);

    readXorMask(&hdr, &image[offset],
                dirEntryP->size - offset,
                tuples, dirEntryP->index, needHeaderDump,
                &haveAlpha, &xorByteCt);

    offset += xorByteCt;

    {
        /* If there is no alpha channel in the XOR mask, use the AND mask as
           the transparency plane.  Else, there are two transparency
           maps. If requested, return the AND mask as a fifth PAM plane.
        */
        bool haveAnd;
        unsigned int andPlane;

        if (!haveAlpha) {
            haveAnd = true;
            andPlane = PAM_TRN_PLANE;
            strcpy (outpam.tuple_type, "RGB_ALPHA");
            outpam.depth  = 4;
        } else if (wantAndMaskPlane) {
            haveAnd = true;
            andPlane = PAM_TRN_PLANE + 1;
            outpam.depth  = 5;
            strcpy(outpam.tuple_type, "RGB_ALPHA_ANDMASK");
        } else {
            haveAnd = false;
            strcpy (outpam.tuple_type, "RGB_ALPHA");
            outpam.depth  = 4;
        }
        if (haveAnd) {
            readAnd(&hdr, &image[offset], dirEntryP->size - offset,
                    tuples, dirEntryP->index, andPlane, outpam.maxval);
        }
    }
    pnm_writepam(&outpam, tuples);
    pnm_freepamarray(tuples, &outpam);

    reportImage(dirEntryP->index, hdr, haveAlpha);
}



static void
reportPngInfo(const unsigned char * const image,
              struct IconDirEntry * const dirEntryP) {

    struct PngIhdr ihdr;

    ihdr.length      = u32_be (image, sizeof(pngSignature)  +0);
    ihdr.signature   = u32_xx (image, sizeof(pngSignature)  +4);
    ihdr.width       = u32_be (image, sizeof(pngSignature)  +8);
    ihdr.height      = u32_be (image, sizeof(pngSignature) +12);
    ihdr.bit_depth   = u8_be  (image, sizeof(pngSignature) +16);
    ihdr.color_type  = u8_be  (image, sizeof(pngSignature) +17);
    ihdr.compression = u8_be  (image, sizeof(pngSignature) +18);
    ihdr.filter      = u8_be  (image, sizeof(pngSignature) +19);
    ihdr.interlace   = u8_be  (image, sizeof(pngSignature) +20);

    if ((ihdr.length != 13)
        || ihdr.signature != *(uint32_t*)"IHDR") {
        pm_message("image %2u: PNG (uncommonly formatted)",
                   dirEntryP->index);
    } else {
        uint32_t depth;
        const char * colorType;

        switch (ihdr.color_type) {
        case 0:
            colorType = "grayscale";
            depth     = ihdr.bit_depth;
            break;

        case 2:
            colorType = "RGB";
            depth     = ihdr.bit_depth * 3;
            break;

        case 3:
            colorType = "RGB/palette";
            depth     = 8;
            break;

        case 4:
            colorType = "grayscale + alpha";
            depth     = ihdr.bit_depth * 2;
            break;

        case 6:
            colorType = "RGB + alpha";
            depth     = ihdr.bit_depth * 4;
            break;

        default:
            colorType = "unknown color system";
            depth     = 0;
            break;
        }
        pm_message("image %2u: PNG %3u x %3u x %2u (%s)",
                   dirEntryP->index,
                   ihdr.width, ihdr.height, depth, colorType);

        if ((dirEntryP->width != ihdr.width)
            || (dirEntryP->height != ihdr.height)) {
            pm_message("image %2u:"
                       " mismatch in header and image dimensions"
                       " (%u x %u vs %u x %u)",
                       dirEntryP->index, dirEntryP->width, dirEntryP->height,
                       ihdr.width, ihdr.height);
        }
        /* Mismatch between dirEntryP->bits_per_pixel and 'depth' is
           normal, because the creator of the winicon file doesn't necessarily
           know the true color resolution.
        */
    }
}



static void
convertPng(const unsigned char * const image,
           FILE *                const ofP,
           struct IconDirEntry * const dirEntryP) {

    pm_bufferDesc imageBuffer;

    reportPngInfo(image, dirEntryP);

    imageBuffer.size              = dirEntryP->size;
    imageBuffer.buffer            = (unsigned char *)image;
    imageBuffer.bytesTransferredP = NULL;

    fflush(stdout);

    pm_system_lp("pngtopam", pm_feed_from_memory, &imageBuffer,
                 NULL /* stdout accepter */, NULL,
                 "pngtopam", "-alphapam", NULL);
}



static uint32_t
bestImage(struct IconDir * const dirP) {

    uint32_t imageIndex;
    uint32_t bestPixelCt;
    uint32_t bestColorCt;
    uint16_t best;

    bestPixelCt = 0;  /* initial value */
    bestColorCt = 0;  /* initial value */
    best        = 0;  /* initial value */

    for (imageIndex = 0; dirP->count > imageIndex; ++imageIndex) {
        struct IconDirEntry * const dirEntryP = &dirP->entries[imageIndex];

        uint32_t const pixelCt = dirEntryP->width * dirEntryP->height;

        uint32_t colorCt;

        /*  32-bit icons have 24 bit color information only.

            Since NT 5.1 (aka WinXP), it is allowed to place 8-bit
            transparency information in the remaining bits (to check,
            you have to read all these bits in the image!), so I prefer
            32-bit images over 24-bit images (which violate the
            spec. anyway).
        */
        if (dirEntryP->bits_per_pixel > 24)
            colorCt = 1u << 25;
        else
            colorCt = 1u << dirEntryP->bits_per_pixel;

        if (dirEntryP->color_count != 0 && colorCt > dirEntryP->color_count)
            colorCt = dirEntryP->color_count;

        if ((pixelCt > bestPixelCt)
            || ((pixelCt == bestPixelCt) && (colorCt > bestColorCt))) {
            /* This is a new best */
            bestPixelCt = pixelCt;
            bestColorCt = colorCt;
            best        = imageIndex;
        }
    }
    return best;
}



static void
convertImage(struct File *         const icoP,
             struct IconDirEntry * const dirEntryP,
             FILE *                const ofP,
             bool                  const needHeaderDump,
             bool                  const wantAndMaskPlane) {

    const unsigned char * image;  /* malloced */

    image = readImage(icoP, dirEntryP);

    if (MEMEQ(image, pngSignature, sizeof (pngSignature)))
        convertPng(image, ofP, dirEntryP);
    else
        convertBmp(image, ofP, dirEntryP, needHeaderDump, wantAndMaskPlane);

    free((void *)image);
}



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

    struct File ico;
    struct IconDir * dirP;
    struct CmdlineInfo cmdline;

    pm_proginit (&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    verbose = cmdline.verbose;

    ico.name =
        streq(cmdline.inputFileName, "-") ?  "<stdin>" : cmdline.inputFileName;
    ico.pos   = 0;
    ico.fileP = pm_openr(cmdline.inputFileName);

    dirP = readIconDir(&ico, cmdline.headerdump);

    if (cmdline.allimages) {
        unsigned int i;
        for (i = 0; i < dirP->count; ++i)
            convertImage(&ico, &dirP->entries[i], stdout,
                         cmdline.headerdump, cmdline.andmasks);
    } else if (cmdline.imageSpec) {
        unsigned int i;
        bool found;
        for (i = 0, found = false; i < dirP->count; ++i) {
            if (dirP->entries[i].index == cmdline.image) {
                found = true;
                convertImage(&ico, &dirP->entries[i], stdout,
                             cmdline.headerdump, cmdline.andmasks);
            }
        }
        if (!found)
            pm_error("no image index %u in.input", cmdline.image);
    } else {
        convertImage(&ico, &dirP->entries[bestImage(dirP)], stdout,
                     cmdline.headerdump, cmdline.andmasks);
    }

    freeIconDir(dirP);

    if (ico.fileP != stdin)
        pm_close(ico.fileP);

    return 0;
}