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