| 10 |
|
#include <string.h> |
| 11 |
|
#include "bmpfile.h" |
| 12 |
|
|
| 13 |
+ |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
| 14 |
+ |
#undef getc |
| 15 |
+ |
#undef putc |
| 16 |
+ |
#define getc getc_unlocked |
| 17 |
+ |
#define putc putc_unlocked |
| 18 |
+ |
#endif |
| 19 |
+ |
|
| 20 |
|
/* get corresponding error message */ |
| 21 |
|
const char * |
| 22 |
|
BMPerrorMessage(int ec) |
| 71 |
|
if (hdr->compr == BI_BITFIELDS && (BMPbitField(hdr)[0] & |
| 72 |
|
BMPbitField(hdr)[1] & BMPbitField(hdr)[2])) |
| 73 |
|
return 0; |
| 74 |
< |
if ((hdr->nColors < 0) | (hdr->impColors < 0)) |
| 75 |
< |
return 0; |
| 76 |
< |
if (hdr->impColors > hdr->nColors) |
| 77 |
< |
return 0; |
| 78 |
< |
if (hdr->nColors > BMPpalLen(hdr)) |
| 79 |
< |
return 0; |
| 74 |
> |
if (hdr->bpp > 8) { |
| 75 |
> |
if (hdr->nColors != 0) |
| 76 |
> |
return 0; |
| 77 |
> |
} else { |
| 78 |
> |
if ((hdr->nColors < 0) | (hdr->nColors > 1<<hdr->bpp)) |
| 79 |
> |
return 0; |
| 80 |
> |
if ((hdr->impColors < 0) | (hdr->impColors > hdr->nColors)) |
| 81 |
> |
return 0; |
| 82 |
> |
} |
| 83 |
|
return 1; |
| 84 |
|
} |
| 85 |
|
|
| 146 |
|
BMPopenReader(int (*cget)(void *), int (*seek)(uint32, void *), void *c_data) |
| 147 |
|
{ |
| 148 |
|
BMPReader *br; |
| 149 |
< |
uint32 bmPos, hdrSiz; |
| 150 |
< |
int palLen; |
| 149 |
> |
uint32 bmPos, hdrSiz, palSiz; |
| 150 |
> |
int magic[2]; /* check magic number */ |
| 151 |
|
|
| 152 |
|
if (cget == NULL) |
| 153 |
|
return NULL; |
| 144 |
– |
int magic[2]; /* check magic number */ |
| 154 |
|
magic[0] = (*cget)(c_data); |
| 155 |
|
if (magic[0] != 'B') |
| 156 |
|
return NULL; |
| 193 |
|
goto err; /* catch premature EOF */ |
| 194 |
|
if (!BMPheaderOK(br->hdr)) |
| 195 |
|
goto err; |
| 196 |
< |
palLen = BMPpalLen(br->hdr); |
| 197 |
< |
if (br->hdr->bpp <= 8) { /* normalize color counts */ |
| 198 |
< |
if (br->hdr->nColors <= 0) |
| 190 |
< |
br->hdr->nColors = palLen; |
| 191 |
< |
if (br->hdr->impColors <= 0) |
| 192 |
< |
br->hdr->impColors = br->hdr->nColors; |
| 193 |
< |
} |
| 196 |
> |
palSiz = sizeof(RGBquad)*br->hdr->nColors; |
| 197 |
> |
if (br->hdr->impColors <= 0) |
| 198 |
> |
br->hdr->impColors = br->hdr->nColors; |
| 199 |
|
/* extend header */ |
| 200 |
< |
if (bmPos < hdrSiz + sizeof(RGBquad)*palLen) |
| 200 |
> |
if (bmPos < hdrSiz + palSiz) |
| 201 |
|
goto err; |
| 202 |
< |
br->hdr->infoSiz = bmPos - (hdrSiz + sizeof(RGBquad)*palLen); |
| 203 |
< |
if (palLen > 0 || br->hdr->infoSiz > 0) { |
| 202 |
> |
br->hdr->infoSiz = bmPos - (hdrSiz + palSiz); |
| 203 |
> |
if (br->hdr->nColors > 0 || br->hdr->infoSiz > 0) { |
| 204 |
|
br->hdr = (BMPHeader *)realloc((void *)br->hdr, |
| 205 |
|
sizeof(BMPHeader) + |
| 206 |
< |
sizeof(RGBquad)*palLen + |
| 202 |
< |
br->hdr->infoSiz); |
| 206 |
> |
palSiz + br->hdr->infoSiz); |
| 207 |
|
if (br->hdr == NULL) |
| 208 |
|
goto err; |
| 209 |
|
} |
| 212 |
|
BMPbitField(br->hdr)[0] = (uint32)rdint32(br); |
| 213 |
|
BMPbitField(br->hdr)[1] = (uint32)rdint32(br); |
| 214 |
|
BMPbitField(br->hdr)[2] = (uint32)rdint32(br); |
| 215 |
< |
} else if (rdbytes((char *)br->hdr->palette, |
| 212 |
< |
sizeof(RGBquad)*palLen, br) != BIR_OK) |
| 215 |
> |
} else if (rdbytes((char *)br->hdr->palette, palSiz, br) != BIR_OK) |
| 216 |
|
goto err; |
| 217 |
|
/* read add'l information */ |
| 218 |
|
if (rdbytes(BMPinfo(br->hdr), br->hdr->infoSiz, br) != BIR_OK) |
| 235 |
|
sizeof(br->scanpos[0])*br->hdr->height); |
| 236 |
|
} |
| 237 |
|
br->scanpos[0] = br->fpos; |
| 238 |
< |
if (BMPreadScanline(br) != BIR_OK) |
| 239 |
< |
goto err; |
| 237 |
< |
return br; |
| 238 |
> |
if (BMPreadScanline(br) == BIR_OK) |
| 239 |
> |
return br; |
| 240 |
|
err: |
| 241 |
|
if (br->hdr != NULL) |
| 242 |
|
free((void *)br->hdr); |
| 257 |
|
return -1; |
| 258 |
|
if (hdr->bpp > 8) /* assume they had a reason for it */ |
| 259 |
|
return 0; |
| 260 |
< |
for (rgbp = hdr->palette, n = hdr->impColors; n-- > 0; rgbp++) |
| 260 |
> |
for (rgbp = hdr->palette, n = hdr->nColors; n-- > 0; rgbp++) |
| 261 |
|
if (((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b))) |
| 262 |
|
return 0; |
| 263 |
|
return 1; /* all colors neutral in map */ |
| 277 |
|
if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS) |
| 278 |
|
return rdbytes((char *)br->scanline, n, br); |
| 279 |
|
/* |
| 280 |
< |
* RLE/RLE8 Decoding |
| 280 |
> |
* RLE4/RLE8 Decoding |
| 281 |
|
* |
| 282 |
|
* Certain aspects of this scheme are completely insane, so |
| 283 |
|
* we don't support them. Fortunately, they rarely appear. |
| 284 |
< |
* One is the mid-file EOD (0x0001) and another is the insane |
| 284 |
> |
* One is the mid-file EOD (0x0001) and another is the ill-conceived |
| 285 |
|
* "delta" (0x0002), which is like a "goto" statement for bitmaps. |
| 286 |
< |
* Whoever thought this up should be shot, then told why |
| 287 |
< |
* it's impossible to support in any reasonable way. |
| 286 |
> |
* Whoever thought this up should be wrestled to the ground and told |
| 287 |
> |
* why it's impossible to support such a scheme in any reasonable way. |
| 288 |
|
* Also, RLE4 mode allows runs to stop halfway through a byte, |
| 289 |
|
* which is likewise uncodeable, so we don't even try. |
| 290 |
|
* Finally, the scanline break is ambiguous -- we assume here that |
| 293 |
|
* the end of the scanline, assuming the next bit of data belongs |
| 294 |
|
* the following scan. If a break follows the last pixel, as it |
| 295 |
|
* seems to in the files I've tested out of Photoshop, you end up |
| 296 |
< |
* painting every other line black. BTW, I assume any skipped |
| 296 |
> |
* painting every other line black. Also, I assume any skipped |
| 297 |
|
* pixels are painted with color 0, which is often black. Nowhere |
| 298 |
|
* is it specified what we should assume for missing pixels. This |
| 299 |
|
* is undoubtedly the most brain-dead format I've ever encountered. |
| 300 |
|
*/ |
| 301 |
|
sp = br->scanline; |
| 302 |
+ |
n = br->hdr->width; |
| 303 |
+ |
if (br->hdr->compr == BI_RLE4) |
| 304 |
+ |
n = (n + 1) >> 1; |
| 305 |
|
while (n > 0) { |
| 306 |
|
int skipOdd, len, val; |
| 307 |
|
|
| 322 |
|
*sp++ = val; |
| 323 |
|
continue; |
| 324 |
|
} |
| 325 |
+ |
/* check for escape */ |
| 326 |
|
switch (rdbyte(len, br)) { |
| 327 |
|
case EOF: |
| 328 |
|
return BIR_TRUNCATED; |
| 329 |
|
case 0: /* end of line */ |
| 330 |
|
while (n--) |
| 331 |
|
*sp++ = 0; |
| 332 |
+ |
/* leaves n == -1 as flag for test after loop */ |
| 333 |
|
continue; |
| 334 |
|
case 1: /* end of bitmap */ |
| 335 |
|
case 2: /* delta */ |
| 354 |
|
return BIR_TRUNCATED; |
| 355 |
|
} |
| 356 |
|
/* verify break at end of line */ |
| 357 |
< |
if (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && |
| 358 |
< |
(n != 1 || br->yscan != br->hdr->height-1))) |
| 357 |
> |
if (!n && (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && |
| 358 |
> |
(n != 1 || br->yscan != br->hdr->height-1)))) |
| 359 |
|
return BIR_RLERROR; |
| 360 |
|
if (br->seek != NULL) /* record next scanline position */ |
| 361 |
|
br->scanpos[br->yscan + 1] = br->fpos; |
| 529 |
|
return hdr; |
| 530 |
|
} |
| 531 |
|
|
| 532 |
< |
/* allocate color-mapped header (defaults minimal grayscale) */ |
| 532 |
> |
/* allocate color-mapped header (defaults to minimal grayscale) */ |
| 533 |
|
BMPHeader * |
| 534 |
|
BMPmappedHeader(int xr, int yr, int infolen, int ncolors) |
| 535 |
|
{ |
| 556 |
|
hdr->height = yr; |
| 557 |
|
hdr->yIsDown = 0; /* default to upwards order */ |
| 558 |
|
hdr->bpp = n; |
| 559 |
< |
hdr->compr = BI_UNCOMPR; |
| 559 |
> |
hdr->compr = BI_UNCOMPR; /* compression needs seek */ |
| 560 |
|
hdr->hRes = hdr->vRes = 2835; /* default to 72 ppi */ |
| 561 |
|
hdr->nColors = ncolors; |
| 562 |
|
hdr->impColors = 0; /* says all colors important */ |
| 606 |
|
{ |
| 607 |
|
if (pos == bw->fpos) |
| 608 |
|
return BIR_OK; |
| 609 |
< |
if (bw->seek == NULL) { |
| 610 |
< |
if (pos < bw->fpos) |
| 604 |
< |
return BIR_SEEKERR; |
| 605 |
< |
while (bw->fpos < pos) |
| 606 |
< |
wrbyte(0, bw); |
| 607 |
< |
return BIR_OK; |
| 608 |
< |
} |
| 609 |
> |
if (bw->seek == NULL) |
| 610 |
> |
return BIR_SEEKERR; |
| 611 |
|
if ((*bw->seek)(pos, bw->c_data) != 0) |
| 612 |
|
return BIR_SEEKERR; |
| 613 |
|
bw->fpos = pos; |
| 630 |
|
return NULL; |
| 631 |
|
if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) |
| 632 |
|
return NULL; /* unsupported */ |
| 633 |
+ |
/* no seek means we may have the wrong file length, but most app's don't care |
| 634 |
|
if (seek == NULL && ((hdr->compr == BI_RLE8) | (hdr->compr == BI_RLE4))) |
| 635 |
|
return NULL; |
| 636 |
+ |
*/ |
| 637 |
|
/* compute sizes */ |
| 638 |
|
hdrSiz = 2 + 6*4 + 2*2 + 6*4; |
| 639 |
|
if (hdr->compr == BI_BITFIELDS) |
| 640 |
|
hdrSiz += sizeof(uint32)*3; |
| 641 |
< |
palSiz = sizeof(RGBquad)*BMPpalLen(hdr); |
| 641 |
> |
palSiz = sizeof(RGBquad)*hdr->nColors; |
| 642 |
|
scanSiz = getScanSiz(hdr); |
| 643 |
|
bmSiz = hdr->height*scanSiz; /* wrong if compressed */ |
| 644 |
|
/* initialize writer */ |
| 700 |
|
continue; |
| 701 |
|
cnt = 1; /* else let's try it */ |
| 702 |
|
while (bp[cnt] == bp[0]) |
| 703 |
< |
if (++cnt >= 5) /* long enough */ |
| 704 |
< |
return pos; |
| 703 |
> |
if (++cnt >= 5) |
| 704 |
> |
return pos; /* long enough */ |
| 705 |
|
} |
| 706 |
|
return pos; /* didn't find any */ |
| 707 |
|
} |
| 738 |
|
n = bw->hdr->width; |
| 739 |
|
while (n > 0) { |
| 740 |
|
int cnt, val; |
| 737 |
– |
|
| 741 |
|
cnt = findNextRun(sp, n); /* 0-255 < n */ |
| 742 |
< |
if (cnt >= 3) { /* output non-run */ |
| 742 |
> |
if (cnt >= 3) { /* output absolute */ |
| 743 |
|
int skipOdd = cnt & 1; |
| 744 |
|
wrbyte(0, bw); |
| 745 |
|
wrbyte(cnt, bw); |
| 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; --n && cnt < 255; cnt++) |
| 756 |
> |
if (*++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; |
| 766 |
< |
bw->fpos = 2; |
| 767 |
< |
wrint32(bw->flen, bw); /* corrected file length */ |
| 764 |
> |
if (wrseek(2, bw) != BIR_OK) |
| 765 |
> |
return BIR_OK; /* no one may care */ |
| 766 |
> |
wrint32(bw->flen, bw); /* correct file length */ |
| 767 |
> |
if (wrseek(34, bw) != BIR_OK) |
| 768 |
> |
return BIR_OK; |
| 769 |
> |
wrint32(bw->flen-bw->fbmp, bw); /* correct bitmap length */ |
| 770 |
|
} else { |
| 771 |
|
wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ |
| 772 |
|
} |