--- ray/src/common/bmpfile.c 2004/03/26 22:58:21 2.3 +++ ray/src/common/bmpfile.c 2004/07/28 00:17:58 2.11 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bmpfile.c,v 2.3 2004/03/26 22:58:21 greg Exp $"; +static const char RCSid[] = "$Id: bmpfile.c,v 2.11 2004/07/28 00:17:58 schorsch Exp $"; #endif /* * Windows and OS/2 BMP file support @@ -10,6 +10,11 @@ static const char RCSid[] = "$Id: bmpfile.c,v 2.3 2004 #include #include "bmpfile.h" +#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ +#define getc getc_unlocked +#define putc putc_unlocked +#endif + /* get corresponding error message */ const char * BMPerrorMessage(int ec) @@ -37,7 +42,7 @@ BMPheaderOK(const BMPHeader *hdr) { if (!hdr) return 0; - if ((hdr->width <= 0 | hdr->height <= 0)) + if ((hdr->width <= 0) | (hdr->height <= 0)) return 0; switch (hdr->bpp) { /* check compression */ case 1: @@ -47,15 +52,15 @@ BMPheaderOK(const BMPHeader *hdr) break; case 16: case 32: - if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_BITFIELDS)) + if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_BITFIELDS)) return 0; break; case 4: - if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_RLE4)) + if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE4)) return 0; break; case 8: - if ((hdr->compr != BI_UNCOMPR & hdr->compr != BI_RLE8)) + if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE8)) return 0; break; default: @@ -64,7 +69,7 @@ BMPheaderOK(const BMPHeader *hdr) if (hdr->compr == BI_BITFIELDS && (BMPbitField(hdr)[0] & BMPbitField(hdr)[1] & BMPbitField(hdr)[2])) return 0; - if ((hdr->nColors < 0 | hdr->impColors < 0)) + if ((hdr->nColors < 0) | (hdr->impColors < 0)) return 0; if (hdr->impColors > hdr->nColors) return 0; @@ -74,7 +79,7 @@ BMPheaderOK(const BMPHeader *hdr) } /* compute uncompressed scan size */ -#define getScanSiz(h) ( (((h)->bpp*(h)->width+7 >>3) + 3) & ~03 ) +#define getScanSiz(h) ( ((((h)->bpp*(h)->width+7) >>3) + 3) & ~03 ) /* get next byte from reader */ #define rdbyte(c,br) ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c) @@ -138,10 +143,10 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, BMPReader *br; uint32 bmPos, hdrSiz; int palLen; + int magic[2]; /* check magic number */ if (cget == NULL) return NULL; - int magic[2]; /* check magic number */ magic[0] = (*cget)(c_data); if (magic[0] != 'B') return NULL; @@ -219,8 +224,8 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, if (br->scanline == NULL) goto err; br->yscan = -1; - if (seek != NULL && (br->hdr->compr == BI_RLE8 | - br->hdr->compr == BI_RLE4)) { + if (seek != NULL && ((br->hdr->compr == BI_RLE8) | + (br->hdr->compr == BI_RLE4))) { BMPReader *newbr = (BMPReader *)realloc((void *)br, sizeof(BMPReader) + sizeof(br->scanpos[0]) * @@ -232,9 +237,8 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, sizeof(br->scanpos[0])*br->hdr->height); } br->scanpos[0] = br->fpos; - if (BMPreadScanline(br) != BIR_OK) - goto err; - return br; + if (BMPreadScanline(br) == BIR_OK) + return br; err: if (br->hdr != NULL) free((void *)br->hdr); @@ -255,7 +259,7 @@ BMPisGrayscale(const BMPHeader *hdr) return -1; if (hdr->bpp > 8) /* assume they had a reason for it */ return 0; - for (rgbp = hdr->palette, n = hdr->impColors; n-- > 0; rgbp++) + for (rgbp = hdr->palette, n = hdr->nColors; n-- > 0; rgbp++) if (((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b))) return 0; return 1; /* all colors neutral in map */ @@ -275,14 +279,14 @@ BMPreadScanline(BMPReader *br) if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS) return rdbytes((char *)br->scanline, n, br); /* - * RLE/RLE8 Decoding + * RLE4/RLE8 Decoding * * Certain aspects of this scheme are completely insane, so * we don't support them. Fortunately, they rarely appear. - * One is the mid-file EOD (0x0001) and another is the insane + * One is the mid-file EOD (0x0001) and another is the ill-conceived * "delta" (0x0002), which is like a "goto" statement for bitmaps. - * Whoever thought this up should be shot, then told why - * it's impossible to support in any reasonable way. + * Whoever thought this up should be wrestled to the ground and told + * why it's impossible to support such a scheme in any reasonable way. * Also, RLE4 mode allows runs to stop halfway through a byte, * which is likewise uncodeable, so we don't even try. * Finally, the scanline break is ambiguous -- we assume here that @@ -291,12 +295,15 @@ BMPreadScanline(BMPReader *br) * the end of the scanline, assuming the next bit of data belongs * the following scan. If a break follows the last pixel, as it * seems to in the files I've tested out of Photoshop, you end up - * painting every other line black. BTW, I assume any skipped + * painting every other line black. Also, I assume any skipped * pixels are painted with color 0, which is often black. Nowhere * is it specified what we should assume for missing pixels. This * is undoubtedly the most brain-dead format I've ever encountered. */ sp = br->scanline; + n = br->hdr->width; + if (br->hdr->compr == BI_RLE4) + n = (n + 1) >> 1; while (n > 0) { int skipOdd, len, val; @@ -317,12 +324,14 @@ BMPreadScanline(BMPReader *br) *sp++ = val; continue; } + /* check for escape */ switch (rdbyte(len, br)) { case EOF: return BIR_TRUNCATED; case 0: /* end of line */ while (n--) *sp++ = 0; + /* leaves n == -1 as flag for test after loop */ continue; case 1: /* end of bitmap */ case 2: /* delta */ @@ -347,8 +356,8 @@ BMPreadScanline(BMPReader *br) return BIR_TRUNCATED; } /* verify break at end of line */ - if (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && - (n != 1 || br->yscan != br->hdr->height-1))) + if (!n && (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 && + (n != 1 || br->yscan != br->hdr->height-1)))) return BIR_RLERROR; if (br->seek != NULL) /* record next scanline position */ br->scanpos[br->yscan + 1] = br->fpos; @@ -444,7 +453,7 @@ BMPdecodePixel(int i, const BMPReader *br) case 8: return br->hdr->palette[br->scanline[i]]; case 1: - return br->hdr->palette[br->scanline[i>>3]>>(7-i&7) & 1]; + return br->hdr->palette[br->scanline[i>>3]>>((7-i)&7) & 1]; case 4: return br->hdr->palette[br->scanline[i>>1]>>(i&1?4:0) & 0xf]; case 16: @@ -522,7 +531,7 @@ BMPtruecolorHeader(int xr, int yr, int infolen) return hdr; } -/* allocate color-mapped header (defaults minimal grayscale) */ +/* allocate color-mapped header (defaults to minimal grayscale) */ BMPHeader * BMPmappedHeader(int xr, int yr, int infolen, int ncolors) { @@ -549,7 +558,7 @@ BMPmappedHeader(int xr, int yr, int infolen, int ncolo hdr->height = yr; hdr->yIsDown = 0; /* default to upwards order */ hdr->bpp = n; - hdr->compr = BI_UNCOMPR; + hdr->compr = BI_UNCOMPR; /* compression needs seek */ hdr->hRes = hdr->vRes = 2835; /* default to 72 ppi */ hdr->nColors = ncolors; hdr->impColors = 0; /* says all colors important */ @@ -599,13 +608,8 @@ wrseek(uint32 pos, BMPWriter *bw) { if (pos == bw->fpos) return BIR_OK; - if (bw->seek == NULL) { - if (pos < bw->fpos) - return BIR_SEEKERR; - while (bw->fpos < pos) - wrbyte(0, bw); - return BIR_OK; - } + if (bw->seek == NULL) + return BIR_SEEKERR; if ((*bw->seek)(pos, bw->c_data) != 0) return BIR_SEEKERR; bw->fpos = pos; @@ -626,10 +630,12 @@ BMPopenWriter(void (*cput)(int, void *), int (*seek)(u return NULL; if (!BMPheaderOK(hdr)) return NULL; - if ((hdr->bpp == 16 | hdr->compr == BI_RLE4)) + if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) return NULL; /* unsupported */ - if (seek == NULL && (hdr->compr == BI_RLE8 | hdr->compr == BI_RLE4)) +/* no seek means we may have the wrong file length, but most app's don't care + if (seek == NULL && ((hdr->compr == BI_RLE8) | (hdr->compr == BI_RLE4))) return NULL; +*/ /* compute sizes */ hdrSiz = 2 + 6*4 + 2*2 + 6*4; if (hdr->compr == BI_BITFIELDS) @@ -691,13 +697,13 @@ findNextRun(const int8 *bp, int len) { int pos, cnt; /* look for run */ - for (pos = 0; len > 0 & pos < 255; pos++, bp++, len--) { + for (pos = 0; (len > 0) & (pos < 255); pos++, bp++, len--) { if (len < 5) /* no hope left? */ continue; cnt = 1; /* else let's try it */ while (bp[cnt] == bp[0]) - if (++cnt >= 5) /* long enough */ - return pos; + if (++cnt >= 5) + return pos; /* long enough */ } return pos; /* didn't find any */ } @@ -734,9 +740,8 @@ BMPwriteScanline(BMPWriter *bw) n = bw->hdr->width; while (n > 0) { int cnt, val; - cnt = findNextRun(sp, n); /* 0-255 < n */ - if (cnt >= 3) { /* output non-run */ + if (cnt >= 3) { /* output absolute */ int skipOdd = cnt & 1; wrbyte(0, bw); wrbyte(cnt, bw); @@ -749,18 +754,21 @@ BMPwriteScanline(BMPWriter *bw) if (n <= 0) /* was that it? */ break; val = *sp; /* output run */ - for (cnt = 1; --n > 0 & *++sp == val; cnt++) - ; + for (cnt = 1; --n && cnt < 255; cnt++) + if (*++sp != val) + break; wrbyte(cnt, bw); wrbyte(val, bw); } - bw->yscan++; /* write file length at end */ + bw->yscan++; /* write line break or EOD */ if (bw->yscan == bw->hdr->height) { wrbyte(0, bw); wrbyte(1, bw); /* end of bitmap marker */ - if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0) - return BIR_SEEKERR; - bw->fpos = 2; - wrint32(bw->flen, bw); /* corrected file length */ + if (wrseek(2, bw) != BIR_OK) + return BIR_OK; /* no one may care */ + wrint32(bw->flen, bw); /* correct file length */ + if (wrseek(34, bw) != BIR_OK) + return BIR_OK; + wrint32(bw->flen-bw->fbmp, bw); /* correct bitmap length */ } else { wrbyte(0, bw); wrbyte(0, bw); /* end of line marker */ }