| 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]) * |
| 255 |
|
return -1; |
| 256 |
|
if (hdr->bpp > 8) /* assume they had a reason for it */ |
| 257 |
|
return 0; |
| 258 |
< |
for (rgbp = hdr->palette, n = hdr->impColors; n-- > 0; rgbp++) |
| 258 |
> |
for (rgbp = hdr->palette, n = hdr->nColors; n-- > 0; rgbp++) |
| 259 |
|
if (((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b))) |
| 260 |
|
return 0; |
| 261 |
|
return 1; /* all colors neutral in map */ |
| 279 |
|
* |
| 280 |
|
* Certain aspects of this scheme are completely insane, so |
| 281 |
|
* we don't support them. Fortunately, they rarely appear. |
| 282 |
< |
* One is the mid-file EOD (0x0001) and another is the insane |
| 282 |
> |
* One is the mid-file EOD (0x0001) and another is the ill-conceived |
| 283 |
|
* "delta" (0x0002), which is like a "goto" statement for bitmaps. |
| 284 |
|
* Whoever thought this up should be shot, then told why |
| 285 |
< |
* it's impossible to support in any reasonable way. |
| 285 |
> |
* it's impossible to support such a scheme in any reasonable way. |
| 286 |
|
* Also, RLE4 mode allows runs to stop halfway through a byte, |
| 287 |
|
* which is likewise uncodeable, so we don't even try. |
| 288 |
|
* Finally, the scanline break is ambiguous -- we assume here that |
| 291 |
|
* the end of the scanline, assuming the next bit of data belongs |
| 292 |
|
* the following scan. If a break follows the last pixel, as it |
| 293 |
|
* seems to in the files I've tested out of Photoshop, you end up |
| 294 |
< |
* painting every other line black. BTW, I assume any skipped |
| 294 |
> |
* painting every other line black. Also, I assume any skipped |
| 295 |
|
* pixels are painted with color 0, which is often black. Nowhere |
| 296 |
|
* is it specified what we should assume for missing pixels. This |
| 297 |
|
* is undoubtedly the most brain-dead format I've ever encountered. |
| 323 |
|
case 0: /* end of line */ |
| 324 |
|
while (n--) |
| 325 |
|
*sp++ = 0; |
| 326 |
+ |
/* leaves n == -1 as flag for test after loop */ |
| 327 |
|
continue; |
| 328 |
|
case 1: /* end of bitmap */ |
| 329 |
|
case 2: /* delta */ |
| 348 |
|
return BIR_TRUNCATED; |
| 349 |
|
} |
| 350 |
|
/* verify break at end of line */ |
| 351 |
< |
if (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && |
| 352 |
< |
(n != 1 || br->yscan != br->hdr->height-1))) |
| 351 |
> |
if (!n && (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && |
| 352 |
> |
(n != 1 || br->yscan != br->hdr->height-1)))) |
| 353 |
|
return BIR_RLERROR; |
| 354 |
|
if (br->seek != NULL) /* record next scanline position */ |
| 355 |
|
br->scanpos[br->yscan + 1] = br->fpos; |
| 445 |
|
case 8: |
| 446 |
|
return br->hdr->palette[br->scanline[i]]; |
| 447 |
|
case 1: |
| 448 |
< |
return br->hdr->palette[br->scanline[i>>3]>>(7-i&7) & 1]; |
| 448 |
> |
return br->hdr->palette[br->scanline[i>>3]>>((7-i)&7) & 1]; |
| 449 |
|
case 4: |
| 450 |
|
return br->hdr->palette[br->scanline[i>>1]>>(i&1?4:0) & 0xf]; |
| 451 |
|
case 16: |
| 523 |
|
return hdr; |
| 524 |
|
} |
| 525 |
|
|
| 526 |
< |
/* allocate color-mapped header (defaults minimal grayscale) */ |
| 526 |
> |
/* allocate color-mapped header (defaults to minimal grayscale) */ |
| 527 |
|
BMPHeader * |
| 528 |
|
BMPmappedHeader(int xr, int yr, int infolen, int ncolors) |
| 529 |
|
{ |
| 550 |
|
hdr->height = yr; |
| 551 |
|
hdr->yIsDown = 0; /* default to upwards order */ |
| 552 |
|
hdr->bpp = n; |
| 553 |
< |
hdr->compr = BI_UNCOMPR; |
| 553 |
> |
hdr->compr = BI_UNCOMPR; /* compression needs seek */ |
| 554 |
|
hdr->hRes = hdr->vRes = 2835; /* default to 72 ppi */ |
| 555 |
|
hdr->nColors = ncolors; |
| 556 |
|
hdr->impColors = 0; /* says all colors important */ |
| 627 |
|
return NULL; |
| 628 |
|
if (!BMPheaderOK(hdr)) |
| 629 |
|
return NULL; |
| 630 |
< |
if ((hdr->bpp == 16 | hdr->compr == BI_RLE4)) |
| 630 |
> |
if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) |
| 631 |
|
return NULL; /* unsupported */ |
| 632 |
< |
if (seek == NULL && (hdr->compr == BI_RLE8 | hdr->compr == BI_RLE4)) |
| 632 |
> |
/* no seek means we may have the wrong file length, but most app's don't care |
| 633 |
> |
if (seek == NULL && ((hdr->compr == BI_RLE8) | (hdr->compr == BI_RLE4))) |
| 634 |
|
return NULL; |
| 635 |
+ |
*/ |
| 636 |
|
/* compute sizes */ |
| 637 |
|
hdrSiz = 2 + 6*4 + 2*2 + 6*4; |
| 638 |
|
if (hdr->compr == BI_BITFIELDS) |
| 694 |
|
{ |
| 695 |
|
int pos, cnt; |
| 696 |
|
/* look for run */ |
| 697 |
< |
for (pos = 0; len > 0 & pos < 255; pos++, bp++, len--) { |
| 697 |
> |
for (pos = 0; (len > 0) & (pos < 255); pos++, bp++, len--) { |
| 698 |
|
if (len < 5) /* no hope left? */ |
| 699 |
|
continue; |
| 700 |
|
cnt = 1; /* else let's try it */ |
| 701 |
|
while (bp[cnt] == bp[0]) |
| 702 |
< |
if (++cnt >= 5) /* long enough */ |
| 703 |
< |
return pos; |
| 702 |
> |
if (++cnt >= 5) |
| 703 |
> |
return pos; /* long enough */ |
| 704 |
|
} |
| 705 |
|
return pos; /* didn't find any */ |
| 706 |
|
} |
| 752 |
|
if (n <= 0) /* was that it? */ |
| 753 |
|
break; |
| 754 |
|
val = *sp; /* output run */ |
| 755 |
< |
for (cnt = 1; --n > 0 & *++sp == val; cnt++) |
| 756 |
< |
; |
| 755 |
> |
for (cnt = 1; cnt < 255; cnt++) |
| 756 |
> |
if (!--n | *++sp != val) |
| 757 |
> |
break; |
| 758 |
|
wrbyte(cnt, bw); |
| 759 |
|
wrbyte(val, bw); |
| 760 |
|
} |
| 761 |
< |
bw->yscan++; /* write file length at end */ |
| 761 |
> |
bw->yscan++; /* write line break or EOD */ |
| 762 |
|
if (bw->yscan == bw->hdr->height) { |
| 763 |
|
wrbyte(0, bw); wrbyte(1, bw); /* end of bitmap marker */ |
| 764 |
|
if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0) |
| 765 |
< |
return BIR_SEEKERR; |
| 765 |
> |
return BIR_OK; /* no one may care */ |
| 766 |
|
bw->fpos = 2; |
| 767 |
< |
wrint32(bw->flen, bw); /* corrected file length */ |
| 767 |
> |
wrint32(bw->flen-bw->fbmp, bw); /* correct bitmap length */ |
| 768 |
|
} else { |
| 769 |
|
wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ |
| 770 |
|
} |