--- ray/src/common/bmpfile.c 2004/03/26 23:30:05 2.4 +++ ray/src/common/bmpfile.c 2025/09/12 19:31:15 2.22 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bmpfile.c,v 2.4 2004/03/26 23:30:05 schorsch Exp $"; +static const char RCSid[] = "$Id: bmpfile.c,v 2.22 2025/09/12 19:31:15 greg Exp $"; #endif /* * Windows and OS/2 BMP file support @@ -10,7 +10,14 @@ static const char RCSid[] = "$Id: bmpfile.c,v 2.4 2004 #include #include "bmpfile.h" -/* get corresponding error message */ +#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ +#undef getc +#undef putc +#define getc getc_unlocked +#define putc putc_unlocked +#endif + +/* Get corresponding error message */ const char * BMPerrorMessage(int ec) { @@ -31,7 +38,7 @@ BMPerrorMessage(int ec) return "Unknown BMP error"; } -/* check than header is sensible */ +/* Check that header is sensible */ static int BMPheaderOK(const BMPHeader *hdr) { @@ -64,12 +71,15 @@ 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)) - return 0; - if (hdr->impColors > hdr->nColors) - return 0; - if (hdr->nColors > BMPpalLen(hdr)) - return 0; + if (hdr->bpp > 8) { + if (hdr->nColors != 0) + return 0; + } else { + if ((hdr->nColors < 0) | (hdr->nColors > 1<bpp)) + return 0; + if ((hdr->impColors < 0) | (hdr->impColors > hdr->nColors)) + return 0; + } return 1; } @@ -79,7 +89,7 @@ BMPheaderOK(const BMPHeader *hdr) /* get next byte from reader */ #define rdbyte(c,br) ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c) -/* read n bytes */ +/* Read n bytes */ static int rdbytes(char *bp, uint32 n, BMPReader *br) { @@ -93,7 +103,7 @@ rdbytes(char *bp, uint32 n, BMPReader *br) return BIR_OK; } -/* read 32-bit integer in littlendian order */ +/* Read 32-bit integer in littlendian order */ static int32 rdint32(BMPReader *br) { @@ -107,7 +117,7 @@ rdint32(BMPReader *br) return i; /* -1 on EOF */ } -/* read 16-bit unsigned integer in littlendian order */ +/* Read 16-bit unsigned integer in littlendian order */ static int rduint16(BMPReader *br) { @@ -119,7 +129,7 @@ rduint16(BMPReader *br) return i; /* -1 on EOF */ } -/* seek on reader or return 0 (BIR_OK) on success */ +/* Seek on reader or return 0 (BIR_OK) on success */ static int rdseek(uint32 pos, BMPReader *br) { @@ -131,17 +141,18 @@ rdseek(uint32 pos, BMPReader *br) return BIR_OK; } -/* open BMP stream for reading and get first scanline */ +/* Open BMP stream for reading and get first scanline */ BMPReader * BMPopenReader(int (*cget)(void *), int (*seek)(uint32, void *), void *c_data) { BMPReader *br; - uint32 bmPos, hdrSiz; - int palLen; + uint32 bmPos, hdrSiz, palSiz; + int magic[2]; /* check magic number */ if (cget == NULL) return NULL; - int magic[2]; /* check magic number */ + if (cget == &stdio_getc && c_data == NULL) + return NULL; /* stdio error condition */ magic[0] = (*cget)(c_data); if (magic[0] != 'B') return NULL; @@ -179,27 +190,24 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, br->hdr->hRes = rdint32(br); /* horizontal resolution */ br->hdr->vRes = rdint32(br); /* vertical resolution */ br->hdr->nColors = rdint32(br); /* # colors used */ + if (!br->hdr->nColors && br->hdr->bpp <= 8) + br->hdr->nColors = 1<hdr->bpp; br->hdr->impColors = rdint32(br); /* # important colors */ if (br->hdr->impColors < 0) goto err; /* catch premature EOF */ if (!BMPheaderOK(br->hdr)) goto err; - palLen = BMPpalLen(br->hdr); - if (br->hdr->bpp <= 8) { /* normalize color counts */ - if (br->hdr->nColors <= 0) - br->hdr->nColors = palLen; - if (br->hdr->impColors <= 0) - br->hdr->impColors = br->hdr->nColors; - } + palSiz = sizeof(RGBquad)*br->hdr->nColors; + if (br->hdr->impColors <= 0) + br->hdr->impColors = br->hdr->nColors; /* extend header */ - if (bmPos < hdrSiz + sizeof(RGBquad)*palLen) + if (bmPos < hdrSiz + palSiz) goto err; - br->hdr->infoSiz = bmPos - (hdrSiz + sizeof(RGBquad)*palLen); - if (palLen > 0 || br->hdr->infoSiz > 0) { - br->hdr = (BMPHeader *)realloc((void *)br->hdr, + br->hdr->infoSiz = bmPos - (hdrSiz + palSiz); + if (br->hdr->nColors > 0 || br->hdr->infoSiz > 0) { + br->hdr = (BMPHeader *)realloc(br->hdr, sizeof(BMPHeader) + - sizeof(RGBquad)*palLen + - br->hdr->infoSiz); + palSiz + br->hdr->infoSiz); if (br->hdr == NULL) goto err; } @@ -208,8 +216,7 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, BMPbitField(br->hdr)[0] = (uint32)rdint32(br); BMPbitField(br->hdr)[1] = (uint32)rdint32(br); BMPbitField(br->hdr)[2] = (uint32)rdint32(br); - } else if (rdbytes((char *)br->hdr->palette, - sizeof(RGBquad)*palLen, br) != BIR_OK) + } else if (rdbytes((char *)br->hdr->palette, palSiz, br) != BIR_OK) goto err; /* read add'l information */ if (rdbytes(BMPinfo(br->hdr), br->hdr->infoSiz, br) != BIR_OK) @@ -221,30 +228,29 @@ BMPopenReader(int (*cget)(void *), int (*seek)(uint32, br->yscan = -1; if (seek != NULL && ((br->hdr->compr == BI_RLE8) | (br->hdr->compr == BI_RLE4))) { - BMPReader *newbr = (BMPReader *)realloc((void *)br, + BMPReader *newbr = (BMPReader *)realloc(br, sizeof(BMPReader) + sizeof(br->scanpos[0]) * br->hdr->height); if (newbr == NULL) goto err; br = newbr; - memset((void *)(br->scanpos + 1), 0, + memset(br->scanpos+1, 0, 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); + free(br->hdr); if (br->scanline != NULL) - free((void *)br->scanline); - free((void *)br); + free(br->scanline); + free(br); return NULL; } -/* determine if image is grayscale */ +/* Determine if image is grayscale */ int BMPisGrayscale(const BMPHeader *hdr) { @@ -256,12 +262,12 @@ BMPisGrayscale(const BMPHeader *hdr) if (hdr->bpp > 8) /* assume they had a reason for it */ return 0; for (rgbp = hdr->palette, n = hdr->impColors; n-- > 0; rgbp++) - if (((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b))) + if ((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b)) return 0; return 1; /* all colors neutral in map */ } -/* read and decode next BMP scanline */ +/* Read and decode next BMP scanline */ int BMPreadScanline(BMPReader *br) { @@ -275,14 +281,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 +297,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; + sp = (int8 *)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 +326,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,15 +358,15 @@ 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; return BIR_OK; } -/* read a specific scanline */ +/* Read a specific scanline */ int BMPseekScanline(int y, BMPReader *br) { @@ -398,7 +409,7 @@ BMPseekScanline(int y, BMPReader *br) return BIR_OK; } -/* get ith pixel from last scanline */ +/* Get ith pixel from last scanline */ RGBquad BMPdecodePixel(int i, const BMPReader *br) { @@ -444,7 +455,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: @@ -462,15 +473,15 @@ BMPdecodePixel(int i, const BMPReader *br) return black; /* should never happen */ } -/* free BMP reader resources */ +/* Free BMP reader resources */ void BMPfreeReader(BMPReader *br) { if (br == NULL) return; - free((void *)br->hdr); - free((void *)br->scanline); - free((void *)br); + free(br->hdr); + free(br->scanline); + free(br); } /* stdio getc() callback */ @@ -499,7 +510,7 @@ stdio_fseek(uint32 pos, void *p) return fseek((FILE *)p, (long)pos, 0); } -/* allocate uncompressed (24-bit) RGB header */ +/* Allocate uncompressed (24-bit) RGB header */ BMPHeader * BMPtruecolorHeader(int xr, int yr, int infolen) { @@ -518,11 +529,12 @@ BMPtruecolorHeader(int xr, int yr, int infolen) hdr->compr = BI_UNCOMPR; hdr->hRes = hdr->vRes = 2835; /* default to 72 ppi */ hdr->nColors = hdr->impColors = 0; - hdr->infoSiz = infolen; + if ((hdr->infoSiz = infolen) > 0) + memset(BMPinfo(hdr), 0, hdr->infoSiz); 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) { @@ -540,7 +552,7 @@ BMPmappedHeader(int xr, int yr, int infolen, int ncolo else return NULL; hdr = (BMPHeader *)malloc(sizeof(BMPHeader) + - sizeof(RGBquad)*(1<palette) + infolen); if (hdr == NULL) @@ -549,12 +561,12 @@ 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 */ hdr->infoSiz = infolen; - memset((void *)hdr->palette, 0, sizeof(RGBquad)*(1<palette, 0, sizeof(RGBquad)*((size_t)1<palette[n].r = hdr->palette[n].g = hdr->palette[n].b = n*255/(ncolors-1); @@ -567,7 +579,7 @@ BMPmappedHeader(int xr, int yr, int infolen, int ncolo ((bw)->flen = (bw)->fpos) : \ (bw)->fpos ) -/* write out a string of bytes */ +/* Write out a string of bytes */ static void wrbytes(char *bp, uint32 n, BMPWriter *bw) { @@ -575,7 +587,7 @@ wrbytes(char *bp, uint32 n, BMPWriter *bw) wrbyte(*bp++, bw); } -/* write 32-bit integer in littlendian order */ +/* Write 32-bit integer in littlendian order */ static void wrint32(int32 i, BMPWriter *bw) { @@ -585,7 +597,7 @@ wrint32(int32 i, BMPWriter *bw) wrbyte(i>>24 & 0xff, bw); } -/* write 16-bit unsigned integer in littlendian order */ +/* Write 16-bit unsigned integer in littlendian order */ static void wruint16(uint16 ui, BMPWriter *bw) { @@ -593,19 +605,14 @@ wruint16(uint16 ui, BMPWriter *bw) wrbyte(ui>>8 & 0xff, bw); } -/* seek to the specified file position, returning 0 (BIR_OK) on success */ +/* Seek to the specified file position, returning 0 (BIR_OK) on success */ static int 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; @@ -614,7 +621,7 @@ wrseek(uint32 pos, BMPWriter *bw) return BIR_OK; } -/* open BMP stream for writing */ +/* Open BMP stream for writing */ BMPWriter * BMPopenWriter(void (*cput)(int, void *), int (*seek)(uint32, void *), void *c_data, BMPHeader *hdr) @@ -624,17 +631,21 @@ BMPopenWriter(void (*cput)(int, void *), int (*seek)(u /* check arguments */ if (cput == NULL) return NULL; + if (cput == &stdio_putc && c_data == NULL) + return NULL; /* stdio error condition */ if (!BMPheaderOK(hdr)) return NULL; if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) return NULL; /* unsupported */ +/* 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) hdrSiz += sizeof(uint32)*3; - palSiz = sizeof(RGBquad)*BMPpalLen(hdr); + palSiz = sizeof(RGBquad)*hdr->nColors; scanSiz = getScanSiz(hdr); bmSiz = hdr->height*scanSiz; /* wrong if compressed */ /* initialize writer */ @@ -645,7 +656,7 @@ BMPopenWriter(void (*cput)(int, void *), int (*seek)(u bw->yscan = 0; bw->scanline = (uint8 *)calloc(scanSiz, sizeof(uint8)); if (bw->scanline == NULL) { - free((void *)bw); + free(bw); return NULL; } bw->fbmp = hdrSiz + palSiz + hdr->infoSiz; @@ -685,7 +696,7 @@ BMPopenWriter(void (*cput)(int, void *), int (*seek)(u return bw; } -/* find position of next run of 5 or more identical bytes, or 255 if none */ +/* Find position of next run of 5 or more identical bytes, or 255 if none */ static int findNextRun(const int8 *bp, int len) { @@ -696,13 +707,13 @@ findNextRun(const int8 *bp, int len) 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 */ } -/* write the current scanline */ +/* Write the current scanline */ int BMPwriteScanline(BMPWriter *bw) { @@ -730,13 +741,12 @@ BMPwriteScanline(BMPWriter *bw) * (0x0000) except for the last, which ends in a bitmap break * (0x0001). We don't support RLE4 at all; it's too awkward. */ - sp = bw->scanline; + sp = (const int8 *)bw->scanline; 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); @@ -748,32 +758,35 @@ BMPwriteScanline(BMPWriter *bw) } if (n <= 0) /* was that it? */ break; - val = *sp; /* output run */ - for (cnt = 1; (--n > 0) & (*++sp == val); cnt++) - ; + val = *sp++; /* output run */ + for (cnt = 1; --n && cnt < 255; cnt++, sp++) + 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 */ } return BIR_OK; } -/* free BMP writer resources */ +/* Free BMP writer resources */ void BMPfreeWriter(BMPWriter *bw) { if (bw == NULL) return; - free((void *)bw->hdr); - free((void *)bw->scanline); - free((void *)bw); + free(bw->hdr); + free(bw->scanline); + free(bw); }