17 |
|
#define putc putc_unlocked |
18 |
|
#endif |
19 |
|
|
20 |
< |
/* get corresponding error message */ |
20 |
> |
/* Get corresponding error message */ |
21 |
|
const char * |
22 |
|
BMPerrorMessage(int ec) |
23 |
|
{ |
38 |
|
return "Unknown BMP error"; |
39 |
|
} |
40 |
|
|
41 |
< |
/* check than header is sensible */ |
41 |
> |
/* Check that header is sensible */ |
42 |
|
static int |
43 |
|
BMPheaderOK(const BMPHeader *hdr) |
44 |
|
{ |
89 |
|
/* get next byte from reader */ |
90 |
|
#define rdbyte(c,br) ((br)->fpos += (c=(*(br)->cget)((br)->c_data))!=EOF, c) |
91 |
|
|
92 |
< |
/* read n bytes */ |
92 |
> |
/* Read n bytes */ |
93 |
|
static int |
94 |
|
rdbytes(char *bp, uint32 n, BMPReader *br) |
95 |
|
{ |
103 |
|
return BIR_OK; |
104 |
|
} |
105 |
|
|
106 |
< |
/* read 32-bit integer in littlendian order */ |
106 |
> |
/* Read 32-bit integer in littlendian order */ |
107 |
|
static int32 |
108 |
|
rdint32(BMPReader *br) |
109 |
|
{ |
117 |
|
return i; /* -1 on EOF */ |
118 |
|
} |
119 |
|
|
120 |
< |
/* read 16-bit unsigned integer in littlendian order */ |
120 |
> |
/* Read 16-bit unsigned integer in littlendian order */ |
121 |
|
static int |
122 |
|
rduint16(BMPReader *br) |
123 |
|
{ |
129 |
|
return i; /* -1 on EOF */ |
130 |
|
} |
131 |
|
|
132 |
< |
/* seek on reader or return 0 (BIR_OK) on success */ |
132 |
> |
/* Seek on reader or return 0 (BIR_OK) on success */ |
133 |
|
static int |
134 |
|
rdseek(uint32 pos, BMPReader *br) |
135 |
|
{ |
141 |
|
return BIR_OK; |
142 |
|
} |
143 |
|
|
144 |
< |
/* open BMP stream for reading and get first scanline */ |
144 |
> |
/* Open BMP stream for reading and get first scanline */ |
145 |
|
BMPReader * |
146 |
|
BMPopenReader(int (*cget)(void *), int (*seek)(uint32, void *), void *c_data) |
147 |
|
{ |
151 |
|
|
152 |
|
if (cget == NULL) |
153 |
|
return NULL; |
154 |
+ |
if (cget == &stdio_getc && c_data == NULL) |
155 |
+ |
return NULL; /* stdio error condition */ |
156 |
|
magic[0] = (*cget)(c_data); |
157 |
|
if (magic[0] != 'B') |
158 |
|
return NULL; |
250 |
|
return NULL; |
251 |
|
} |
252 |
|
|
253 |
< |
/* determine if image is grayscale */ |
253 |
> |
/* Determine if image is grayscale */ |
254 |
|
int |
255 |
|
BMPisGrayscale(const BMPHeader *hdr) |
256 |
|
{ |
267 |
|
return 1; /* all colors neutral in map */ |
268 |
|
} |
269 |
|
|
270 |
< |
/* read and decode next BMP scanline */ |
270 |
> |
/* Read and decode next BMP scanline */ |
271 |
|
int |
272 |
|
BMPreadScanline(BMPReader *br) |
273 |
|
{ |
366 |
|
return BIR_OK; |
367 |
|
} |
368 |
|
|
369 |
< |
/* read a specific scanline */ |
369 |
> |
/* Read a specific scanline */ |
370 |
|
int |
371 |
|
BMPseekScanline(int y, BMPReader *br) |
372 |
|
{ |
409 |
|
return BIR_OK; |
410 |
|
} |
411 |
|
|
412 |
< |
/* get ith pixel from last scanline */ |
412 |
> |
/* Get ith pixel from last scanline */ |
413 |
|
RGBquad |
414 |
|
BMPdecodePixel(int i, const BMPReader *br) |
415 |
|
{ |
473 |
|
return black; /* should never happen */ |
474 |
|
} |
475 |
|
|
476 |
< |
/* free BMP reader resources */ |
476 |
> |
/* Free BMP reader resources */ |
477 |
|
void |
478 |
|
BMPfreeReader(BMPReader *br) |
479 |
|
{ |
510 |
|
return fseek((FILE *)p, (long)pos, 0); |
511 |
|
} |
512 |
|
|
513 |
< |
/* allocate uncompressed (24-bit) RGB header */ |
513 |
> |
/* Allocate uncompressed (24-bit) RGB header */ |
514 |
|
BMPHeader * |
515 |
|
BMPtruecolorHeader(int xr, int yr, int infolen) |
516 |
|
{ |
533 |
|
return hdr; |
534 |
|
} |
535 |
|
|
536 |
< |
/* allocate color-mapped header (defaults to minimal grayscale) */ |
536 |
> |
/* Allocate color-mapped header (defaults to minimal grayscale) */ |
537 |
|
BMPHeader * |
538 |
|
BMPmappedHeader(int xr, int yr, int infolen, int ncolors) |
539 |
|
{ |
578 |
|
((bw)->flen = (bw)->fpos) : \ |
579 |
|
(bw)->fpos ) |
580 |
|
|
581 |
< |
/* write out a string of bytes */ |
581 |
> |
/* Write out a string of bytes */ |
582 |
|
static void |
583 |
|
wrbytes(char *bp, uint32 n, BMPWriter *bw) |
584 |
|
{ |
586 |
|
wrbyte(*bp++, bw); |
587 |
|
} |
588 |
|
|
589 |
< |
/* write 32-bit integer in littlendian order */ |
589 |
> |
/* Write 32-bit integer in littlendian order */ |
590 |
|
static void |
591 |
|
wrint32(int32 i, BMPWriter *bw) |
592 |
|
{ |
596 |
|
wrbyte(i>>24 & 0xff, bw); |
597 |
|
} |
598 |
|
|
599 |
< |
/* write 16-bit unsigned integer in littlendian order */ |
599 |
> |
/* Write 16-bit unsigned integer in littlendian order */ |
600 |
|
static void |
601 |
|
wruint16(uint16 ui, BMPWriter *bw) |
602 |
|
{ |
604 |
|
wrbyte(ui>>8 & 0xff, bw); |
605 |
|
} |
606 |
|
|
607 |
< |
/* seek to the specified file position, returning 0 (BIR_OK) on success */ |
607 |
> |
/* Seek to the specified file position, returning 0 (BIR_OK) on success */ |
608 |
|
static int |
609 |
|
wrseek(uint32 pos, BMPWriter *bw) |
610 |
|
{ |
620 |
|
return BIR_OK; |
621 |
|
} |
622 |
|
|
623 |
< |
/* open BMP stream for writing */ |
623 |
> |
/* Open BMP stream for writing */ |
624 |
|
BMPWriter * |
625 |
|
BMPopenWriter(void (*cput)(int, void *), int (*seek)(uint32, void *), |
626 |
|
void *c_data, BMPHeader *hdr) |
630 |
|
/* check arguments */ |
631 |
|
if (cput == NULL) |
632 |
|
return NULL; |
633 |
+ |
if (cput == &stdio_putc && c_data == NULL) |
634 |
+ |
return NULL; /* stdio error condition */ |
635 |
|
if (!BMPheaderOK(hdr)) |
636 |
|
return NULL; |
637 |
|
if ((hdr->bpp == 16) | (hdr->compr == BI_RLE4)) |
695 |
|
return bw; |
696 |
|
} |
697 |
|
|
698 |
< |
/* find position of next run of 5 or more identical bytes, or 255 if none */ |
698 |
> |
/* Find position of next run of 5 or more identical bytes, or 255 if none */ |
699 |
|
static int |
700 |
|
findNextRun(const int8 *bp, int len) |
701 |
|
{ |
712 |
|
return pos; /* didn't find any */ |
713 |
|
} |
714 |
|
|
715 |
< |
/* write the current scanline */ |
715 |
> |
/* Write the current scanline */ |
716 |
|
int |
717 |
|
BMPwriteScanline(BMPWriter *bw) |
718 |
|
{ |
779 |
|
return BIR_OK; |
780 |
|
} |
781 |
|
|
782 |
< |
/* free BMP writer resources */ |
782 |
> |
/* Free BMP writer resources */ |
783 |
|
void |
784 |
|
BMPfreeWriter(BMPWriter *bw) |
785 |
|
{ |