-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch2.diff
1514 lines (1503 loc) · 46.5 KB
/
patch2.diff
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
--- ./netpbm-10.47.67/converter/other/bmptopnm.c 2017-06-29 01:11:44.000000000 +0300
+++ ./bmp-to-pgm/bmptopnm.h 2018-03-22 15:40:57.444505718 +0300
@@ -1,5 +1,5 @@
/*****************************************************************************
- bmptopnm.c
+ bmptopnm.h
******************************************************************************
Bmptopnm - Converts from a Microsoft Windows or OS/2 .BMP file to a
@@ -23,24 +23,16 @@
nobody is using 16 bit BMP.
*****************************************************************************/
-#include <string.h>
-#include <limits.h>
-#include <assert.h>
+#ifndef _BMPTOPNM_H_
+#define _BMPTOPNM_H_
-#include "pm_c_util.h"
-#include "pnm.h"
-#include "shhopt.h"
-#include "nstring.h"
-#include "bmp.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
/* MAXCOLORS is the maximum size of a color map in a BMP image */
#define MAXCOLORS 256
-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 {
@@ -107,1241 +99,7 @@
struct pixelformat pixelformat;
};
-
-
-struct cmdline_info {
- /* All the information the user supplied in the command line,
- in a form easy for the program to use.
- */
- const char *input_filespec; /* Filespecs of input files */
- int verbose; /* -verbose option */
-};
-
-static const char *ifname;
-
-
-
-static void
-parse_command_line(int argc, char ** argv,
- struct cmdline_info *cmdline_p) {
-/*----------------------------------------------------------------------------
- Note that the file spec array we return is stored in the storage that
- was passed to us as the argv array.
------------------------------------------------------------------------------*/
- optStruct *option_def = malloc(100*sizeof(optStruct));
- /* Instructions to OptParseOptions2 on how to parse our options.
- */
- optStruct2 opt;
-
- unsigned int option_def_index;
-
- option_def_index = 0; /* incremented by OPTENTRY */
- OPTENTRY(0, "verbose", OPT_FLAG, &cmdline_p->verbose, 0);
-
- /* Set the defaults */
- cmdline_p->verbose = FALSE;
-
- 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 */
-
- optParseOptions2(&argc, argv, opt, 0);
- /* Uses and sets argc, argv, and some of *cmdline_p and others. */
-
- if (argc-1 == 0)
- cmdline_p->input_filespec = "-";
- else if (argc-1 != 1)
- pm_error("Program takes zero or one argument (filename). You "
- "specified %d", argc-1);
- else
- cmdline_p->input_filespec = 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;
-}
-
-
-
-typedef struct {
- long dummy[12];
-} cieXyzTriple;
-
-static cieXyzTriple
-GetCieXyzTriple(FILE * const fp) {
-
- cieXyzTriple v;
- unsigned int i;
-
- for (i = 0; i < 12; ++i)
- if (pm_readlittlelong(fp, &v.dummy[i]) == -1)
- pm_error(er_read, ifname);
-
- return v;
-}
-
-
-
-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 short xHotSpot;
- unsigned short yHotSpot;
- unsigned long offBits;
- unsigned long int fileSize;
-
-
- 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;
-
- *bytesReadP = 14;
-}
-
-
-
-static void
-readOs2InfoHeader(FILE * const ifP,
- struct bmpInfoHeader * const headerP) {
-
- unsigned short colsField, rowsField;
- unsigned short planesField, bitCountField;
-
- headerP->class = C_OS2;
-
- 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->compression = COMP_RGB;
-
- pm_message("OS/2 BMP, %dx%dx%d",
- headerP->cols, headerP->rows, headerP->cBitCount);
-}
-
-
-
-static void
-validateCompression(unsigned long const compression,
- enum rowOrder const rowOrder,
- unsigned int const cBitCount) {
-
- if (compression != COMP_RGB && compression != COMP_BITFIELDS &&
- compression != COMP_RLE4 && compression != COMP_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, COMP_RGB, COMP_BITFIELDS,
- COMP_RLE4, COMP_RLE8);
-
- if ((compression == COMP_RLE4 || compression == COMP_RLE8) &&
- rowOrder == TOPDOWN )
- pm_error("Invalid BMP header. Claims image is top-down and also "
- "compressed, which is an impossible combination.");
-
- if ( (compression == COMP_RLE4 && cBitCount !=4) ||
- (compression == COMP_RLE8 && cBitCount !=8) )
- pm_error("Invalid BMP header. "
- "Compression type (%s) disagrees with "
- "number of bits per pixel (%u).",
- compression == COMP_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 colorsimportant; /* ColorsImportant value from header */
- int colorsused; /* ColorsUsed value from header */
- long colsField;
-
- headerP->class = C_WIN;
-
- colsField = GetLong(ifP);
-
- if (colsField <= 0)
- pm_error("Invalid BMP file: says width is %ld", colsField);
-
- headerP->cols = (unsigned long)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;
- }
- }
- headerP->cPlanes = GetShort(ifP);
- headerP->cBitCount = GetShort(ifP);
-
- {
- unsigned long int const compression = GetLong(ifP);
-
- headerP->bitFields = (compression == COMP_BITFIELDS);
-
- validateCompression(compression, headerP->rowOrder,
- headerP->cBitCount);
-
- 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
- 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 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
-readV4InfoHeaderExtension(FILE * const ifP,
- struct bmpInfoHeader * const headerP) {
-
- 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(GetLong(ifP));
- headerP->pixelformat.grn = bitPositionFromMask(GetLong(ifP));
- headerP->pixelformat.blu = bitPositionFromMask(GetLong(ifP));
- headerP->pixelformat.trn = bitPositionFromMask(GetLong(ifP));
-
- computeConventionalBgr(&headerP->pixelformat, headerP->cBitCount);
- } else
- headerP->pixelformat = defaultPixelformat(headerP->cBitCount);
-
- GetLong(ifP); /* Color space */
- GetCieXyzTriple(ifP); /* Endpoints */
- GetLong(ifP); /* GammaRed */
- GetLong(ifP); /* GammaGreen */
- GetLong(ifP); /* GammaBlue */
-}
-
-
-
-static void
-defaultV4InfoHeaderExtension(struct bmpInfoHeader * const headerP) {
-
- headerP->pixelformat = defaultPixelformat(headerP->cBitCount);
-
-}
-
-
-
-static void
-readWindowsInfoHeader(FILE * const ifP,
- unsigned int const cInfoHeaderSize,
- struct bmpInfoHeader * const headerP) {
-
- /* 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.
- */
- readWindowsBasic40ByteInfoHeader(ifP, headerP);
-
- if (cInfoHeaderSize >= 108)
- readV4InfoHeaderExtension(ifP, headerP);
- else
- defaultV4InfoHeaderExtension(headerP);
-
- if (cInfoHeaderSize >= 124) {
- /* Read off the V5 info header extension. */
- GetLong(ifP); /* Intent */
- GetLong(ifP); /* ProfileData */
- GetLong(ifP); /* ProfileSize */
- GetLong(ifP); /* Reserved */
- }
-
- pm_message("Windows BMP, %dx%dx%d",
- headerP->cols, headerP->rows, headerP->cBitCount);
-}
-
-
-
-static void
-BMPreadinfoheader(FILE * const ifP,
- unsigned int * const bytesReadP,
- struct bmpInfoHeader * const headerP) {
-
- unsigned int const cInfoHeaderSize = GetLong(ifP);
-
- switch (cInfoHeaderSize) {
- case 12:
- readOs2InfoHeader(ifP, headerP);
- break;
- case 40:
- readWindowsInfoHeader(ifP, cInfoHeaderSize, headerP);
- break;
- case 108:
- pm_error("%s: this is a Version 4 Windows BMP; "
- "this program knows only Version 1");
- break;
- case 124:
- pm_error("%s: this is a Version 5 Windows BMP; "
- "this program knows only Version 1");
- break;
- default:
- pm_error("%s: unknown Info Header size: %u bytes",
- ifname, cInfoHeaderSize);
- break;
- }
- *bytesReadP = cInfoHeaderSize;
-}
-
-
-
-static void
-BMPreadcolormap(FILE * const ifP,
- int 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.
------------------------------------------------------------------------------*/
-
- unsigned int i;
-
- xel * colormap;
- unsigned int bytesRead;
-
- colormap = pnm_allocrow(MAX(1,cmapsize));
-
- bytesRead = 0; /* initial value */
-
- for (i = 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 r, g, b;
-
- b = GetByte(ifP);
- g = GetByte(ifP);
- r = GetByte(ifP);
-
- PNM_ASSIGN(colormap[i], r, g, b);
-
- bytesRead += 3;
-
- if (class == C_WIN) {
- 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
-convertRow(unsigned char const bmprow[],
- xel xelrow[],
- int const cols,
- unsigned int const cBitCount,
- struct pixelformat const pixelformat,
- xel const colormap[]
- ) {
-/*----------------------------------------------------------------------------
- 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)
- xelrow[col] = colormap[bmprow[col]];
- } else if (cBitCount < 8) {
- /* 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;
- xelrow[col] = colormap[index];
- }
- } else
- pm_error("Internal error: invalid cBitCount in convertRow()");
-}
-
-
-
-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
-nibbleAlign(unsigned char * const ptr,
- unsigned int const nibbles){
-/*----------------------------------------------------------------------------
- Shift data pointed by ptr one half byte toward the MSB (to the left).
-
- Example:
-
- (Numbers in hex, 8 nibbles)
- 5F 13 7E 89 A1
- becomes 51 37 E8 9A 10
------------------------------------------------------------------------------*/
- unsigned int const fullByteCount = (nibbles-1) / 2;
- unsigned int i;
-
- ptr[0] = ptr[0] & ptr[1] >> 4;
-
- for (i = 0; i < fullByteCount; ++i)
- ptr[i+1] = ptr[i+1] << 4 & ptr[i+2] >> 4;
-
- if (nibbles % 2 == 1) /* if there is a final odd nibble */
- ptr[fullByteCount+1] <<= 4; /* shift it a half byte */
-}
-
-
-
-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,
- unsigned long const compression,
- unsigned char ** const BMPraster,
- unsigned int * const bytesReadP) {
-
- bool const RLE4 = (compression == COMP_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 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 */
- nibbleAlign(&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],