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) |
145 |
|
BMPReader *br; |
146 |
|
uint32 bmPos, hdrSiz; |
147 |
|
int palLen; |
148 |
+ |
int magic[2]; /* check magic number */ |
149 |
|
|
150 |
|
if (cget == NULL) |
151 |
|
return NULL; |
144 |
– |
int magic[2]; /* check magic number */ |
152 |
|
magic[0] = (*cget)(c_data); |
153 |
|
if (magic[0] != 'B') |
154 |
|
return NULL; |
239 |
|
sizeof(br->scanpos[0])*br->hdr->height); |
240 |
|
} |
241 |
|
br->scanpos[0] = br->fpos; |
242 |
< |
if (BMPreadScanline(br) != BIR_OK) |
243 |
< |
goto err; |
237 |
< |
return br; |
242 |
> |
if (BMPreadScanline(br) == BIR_OK) |
243 |
> |
return br; |
244 |
|
err: |
245 |
|
if (br->hdr != NULL) |
246 |
|
free((void *)br->hdr); |
281 |
|
if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS) |
282 |
|
return rdbytes((char *)br->scanline, n, br); |
283 |
|
/* |
284 |
< |
* RLE/RLE8 Decoding |
284 |
> |
* RLE4/RLE8 Decoding |
285 |
|
* |
286 |
|
* Certain aspects of this scheme are completely insane, so |
287 |
|
* we don't support them. Fortunately, they rarely appear. |
288 |
|
* One is the mid-file EOD (0x0001) and another is the ill-conceived |
289 |
|
* "delta" (0x0002), which is like a "goto" statement for bitmaps. |
290 |
< |
* Whoever thought this up should be shot, then told why |
291 |
< |
* it's impossible to support such a scheme in any reasonable way. |
290 |
> |
* Whoever thought this up should be wrestled to the ground and told |
291 |
> |
* why it's impossible to support such a scheme in any reasonable way. |
292 |
|
* Also, RLE4 mode allows runs to stop halfway through a byte, |
293 |
|
* which is likewise uncodeable, so we don't even try. |
294 |
|
* Finally, the scanline break is ambiguous -- we assume here that |
303 |
|
* is undoubtedly the most brain-dead format I've ever encountered. |
304 |
|
*/ |
305 |
|
sp = br->scanline; |
306 |
+ |
n = br->hdr->width; |
307 |
+ |
if (br->hdr->compr == BI_RLE4) |
308 |
+ |
n = (n + 1) >> 1; |
309 |
|
while (n > 0) { |
310 |
|
int skipOdd, len, val; |
311 |
|
|
326 |
|
*sp++ = val; |
327 |
|
continue; |
328 |
|
} |
329 |
+ |
/* check for escape */ |
330 |
|
switch (rdbyte(len, br)) { |
331 |
|
case EOF: |
332 |
|
return BIR_TRUNCATED; |
610 |
|
{ |
611 |
|
if (pos == bw->fpos) |
612 |
|
return BIR_OK; |
613 |
< |
if (bw->seek == NULL) { |
614 |
< |
if (pos < bw->fpos) |
605 |
< |
return BIR_SEEKERR; |
606 |
< |
while (bw->fpos < pos) |
607 |
< |
wrbyte(0, bw); |
608 |
< |
return BIR_OK; |
609 |
< |
} |
613 |
> |
if (bw->seek == NULL) |
614 |
> |
return BIR_SEEKERR; |
615 |
|
if ((*bw->seek)(pos, bw->c_data) != 0) |
616 |
|
return BIR_SEEKERR; |
617 |
|
bw->fpos = pos; |
742 |
|
n = bw->hdr->width; |
743 |
|
while (n > 0) { |
744 |
|
int cnt, val; |
740 |
– |
|
745 |
|
cnt = findNextRun(sp, n); /* 0-255 < n */ |
746 |
< |
if (cnt >= 3) { /* output non-run */ |
746 |
> |
if (cnt >= 3) { /* output absolute */ |
747 |
|
int skipOdd = cnt & 1; |
748 |
|
wrbyte(0, bw); |
749 |
|
wrbyte(cnt, bw); |
756 |
|
if (n <= 0) /* was that it? */ |
757 |
|
break; |
758 |
|
val = *sp; /* output run */ |
759 |
< |
for (cnt = 1; cnt < 255; cnt++) |
760 |
< |
if (!--n | *++sp != val) |
759 |
> |
for (cnt = 1; --n && cnt < 255; cnt++) |
760 |
> |
if (*++sp != val) |
761 |
|
break; |
762 |
|
wrbyte(cnt, bw); |
763 |
|
wrbyte(val, bw); |
765 |
|
bw->yscan++; /* write line break or EOD */ |
766 |
|
if (bw->yscan == bw->hdr->height) { |
767 |
|
wrbyte(0, bw); wrbyte(1, bw); /* end of bitmap marker */ |
768 |
< |
if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0) |
768 |
> |
if (wrseek(2, bw) != BIR_OK) |
769 |
|
return BIR_OK; /* no one may care */ |
770 |
< |
bw->fpos = 2; |
770 |
> |
wrint32(bw->flen, bw); /* correct file length */ |
771 |
> |
if (wrseek(34, bw) != BIR_OK) |
772 |
> |
return BIR_OK; |
773 |
|
wrint32(bw->flen-bw->fbmp, bw); /* correct bitmap length */ |
774 |
|
} else { |
775 |
|
wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ |