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.1 by greg, Fri Mar 26 03:11:50 2004 UTC vs.
Revision 2.12 by greg, Tue Sep 14 02:53:50 2004 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->nColors < 0) | (hdr->impColors < 0))
75 >                return 0;
76 >        if (hdr->impColors > hdr->nColors)
77 >                return 0;
78 >        if (hdr->nColors > BMPpalLen(hdr))
79 >                return 0;
80 >        return 1;
81   }
82  
83 +                                        /* compute uncompressed scan size */
84 + #define getScanSiz(h)   ( ((((h)->bpp*(h)->width+7) >>3) + 3) & ~03 )
85 +
86                                          /* get next byte from reader */
87   #define rdbyte(c,br)    ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c)
88  
# Line 106 | Line 145 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
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;
112        int     magic[2];               /* check magic number */
152          magic[0] = (*cget)(c_data);
153          if (magic[0] != 'B')
154                  return NULL;
# Line 130 | Line 169 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
169          (void)rdint32(br);                      /* file size */
170          (void)rdint32(br);                      /* reserved word */
171          bmPos = rdint32(br);                    /* offset to bitmap */
172 <        hdrSiz = rdint32(br) + 14;              /* header size */
172 >        hdrSiz = 2 + 3*4 + rdint32(br);         /* header size */
173          if (hdrSiz < 2 + 6*4 + 2*2 + 6*4)
174                  goto err;
175          br->hdr->width = rdint32(br);           /* bitmap width */
176          br->hdr->height = rdint32(br);          /* bitmap height */
177 <        if ((br->hdr->width <= 0 | br->hdr->height == 0))
177 >        if (((br->hdr->width <= 0) | (br->hdr->height == 0)))
178                  goto err;
179          if ((br->hdr->yIsDown = br->hdr->height < 0))
180                  br->hdr->height = -br->hdr->height;
181          if (rduint16(br) != 1)                  /* number of planes */
182                  goto err;
183          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;
184          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 */
185          (void)rdint32(br);                      /* bitmap size */
186          br->hdr->hRes = rdint32(br);            /* horizontal resolution */
187          br->hdr->vRes = rdint32(br);            /* vertical resolution */
# Line 156 | Line 189 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
189          br->hdr->impColors = rdint32(br);       /* # important colors */
190          if (br->hdr->impColors < 0)
191                  goto err;                       /* catch premature EOF */
192 <        palLen = 0;
193 <        if (br->hdr->bpp <= 8) {
194 <                palLen = 1 << br->hdr->bpp;
192 >        if (!BMPheaderOK(br->hdr))
193 >                goto err;
194 >        palLen = BMPpalLen(br->hdr);
195 >        if (br->hdr->bpp <= 8) {                /* normalize color counts */
196                  if (br->hdr->nColors <= 0)
197                          br->hdr->nColors = palLen;
164                else if (br->hdr->nColors > palLen)
165                        goto err;
198                  if (br->hdr->impColors <= 0)
199                          br->hdr->impColors = br->hdr->nColors;
200 <                else if (br->hdr->impColors > br->hdr->nColors)
169 <                        goto err;
170 <        } else if (br->hdr->nColors > 0 || br->hdr->compr != BI_UNCOMPR)
171 <                goto err;
200 >        }
201                                                  /* extend header */
202          if (bmPos < hdrSiz + sizeof(RGBquad)*palLen)
203                  goto err;
# Line 176 | Line 205 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
205          if (palLen > 0 || br->hdr->infoSiz > 0) {
206                  br->hdr = (BMPHeader *)realloc((void *)br->hdr,
207                                          sizeof(BMPHeader) +
208 <                                        sizeof(RGBquad)*palLen -
180 <                                        sizeof(br->hdr->palette) +
208 >                                        sizeof(RGBquad)*palLen +
209                                          br->hdr->infoSiz);
210                  if (br->hdr == NULL)
211                          goto err;
212          }
213 <                                                /* read color palette */
214 <        if (rdbytes((char *)br->hdr->palette,
213 >                                                /* read colors or fields */
214 >        if (br->hdr->compr == BI_BITFIELDS) {
215 >                BMPbitField(br->hdr)[0] = (uint32)rdint32(br);
216 >                BMPbitField(br->hdr)[1] = (uint32)rdint32(br);
217 >                BMPbitField(br->hdr)[2] = (uint32)rdint32(br);
218 >        } else if (rdbytes((char *)br->hdr->palette,
219                          sizeof(RGBquad)*palLen, br) != BIR_OK)
220                  goto err;
221                                                  /* read add'l information */
# Line 194 | Line 226 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
226          if (br->scanline == NULL)
227                  goto err;
228          br->yscan = -1;
229 <        if (seek != NULL && br->hdr->compr != BI_UNCOMPR) {
229 >        if (seek != NULL && ((br->hdr->compr == BI_RLE8) |
230 >                                        (br->hdr->compr == BI_RLE4))) {
231                  BMPReader       *newbr = (BMPReader *)realloc((void *)br,
232                                                  sizeof(BMPReader) +
233                                                  sizeof(br->scanpos[0]) *
# Line 206 | Line 239 | BMPopenReader(int (*cget)(void *), int (*seek)(uint32,
239                                  sizeof(br->scanpos[0])*br->hdr->height);
240          }
241          br->scanpos[0] = br->fpos;
242 <        if (BMPreadScanline(br) != BIR_OK)
243 <                goto err;
211 <        return br;
242 >        if (BMPreadScanline(br) == BIR_OK)
243 >                return br;
244   err:
245          if (br->hdr != NULL)
246                  free((void *)br->hdr);
# Line 229 | Line 261 | BMPisGrayscale(const BMPHeader *hdr)
261                  return -1;
262          if (hdr->bpp > 8)               /* assume they had a reason for it */
263                  return 0;
264 <        for (rgbp = hdr->palette, n = hdr->impColors; n-- > 0; rgbp++)
265 <                if ((rgbp->r != rgbp->g | rgbp->g != rgbp->b))
264 >        for (rgbp = hdr->palette, n = hdr->nColors; n-- > 0; rgbp++)
265 >                if (((rgbp->r != rgbp->g) | (rgbp->g != rgbp->b)))
266                          return 0;
267          return 1;                       /* all colors neutral in map */
268   }
# Line 239 | Line 271 | BMPisGrayscale(const BMPHeader *hdr)
271   int
272   BMPreadScanline(BMPReader *br)
273   {
274 +        int     n;
275 +        int8    *sp;
276 +
277          if (br->yscan + 1 >= br->hdr->height)
278                  return BIR_EOF;
279          br->yscan++;                    /* prepare to read */
280 <        if (br->hdr->compr == BI_UNCOMPR)
281 <                return rdbytes((char *)br->scanline, getScanSiz(br->hdr), br);
282 < /* XXX need to perform actual decompression */
283 <        return BIR_UNSUPPORTED;
284 <        if (br->seek != NULL)
280 >        n = getScanSiz(br->hdr);        /* reading uncompressed data? */
281 >        if (br->hdr->compr == BI_UNCOMPR || br->hdr->compr == BI_BITFIELDS)
282 >                return rdbytes((char *)br->scanline, n, br);
283 >        /*
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 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
295 >         * it is required at the end of each scanline, though I haven't
296 >         * found anywhere this is written.  Otherwise, we would read to
297 >         * the end of the scanline, assuming the next bit of data belongs
298 >         * the following scan.  If a break follows the last pixel, as it
299 >         * seems to in the files I've tested out of Photoshop, you end up
300 >         * painting every other line black.  Also, I assume any skipped
301 >         * pixels are painted with color 0, which is often black.  Nowhere
302 >         * is it specified what we should assume for missing pixels.  This
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 >
312 >                if (rdbyte(len, br) == EOF)
313 >                        return BIR_TRUNCATED;
314 >                if (len > 0) {          /* got a run */
315 >                        if (br->hdr->compr == BI_RLE4) {
316 >                                if (len & 1)
317 >                                        return BIR_UNSUPPORTED;
318 >                                len >>= 1;
319 >                        }
320 >                        if (len > n)
321 >                                return BIR_RLERROR;
322 >                        if (rdbyte(val, br) == EOF)
323 >                                return BIR_TRUNCATED;
324 >                        n -= len;
325 >                        while (len--)
326 >                                *sp++ = val;
327 >                        continue;
328 >                }
329 >                                        /* check for escape */
330 >                switch (rdbyte(len, br)) {
331 >                case EOF:
332 >                        return BIR_TRUNCATED;
333 >                case 0:                 /* end of line */
334 >                        while (n--)
335 >                                *sp++ = 0;
336 >                        /* leaves n == -1 as flag for test after loop */
337 >                        continue;
338 >                case 1:                 /* end of bitmap */
339 >                case 2:                 /* delta */
340 >                        return BIR_UNSUPPORTED;
341 >                }
342 >                                        /* absolute mode */
343 >                if (br->hdr->compr == BI_RLE4) {
344 >                        if (len & 1)
345 >                                return BIR_UNSUPPORTED;
346 >                        len >>= 1;
347 >                }
348 >                skipOdd = len & 1;
349 >                if (len > n)
350 >                        return BIR_RLERROR;
351 >                n -= len;
352 >                while (len--) {
353 >                        if (rdbyte(val, br) == EOF)
354 >                                return BIR_TRUNCATED;
355 >                        *sp++ = val;
356 >                }
357 >                if (skipOdd && rdbyte(val, br) == EOF)
358 >                        return BIR_TRUNCATED;
359 >        }
360 >                                        /* verify break at end of line */
361 >        if (!n && (rdbyte(n, br) != 0 || (rdbyte(n, br) != 0 &&
362 >                                (n != 1 || br->yscan != br->hdr->height-1))))
363 >                return BIR_RLERROR;
364 >        if (br->seek != NULL)           /* record next scanline position */
365                  br->scanpos[br->yscan + 1] = br->fpos;
366          return BIR_OK;
367   }
# Line 270 | Line 385 | BMPseekScanline(int y, BMPReader *br)
385          if (y != br->yscan + 1 && br->seek != NULL) {
386                  int     yseek;
387                  uint32  seekp;
388 <                if (br->hdr->compr == BI_UNCOMPR) {
388 >                if (br->hdr->compr == BI_UNCOMPR ||
389 >                                        br->hdr->compr == BI_BITFIELDS) {
390                          yseek = y;
391                          seekp = br->scanpos[0] + y*getScanSiz(br->hdr);
392                  } else {
# Line 295 | Line 411 | BMPseekScanline(int y, BMPReader *br)
411  
412   /* get ith pixel from last scanline */
413   RGBquad
414 < BMPdecodePixel(int i, BMPReader *br)
414 > BMPdecodePixel(int i, const BMPReader *br)
415   {
416 +        static const uint32     std16mask[3] = {0x7c00, 0x3e0, 0x1f};
417          static const RGBquad    black = {0, 0, 0, 0};
418 +        const uint32            *mask;
419 +        const uint8             *pp;
420 +        uint32                  pval, v;
421 +        RGBquad                 cval;
422          
423 <        if ((br == NULL | i < 0) || i >= br->hdr->width)
423 >        if (((br == NULL) | (i < 0)) || i >= br->hdr->width)
424                  return black;
425  
426 +        cval.padding = 0;
427 +
428          switch (br->hdr->bpp) {
429 <        case 24: {
430 <                uint8   *bgr = br->scanline + 3*i;
431 <                RGBquad cval;
432 <                cval.b = bgr[0]; cval.g = bgr[1]; cval.r = bgr[2];
433 <                cval.padding = 0;
429 >        case 24:
430 >                pp = br->scanline + 3*i;
431 >                cval.b = *pp++;
432 >                cval.g = *pp++;
433 >                cval.r = *pp;
434                  return cval;
312                }
435          case 32:
436 <                return ((RGBquad *)br->scanline)[i];
436 >                if (br->hdr->compr == BI_UNCOMPR)
437 >                        return ((RGBquad *)br->scanline)[i];
438 >                                                /* convert bit fields */
439 >                pp = br->scanline + 4*i;
440 >                pval = *pp++;
441 >                pval |= *pp++ << 8;
442 >                pval |= *pp++ << 16;
443 >                pval |= *pp << 24;
444 >                mask = BMPbitField(br->hdr);
445 >                v = pval & mask[0];
446 >                while (v & ~0xff) v >>= 8;
447 >                cval.r = v;
448 >                v = pval & mask[1];
449 >                while (v & ~0xff) v >>= 8;
450 >                cval.g = v;
451 >                v = pval & mask[2];
452 >                while (v & ~0xff) v >>= 8;
453 >                cval.b = v;
454 >                return cval;
455          case 8:
456                  return br->hdr->palette[br->scanline[i]];
457          case 1:
458 <                return br->hdr->palette[br->scanline[i>>3]>>(i&7) & 1];
458 >                return br->hdr->palette[br->scanline[i>>3]>>((7-i)&7) & 1];
459          case 4:
460 <                return br->hdr->palette[br->scanline[i>>1]>>((i&1)<<2) & 0xf];
460 >                return br->hdr->palette[br->scanline[i>>1]>>(i&1?4:0) & 0xf];
461 >        case 16:
462 >                pp = br->scanline + 2*i;
463 >                pval = *pp++;
464 >                pval |= *pp++ << 8;
465 >                mask = std16mask;
466 >                if (br->hdr->compr == BI_BITFIELDS)
467 >                        mask = BMPbitField(br->hdr);
468 >                cval.r = ((pval & mask[0]) << 8) / (mask[0] + 1);
469 >                cval.g = ((pval & mask[1]) << 8) / (mask[1] + 1);
470 >                cval.b = ((pval & mask[2]) << 8) / (mask[2] + 1);
471 >                return cval;
472          }
473          return black;                           /* should never happen */
474   }
# Line 409 | Line 560 | BMPmappedHeader(int xr, int yr, int infolen, int ncolo
560          hdr->height = yr;
561          hdr->yIsDown = 0;                       /* default to upwards order */
562          hdr->bpp = n;
563 <        hdr->compr = BI_UNCOMPR;
563 >        hdr->compr = BI_UNCOMPR;                /* compression needs seek */
564          hdr->hRes = hdr->vRes = 2835;           /* default to 72 ppi */
565          hdr->nColors = ncolors;
566          hdr->impColors = 0;                     /* says all colors important */
# Line 423 | Line 574 | BMPmappedHeader(int xr, int yr, int infolen, int ncolo
574  
575                                          /* put byte to writer */
576   #define wrbyte(c,bw)    ( (*(bw)->cput)(c,(bw)->c_data), \
577 <                                ++((bw)->fpos) > (bw)->flen ? \
577 >                                ++(bw)->fpos > (bw)->flen ? \
578                                          ((bw)->flen = (bw)->fpos) : \
579                                          (bw)->fpos )
580  
# Line 442 | Line 593 | wrint32(int32 i, BMPWriter *bw)
593          wrbyte(i& 0xff, bw);
594          wrbyte(i>>8 & 0xff, bw);
595          wrbyte(i>>16 & 0xff, bw);
596 <        wrbyte(i>>24  & 0xff, bw);
596 >        wrbyte(i>>24 & 0xff, bw);
597   }
598  
599   /* write 16-bit unsigned integer in littlendian order */
# Line 459 | Line 610 | wrseek(uint32 pos, BMPWriter *bw)
610   {
611          if (pos == bw->fpos)
612                  return BIR_OK;
613 <        if (bw->seek == NULL) {
614 <                if (pos < bw->fpos)
464 <                        return BIR_SEEKERR;
465 <                while (bw->fpos < pos)
466 <                        wrbyte(0, bw);
467 <                return BIR_OK;
468 <        }
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;
# Line 482 | Line 628 | BMPopenWriter(void (*cput)(int, void *), int (*seek)(u
628          BMPWriter       *bw;
629          uint32          hdrSiz, palSiz, scanSiz, bmSiz;
630                                                  /* check arguments */
631 <        if (cput == NULL || hdr == NULL)
631 >        if (cput == NULL)
632                  return NULL;
633 <        if (hdr->width <= 0 || hdr->height <= 0)
633 >        if (!BMPheaderOK(hdr))
634                  return NULL;
635 <        if (hdr->compr != BI_UNCOMPR && hdr->compr != BI_RLE8)
635 >        if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4))
636                  return NULL;                    /* unsupported */
637 <        if (hdr->bpp > 8 && hdr->compr != BI_UNCOMPR)
637 > /* no seek means we may have the wrong file length, but most app's don't care
638 >        if (seek == NULL && ((hdr->compr == BI_RLE8) | (hdr->compr == BI_RLE4)))
639                  return NULL;
640 <        palSiz = BMPpalLen(hdr);
494 <        if (hdr->nColors > palSiz)
495 <                return NULL;
496 <        if (hdr->compr != BI_UNCOMPR && seek == NULL)
497 <                return NULL;
640 > */
641                                                  /* compute sizes */
642          hdrSiz = 2 + 6*4 + 2*2 + 6*4;
643 <        palSiz *= sizeof(RGBquad);
643 >        if (hdr->compr == BI_BITFIELDS)
644 >                hdrSiz += sizeof(uint32)*3;
645 >        palSiz = sizeof(RGBquad)*BMPpalLen(hdr);
646          scanSiz = getScanSiz(hdr);
647          bmSiz = hdr->height*scanSiz;            /* wrong if compressed */
648                                                  /* initialize writer */
# Line 547 | Line 692 | BMPopenWriter(void (*cput)(int, void *), int (*seek)(u
692   #endif
693          return bw;
694   }
695 +
696 + /* find position of next run of 5 or more identical bytes, or 255 if none */
697 + static int
698 + findNextRun(const int8 *bp, int len)
699 + {
700 +        int     pos, cnt;
701 +                                                /* look for run */
702 +        for (pos = 0; (len > 0) & (pos < 255); pos++, bp++, len--) {
703 +                if (len < 5)                    /* no hope left? */
704 +                        continue;
705 +                cnt = 1;                        /* else let's try it */
706 +                while (bp[cnt] == bp[0])
707 +                        if (++cnt >= 5)
708 +                                return pos;     /* long enough */
709 +        }
710 +        return pos;                             /* didn't find any */
711 + }
712                                  
713   /* write the current scanline */
714   int
715   BMPwriteScanline(BMPWriter *bw)
716   {
717 +        const int8      *sp;
718 +        int             n;
719 +
720          if (bw->yscan >= bw->hdr->height)
721                  return BIR_EOF;
722                                                  /* writing uncompressed? */
723 <        if (bw->hdr->compr == BI_UNCOMPR) {
723 >        if (bw->hdr->compr == BI_UNCOMPR || bw->hdr->compr == BI_BITFIELDS) {
724                  uint32  scanSiz = getScanSiz(bw->hdr);
725                  uint32  slpos = bw->fbmp + bw->yscan*scanSiz;
726                  if (wrseek(slpos, bw) != BIR_OK)
# Line 564 | Line 729 | BMPwriteScanline(BMPWriter *bw)
729                  bw->yscan++;
730                  return BIR_OK;
731          }
732 <                                                /* else write compressed */
733 < /* XXX need to do actual compression */
734 <        return BIR_UNSUPPORTED;
735 <                                                /* write file length at end */
732 >        /*
733 >         * RLE8 Encoding
734 >         *
735 >         * See the notes in BMPreadScanline() on this encoding.  Needless
736 >         * to say, we avoid the nuttier aspects of this specification.
737 >         * We also assume that every scanline ends in a line break
738 >         * (0x0000) except for the last, which ends in a bitmap break
739 >         * (0x0001).  We don't support RLE4 at all; it's too awkward.
740 >         */
741 >        sp = bw->scanline;
742 >        n = bw->hdr->width;
743 >        while (n > 0) {
744 >                int     cnt, val;
745 >                cnt = findNextRun(sp, n);       /* 0-255 < n */
746 >                if (cnt >= 3) {                 /* output absolute */
747 >                        int     skipOdd = cnt & 1;
748 >                        wrbyte(0, bw);
749 >                        wrbyte(cnt, bw);
750 >                        n -= cnt;
751 >                        while (cnt--)
752 >                                wrbyte(*sp++, bw);
753 >                        if (skipOdd)
754 >                                wrbyte(0, bw);
755 >                }
756 >                if (n <= 0)                     /* was that it? */
757 >                        break;
758 >                val = *sp;                      /* output run */
759 >                for (cnt = 1; --n && cnt < 255; cnt++)
760 >                        if (*++sp != val)
761 >                                break;
762 >                wrbyte(cnt, bw);
763 >                wrbyte(val, bw);
764 >        }
765 >        bw->yscan++;                            /* write line break or EOD */
766          if (bw->yscan == bw->hdr->height) {
767 <                if (bw->seek == NULL || (*bw->seek)(2, bw->c_data) != 0)
768 <                        return BIR_SEEKERR;
769 <                bw->fpos = 2;
770 <                wrint32(bw->flen, bw);
767 >                wrbyte(0, bw); wrbyte(1, bw);   /* end of bitmap marker */
768 >                if (wrseek(2, bw) != BIR_OK)
769 >                        return BIR_OK;          /* no one may care */
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 */
776          }
777          return BIR_OK;
778   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines