ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bmpfile.c
(Generate patch)

Comparing ray/src/common/bmpfile.c (file contents):
Revision 2.2 by schorsch, Fri Mar 26 21:29:19 2004 UTC vs.
Revision 2.13 by greg, Fri Mar 18 21:04:05 2005 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   *  Windows and OS/2 BMP file support
6   */
7  
8 /* XXX reading and writing of compressed files currently unsupported */
9
8   #include <stdio.h>
9   #include <stdlib.h>
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)
# Line 25 | Line 30 | BMPerrorMessage(int ec)
30                  return "Truncated BMP image";
31          case BIR_UNSUPPORTED:
32                  return "Unsupported BMP feature";
33 +        case BIR_RLERROR:
34 +                return "BMP runlength encoding error";
35          case BIR_SEEKERR:
36                  return "BMP seek error";
37          }
38          return "Unknown BMP error";
39   }
40  
41 < /* compute uncompressed scanline size */
42 < static uint32
43 < getScanSiz(BMPHeader *hdr)
41 > /* check than header is sensible */
42 > static int
43 > BMPheaderOK(const BMPHeader *hdr)
44   {
45 <        uint32  scanSiz;
46 <                                                /* bytes per scanline */
47 <        scanSiz = (hdr->bpp*hdr->width + 7) >> 3;
48 <        if (scanSiz & 3)                        /* 32-bit word boundary */
49 <                scanSiz += 4 - (scanSiz & 3);
50 <
51 <        return scanSiz;
45 >        if (!hdr)
46 >                return 0;
47 >        if ((hdr->width <= 0) | (hdr->height <= 0))
48 >                return 0;
49 >        switch (hdr->bpp) {             /* check compression */
50 >        case 1:
51 >        case 24:
52 >                if (hdr->compr != BI_UNCOMPR)
53 >                        return 0;
54 >                break;
55 >        case 16:
56 >        case 32:
57 >                if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_BITFIELDS))
58 >                        return 0;
59 >                break;
60 >        case 4:
61 >                if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE4))
62 >                        return 0;
63 >                break;
64 >        case 8:
65 >                if ((hdr->compr != BI_UNCOMPR) & (hdr->compr != BI_RLE8))
66 >                        return 0;
67 >                break;
68 >        default:
69 >                return 0;
70 >        }
71 >        if (hdr->compr == BI_BITFIELDS && (BMPbitField(hdr)[0] &
72 >                                BMPbitField(hdr)[1] & BMPbitField(hdr)[2]))
73 >                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  
86 +                                        /* compute uncompressed scan size */
87 + #define getScanSiz(h)   ( ((((h)->bpp*(h)->width+7) >>3) + 3) & ~03 )
88 +
89                                          /* get next byte from reader */
90   #define rdbyte(c,br)    ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c)
91  
# Line 104 | Line 146 | BMPReader *
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;
112        int     magic[2];               /* check magic number */
154          magic[0] = (*cget)(c_data);
155          if (magic[0] != 'B')
156                  return NULL;
# Line 130 | Line 171 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
171          (void)rdint32(br);                      /* file size */
172          (void)rdint32(br);                      /* reserved word */
173          bmPos = rdint32(br);                    /* offset to bitmap */
174 <        hdrSiz = rdint32(br) + 14;              /* header size */
174 >        hdrSiz = 2 + 3*4 + rdint32(br);         /* header size */
175          if (hdrSiz < 2 + 6*4 + 2*2 + 6*4)
176                  goto err;
177          br->hdr->width = rdint32(br);           /* bitmap width */
# Line 142 | Line 183 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
183          if (rduint16(br) != 1)                  /* number of planes */
184                  goto err;
185          br->hdr->bpp = rduint16(br);            /* bits per pixel */
145        if (br->hdr->bpp != 1 && br->hdr->bpp != 4 &&
146                        br->hdr->bpp != 8 && br->hdr->bpp != 24 &&
147                        br->hdr->bpp != 32)
148                goto err;
186          br->hdr->compr = rdint32(br);           /* compression mode */
150        if (br->hdr->compr != BI_UNCOMPR && br->hdr->compr != BI_RLE8)
151                goto err;                       /* don't support the rest */
187          (void)rdint32(br);                      /* bitmap size */
188          br->hdr->hRes = rdint32(br);            /* horizontal resolution */
189          br->hdr->vRes = rdint32(br);            /* vertical resolution */
# Line 156 | Line 191 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
191          br->hdr->impColors = rdint32(br);       /* # important colors */
192          if (br->hdr->impColors < 0)
193                  goto err;                       /* catch premature EOF */
194 <        palLen = 0;
160 <        if (br->hdr->bpp <= 8) {
161 <                palLen = 1 << br->hdr->bpp;
162 <                if (br->hdr->nColors <= 0)
163 <                        br->hdr->nColors = palLen;
164 <                else if (br->hdr->nColors > palLen)
165 <                        goto err;
166 <                if (br->hdr->impColors <= 0)
167 <                        br->hdr->impColors = br->hdr->nColors;
168 <                else if (br->hdr->impColors > br->hdr->nColors)
169 <                        goto err;
170 <        } else if (br->hdr->nColors > 0 || br->hdr->compr != BI_UNCOMPR)
194 >        if (!BMPheaderOK(br->hdr))
195                  goto err;
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 -
180 <                                        sizeof(br->hdr->palette) +
181 <                                        br->hdr->infoSiz);
206 >                                        palSiz + br->hdr->infoSiz);
207                  if (br->hdr == NULL)
208                          goto err;
209          }
210 <                                                /* read color palette */
211 <        if (rdbytes((char *)br->hdr->palette,
212 <                        sizeof(RGBquad)*palLen, br) != BIR_OK)
210 >                                                /* read colors or fields */
211 >        if (br->hdr->compr == BI_BITFIELDS) {
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, palSiz, br) != BIR_OK)
216                  goto err;
217                                                  /* read add'l information */
218          if (rdbytes(BMPinfo(br->hdr), br->hdr->infoSiz, br) != BIR_OK)
# Line 194 | Line 222 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
222          if (br->scanline == NULL)
223                  goto err;
224          br->yscan = -1;
225 <        if (seek != NULL && br->hdr->compr != BI_UNCOMPR) {
225 >        if (seek != NULL && ((br->hdr->compr == BI_RLE8) |
226 >                                        (br->hdr->compr == BI_RLE4))) {
227                  BMPReader       *newbr = (BMPReader *)realloc((void *)br,
228                                                  sizeof(BMPReader) +
229                                                  sizeof(br->scanpos[0]) *
# Line 206 | Line 235 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
235                                  sizeof(br->scanpos[0])*br->hdr->height);
236          }
237          br->scanpos[0] = br->fpos;
238 <        if (BMPreadScanline(br) != BIR_OK)
239 <                goto err;
211 <        return br;
238 >        if (BMPreadScanline(br) == BIR_OK)
239 >                return br;
240   err:
241          if (br->hdr != NULL)
242                  free((void *)br->hdr);
# Line 229 | Line 257 | BMPisGrayscale(const BMPHeader *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 */
# Line 239 | Line 267 | BMPisGrayscale(const BMPHeader *hdr)
267   int
268   BMPreadScanline(BMPReader *br)
269   {
270 +        int     n;
271 +        int8    *sp;
272 +
273          if (br->yscan + 1 >= br->hdr->height)
274                  return BIR_EOF;
275          br->yscan++;                    /* prepare to read */
276 <        if (br->hdr->compr == BI_UNCOMPR)
277 <                return rdbytes((char *)br->scanline, getScanSiz(br->hdr), br);
278 < /* XXX need to perform actual decompression */
279 <        return BIR_UNSUPPORTED;
280 <        if (br->seek != NULL)
276 >        n = getScanSiz(br->hdr);        /* reading uncompressed data? */
277 >        if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS)
278 >                return rdbytes((char *)br->scanline, n, br);
279 >        /*
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 ill-conceived
285 >         * "delta" (0x0002), which is like a "goto" statement for bitmaps.
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
291 >         * it is required at the end of each scanline, though I haven't
292 >         * found anywhere this is written.  Otherwise, we would read to
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.  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 >
308 >                if (rdbyte(len, br) == EOF)
309 >                        return BIR_TRUNCATED;
310 >                if (len > 0) {          /* got a run */
311 >                        if (br->hdr->compr == BI_RLE4) {
312 >                                if (len & 1)
313 >                                        return BIR_UNSUPPORTED;
314 >                                len >>= 1;
315 >                        }
316 >                        if (len > n)
317 >                                return BIR_RLERROR;
318 >                        if (rdbyte(val, br) == EOF)
319 >                                return BIR_TRUNCATED;
320 >                        n -= len;
321 >                        while (len--)
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 */
336 >                        return BIR_UNSUPPORTED;
337 >                }
338 >                                        /* absolute mode */
339 >                if (br->hdr->compr == BI_RLE4) {
340 >                        if (len & 1)
341 >                                return BIR_UNSUPPORTED;
342 >                        len >>= 1;
343 >                }
344 >                skipOdd = len & 1;
345 >                if (len > n)
346 >                        return BIR_RLERROR;
347 >                n -= len;
348 >                while (len--) {
349 >                        if (rdbyte(val, br) == EOF)
350 >                                return BIR_TRUNCATED;
351 >                        *sp++ = val;
352 >                }
353 >                if (skipOdd && rdbyte(val, br) == EOF)
354 >                        return BIR_TRUNCATED;
355 >        }
356 >                                        /* verify break at end of line */
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;
362          return BIR_OK;
363   }
# Line 270 | Line 381 | BMPseekScanline(int y, BMPReader *br)
381          if (y != br->yscan + 1 && br->seek != NULL) {
382                  int     yseek;
383                  uint32  seekp;
384 <                if (br->hdr->compr == BI_UNCOMPR) {
384 >                if (br->hdr->compr == BI_UNCOMPR ||
385 >                                        br->hdr->compr == BI_BITFIELDS) {
386                          yseek = y;
387                          seekp = br->scanpos[0] + y*getScanSiz(br->hdr);
388                  } else {
# Line 295 | Line 407 | BMPseekScanline(int y, BMPReader *br)
407  
408   /* get ith pixel from last scanline */
409   RGBquad
410 < BMPdecodePixel(int i, BMPReader *br)
410 > BMPdecodePixel(int i, const BMPReader *br)
411   {
412 +        static const uint32     std16mask[3] = {0x7c00, 0x3e0, 0x1f};
413          static const RGBquad    black = {0, 0, 0, 0};
414 +        const uint32            *mask;
415 +        const uint8             *pp;
416 +        uint32                  pval, v;
417 +        RGBquad                 cval;
418          
419          if (((br == NULL) | (i < 0)) || i >= br->hdr->width)
420                  return black;
421  
422 +        cval.padding = 0;
423 +
424          switch (br->hdr->bpp) {
425 <        case 24: {
426 <                uint8   *bgr = br->scanline + 3*i;
427 <                RGBquad cval;
428 <                cval.b = bgr[0]; cval.g = bgr[1]; cval.r = bgr[2];
429 <                cval.padding = 0;
425 >        case 24:
426 >                pp = br->scanline + 3*i;
427 >                cval.b = *pp++;
428 >                cval.g = *pp++;
429 >                cval.r = *pp;
430                  return cval;
312                }
431          case 32:
432 <                return ((RGBquad *)br->scanline)[i];
432 >                if (br->hdr->compr == BI_UNCOMPR)
433 >                        return ((RGBquad *)br->scanline)[i];
434 >                                                /* convert bit fields */
435 >                pp = br->scanline + 4*i;
436 >                pval = *pp++;
437 >                pval |= *pp++ << 8;
438 >                pval |= *pp++ << 16;
439 >                pval |= *pp << 24;
440 >                mask = BMPbitField(br->hdr);
441 >                v = pval & mask[0];
442 >                while (v & ~0xff) v >>= 8;
443 >                cval.r = v;
444 >                v = pval & mask[1];
445 >                while (v & ~0xff) v >>= 8;
446 >                cval.g = v;
447 >                v = pval & mask[2];
448 >                while (v & ~0xff) v >>= 8;
449 >                cval.b = v;
450 >                return cval;
451          case 8:
452                  return br->hdr->palette[br->scanline[i]];
453          case 1:
454 <                return br->hdr->palette[br->scanline[i>>3]>>(i&7) & 1];
454 >                return br->hdr->palette[br->scanline[i>>3]>>((7-i)&7) & 1];
455          case 4:
456 <                return br->hdr->palette[br->scanline[i>>1]>>((i&1)<<2) & 0xf];
456 >                return br->hdr->palette[br->scanline[i>>1]>>(i&1?4:0) & 0xf];
457 >        case 16:
458 >                pp = br->scanline + 2*i;
459 >                pval = *pp++;
460 >                pval |= *pp++ << 8;
461 >                mask = std16mask;
462 >                if (br->hdr->compr == BI_BITFIELDS)
463 >                        mask = BMPbitField(br->hdr);
464 >                cval.r = ((pval & mask[0]) << 8) / (mask[0] + 1);
465 >                cval.g = ((pval & mask[1]) << 8) / (mask[1] + 1);
466 >                cval.b = ((pval & mask[2]) << 8) / (mask[2] + 1);
467 >                return cval;
468          }
469          return black;                           /* should never happen */
470   }
# Line 409 | Line 556 | BMPmappedHeader(int xr, int yr, int infolen, int ncolo
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 */
# Line 423 | Line 570 | BMPmappedHeader(int xr, int yr, int infolen, int ncolo
570  
571                                          /* put byte to writer */
572   #define wrbyte(c,bw)    ( (*(bw)->cput)(c,(bw)->c_data), \
573 <                                ++((bw)->fpos) > (bw)->flen ? \
573 >                                ++(bw)->fpos > (bw)->flen ? \
574                                          ((bw)->flen = (bw)->fpos) : \
575                                          (bw)->fpos )
576  
# Line 442 | Line 589 | wrint32(int32 i, BMPWriter *bw)
589          wrbyte(i& 0xff, bw);
590          wrbyte(i>>8 & 0xff, bw);
591          wrbyte(i>>16 & 0xff, bw);
592 <        wrbyte(i>>24  & 0xff, bw);
592 >        wrbyte(i>>24 & 0xff, bw);
593   }
594  
595   /* write 16-bit unsigned integer in littlendian order */
# Line 459 | Line 606 | wrseek(uint32 pos, BMPWriter *bw)
606   {
607          if (pos == bw->fpos)
608                  return BIR_OK;
609 <        if (bw->seek == NULL) {
610 <                if (pos < bw->fpos)
464 <                        return BIR_SEEKERR;
465 <                while (bw->fpos < pos)
466 <                        wrbyte(0, bw);
467 <                return BIR_OK;
468 <        }
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;
# Line 482 | Line 624 | BMPopenWriter(void (*cput)(int, void *), int (*seek)(u
624          BMPWriter       *bw;
625          uint32          hdrSiz, palSiz, scanSiz, bmSiz;
626                                                  /* check arguments */
627 <        if (cput == NULL || hdr == NULL)
627 >        if (cput == NULL)
628                  return NULL;
629 <        if (hdr->width <= 0 || hdr->height <= 0)
629 >        if (!BMPheaderOK(hdr))
630                  return NULL;
631 <        if (hdr->compr != BI_UNCOMPR && hdr->compr != BI_RLE8)
631 >        if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4))
632                  return NULL;                    /* unsupported */
633 <        if (hdr->bpp > 8 && hdr->compr != BI_UNCOMPR)
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 <        palSiz = BMPpalLen(hdr);
494 <        if (hdr->nColors > palSiz)
495 <                return NULL;
496 <        if (hdr->compr != BI_UNCOMPR && seek == NULL)
497 <                return NULL;
636 > */
637                                                  /* compute sizes */
638          hdrSiz = 2 + 6*4 + 2*2 + 6*4;
639 <        palSiz *= sizeof(RGBquad);
639 >        if (hdr->compr == BI_BITFIELDS)
640 >                hdrSiz += sizeof(uint32)*3;
641 >        palSiz = sizeof(RGBquad)*hdr->nColors;
642          scanSiz = getScanSiz(hdr);
643          bmSiz = hdr->height*scanSiz;            /* wrong if compressed */
644                                                  /* initialize writer */
# Line 547 | Line 688 | BMPopenWriter(void (*cput)(int, void *), int (*seek)(u
688   #endif
689          return bw;
690   }
691 +
692 + /* find position of next run of 5 or more identical bytes, or 255 if none */
693 + static int
694 + findNextRun(const int8 *bp, int len)
695 + {
696 +        int     pos, cnt;
697 +                                                /* look for run */
698 +        for (pos = 0; (len > 0) & (pos < 255); pos++, bp++, len--) {
699 +                if (len < 5)                    /* no hope left? */
700 +                        continue;
701 +                cnt = 1;                        /* else let's try it */
702 +                while (bp[cnt] == bp[0])
703 +                        if (++cnt >= 5)
704 +                                return pos;     /* long enough */
705 +        }
706 +        return pos;                             /* didn't find any */
707 + }
708                                  
709   /* write the current scanline */
710   int
711   BMPwriteScanline(BMPWriter *bw)
712   {
713 +        const int8      *sp;
714 +        int             n;
715 +
716          if (bw->yscan >= bw->hdr->height)
717                  return BIR_EOF;
718                                                  /* writing uncompressed? */
719 <        if (bw->hdr->compr == BI_UNCOMPR) {
719 >        if (bw->hdr->compr == BI_UNCOMPR || bw->hdr->compr == BI_BITFIELDS) {
720                  uint32  scanSiz = getScanSiz(bw->hdr);
721                  uint32  slpos = bw->fbmp + bw->yscan*scanSiz;
722                  if (wrseek(slpos, bw) != BIR_OK)
# Line 564 | Line 725 | BMPwriteScanline(BMPWriter *bw)
725                  bw->yscan++;
726                  return BIR_OK;
727          }
728 <                                                /* else write compressed */
729 < /* XXX need to do actual compression */
730 <        return BIR_UNSUPPORTED;
731 <                                                /* write file length at end */
728 >        /*
729 >         * RLE8 Encoding
730 >         *
731 >         * See the notes in BMPreadScanline() on this encoding.  Needless
732 >         * to say, we avoid the nuttier aspects of this specification.
733 >         * We also assume that every scanline ends in a line break
734 >         * (0x0000) except for the last, which ends in a bitmap break
735 >         * (0x0001).  We don't support RLE4 at all; it's too awkward.
736 >         */
737 >        sp = bw->scanline;
738 >        n = bw->hdr->width;
739 >        while (n > 0) {
740 >                int     cnt, val;
741 >                cnt = findNextRun(sp, n);       /* 0-255 < n */
742 >                if (cnt >= 3) {                 /* output absolute */
743 >                        int     skipOdd = cnt & 1;
744 >                        wrbyte(0, bw);
745 >                        wrbyte(cnt, bw);
746 >                        n -= cnt;
747 >                        while (cnt--)
748 >                                wrbyte(*sp++, bw);
749 >                        if (skipOdd)
750 >                                wrbyte(0, bw);
751 >                }
752 >                if (n <= 0)                     /* was that it? */
753 >                        break;
754 >                val = *sp;                      /* output run */
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 line break or EOD */
762          if (bw->yscan == bw->hdr->height) {
763 <                if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0)
764 <                        return BIR_SEEKERR;
765 <                bw->fpos = 2;
766 <                wrint32(bw->flen, bw);
763 >                wrbyte(0, bw); wrbyte(1, bw);   /* end of bitmap marker */
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          }
773          return BIR_OK;
774   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines