1 |
greg |
2.3 |
/* RCSid $Id: bmpfile.h,v 2.2 2004/03/26 22:58:21 greg Exp $ */ |
2 |
greg |
2.1 |
/* |
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 |
greg |
2.3 |
/* the following fields may be altered before the open call */ |
30 |
greg |
2.1 |
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 |
greg |
2.3 |
int compr; /* compression */ |
36 |
greg |
2.1 |
int32 width; /* bitmap width (pixels) */ |
37 |
|
|
int32 height; /* bitmap height (pixels) */ |
38 |
greg |
2.3 |
/* the following fields must not be altered after allocation */ |
39 |
greg |
2.1 |
int bpp; /* bits per sample (1,4,8,16,24,32) */ |
40 |
|
|
uint32 infoSiz; /* info buffer size (bytes) */ |
41 |
greg |
2.3 |
/* but the color table should be filled by writer before open call */ |
42 |
greg |
2.2 |
RGBquad palette[3]; /* color palette (extends struct) */ |
43 |
greg |
2.1 |
} BMPHeader; |
44 |
|
|
|
45 |
|
|
/* color palette length */ |
46 |
|
|
#define BMPpalLen(h) ((h)->bpp <= 8 ? 1<<(h)->bpp : 0) |
47 |
greg |
2.2 |
|
48 |
|
|
/* access to bit field triple */ |
49 |
|
|
#define BMPbitField(h) ((uint32 *)(h)->palette) |
50 |
greg |
2.1 |
|
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 |
greg |
2.2 |
#define BIR_RLERROR 3 /* RLE error */ |
60 |
|
|
#define BIR_SEEKERR 4 /* could not seek */ |
61 |
greg |
2.1 |
|
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 |
greg |
2.3 |
/* modify yscan only if seek is defined & data is uncompressed */ |
82 |
greg |
2.1 |
int yscan; /* scanline for next write */ |
83 |
greg |
2.3 |
/* the following fields should not be altered directly */ |
84 |
greg |
2.1 |
BMPHeader *hdr; /* allocated header */ |
85 |
|
|
uint32 fbmp; /* beginning of bitmap data */ |
86 |
|
|
uint32 fpos; /* current file position */ |
87 |
|
|
uint32 flen; /* last character written */ |
88 |
|
|
/* put character callback */ |
89 |
|
|
void (*cput)(int, void *); |
90 |
|
|
/* absolute seek callback (or NULL) */ |
91 |
|
|
int (*seek)(uint32, void *); |
92 |
|
|
void *c_data; /* client's opaque data */ |
93 |
|
|
} BMPWriter; |
94 |
|
|
|
95 |
|
|
/* open BMP stream for reading */ |
96 |
|
|
BMPReader *BMPopenReader(int (*cget)(void *), |
97 |
|
|
int (*seek)(uint32, void *), void *c_data); |
98 |
|
|
|
99 |
|
|
/* determine if image is grayscale */ |
100 |
|
|
int BMPisGrayscale(const BMPHeader *hdr); |
101 |
|
|
|
102 |
|
|
/* read next BMP scanline */ |
103 |
|
|
int BMPreadScanline(BMPReader *br); |
104 |
|
|
|
105 |
|
|
/* read a specific scanline */ |
106 |
|
|
int BMPseekScanline(int y, BMPReader *br); |
107 |
|
|
|
108 |
|
|
/* get ith pixel from last scanline */ |
109 |
greg |
2.2 |
RGBquad BMPdecodePixel(int i, const BMPReader *br); |
110 |
greg |
2.1 |
|
111 |
|
|
/* free BMP reader resources */ |
112 |
|
|
void BMPfreeReader(BMPReader *br); |
113 |
|
|
|
114 |
|
|
/* allocate uncompressed RGB header */ |
115 |
|
|
BMPHeader *BMPtruecolorHeader(int xr, int yr, int infolen); |
116 |
|
|
|
117 |
|
|
/* allocate color-mapped header */ |
118 |
|
|
BMPHeader *BMPmappedHeader(int xr, int yr, int infolen, int ncolors); |
119 |
|
|
|
120 |
|
|
/* open BMP stream for writing */ |
121 |
|
|
BMPWriter *BMPopenWriter(void (*cput)(int, void *), |
122 |
|
|
int (*seek)(uint32, void *), void *c_data, |
123 |
|
|
BMPHeader *hdr); |
124 |
|
|
|
125 |
|
|
/* write the prepared scanline */ |
126 |
|
|
int BMPwriteScanline(BMPWriter *bw); |
127 |
|
|
|
128 |
|
|
/* free BMP writer resources */ |
129 |
|
|
void BMPfreeWriter(BMPWriter *bw); |
130 |
|
|
|
131 |
|
|
/* get corresponding error message */ |
132 |
|
|
const char *BMPerrorMessage(int ec); |
133 |
|
|
|
134 |
|
|
/* stdio callback functions */ |
135 |
|
|
int stdio_getc(void *p); |
136 |
|
|
void stdio_putc(int c, void *p); |
137 |
|
|
int stdio_fseek(uint32 pos, void *p); |
138 |
|
|
|
139 |
|
|
/* open stdio input stream */ |
140 |
|
|
#define BMPopenInputStream(fp) BMPopenReader(&stdio_getc, NULL, (void *)fp) |
141 |
|
|
|
142 |
|
|
/* open input file */ |
143 |
|
|
#define BMPopenInputFile(fn) BMPopenReader(&stdio_getc, &stdio_fseek, \ |
144 |
|
|
(void *)fopen(fn, "r")) |
145 |
|
|
|
146 |
|
|
/* close stdio input file or stream */ |
147 |
|
|
#define BMPcloseInput(br) ( fclose((FILE *)(br)->c_data), \ |
148 |
|
|
BMPfreeReader(br) ) |
149 |
|
|
|
150 |
|
|
/* open stdio output stream */ |
151 |
|
|
#define BMPopenOutputStream(fp,hdr) \ |
152 |
|
|
BMPopenWriter(&stdio_putc, NULL, (void *)fp, hdr) |
153 |
|
|
|
154 |
|
|
/* open stdio output file */ |
155 |
|
|
#define BMPopenOutputFile(fn,hdr) \ |
156 |
|
|
BMPopenWriter(&stdio_putc, &stdio_fseek, \ |
157 |
|
|
(void *)fopen(fn, "w"), hdr) |
158 |
|
|
|
159 |
|
|
/* close stdio output file or stream */ |
160 |
|
|
#define BMPcloseOutput(bw) ( fclose((FILE *)(bw)->c_data), \ |
161 |
|
|
BMPfreeWriter(bw) ) |
162 |
|
|
|
163 |
|
|
#ifdef __cplusplus |
164 |
|
|
} |
165 |
|
|
#endif |
166 |
|
|
#endif /* ! _RAD_BMPFILE_H_ */ |