| 10 |
|
#include <string.h> |
| 11 |
|
#include "bmpfile.h" |
| 12 |
|
|
| 13 |
+ |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ |
| 14 |
+ |
#define getc getc_unlocked |
| 15 |
+ |
#define putc putc_unlocked |
| 16 |
+ |
#endif |
| 17 |
+ |
|
| 18 |
|
/* get corresponding error message */ |
| 19 |
|
const char * |
| 20 |
|
BMPerrorMessage(int ec) |
| 237 |
|
sizeof(br->scanpos[0])*br->hdr->height); |
| 238 |
|
} |
| 239 |
|
br->scanpos[0] = br->fpos; |
| 240 |
< |
if (BMPreadScanline(br) != BIR_OK) |
| 241 |
< |
goto err; |
| 237 |
< |
return br; |
| 240 |
> |
if (BMPreadScanline(br) == BIR_OK) |
| 241 |
> |
return br; |
| 242 |
|
err: |
| 243 |
|
if (br->hdr != NULL) |
| 244 |
|
free((void *)br->hdr); |
| 279 |
|
if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS) |
| 280 |
|
return rdbytes((char *)br->scanline, n, br); |
| 281 |
|
/* |
| 282 |
< |
* RLE/RLE8 Decoding |
| 282 |
> |
* RLE4/RLE8 Decoding |
| 283 |
|
* |
| 284 |
|
* Certain aspects of this scheme are completely insane, so |
| 285 |
|
* we don't support them. Fortunately, they rarely appear. |
| 286 |
|
* One is the mid-file EOD (0x0001) and another is the ill-conceived |
| 287 |
|
* "delta" (0x0002), which is like a "goto" statement for bitmaps. |
| 288 |
< |
* Whoever thought this up should be shot, then told why |
| 289 |
< |
* it's impossible to support such a scheme in any reasonable way. |
| 288 |
> |
* Whoever thought this up should be wrestled to the ground and told |
| 289 |
> |
* why it's impossible to support such a scheme in any reasonable way. |
| 290 |
|
* Also, RLE4 mode allows runs to stop halfway through a byte, |
| 291 |
|
* which is likewise uncodeable, so we don't even try. |
| 292 |
|
* Finally, the scanline break is ambiguous -- we assume here that |
| 301 |
|
* is undoubtedly the most brain-dead format I've ever encountered. |
| 302 |
|
*/ |
| 303 |
|
sp = br->scanline; |
| 304 |
+ |
n = br->hdr->width; |
| 305 |
+ |
if (br->hdr->compr == BI_RLE4) |
| 306 |
+ |
n = (n + 1) >> 1; |
| 307 |
|
while (n > 0) { |
| 308 |
|
int skipOdd, len, val; |
| 309 |
|
|
| 324 |
|
*sp++ = val; |
| 325 |
|
continue; |
| 326 |
|
} |
| 327 |
+ |
/* check for escape */ |
| 328 |
|
switch (rdbyte(len, br)) { |
| 329 |
|
case EOF: |
| 330 |
|
return BIR_TRUNCATED; |
| 608 |
|
{ |
| 609 |
|
if (pos == bw->fpos) |
| 610 |
|
return BIR_OK; |
| 611 |
< |
if (bw->seek == NULL) { |
| 612 |
< |
if (pos < bw->fpos) |
| 605 |
< |
return BIR_SEEKERR; |
| 606 |
< |
while (bw->fpos < pos) |
| 607 |
< |
wrbyte(0, bw); |
| 608 |
< |
return BIR_OK; |
| 609 |
< |
} |
| 611 |
> |
if (bw->seek == NULL) |
| 612 |
> |
return BIR_SEEKERR; |
| 613 |
|
if ((*bw->seek)(pos, bw->c_data) != 0) |
| 614 |
|
return BIR_SEEKERR; |
| 615 |
|
bw->fpos = pos; |
| 740 |
|
n = bw->hdr->width; |
| 741 |
|
while (n > 0) { |
| 742 |
|
int cnt, val; |
| 740 |
– |
|
| 743 |
|
cnt = findNextRun(sp, n); /* 0-255 < n */ |
| 744 |
< |
if (cnt >= 3) { /* output non-run */ |
| 744 |
> |
if (cnt >= 3) { /* output absolute */ |
| 745 |
|
int skipOdd = cnt & 1; |
| 746 |
|
wrbyte(0, bw); |
| 747 |
|
wrbyte(cnt, bw); |
| 754 |
|
if (n <= 0) /* was that it? */ |
| 755 |
|
break; |
| 756 |
|
val = *sp; /* output run */ |
| 757 |
< |
for (cnt = 1; cnt < 255; cnt++) |
| 758 |
< |
if (!--n | *++sp != val) |
| 757 |
> |
for (cnt = 1; --n && cnt < 255; cnt++) |
| 758 |
> |
if (*++sp != val) |
| 759 |
|
break; |
| 760 |
|
wrbyte(cnt, bw); |
| 761 |
|
wrbyte(val, bw); |
| 763 |
|
bw->yscan++; /* write line break or EOD */ |
| 764 |
|
if (bw->yscan == bw->hdr->height) { |
| 765 |
|
wrbyte(0, bw); wrbyte(1, bw); /* end of bitmap marker */ |
| 766 |
< |
if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0) |
| 766 |
> |
if (wrseek(2, bw) != BIR_OK) |
| 767 |
|
return BIR_OK; /* no one may care */ |
| 768 |
< |
bw->fpos = 2; |
| 768 |
> |
wrint32(bw->flen, bw); /* correct file length */ |
| 769 |
> |
if (wrseek(34, bw) != BIR_OK) |
| 770 |
> |
return BIR_OK; |
| 771 |
|
wrint32(bw->flen-bw->fbmp, bw); /* correct bitmap length */ |
| 772 |
|
} else { |
| 773 |
|
wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ |