//============================================================================== // bmpfile.hpp // Classes and functions to read and write Microsoft Windows bitmap (BMP) // graphic image files. // // Acknowledgements // Derived from source code written by David R. Tribble, Jan 1992. // // Copyright ©2008 by David R. Tribble, all rights reserved. // Permission is granted to any person or entity except those designated // by the United States Department of State as a terrorist, or terrorist // government or agency, to use and distribute this source code provided // that the original copyright notice remains present and unaltered. //============================================================================== #ifndef drt_bmpfile_h #define drt_bmpfile_h 106 // Identification #ifndef NO_H_IDENT static char drt_bmpfile_hpp_id[] = "@(#)drt/bmp/bmpfile.hpp $Revision: 1.6 $$Date: 2008/06/28 16:25:47 $"; #endif #ifndef __cplusplus #error This is a C++ header file #endif // System includes #ifndef sys_stdint_h #include #define sys_stdint_h 1 #endif #ifndef sys_stdio_h #include #define sys_stdio_h 1 #endif #ifdef errno #define sys_errno errno #else #define sys_errno ::errno #endif // Local includes #ifndef drt_bmpdefs_h #include "bmpdefs.h" #endif //------------------------------------------------------------------------------ // Class DrtBmpFile // Functions to read and write BMP graphic image files. // // @since // 1.1, 2008-04-27 //------------------------------------------------------------------------------ #define DrtBmpFile_VS 101 // Class version #define DrtBmpFile_MAGIC 0xBA0EB001 // Class magic number class DrtBmpFile { public: // Constants enum Mode // Open mode { M_NONE = 0, // Closed M_READ = 1, // Read M_WRITE = 2, // Write M_READWRITE = 3, // Read and write }; enum Flags // Bitflags { F_OS2 = 0x0001, // OS/2 BMP file F_RGB3 = 0x0002, // RGB map entries are }; static const struct bmp_rgb palette16[0x10]; // Standard 16-color palette protected: // Constants enum Flags2 // More bitflags { F_HAS_HDR = 0x0100, // File header is set F_DID_HDR = 0x0200, // File header was read/written F_DID_MAP = 0x0400, // RGB palette was read/written }; public: // Static functions static DrtBmpFile * open(const char *fname, int mode); static int dump_header(FILE *out, const bmp_header *hdr); static int dump_palette(FILE *out, const struct bmp_rgb tab[], int sz); protected: // Variables unsigned int m_magic; // Class magic number int m_vers; // Class version FILE * m_fp; // BMP file stream struct bmp_header m_hdr; // File header struct bmp_rgb * m_map; // RGB color map palette int m_mode; // Open mode (M_XXX) unsigned int m_flags; // Bitflags (F_XXX) unsigned int m_ncols; // Colors in palette unsigned int m_linesz; // Bytes per line (4n) unsigned int m_byteno; // Current byte number of line unsigned char m_rep; // Repeat count unsigned char m_pix; // Repeated pixel value unsigned char m_byte; // Current pixels byte unsigned char m_bitno; // Current pixel bit number int m_datapos; // Buffer pointer unsigned char m_data[1024]; // Pixel data byte buffer public: // Functions /**/ ~DrtBmpFile(); // Destructor int close(); bool is_open() const; int dump_header(FILE *out); int dump_palette(FILE *out); const struct bmp_header * get_header() const; bool has_palette() const; int get_palette_size() const; const struct bmp_rgb * get_palette() const; bool read_header(); bool read_palette(); int32_t read_pixel(); int32_t read_rgb(struct bmp_rgbpix *color); bool read_eoln(); bool write_header(); bool write_palette(); bool write_pixel(int32_t pix); bool write_rgb(const struct bmp_rgbpix *color); bool write_eoln(); bool write_eof(); protected: // Functions /**/ DrtBmpFile(); // Default constructor int read_byte(); private: // Functions /**/ DrtBmpFile(const DrtBmpFile &o); const DrtBmpFile & operator =(const DrtBmpFile &o); }; #endif // drt_bmpfile_h // End bmpfile.h