Back to index

radiance  4R0+20100331
bmpfile.h
Go to the documentation of this file.
00001 /* RCSid $Id: bmpfile.h,v 2.6 2008/05/31 19:38:36 greg Exp $ */
00002 /*
00003  *  Windows and OS/2 BMP file support
00004  */
00005 
00006 #ifndef _RAD_BMPFILE_H_
00007 #define _RAD_BMPFILE_H_
00008 
00009 #include "tiff.h"                  /* needed for int32, etc. */
00010 
00011 #ifdef __cplusplus
00012 extern "C" {
00013 #endif
00014 
00015                                    /* compression modes */
00016 #define BI_UNCOMPR          0             /* no compression */
00017 #define BI_RLE8                    1             /* runlength on bytes */
00018 #define BI_RLE4                    2             /* runlength on half-bytes */
00019 #define BI_BITFIELDS        3             /* uncompressed w/ bitfields */
00020 
00021 /* A Color table entry */
00022 typedef struct {
00023        uint8         b, g, r;      /* blue, green, & red bytes */
00024        uint8         padding;      /* padding to 32-bit boundary */
00025 } RGBquad;
00026 
00027 /* Allocated BMP header data */
00028 typedef struct {
00029        /* the following fields may be altered before the open call */
00030        int           yIsDown;      /* scanlines proceed downward? */
00031        int32         hRes;         /* horizontal resolution pixels/meter */
00032        int32         vRes;         /* vertical resolution pixels/meter */
00033        int           nColors;      /* total color palette size */
00034        int           impColors;      /* number of colors actually used */
00035        int           compr;        /* compression */
00036        int32         width;        /* bitmap width (pixels) */
00037        int32         height;              /* bitmap height (pixels) */
00038        /* the following fields must not be altered after allocation */
00039        int           bpp;          /* bits per sample (1,4,8,16,24,32) */
00040        uint32        infoSiz;      /* info buffer size (bytes) */
00041        /* but the color table should be filled by writer before open call */
00042        RGBquad              palette[3];     /* color palette (extends struct) */
00043 } BMPHeader;
00044                             
00045                                    /* access to bit field triple */
00046 #define BMPbitField(h)  ((uint32 *)(h)->palette)
00047 
00048                                    /* info buffer access */
00049 #define BMPinfo(h)      ((char *)((h)->palette + (h)->nColors))
00050 
00051                                    /* function return values */
00052 #define BIR_OK                     0             /* all is well */
00053 #define BIR_EOF                    (-1)          /* reached end of file */
00054 #define BIR_TRUNCATED              1             /* unexpected EOF */
00055 #define BIR_UNSUPPORTED            2             /* unsupported encoding */
00056 #define BIR_RLERROR         3             /* RLE error */
00057 #define BIR_SEEKERR         4             /* could not seek */
00058 
00059 /* A BMP reader structure */
00060 typedef struct BMPReader {
00061        /* members in this structure should be considered read-only */
00062        uint8         *scanline;      /* unpacked scanline data */
00063        int           yscan;        /* last scanline read */
00064        BMPHeader       *hdr;              /* pointer to allocated header */
00065        uint32        fpos;         /* current file position */
00066                                    /* get character callback */
00067        int           (*cget)(void *);
00068                                    /* absolute seek callback (or NULL) */
00069        int           (*seek)(uint32, void *);
00070        void          *c_data;      /* client's opaque data */
00071        uint32        scanpos[1];     /* recorded scanline position(s) */
00072 } BMPReader;
00073 
00074 /* A BMP writer structure */
00075 typedef struct {
00076        /* the scanline data is filled in by caller before each write */
00077        uint8         *scanline;      /* caller-prepared scanline data */
00078        /* modify yscan only if seek is defined & data is uncompressed */
00079        int           yscan;        /* scanline for next write */
00080        /* the following fields should not be altered directly */
00081        BMPHeader       *hdr;              /* allocated header */
00082        uint32        fbmp;         /* beginning of bitmap data */
00083        uint32        fpos;         /* current file position */
00084        uint32        flen;         /* last character written */
00085                                    /* put character callback */
00086        void          (*cput)(int, void *);
00087                                    /* absolute seek callback (or NULL) */
00088        int           (*seek)(uint32, void *);
00089        void          *c_data;      /* client's opaque data */
00090 } BMPWriter;
00091 
00092                                    /* open BMP stream for reading */
00093 BMPReader       *BMPopenReader(int (*cget)(void *),
00094                             int (*seek)(uint32, void *), void *c_data);
00095 
00096                                    /* determine if image is grayscale */
00097 int           BMPisGrayscale(const BMPHeader *hdr);
00098 
00099                                    /* read next BMP scanline */
00100 int           BMPreadScanline(BMPReader *br);
00101 
00102                                    /* read a specific scanline */
00103 int           BMPseekScanline(int y, BMPReader *br);
00104 
00105                                    /* get ith pixel from last scanline */
00106 RGBquad              BMPdecodePixel(int i, const BMPReader *br);
00107 
00108                                    /* free BMP reader resources */
00109 void          BMPfreeReader(BMPReader *br);
00110 
00111                                    /* allocate uncompressed RGB header */
00112 BMPHeader       *BMPtruecolorHeader(int xr, int yr, int infolen);
00113 
00114                                    /* allocate color-mapped header */
00115 BMPHeader       *BMPmappedHeader(int xr, int yr, int infolen, int ncolors);
00116 
00117                                    /* open BMP stream for writing */
00118 BMPWriter     *BMPopenWriter(void (*cput)(int, void *),
00119                             int (*seek)(uint32, void *), void *c_data,
00120                                    BMPHeader *hdr);
00121                             
00122                                    /* write the prepared scanline */
00123 int           BMPwriteScanline(BMPWriter *bw);
00124 
00125                                    /* free BMP writer resources */
00126 void          BMPfreeWriter(BMPWriter *bw);
00127 
00128                                    /* get corresponding error message */
00129 const char      *BMPerrorMessage(int ec);
00130 
00131                                    /* stdio callback functions */
00132 int           stdio_getc(void *p);
00133 void          stdio_putc(int c, void *p);
00134 int           stdio_fseek(uint32 pos, void *p);
00135 
00136                                    /* open stdio input stream */
00137 #define BMPopenInputStream(fp)  BMPopenReader(&stdio_getc, NULL, (void *)fp)
00138 
00139                                    /* open input file */
00140 #define BMPopenInputFile(fn)    BMPopenReader(&stdio_getc, &stdio_fseek, \
00141                                    (void *)fopen(fn, "rb"))
00142 
00143                                    /* close stdio input file or stream */
00144 #define BMPcloseInput(br)       ( fclose((FILE *)(br)->c_data), \
00145                                    BMPfreeReader(br) )
00146 
00147                                    /* open stdio output stream */
00148 #define BMPopenOutputStream(fp,hdr) \
00149                      BMPopenWriter(&stdio_putc, NULL, (void *)fp, hdr)
00150 
00151                                    /* open stdio output file */
00152 #define BMPopenOutputFile(fn,hdr) \
00153                      BMPopenWriter(&stdio_putc, &stdio_fseek, \
00154                                    (void *)fopen(fn, "wb"), hdr)
00155 
00156                                    /* close stdio output file or stream */
00157 #define BMPcloseOutput(bw)      ( fclose((FILE *)(bw)->c_data), \
00158                                    BMPfreeWriter(bw) )
00159 
00160 #ifdef __cplusplus
00161 }
00162 #endif
00163 #endif  /* ! _RAD_BMPFILE_H_ */