1 |
/* RCSid $Id: bmpfile.h,v 2.3 2004/03/27 05:43:37 greg Exp $ */ |
2 |
/* |
3 |
* Windows and OS/2 BMP file support |
4 |
*/ |
5 |
|
6 |
#ifndef _RAD_BMPFILE_H_ |
7 |
#define _RAD_BMPFILE_H_ |
8 |
|
9 |
#include "tifftypes.h" |
10 |
|
11 |
#ifdef __cplusplus |
12 |
extern "C" { |
13 |
#endif |
14 |
|
15 |
/* compression modes */ |
16 |
#define BI_UNCOMPR 0 /* no compression */ |
17 |
#define BI_RLE8 1 /* runlength on bytes */ |
18 |
#define BI_RLE4 2 /* runlength on half-bytes */ |
19 |
#define BI_BITFIELDS 3 /* uncompressed w/ bitfields */ |
20 |
|
21 |
/* A Color table entry */ |
22 |
typedef struct { |
23 |
uint8 b, g, r; /* blue, green, & red bytes */ |
24 |
uint8 padding; /* padding to 32-bit boundary */ |
25 |
} RGBquad; |
26 |
|
27 |
/* Allocated BMP header data */ |
28 |
typedef struct { |
29 |
/* the following fields may be altered before the open call */ |
30 |
int yIsDown; /* scanlines proceed downward? */ |
31 |
int32 hRes; /* horizontal resolution pixels/meter */ |
32 |
int32 vRes; /* vertical resolution pixels/meter */ |
33 |
int nColors; /* total color palette size */ |
34 |
int impColors; /* number of colors actually used */ |
35 |
int compr; /* compression */ |
36 |
int32 width; /* bitmap width (pixels) */ |
37 |
int32 height; /* bitmap height (pixels) */ |
38 |
/* the following fields must not be altered after allocation */ |
39 |
int bpp; /* bits per sample (1,4,8,16,24,32) */ |
40 |
uint32 infoSiz; /* info buffer size (bytes) */ |
41 |
/* but the color table should be filled by writer before open call */ |
42 |
RGBquad palette[3]; /* color palette (extends struct) */ |
43 |
} BMPHeader; |
44 |
|
45 |
/* access to bit field triple */ |
46 |
#define BMPbitField(h) ((uint32 *)(h)->palette) |
47 |
|
48 |
/* info buffer access */ |
49 |
#define BMPinfo(h) ((char *)((h)->palette + (h)->nColors)) |
50 |
|
51 |
/* function return values */ |
52 |
#define BIR_OK 0 /* all is well */ |
53 |
#define BIR_EOF (-1) /* reached end of file */ |
54 |
#define BIR_TRUNCATED 1 /* unexpected EOF */ |
55 |
#define BIR_UNSUPPORTED 2 /* unsupported encoding */ |
56 |
#define BIR_RLERROR 3 /* RLE error */ |
57 |
#define BIR_SEEKERR 4 /* could not seek */ |
58 |
|
59 |
/* A BMP reader structure */ |
60 |
typedef struct BMPReader { |
61 |
/* members in this structure should be considered read-only */ |
62 |
uint8 *scanline; /* unpacked scanline data */ |
63 |
int yscan; /* last scanline read */ |
64 |
BMPHeader *hdr; /* pointer to allocated header */ |
65 |
uint32 fpos; /* current file position */ |
66 |
/* get character callback */ |
67 |
int (*cget)(void *); |
68 |
/* absolute seek callback (or NULL) */ |
69 |
int (*seek)(uint32, void *); |
70 |
void *c_data; /* client's opaque data */ |
71 |
uint32 scanpos[1]; /* recorded scanline position(s) */ |
72 |
} BMPReader; |
73 |
|
74 |
/* A BMP writer structure */ |
75 |
typedef struct { |
76 |
/* the scanline data is filled in by caller before each write */ |
77 |
uint8 *scanline; /* caller-prepared scanline data */ |
78 |
/* modify yscan only if seek is defined & data is uncompressed */ |
79 |
int yscan; /* scanline for next write */ |
80 |
/* the following fields should not be altered directly */ |
81 |
BMPHeader *hdr; /* allocated header */ |
82 |
uint32 fbmp; /* beginning of bitmap data */ |
83 |
uint32 fpos; /* current file position */ |
84 |
uint32 flen; /* last character written */ |
85 |
/* put character callback */ |
86 |
void (*cput)(int, void *); |
87 |
/* absolute seek callback (or NULL) */ |
88 |
int (*seek)(uint32, void *); |
89 |
void *c_data; /* client's opaque data */ |
90 |
} BMPWriter; |
91 |
|
92 |
/* open BMP stream for reading */ |
93 |
BMPReader *BMPopenReader(int (*cget)(void *), |
94 |
int (*seek)(uint32, void *), void *c_data); |
95 |
|
96 |
/* determine if image is grayscale */ |
97 |
int BMPisGrayscale(const BMPHeader *hdr); |
98 |
|
99 |
/* read next BMP scanline */ |
100 |
int BMPreadScanline(BMPReader *br); |
101 |
|
102 |
/* read a specific scanline */ |
103 |
int BMPseekScanline(int y, BMPReader *br); |
104 |
|
105 |
/* get ith pixel from last scanline */ |
106 |
RGBquad BMPdecodePixel(int i, const BMPReader *br); |
107 |
|
108 |
/* free BMP reader resources */ |
109 |
void BMPfreeReader(BMPReader *br); |
110 |
|
111 |
/* allocate uncompressed RGB header */ |
112 |
BMPHeader *BMPtruecolorHeader(int xr, int yr, int infolen); |
113 |
|
114 |
/* allocate color-mapped header */ |
115 |
BMPHeader *BMPmappedHeader(int xr, int yr, int infolen, int ncolors); |
116 |
|
117 |
/* open BMP stream for writing */ |
118 |
BMPWriter *BMPopenWriter(void (*cput)(int, void *), |
119 |
int (*seek)(uint32, void *), void *c_data, |
120 |
BMPHeader *hdr); |
121 |
|
122 |
/* write the prepared scanline */ |
123 |
int BMPwriteScanline(BMPWriter *bw); |
124 |
|
125 |
/* free BMP writer resources */ |
126 |
void BMPfreeWriter(BMPWriter *bw); |
127 |
|
128 |
/* get corresponding error message */ |
129 |
const char *BMPerrorMessage(int ec); |
130 |
|
131 |
/* stdio callback functions */ |
132 |
int stdio_getc(void *p); |
133 |
void stdio_putc(int c, void *p); |
134 |
int stdio_fseek(uint32 pos, void *p); |
135 |
|
136 |
/* open stdio input stream */ |
137 |
#define BMPopenInputStream(fp) BMPopenReader(&stdio_getc, NULL, (void *)fp) |
138 |
|
139 |
/* open input file */ |
140 |
#define BMPopenInputFile(fn) BMPopenReader(&stdio_getc, &stdio_fseek, \ |
141 |
(void *)fopen(fn, "r")) |
142 |
|
143 |
/* close stdio input file or stream */ |
144 |
#define BMPcloseInput(br) ( fclose((FILE *)(br)->c_data), \ |
145 |
BMPfreeReader(br) ) |
146 |
|
147 |
/* open stdio output stream */ |
148 |
#define BMPopenOutputStream(fp,hdr) \ |
149 |
BMPopenWriter(&stdio_putc, NULL, (void *)fp, hdr) |
150 |
|
151 |
/* open stdio output file */ |
152 |
#define BMPopenOutputFile(fn,hdr) \ |
153 |
BMPopenWriter(&stdio_putc, &stdio_fseek, \ |
154 |
(void *)fopen(fn, "w"), hdr) |
155 |
|
156 |
/* close stdio output file or stream */ |
157 |
#define BMPcloseOutput(bw) ( fclose((FILE *)(bw)->c_data), \ |
158 |
BMPfreeWriter(bw) ) |
159 |
|
160 |
#ifdef __cplusplus |
161 |
} |
162 |
#endif |
163 |
#endif /* ! _RAD_BMPFILE_H_ */ |