1 |
/* RCSid $Id$ */ |
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 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 |
/* the following fields should not be altered after allocation */ |
36 |
int32 width; /* bitmap width (pixels) */ |
37 |
int32 height; /* bitmap height (pixels) */ |
38 |
int bpp; /* bits per sample (1,4,8,16,24,32) */ |
39 |
int compr; /* compression */ |
40 |
uint32 infoSiz; /* info buffer size (bytes) */ |
41 |
/* the color table should be filled by writer before open call */ |
42 |
RGBquad palette[3]; /* color palette (extends struct) */ |
43 |
} BMPHeader; |
44 |
|
45 |
/* color palette length */ |
46 |
#define BMPpalLen(h) ((h)->bpp <= 8 ? 1<<(h)->bpp : 0) |
47 |
|
48 |
/* access to bit field triple */ |
49 |
#define BMPbitField(h) ((uint32 *)(h)->palette) |
50 |
|
51 |
/* info buffer access */ |
52 |
#define BMPinfo(h) ((char *)((h)->palette + BMPpalLen(h))) |
53 |
|
54 |
/* function return values */ |
55 |
#define BIR_OK 0 /* all is well */ |
56 |
#define BIR_EOF (-1) /* reached end of file */ |
57 |
#define BIR_TRUNCATED 1 /* unexpected EOF */ |
58 |
#define BIR_UNSUPPORTED 2 /* unsupported encoding */ |
59 |
#define BIR_RLERROR 3 /* RLE error */ |
60 |
#define BIR_SEEKERR 4 /* could not seek */ |
61 |
|
62 |
/* A BMP reader structure */ |
63 |
typedef struct BMPReader { |
64 |
/* members in this structure should be considered read-only */ |
65 |
uint8 *scanline; /* unpacked scanline data */ |
66 |
int yscan; /* last scanline read */ |
67 |
BMPHeader *hdr; /* pointer to allocated header */ |
68 |
uint32 fpos; /* current file position */ |
69 |
/* get character callback */ |
70 |
int (*cget)(void *); |
71 |
/* absolute seek callback (or NULL) */ |
72 |
int (*seek)(uint32, void *); |
73 |
void *c_data; /* client's opaque data */ |
74 |
uint32 scanpos[1]; /* recorded scanline position(s) */ |
75 |
} BMPReader; |
76 |
|
77 |
/* A BMP writer structure */ |
78 |
typedef struct { |
79 |
/* the scanline data is filled in by caller before each write */ |
80 |
uint8 *scanline; /* caller-prepared scanline data */ |
81 |
int yscan; /* scanline for next write */ |
82 |
/* the following fields should not be altered by the caller */ |
83 |
BMPHeader *hdr; /* allocated header */ |
84 |
uint32 fbmp; /* beginning of bitmap data */ |
85 |
uint32 fpos; /* current file position */ |
86 |
uint32 flen; /* last character written */ |
87 |
/* put character callback */ |
88 |
void (*cput)(int, void *); |
89 |
/* absolute seek callback (or NULL) */ |
90 |
int (*seek)(uint32, void *); |
91 |
void *c_data; /* client's opaque data */ |
92 |
} BMPWriter; |
93 |
|
94 |
/* open BMP stream for reading */ |
95 |
BMPReader *BMPopenReader(int (*cget)(void *), |
96 |
int (*seek)(uint32, void *), void *c_data); |
97 |
|
98 |
/* determine if image is grayscale */ |
99 |
int BMPisGrayscale(const BMPHeader *hdr); |
100 |
|
101 |
/* read next BMP scanline */ |
102 |
int BMPreadScanline(BMPReader *br); |
103 |
|
104 |
/* read a specific scanline */ |
105 |
int BMPseekScanline(int y, BMPReader *br); |
106 |
|
107 |
/* get ith pixel from last scanline */ |
108 |
RGBquad BMPdecodePixel(int i, const BMPReader *br); |
109 |
|
110 |
/* free BMP reader resources */ |
111 |
void BMPfreeReader(BMPReader *br); |
112 |
|
113 |
/* allocate uncompressed RGB header */ |
114 |
BMPHeader *BMPtruecolorHeader(int xr, int yr, int infolen); |
115 |
|
116 |
/* allocate color-mapped header */ |
117 |
BMPHeader *BMPmappedHeader(int xr, int yr, int infolen, int ncolors); |
118 |
|
119 |
/* open BMP stream for writing */ |
120 |
BMPWriter *BMPopenWriter(void (*cput)(int, void *), |
121 |
int (*seek)(uint32, void *), void *c_data, |
122 |
BMPHeader *hdr); |
123 |
|
124 |
/* write the prepared scanline */ |
125 |
int BMPwriteScanline(BMPWriter *bw); |
126 |
|
127 |
/* free BMP writer resources */ |
128 |
void BMPfreeWriter(BMPWriter *bw); |
129 |
|
130 |
/* get corresponding error message */ |
131 |
const char *BMPerrorMessage(int ec); |
132 |
|
133 |
/* stdio callback functions */ |
134 |
int stdio_getc(void *p); |
135 |
void stdio_putc(int c, void *p); |
136 |
int stdio_fseek(uint32 pos, void *p); |
137 |
|
138 |
/* open stdio input stream */ |
139 |
#define BMPopenInputStream(fp) BMPopenReader(&stdio_getc, NULL, (void *)fp) |
140 |
|
141 |
/* open input file */ |
142 |
#define BMPopenInputFile(fn) BMPopenReader(&stdio_getc, &stdio_fseek, \ |
143 |
(void *)fopen(fn, "r")) |
144 |
|
145 |
/* close stdio input file or stream */ |
146 |
#define BMPcloseInput(br) ( fclose((FILE *)(br)->c_data), \ |
147 |
BMPfreeReader(br) ) |
148 |
|
149 |
/* open stdio output stream */ |
150 |
#define BMPopenOutputStream(fp,hdr) \ |
151 |
BMPopenWriter(&stdio_putc, NULL, (void *)fp, hdr) |
152 |
|
153 |
/* open stdio output file */ |
154 |
#define BMPopenOutputFile(fn,hdr) \ |
155 |
BMPopenWriter(&stdio_putc, &stdio_fseek, \ |
156 |
(void *)fopen(fn, "w"), hdr) |
157 |
|
158 |
/* close stdio output file or stream */ |
159 |
#define BMPcloseOutput(bw) ( fclose((FILE *)(bw)->c_data), \ |
160 |
BMPfreeWriter(bw) ) |
161 |
|
162 |
#ifdef __cplusplus |
163 |
} |
164 |
#endif |
165 |
#endif /* ! _RAD_BMPFILE_H_ */ |