This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvbmp.c
1183 lines (952 loc) · 29.3 KB
/
xvbmp.c
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
/*
* xvbmp.c - I/O routines for .BMP files (MS Windows 3.x and later; OS/2)
*
* LoadBMP(fname, numcols)
* WriteBMP(fp, pic, ptype, w, h, r, g, b, numcols, style);
*/
#include "copyright.h"
#include "xv.h"
/* Comments on error-handling:
A truncated file is not considered a Major Error. The file is loaded,
and the rest of the pic is filled with 0's.
A file with garbage characters in it is an unloadable file. All allocated
stuff is tossed, and LoadBMP returns non-zero.
Not being able to malloc is a Fatal Error. The program is aborted. */
#define BI_RGB 0 /* a.k.a. uncompressed */
#define BI_RLE8 1
#define BI_RLE4 2
#define BI_BITFIELDS 3 /* BMP version 4 */
#define BI_JPEG 4 /* BMP version 5 (not yet supported) */
#define BI_PNG 5 /* BMP version 5 (not yet supported) */
#define WIN_OS2_OLD 12
#define OS2_NEW 64
#define WIN_3X 40
#define WIN_95_NT4 108
#define WIN_98_2K 124
#define WIN_NT_2K 128
#if (defined(UINT_MAX) && UINT_MAX != 0xffffffffU)
# error XV's BMP code requires 32-bit unsigned integer type, but u_int isn't
#endif
static long filesize;
static int loadBMP1 PARM((FILE *, byte *, u_int, u_int, int));
static int loadBMP4 PARM((FILE *, byte *, u_int, u_int, u_int, int));
static int loadBMP8 PARM((FILE *, byte *, u_int, u_int, u_int, int));
static int loadBMP16 PARM((FILE *, byte *, u_int, u_int, u_int *, int));
static int loadBMP24 PARM((FILE *, byte *, u_int, u_int, u_int, int));
static int loadBMP32 PARM((FILE *, byte *, u_int, u_int, u_int *, int));
static u_int getshort PARM((FILE *));
static u_int getint PARM((FILE *));
static void putshort PARM((FILE *, int));
static void putint PARM((FILE *, int));
static void writeBMP1 PARM((FILE *, byte *, int, int));
static void writeBMP4 PARM((FILE *, byte *, int, int));
static void writeBMP8 PARM((FILE *, byte *, int, int));
static void writeBMP24 PARM((FILE *, byte *, int, int));
static int bmpError PARM((const char *, const char *));
#define FERROR(fp) (ferror(fp) || feof(fp))
/*******************************************/
int LoadBMP(fname, pinfo)
char *fname;
PICINFO *pinfo;
/*******************************************/
{
FILE *fp;
int i, c, c1, rv, bPad, rightsideup = 0;
u_int bfSize, bfOffBits, biSize, biWidth, biPlanes;
u_int biBitCount, biCompression, biSizeImage, biXPelsPerMeter;
u_int biYPelsPerMeter, biClrUsed, biClrImportant;
u_int npixels;
u_int colormask[3];
int biHeight;
char buf[512], rgb_bits[16];
const char *cmpstr, *bname;
byte *pic24, *pic8;
/* returns '1' on success */
pic8 = pic24 = (byte *) NULL;
bname = BaseName(fname);
fp = xv_fopen(fname,"r");
if (!fp) return (bmpError(bname, "couldn't open file"));
fseek(fp, 0L, 2); /* figure out the file size */
filesize = ftell(fp);
fseek(fp, 0L, 0);
/* read the file type (first two bytes) */
c = getc(fp); c1 = getc(fp);
if (c!='B' || c1!='M') { bmpError(bname,"file type != 'BM'"); goto ERROR; }
bfSize = getint(fp);
getshort(fp); /* reserved and ignored */
getshort(fp);
bfOffBits = getint(fp);
biSize = getint(fp);
if (biSize == WIN_3X || biSize == OS2_NEW ||
biSize == WIN_95_NT4 || biSize == WIN_98_2K || biSize == WIN_NT_2K) {
biWidth = getint(fp);
biHeight = getint(fp);
biPlanes = getshort(fp);
biBitCount = getshort(fp);
biCompression = getint(fp);
biSizeImage = getint(fp);
biXPelsPerMeter = getint(fp);
biYPelsPerMeter = getint(fp);
biClrUsed = getint(fp);
biClrImportant = getint(fp);
}
else { /* old bitmap format */
biWidth = getshort(fp); /* Types have changed ! */
biHeight = getshort(fp);
biPlanes = getshort(fp);
biBitCount = getshort(fp);
/* not in old versions, so have to compute them */
biSizeImage = (((biPlanes * biBitCount*biWidth)+31)/32)*4*biHeight;
biCompression = BI_RGB;
biXPelsPerMeter = biYPelsPerMeter = 0;
biClrUsed = biClrImportant = 0;
}
/* Height could be negative for uncompressed BMP files if data is
stored right-side-up */
npixels = biWidth * biHeight;
if (biWidth != 0 && npixels/biWidth != biHeight && biCompression == BI_RGB) {
u_int negHeight, npixels2;
negHeight = biHeight * -1;
npixels2 = biWidth * negHeight;
if (npixels2/biWidth == negHeight) {
biHeight = negHeight;
npixels = npixels2;
rightsideup = 1;
}
}
if (DEBUG>1) {
fprintf(stderr,"\nLoadBMP:\tbfSize=%d, bfOffBits=%d\n",bfSize,bfOffBits);
fprintf(stderr,"\t\tbiSize=%d, biWidth=%d, biHeight=%d, biPlanes=%d\n",
biSize, biWidth, biHeight, biPlanes);
fprintf(stderr,"\t\tbiBitCount=%d, biCompression=%d, biSizeImage=%d\n",
biBitCount, biCompression, biSizeImage);
fprintf(stderr,"\t\tbiX,YPelsPerMeter=%d,%d biClrUsed=%d, biClrImp=%d\n",
biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant);
}
if (FERROR(fp)) { bmpError(bname,"EOF reached in file header"); goto ERROR; }
/* error-checking */
if ((biBitCount!=1 && biBitCount!=4 && biBitCount!=8 &&
biBitCount!=16 && biBitCount!=24 && biBitCount!=32) ||
biPlanes!=1 || biCompression>BI_PNG ||
biWidth<=0 || biHeight<=0 ||
(biClrUsed && biClrUsed > (1 << biBitCount))) {
sprintf(buf,
"Unsupported BMP type (%dx%d, Bits=%d, Colors=%d, Planes=%d, "
"Compr=%d)",
biWidth, biHeight, biBitCount, biClrUsed, biPlanes, biCompression);
bmpError(bname, buf);
goto ERROR;
}
if (biCompression>BI_BITFIELDS) {
sprintf(buf, "Unsupported BMP compression method (%s)",
biCompression == BI_JPEG? "JPEG" :
biCompression == BI_PNG? "PNG" :
"unknown/newer than v5");
bmpError(bname, buf);
goto ERROR;
}
if (((biBitCount==1 || biBitCount==24) && biCompression != BI_RGB) ||
(biBitCount==4 && biCompression!=BI_RGB && biCompression!=BI_RLE4) ||
(biBitCount==8 && biCompression!=BI_RGB && biCompression!=BI_RLE8) ||
((biBitCount==16 || biBitCount==32) &&
biCompression!=BI_RGB && biCompression!=BI_BITFIELDS)) {
sprintf(buf,"Unsupported BMP type (bitCount=%d, Compression=%d)",
biBitCount, biCompression);
bmpError(bname, buf);
goto ERROR;
}
bPad = 0;
if (biSize != WIN_OS2_OLD) {
/* skip ahead to colormap, using biSize */
c = biSize - 40; /* 40 bytes read from biSize to biClrImportant */
for (i=0; i<c; i++)
getc(fp);
bPad = bfOffBits - (biSize + 14);
}
/* 16-bit or 32-bit color mask */
if (biCompression==BI_BITFIELDS) {
colormask[0] = getint(fp);
colormask[1] = getint(fp);
colormask[2] = getint(fp);
bPad -= 12;
}
if (biClrUsed > (1 << biBitCount))
biClrUsed = (1 << biBitCount);
/* load up colormap, if any */
if (biBitCount == 1 || biBitCount == 4 || biBitCount == 8) {
int i, cmaplen;
cmaplen = (biClrUsed) ? biClrUsed : 1 << biBitCount;
for (i=0; i<cmaplen; i++) {
pinfo->b[i] = getc(fp);
pinfo->g[i] = getc(fp);
pinfo->r[i] = getc(fp);
if (biSize != WIN_OS2_OLD) {
getc(fp);
bPad -= 4;
}
}
if (FERROR(fp))
{ bmpError(bname,"EOF reached in BMP colormap"); goto ERROR; }
if (DEBUG>1) {
fprintf(stderr,"LoadBMP: BMP colormap: (RGB order)\n");
for (i=0; i<cmaplen; i++) {
fprintf(stderr,"%02x%02x%02x ", pinfo->r[i],pinfo->g[i],pinfo->b[i]);
}
fprintf(stderr,"\n\n");
}
}
if (biSize != WIN_OS2_OLD) {
/* Waste any unused bytes between the colour map (if present)
and the start of the actual bitmap data. */
while (bPad > 0) {
(void) getc(fp);
bPad--;
}
}
/* create pic8 or pic24 */
if (biBitCount==16 || biBitCount==24 || biBitCount==32) {
u_int count = 3 * npixels;
if (biWidth == 0 || biHeight == 0 || npixels/biWidth != biHeight ||
count/3 != npixels)
return (bmpError(bname, "image dimensions too large"));
pic24 = (byte *) calloc((size_t) (count + 1), (size_t) 1);
if (!pic24) return (bmpError(bname, "couldn't malloc 'pic24'"));
}
else {
if (biWidth == 0 || biHeight == 0 || npixels/biWidth != biHeight)
return (bmpError(bname, "image dimensions too large"));
pic8 = (byte *) calloc((size_t) (npixels + 1), (size_t) 1);
if (!pic8) return(bmpError(bname, "couldn't malloc 'pic8'"));
}
WaitCursor();
/* load up the image */
switch (biBitCount) {
case 1:
rv = loadBMP1(fp, pic8, biWidth, biHeight, rightsideup);
break;
case 4:
rv = loadBMP4(fp, pic8, biWidth, biHeight, biCompression, rightsideup);
break;
case 8:
rv = loadBMP8(fp, pic8, biWidth, biHeight, biCompression, rightsideup);
break;
case 16:
rv = loadBMP16(fp, pic24, biWidth, biHeight, /* v-- BI_RGB */
biCompression == BI_BITFIELDS? colormask : NULL,
rightsideup);
break;
default:
if (biBitCount == 32 && biCompression == BI_BITFIELDS)
rv = loadBMP32(fp, pic24, biWidth, biHeight, colormask, rightsideup);
else /* 24 or (32 and BI_RGB) */
rv = loadBMP24(fp, pic24, biWidth, biHeight, biBitCount, rightsideup);
break;
}
if (rv) bmpError(bname, "File appears truncated. Winging it.");
fclose(fp);
if (biBitCount > 8) {
pinfo->pic = pic24;
pinfo->type = PIC24;
}
else {
pinfo->pic = pic8;
pinfo->type = PIC8;
}
cmpstr = "";
if (biCompression == BI_RLE4) cmpstr = ", RLE4 compressed";
else if (biCompression == BI_RLE8) cmpstr = ", RLE8 compressed";
else if (biCompression == BI_BITFIELDS) {
int bit, c[3], i;
u_int mask;
for (i = 0; i < 3; ++i) {
mask = colormask[i];
c[i] = 0;
for (bit = 0; bit < 32; ++bit) {
if (mask & 1)
++c[i];
mask >>= 1;
}
}
sprintf(rgb_bits, ", RGB%d%d%d", c[0], c[1], c[2]);
cmpstr = rgb_bits;
}
pinfo->w = biWidth; pinfo->h = biHeight;
pinfo->normw = pinfo->w; pinfo->normh = pinfo->h;
pinfo->frmType = F_BMP;
pinfo->colType = F_FULLCOLOR;
sprintf(pinfo->fullInfo, "%sBMP, %d bit%s per pixel%s. (%ld bytes)",
((biSize==WIN_OS2_OLD) ? "OS/2 1.x " :
(biSize==OS2_NEW) ? "OS/2 2.x " :
(biSize==WIN_3X) ? "Windows 3.x " :
(biSize==WIN_95_NT4) ? "Windows 95/NT4 " :
(biSize==WIN_98_2K) ? "Windows 98/2000" :
(biSize==WIN_NT_2K) ? "Windows NT/2000 " : ""),
biBitCount, (biBitCount == 1) ? "" : "s",
cmpstr, filesize);
sprintf(pinfo->shrtInfo, "%dx%d BMP.", biWidth, biHeight);
pinfo->comment = (char *) NULL;
return 1;
ERROR:
fclose(fp);
return 0;
}
/*******************************************/
static int loadBMP1(fp, pic8, w, h, rightsideup)
FILE *fp;
byte *pic8;
u_int w,h;
int rightsideup;
{
int i,j,c,bitnum,padw,ibegin,iend,iinc;
byte *pp = pic8 + ((h - 1) * w);
size_t l = w*h;
c = 0;
padw = ((w + 31)/32) * 32; /* 'w', padded to be a multiple of 32 */
if (rightsideup) {
ibegin = 0;
iend = h;
iinc = 1;
}
else {
ibegin = h-1;
iend = -1;
iinc = -1;
}
for (i = ibegin; i != iend && (pp - pic8 <= l); i += iinc) {
pp = pic8 + (i * w);
if ((i&0x3f)==0) WaitCursor();
for (j=bitnum=0; j<padw; j++,bitnum++) {
if ((bitnum&7) == 0) { /* read the next byte */
c = getc(fp);
bitnum = 0;
}
if (j<w) {
*pp++ = (c & 0x80) ? 1 : 0;
c <<= 1;
}
}
if (FERROR(fp)) break;
}
return (FERROR(fp));
}
/*******************************************/
static int loadBMP4(fp, pic8, w, h, comp, rightsideup)
FILE *fp;
byte *pic8;
u_int w,h,comp;
int rightsideup;
{
int i,j,c,c1,x,y,nybnum,padw,rv;
int begin, end, inc;
byte *pp = pic8 + ((h - 1) * w);
size_t l = w*h;
rv = 0;
c = c1 = 0;
if (comp == BI_RGB) { /* read uncompressed data */
if (rightsideup) {
begin = 0;
end = h;
inc = 1;
}
else {
begin = h-1;
end = -1;
inc = -1;
}
padw = ((w + 7)/8) * 8; /* 'w' padded to a multiple of 8pix (32 bits) */
for (i = begin; i != end && (pp - pic8 <= l); i += inc) {
pp = pic8 + (i * w);
if ((i&0x3f)==0) WaitCursor();
for (j=nybnum=0; j<padw; j++,nybnum++) {
if ((nybnum & 1) == 0) { /* read next byte */
c = getc(fp);
nybnum = 0;
}
if (j<w) {
*pp++ = (c & 0xf0) >> 4;
c <<= 4;
}
}
if (FERROR(fp)) break;
}
}
else if (comp == BI_RLE4) { /* read RLE4 compressed data */
x = y = 0;
pp = pic8 + x + (h-y-1)*w;
while (y<h) {
c = getc(fp); if (c == EOF) { rv = 1; break; }
if (c) { /* encoded mode */
c1 = getc(fp);
for (i=0; i<c && (pp - pic8 <= l); i++,x++,pp++)
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
}
else { /* c==0x00 : escape codes */
c = getc(fp); if (c == EOF) { rv = 1; break; }
if (c == 0x00) { /* end of line */
x=0; y++; pp = pic8 + x + (h-y-1)*w;
}
else if (c == 0x01) break; /* end of pic8 */
else if (c == 0x02) { /* delta */
c = getc(fp); x += c;
c = getc(fp); y += c;
pp = pic8 + x + (h-y-1)*w;
}
else { /* absolute mode */
for (i=0; i<c && (pp - pic8 <= l); i++, x++, pp++) {
if ((i&1) == 0) c1 = getc(fp);
*pp = (i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f);
}
if (((c&3)==1) || ((c&3)==2)) getc(fp); /* read pad byte */
}
} /* escape processing */
if (FERROR(fp)) break;
} /* while */
}
else {
fprintf(stderr,"unknown BMP compression type 0x%0x\n", comp);
}
if (FERROR(fp)) rv = 1;
return rv;
}
/*******************************************/
static int loadBMP8(fp, pic8, w, h, comp, rightsideup)
FILE *fp;
byte *pic8;
u_int w,h,comp;
int rightsideup;
{
int i,j,c,c1,padw,x,y,rv;
int begin, end, inc;
byte *pp = pic8 + ((h - 1) * w);
size_t l = w*h;
byte *pend;
rv = 0;
pend = pic8 + l;
if (comp == BI_RGB) { /* read uncompressed data */
if (rightsideup) {
begin = 0;
end = h;
inc = 1;
}
else {
begin = h-1;
end = -1;
inc = -1;
}
padw = ((w + 3)/4) * 4; /* 'w' padded to a multiple of 4pix (32 bits) */
for (i = begin; i != end && (pp - pic8 <= l); i += inc) {
pp = pic8 + (i * w);
if ((i&0x3f)==0) WaitCursor();
for (j=0; j<padw; j++) {
c = getc(fp); if (c==EOF) rv = 1;
if (j<w) *pp++ = c;
}
if (FERROR(fp)) break;
}
}
else if (comp == BI_RLE8) { /* read RLE8 compressed data */
x = y = 0;
pp = pic8 + x + (h-y-1)*w;
while (y<h && pp<=pend) {
c = getc(fp); if (c == EOF) { rv = 1; break; }
if (c) { /* encoded mode */
c1 = getc(fp);
for (i=0; i<c && pp<=pend; i++,x++,pp++) *pp = c1;
}
else { /* c==0x00 : escape codes */
c = getc(fp); if (c == EOF) { rv = 1; break; }
if (c == 0x00) { /* end of line */
x=0; y++; pp = pic8 + x + (h-y-1)*w;
}
else if (c == 0x01) break; /* end of pic8 */
else if (c == 0x02) { /* delta */
c = getc(fp); x += c;
c = getc(fp); y += c;
pp = pic8 + x + (h-y-1)*w;
}
else { /* absolute mode */
for (i=0; i<c && pp<=pend; i++, x++, pp++) {
c1 = getc(fp);
*pp = c1;
}
if (c & 1) getc(fp); /* odd length run: read an extra pad byte */
}
} /* escape processing */
if (FERROR(fp)) break;
} /* while */
}
else {
fprintf(stderr,"unknown BMP compression type 0x%0x\n", comp);
}
if (FERROR(fp)) rv = 1;
return rv;
}
/*******************************************/
static int loadBMP16(fp, pic24, w, h, mask, rightsideup)
FILE *fp;
byte *pic24;
u_int w, h, *mask;
int rightsideup;
{
int x, y, ybegin, yend, yinc;
byte *pp = pic24 + ((h - 1) * w * 3);
size_t l = w*h*3;
u_int buf, colormask[6];
int i, bit, bitshift[6], colorbits[6], bitshift2[6];
if (mask == NULL) { /* RGB555 */
colormask[0] = 0x00007c00;
colormask[1] = 0x000003e0;
colormask[2] = 0x0000001f;
colormask[3] = 0x7c000000;
colormask[4] = 0x03e00000;
colormask[5] = 0x001f0000;
bitshift[0] = 7; bitshift2[0] = 0;
bitshift[1] = 2; bitshift2[1] = 0;
bitshift[2] = 0; bitshift2[2] = 3;
bitshift[3] = 23; bitshift2[3] = 0;
bitshift[4] = 18; bitshift2[4] = 0;
bitshift[5] = 13; bitshift2[5] = 0;
} else {
colormask[0] = mask[0];
colormask[1] = mask[1];
colormask[2] = mask[2];
colormask[3] = (mask[0] & 0xffff) << 16;
colormask[4] = (mask[1] & 0xffff) << 16;
colormask[5] = (mask[2] & 0xffff) << 16;
for (i = 0; i < 3; ++i) {
buf = colormask[i];
bitshift[i] = 0;
for (bit = 0; bit < 32; ++bit) {
if (buf & 1)
break;
else
++bitshift[i];
buf >>= 1;
}
bitshift[i+3] = bitshift[i] + 16;
colorbits[i] = 0;
for (; bit < 32; ++bit) {
if (buf & 1)
++colorbits[i];
else
break;
buf >>= 1;
}
if (colorbits[i] > 8) { /* over 8-bit depth */
bitshift[i] += (colorbits[i] - 8);
bitshift[i+3] = bitshift[i] + 16;
bitshift2[i] = bitshift2[i+3] = 0;
} else
bitshift2[i] = bitshift2[i+3] = 8 - colorbits[i];
}
}
if (DEBUG > 1)
fprintf(stderr, "loadBMP16: bitfields\n"
"\tR: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n"
"\t (mask = %08x, shift >>%2d, <<%2d)\n"
"\tG: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n"
"\t (mask = %08x, shift >>%2d, <<%2d)\n"
"\tB: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n"
"\t (mask = %08x, shift >>%2d, <<%2d)\n",
colorbits[0], colormask[0], bitshift[0], bitshift2[0],
colormask[3], bitshift[3], bitshift2[3],
colorbits[1], colormask[1], bitshift[1], bitshift2[1],
colormask[4], bitshift[4], bitshift2[4],
colorbits[2], colormask[2], bitshift[2], bitshift2[2],
colormask[5], bitshift[5], bitshift2[5]);
if (rightsideup) {
ybegin = 0;
yend = h;
yinc = 1;
}
else {
ybegin = h-1;
yend = -1;
yinc = -1;
}
for (y = ybegin; y != yend && (pp - pic24 <= l); y += yinc) {
pp = pic24 + (3 * w * y);
if ((y&0x3f)==0) WaitCursor();
for (x = w; x > 1; x -= 2) {
buf = getint(fp);
*(pp++) = (buf & colormask[0]) >> bitshift[0] << bitshift2[0];
*(pp++) = (buf & colormask[1]) >> bitshift[1] << bitshift2[1];
*(pp++) = (buf & colormask[2]) >> bitshift[2] << bitshift2[2];
*(pp++) = (buf & colormask[3]) >> bitshift[3] << bitshift2[3];
*(pp++) = (buf & colormask[4]) >> bitshift[4] << bitshift2[4];
*(pp++) = (buf & colormask[5]) >> bitshift[5] << bitshift2[5];
}
if (w & 1) { /* padded to 2 pix */
buf = getint(fp);
*(pp++) = (buf & colormask[0]) >> bitshift[0];
*(pp++) = (buf & colormask[1]) >> bitshift[1];
*(pp++) = (buf & colormask[2]) >> bitshift[2];
}
}
return FERROR(fp)? 1 : 0;
}
/*******************************************/
static int loadBMP24(fp, pic24, w, h, bits, rightsideup) /* also handles 32-bit BI_RGB */
FILE *fp;
byte *pic24;
u_int w,h, bits;
int rightsideup;
{
int i,j,padb,rv,ibegin,iend,iinc;
byte *pp = pic24 + ((h - 1) * w * 3);
size_t l = w*h*3;
rv = 0;
padb = (4 - ((w*3) % 4)) & 0x03; /* # of pad bytes to read at EOscanline */
if (bits==32) padb = 0;
if (rightsideup) {
ibegin = 0;
iend = h;
iinc = 1;
}
else {
ibegin = h-1;
iend = -1;
iinc = -1;
}
for (i=ibegin; i != iend; i+=iinc) {
pp = pic24 + (i * w * 3);
if ((i&0x3f)==0) WaitCursor();
for (j=0; j<w && (pp - pic24 <= l); j++) {
pp[2] = getc(fp); /* blue */
pp[1] = getc(fp); /* green */
pp[0] = getc(fp); /* red */
if (bits==32) getc(fp);
pp += 3;
}
for (j=0; j<padb; j++) getc(fp);
rv = (FERROR(fp));
if (rv) break;
}
return rv;
}
/*******************************************/
static int loadBMP32(fp, pic24, w, h, colormask, rightsideup) /* 32-bit BI_BITFIELDS only */
FILE *fp;
byte *pic24;
u_int w, h, *colormask;
int rightsideup;
{
int x, y, ybegin, yend, yinc;
byte *pp;
u_int buf;
int i, bit, bitshift[3], colorbits[3], bitshift2[3];
for (i = 0; i < 3; ++i) {
buf = colormask[i];
bitshift[i] = 0;
for (bit = 0; bit < 32; ++bit) {
if (buf & 1)
break;
else
++bitshift[i];
buf >>= 1;
}
colorbits[i] = 0;
for (; bit < 32; ++bit) {
if (buf & 1)
++colorbits[i];
else
break;
buf >>= 1;
}
if (colorbits[i] > 8) { /* over 8-bit depth */
bitshift[i] += (colorbits[i] - 8);
bitshift2[i] = 0;
} else
bitshift2[i] = 8 - colorbits[i];
}
if (DEBUG > 1)
fprintf(stderr, "loadBMP32: bitfields\n"
"\tR: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n"
"\tG: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n"
"\tB: bits = %2d, mask = %08x, shift >>%2d, <<%2d\n",
colorbits[0], colormask[0], bitshift[0], bitshift2[0],
colorbits[1], colormask[1], bitshift[1], bitshift2[1],
colorbits[2], colormask[2], bitshift[2], bitshift2[2]);
if (rightsideup) {
ybegin = 0;
yend = h;
yinc = 1;
}
else {
ybegin = h-1;
yend = -1;
yinc = -1;
}
for (y = ybegin; y != yend; y += yinc) {
pp = pic24 + (3 * w * y);
if ((y&0x3f)==0) WaitCursor();
for(x = w; x > 0; x --) {
buf = getint(fp);
*(pp++) = (buf & colormask[0]) >> bitshift[0] << bitshift2[0];
*(pp++) = (buf & colormask[1]) >> bitshift[1] << bitshift2[1];
*(pp++) = (buf & colormask[2]) >> bitshift[2] << bitshift2[2];
}
}
return FERROR(fp)? 1 : 0;
}
/*******************************************/
static u_int getshort(fp)
FILE *fp;
{
int c, c1;
c = getc(fp); c1 = getc(fp);
return ((u_int) c) + (((u_int) c1) << 8);
}
/*******************************************/
static u_int getint(fp)
FILE *fp;
{
int c, c1, c2, c3;
c = getc(fp); c1 = getc(fp); c2 = getc(fp); c3 = getc(fp);
return ((u_int) c) +
(((u_int) c1) << 8) +
(((u_int) c2) << 16) +
(((u_int) c3) << 24);
}
/*******************************************/
static void putshort(fp, i)
FILE *fp;
int i;
{
int c, c1;
c = ((u_int) i) & 0xff; c1 = (((u_int) i)>>8) & 0xff;
putc(c, fp); putc(c1,fp);
}
/*******************************************/
static void putint(fp, i)
FILE *fp;
int i;
{
int c, c1, c2, c3;
c = ((u_int) i) & 0xff;
c1 = (((u_int) i)>>8) & 0xff;
c2 = (((u_int) i)>>16) & 0xff;
c3 = (((u_int) i)>>24) & 0xff;
putc(c, fp); putc(c1,fp); putc(c2,fp); putc(c3,fp);
}
static byte pc2nc[256],r1[256],g1[256],b1[256];
/*******************************************/
int WriteBMP(fp,pic824,ptype,w,h,rmap,gmap,bmap,numcols,colorstyle)
FILE *fp;
byte *pic824;
int ptype,w,h;
byte *rmap, *gmap, *bmap;
int numcols, colorstyle;
{
/*
* if PIC8, and colorstyle == F_FULLCOLOR, F_GREYSCALE, or F_REDUCED,
* the program writes an uncompressed 4- or 8-bit image (depending on
* the value of numcols)
*
* if PIC24, and colorstyle == F_FULLCOLOR, program writes an uncompressed
* 24-bit image
* if PIC24 and colorstyle = F_GREYSCALE, program writes an uncompressed
* 8-bit image
* note that PIC24 and F_BWDITHER/F_REDUCED won't happen
*
* if colorstyle == F_BWDITHER, it writes a 1-bit image
*
*/
int i,j, nc, nbits, bperlin, cmaplen, npixels;
byte *graypic, *sp, *dp, graymap[256];
nc = nbits = cmaplen = 0;
graypic = NULL;
if (ptype == PIC24 && colorstyle == F_GREYSCALE) {
/* generate a faked 8-bit per pixel image with a grayscale cmap,
so that it can just fall through existing 8-bit code */
npixels = w * h;
if (w <= 0 || h <= 0 || npixels/w != h) {
SetISTR(ISTR_WARNING, "image dimensions too large");
return -1;
}
graypic = (byte *) malloc((size_t) npixels);
if (!graypic) FatalError("unable to malloc in WriteBMP()");
for (i=0,sp=pic824,dp=graypic; i<npixels; i++,sp+=3, dp++) {
*dp = MONO(sp[0],sp[1],sp[2]);
}
for (i=0; i<256; i++) graymap[i] = i;
rmap = gmap = bmap = graymap;
numcols = 256;
ptype = PIC8;
pic824 = graypic;
}
if (ptype == PIC24) { /* is F_FULLCOLOR */
nbits = 24;
cmaplen = 0;
nc = 0;
}
else if (ptype == PIC8) {
/* we may have duplicate colors in the colormap, and we'd prefer not to.
* build r1,g1,b1 (a contiguous, minimum set colormap), and pc2nc[], a
* array that maps 'pic8' values (0-numcols) into corresponding values
* in the r1,g1,b1 colormaps (0-nc)
*/
for (i=0; i<256; i++) { pc2nc[i] = r1[i] = g1[i] = b1[i] = 0; }
nc = 0;
for (i=0; i<numcols; i++) {
/* see if color #i is a duplicate */
for (j=0; j<i; j++) {
if (rmap[i] == rmap[j] && gmap[i] == gmap[j] &&
bmap[i] == bmap[j]) break;
}
if (j==i) { /* wasn't found */
pc2nc[i] = nc;
r1[nc] = rmap[i];
g1[nc] = gmap[i];
b1[nc] = bmap[i];
nc++;
}
else pc2nc[i] = pc2nc[j];
}
/* determine how many bits per pixel we'll be writing */
if (colorstyle == F_BWDITHER || nc <= 2) nbits = 1;
else if (nc<=16) nbits = 4;
else nbits = 8;
cmaplen = 1<<nbits; /* # of entries in cmap */
}
bperlin = ((w * nbits + 31) / 32) * 4; /* # bytes written per line */
putc('B', fp); putc('M', fp); /* BMP file magic number */
/* compute filesize and write it */
i = 14 + /* size of bitmap file header */
40 + /* size of bitmap info header */