37 |
|
{ |
38 |
|
if (!hdr) |
39 |
|
return 0; |
40 |
< |
if ((hdr->width <= 0 | hdr->height <= 0)) |
40 |
> |
if ((hdr->width <= 0) | (hdr->height <= 0)) |
41 |
|
return 0; |
42 |
|
switch (hdr->bpp) { /* check compression */ |
43 |
|
case 1: |
47 |
|
break; |
48 |
|
case 16: |
49 |
|
case 32: |
50 |
< |
if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_BITFIELDS)) |
50 |
> |
if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_BITFIELDS)) |
51 |
|
return 0; |
52 |
|
break; |
53 |
|
case 4: |
54 |
< |
if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_RLE4)) |
54 |
> |
if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE4)) |
55 |
|
return 0; |
56 |
|
break; |
57 |
|
case 8: |
58 |
< |
if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_RLE8)) |
58 |
> |
if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE8)) |
59 |
|
return 0; |
60 |
|
break; |
61 |
|
default: |
64 |
|
if (hdr->compr == BI_BITFIELDS && (BMPbitField(hdr)[0] & |
65 |
|
BMPbitField(hdr)[1] & BMPbitField(hdr)[2])) |
66 |
|
return 0; |
67 |
< |
if ((hdr->nColors < 0 | hdr->impColors < 0)) |
67 |
> |
if ((hdr->nColors < 0) | (hdr->impColors < 0)) |
68 |
|
return 0; |
69 |
|
if (hdr->impColors > hdr->nColors) |
70 |
|
return 0; |
74 |
|
} |
75 |
|
|
76 |
|
/* compute uncompressed scan size */ |
77 |
< |
#define getScanSiz(h) ( (((h)->bpp*(h)->width+7 >>3) + 3) & ~03 ) |
77 |
> |
#define getScanSiz(h) ( ((((h)->bpp*(h)->width+7) >>3) + 3) & ~03 ) |
78 |
|
|
79 |
|
/* get next byte from reader */ |
80 |
|
#define rdbyte(c,br) ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c) |
219 |
|
if (br->scanline == NULL) |
220 |
|
goto err; |
221 |
|
br->yscan = -1; |
222 |
< |
if (seek != NULL && (br->hdr->compr == BI_RLE8 | |
223 |
< |
br->hdr->compr == BI_RLE4)) { |
222 |
> |
if (seek != NULL && ((br->hdr->compr == BI_RLE8) | |
223 |
> |
(br->hdr->compr == BI_RLE4))) { |
224 |
|
BMPReader *newbr = (BMPReader *)realloc((void *)br, |
225 |
|
sizeof(BMPReader) + |
226 |
|
sizeof(br->scanpos[0]) * |
444 |
|
case 8: |
445 |
|
return br->hdr->palette[br->scanline[i]]; |
446 |
|
case 1: |
447 |
< |
return br->hdr->palette[br->scanline[i>>3]>>(7-i&7) & 1]; |
447 |
> |
return br->hdr->palette[br->scanline[i>>3]>>((7-i)&7) & 1]; |
448 |
|
case 4: |
449 |
|
return br->hdr->palette[br->scanline[i>>1]>>(i&1?4:0) & 0xf]; |
450 |
|
case 16: |
522 |
|
return hdr; |
523 |
|
} |
524 |
|
|
525 |
< |
/* allocate color-mapped header (defaults minimal grayscale) */ |
525 |
> |
/* allocate color-mapped header (defaults to minimal grayscale) */ |
526 |
|
BMPHeader * |
527 |
|
BMPmappedHeader(int xr, int yr, int infolen, int ncolors) |
528 |
|
{ |
549 |
|
hdr->height = yr; |
550 |
|
hdr->yIsDown = 0; /* default to upwards order */ |
551 |
|
hdr->bpp = n; |
552 |
< |
hdr->compr = BI_UNCOMPR; |
552 |
> |
hdr->compr = BI_UNCOMPR; /* compression needs seek */ |
553 |
|
hdr->hRes = hdr->vRes = 2835; /* default to 72 ppi */ |
554 |
|
hdr->nColors = ncolors; |
555 |
|
hdr->impColors = 0; /* says all colors important */ |
626 |
|
return NULL; |
627 |
|
if (!BMPheaderOK(hdr)) |
628 |
|
return NULL; |
629 |
< |
if ((hdr->bpp == 16 | hdr->compr == BI_RLE4)) |
629 |
> |
if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) |
630 |
|
return NULL; /* unsupported */ |
631 |
< |
if (seek == NULL && (hdr->compr == BI_RLE8 | hdr->compr == BI_RLE4)) |
631 |
> |
/* no seek means we may have the wrong file length, but most app's don't care |
632 |
> |
if (seek == NULL && ((hdr->compr == BI_RLE8) | (hdr->compr == BI_RLE4))) |
633 |
|
return NULL; |
634 |
+ |
*/ |
635 |
|
/* compute sizes */ |
636 |
|
hdrSiz = 2 + 6*4 + 2*2 + 6*4; |
637 |
|
if (hdr->compr == BI_BITFIELDS) |
693 |
|
{ |
694 |
|
int pos, cnt; |
695 |
|
/* look for run */ |
696 |
< |
for (pos = 0; len > 0 & pos < 255; pos++, bp++, len--) { |
696 |
> |
for (pos = 0; (len > 0) & (pos < 255); pos++, bp++, len--) { |
697 |
|
if (len < 5) /* no hope left? */ |
698 |
|
continue; |
699 |
|
cnt = 1; /* else let's try it */ |
700 |
|
while (bp[cnt] == bp[0]) |
701 |
< |
if (++cnt >= 5) /* long enough */ |
702 |
< |
return pos; |
701 |
> |
if (++cnt >= 5) |
702 |
> |
return pos; /* long enough */ |
703 |
|
} |
704 |
|
return pos; /* didn't find any */ |
705 |
|
} |
751 |
|
if (n <= 0) /* was that it? */ |
752 |
|
break; |
753 |
|
val = *sp; /* output run */ |
754 |
< |
for (cnt = 1; --n > 0 & *++sp == val; cnt++) |
755 |
< |
; |
754 |
> |
for (cnt = 1; cnt < 255; cnt++) |
755 |
> |
if (!--n | *++sp != val) |
756 |
> |
break; |
757 |
|
wrbyte(cnt, bw); |
758 |
|
wrbyte(val, bw); |
759 |
|
} |
760 |
< |
bw->yscan++; /* write file length at end */ |
760 |
> |
bw->yscan++; /* write line break or EOD */ |
761 |
|
if (bw->yscan == bw->hdr->height) { |
762 |
|
wrbyte(0, bw); wrbyte(1, bw); /* end of bitmap marker */ |
763 |
|
if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0) |
764 |
< |
return BIR_SEEKERR; |
764 |
> |
return BIR_OK; /* no one may care */ |
765 |
|
bw->fpos = 2; |
766 |
< |
wrint32(bw->flen, bw); /* corrected file length */ |
766 |
> |
wrint32(bw->flen-bw->fbmp, bw); /* output correct bitmap length */ |
767 |
|
} else { |
768 |
|
wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ |
769 |
|
} |