bmp.h

Go to the documentation of this file.
00001 
00007 #ifndef CTLDEF_H
00008 #include "ctl/ctldef.h"
00009 #endif
00010 #ifndef OPC_H
00011 #include "opc/opc.h"
00012 #endif
00013 
00014 #if !defined(_WINGDI_)
00015 #pragma pack(push,1)
00016 #define BI_RGB        0L
00017 #define BI_RLE8       1L
00018 #define BI_RLE4       2L
00019 #define BI_BITFIELDS  3L
00020 typedef struct tagBITMAPINFOHEADER
00021 {
00022     uint32      biSize;             /* Specifies the number of bytes required by the structure. */
00023     int32       biWidth;            /* Specifies the width of the bitmap, in pixels. (Windows 98, Windows 2000: If biCompression is BI_JPEG or BI_PNG, the biWidth member specifies the width of the decompressed JPEG or PNG image file, respectively) */
00024     int32       biHeight;           /* Specifies the height of the bitmap, in pixels. If biHeight is positive, the bitmap is a bottom-up DIB and its origin is the lower-left corner. If biHeight is negative, the bitmap is a top-down DIB and its origin is the upper-left corner.  If biHeight is negative, indicating a top-down DIB, biCompression must be either BI_RGB or BI_BITFIELDS. Top-down DIBs cannot be compressed. */ 
00025     uint16      biPlanes;           /* Specifies the number of planes for the target device. This value must be set to 1. */ 
00026     uint16      biBitCount;         /* Number of bits in a pixel.  See Win32 SDK doco */ 
00027     uint32      biCompression;      /* BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS,BI_JPEG,BI_PNG */ 
00028     uint32      biSizeImage;        /* Specifies the size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps.  */ 
00029     int32       biXPelsPerMeter;    /* Specifies the horizontal resolution, in pixels-per-meter, of the target device for the bitmap. An application can use this value to select a bitmap from a resource group that best matches the characteristics of the current device. */ 
00030     int32       biYPelsPerMeter;    /* Specifies the vertical resolution, in pixels-per-meter, of the target device for the bitmap.  */ 
00031     uint32      biClrUsed;          /* Specifies the number of color indexes in the color table that are actually used by the bitmap. If this value is zero, the bitmap uses the maximum number of colors corresponding to the value of the biBitCount member for the compression mode specified by biCompression. */
00032     uint32      biClrImportant;     /* Specifies the number of color indexes that are required for displaying the bitmap. If this value is zero, all colors are required.  */
00033 } BITMAPINFOHEADER;
00034 typedef struct tagRGBQUAD 
00035 {
00036     uint8       rgbBlue;
00037     uint8       rgbGreen;
00038     uint8       rgbRed;
00039     uint8       rgbReserved;
00040 } RGBQUAD;
00041 typedef struct tagBITMAPINFO 
00042 {
00043     BITMAPINFOHEADER    bmiHeader;
00044     RGBQUAD             bmiColors[1];
00045 } BITMAPINFO;
00046 typedef struct tagBITMAPFILEHEADER 
00047 {
00048     uint16      bfType      __attribute__ ((packed));               /* Specifies the file type, must be "BM" */
00049     uint32      bfSize      __attribute__ ((packed));               /* Specifies the size, in bytes, of the bitmap file. */
00050     uint16      bfReserved1 __attribute__ ((packed));       /* Reserved: 0 */
00051     uint16      bfReserved2 __attribute__ ((packed));       /* Reserved: 0 */
00052     uint32      bfOffBits   __attribute__ ((packed));           /* Specifies the offset, in bytes, from the BITMAPFILEHEADER structure to the bitmap bits.  */
00053 } BITMAPFILEHEADER;
00054 #pragma pack(pop)
00055 #endif 
00057 /*
00058  * BITMAPINFO Macros
00059  *
00060  * Where 'bmi' is specified, the macro expects a pointer to a BITMAPINFO structure.
00061  *
00062  * BMI_Alloc()              Allocate a BITMAPINFO structure which will hold ANY format
00063  * BMI_Free(bmi)            Release a BITMAPINFO created with BMI_Alloc()
00064  * BMI_BMI_Copy(bmi)        Return a copy of a BITMAPINFO (typically to modify for translation)
00065  * BMI_UpsideDown(bmi)      Return TRUE if BITMAPINFO is in an upside-down format
00066  * BMI_Wide(bmi)            Return absolute width of bitmap
00067  * BMI_High(bmi)            Return absolute height of bitmap
00068  * BMI_Depth(bmi)           Return bit count of a bitmap
00069  * BMI_Compressed(bmi)      Return TRUE if this BITMAPINFO is in a compressed format
00070  * BMI_LineSize(bmi)        Returns the width of a bitmap line in bytes (bitmaps round to four byte alignment!)
00071  * BMI_BitmapSize(bmi)      Returns the size of a BITMAP 
00072  * BMI_ColorCount(bmi)      Returns count of colors in a BITMAP
00073  * BMI_Header(bmi)          Returns pointer to BITMAPINFOHEADER of BITMAPINFO
00074  * BMI_ColorData(bmi)       Returns first byte of color data
00075  * BMI_Origin(bmi,pMap)     Returns pointer to raster origin of uncompressed bitmap data 
00076  * BMI_LineOffset(bmi)      Returns the offset to the next raster coordinate line in a bitmap
00077  * BMI_Clip(bmi,rect)       Fills out rect as a clipping rectangle for a DIB bitmap
00078  * BMI_BMI_Size(bmi)        Returns size of a BITMAPINFO as it would appear in a file
00079  * BMI_SetColorIndexes(bmi) Set colors in BMI_ as indexes into a logical palette
00080  * BMI_ParseData(img,bmi,bits) Given an image of a bitmap in memory, get bmi and bits
00081  */
00082 #define BMI_SIZE                (sizeof(BITMAPINFO) + (255 * sizeof(RGBQUAD)))
00083 #define BMI_Alloc()             ((BITMAPINFO *)calloc( 1, BMI_SIZE ))
00084 #define BMI_Free(bmi)           free(bmi)
00085 #define BMI_BMI_Copy(bmi)       memcpy( BMI_Alloc(), bmi, BMI_BMI_Size(bmi) )
00086 #define BMI_UpsideDown(bmi)     ((bmi)->bmiHeader.biHeight > 0)
00087 #define BMI_Wide(bmi)           (abs((bmi)->bmiHeader.biWidth))
00088 #define BMI_High(bmi)           (abs((bmi)->bmiHeader.biHeight))
00089 #define BMI_Depth(bmi)          ((bmi)->bmiHeader.biBitCount)
00090 #define BMI_Compressed(bmi)     !((bmi)->bmiHeader.biCompression==BI_RGB||(bmi)->bmiHeader.biCompression==BI_BITFIELDS)
00091 #define BMI_LineSize(bmi)       ((((BMI_Wide(bmi)*BMI_Depth(bmi))+31)&~31)>>3)
00092 #define BMI_BitmapSize(bmi)     (BMI_Compressed(bmi)?(bmi)->bmiHeader.biSizeImage:(BMI_LineSize(bmi)*BMI_High(bmi)))
00093 #define BMI_ColorCount(bmi)     ((bmi)->bmiHeader.biClrUsed ? (bmi)->bmiHeader.biClrUsed : (1 << BMI_Depth(bmi)))
00094 #define BMI_Header(bmi)         (&(bmi)->bmiHeader)
00095 #define BMI_ColorData(bmi)      ((bmi)->bmiColors)
00096 #define BMI_Origin(bmi,bits)    ( BMI_UpsideDown(bmi) ? (void*)(((uint8 *)(bits)) + (BMI_LineSize(bmi)*(BMI_High(bmi)-1))) : (void*)(bits) )
00097 #define BMI_LineOffset(bmi)     (BMI_UpsideDown(bmi)?-(int)BMI_LineSize(bmi):(int)BMI_LineSize(bmi))
00098 #define BMI_LinePtr(bmi,bits,line) ((void *)((uint8 *)BMI_Origin(bmi,bits)+((line)*BMI_LineOffset(bmi))))
00099 #define BMI_Clip(bmi,rect)      (((rect).left = (rect).top = 0),(rect).right=BMI_Wide(bmi),(rect).bottom=BMI_High(bmi))
00100 #define BMI_BMI_Size(bmi)       ( (bmi)->bmiHeader.biSize + \
00101     ( ( (bmi)->bmiHeader.biBitCount <= 8 ) ? \
00102     ( ((bmi)->bmiHeader.biClrUsed ? (bmi)->bmiHeader.biClrUsed : (1 << (bmi)->bmiHeader.biBitCount)) * sizeof(RGBQUAD) ) : \
00103     ( ((bmi)->bmiHeader.biBitCount == 24 || (bmi)->bmiHeader.biCompression != BI_BITFIELDS) ? 0 : (3 * sizeof(uint32)) ) ) )
00104 #define BMI_SetColorIndexes( bmi ) \
00105 { \
00106     uint16 *pData = (uint16 *)BMI_ColorData(bmi); \
00107     int curr; \
00108     for( curr = 0; curr < 256; ++curr ) \
00109         *pData++ = curr; \
00110 }
00111 
00118 #define BMI_ParseData( image, bmi, bits )\
00119 {\
00120     if( *((uint16 *)(image)) == *((uint16 *)"BM") ) \
00121     {\
00122         /* Loaded from a file; strip file header out */\
00123         (bmi) = (BITMAPINFO *)( (uint8 *)(image) + sizeof(BITMAPFILEHEADER) );\
00124         (bits) = (uint8 *)(image) + ((BITMAPFILEHEADER *)image)->bfOffBits;\
00125     }\
00126     else \
00127     {\
00128         /* <Probably> loaded from a resource */\
00129         (bmi) = (BITMAPINFO *)(image);\
00130         (bits) = (uint8 *)(bmi) + BMI_BMI_Size( bmi );\
00131     }\
00132 }
00133 
00140 #define BMI_Init( bmi, wide, high, bpp ) \
00141 {\
00142     BITMAPINFOHEADER* pbmi = &(bmi)->bmiHeader;\
00143     pbmi->biSize = sizeof(BITMAPINFOHEADER);\
00144     pbmi->biWidth = (wide);\
00145     pbmi->biHeight = (high);\
00146     pbmi->biPlanes = 1;\
00147     pbmi->biBitCount = (bpp);\
00148     pbmi->biCompression = BI_RGB;\
00149     pbmi->biSizeImage = 0;\
00150     pbmi->biXPelsPerMeter = 0;\
00151     pbmi->biYPelsPerMeter = 0;\
00152     pbmi->biClrUsed = 0;\
00153     pbmi->biClrImportant = 0;\
00154 }
00155 
00160 #define BMI_Create( bmi, bits )     { (bits) = calloc( BMI_LineSize(bmi), BMI_High(bmi) ); }
00161 
00166 #define BMI_Release( bmi, bits )    { free( bits ); }
00167 
00168 /*
00169  * Iterate each row of BMI as RGBQUAD data
00170  */
00171 #define BMI_foreach( bmi, bits, pixel ) \
00172     RGBQUAD pixel##_buff[2048];\
00173     int pixel##_row;\
00174     const RGBQUAD* pixel##_end = pixel##_buff + BMI_Wide(bmi);\
00175     RGBQUAD* pixel;\
00176     assert( BMI_Wide(bmi) < countof(pixel##_buff) );\
00177     for( pixel##_row = 0; BMI_ReadRow( pixel##_buff, bmi, bits, pixel##_row ); ++pixel##_row )\
00178     for( pixel = pixel##_buff; pixel < pixel##_end; ++pixel )\
00179 
00180 int RGBQUAD_Nearest( const RGBQUAD* clut, size_t count, const RGBQUAD* color );
00181 bool BMI_ReadRow( RGBQUAD* pixel, const BITMAPINFO* bmi, const void* bits, int row );
00182 bool BMI_WriteRow( const BITMAPINFO* bmi, void* bits, const RGBQUAD* pixel, int row );
00183 void BMI_Translate( const BITMAPINFO* dst, void* bdst, const BITMAPINFO* src, const void* bsrc );
00184 void* BMI_Unpack( const BITMAPINFO* bmi, const void* bits );
00185 size_t BMI_MakeClut( RGBQUAD* dstlut, size_t clut, const BITMAPINFO* bmi, const void* bits );
00186 
00187 struct BMIClutEntry
00188 {
00189     uint32  count;
00190     RGBQUAD rgb;
00191 };
00192 typedef struct BMIClut
00193 {
00194     struct BMIClutEntry cluts[32768];
00195 } BMIClut;
00196 void BMI_Clut_Begin( BMIClut* clut );
00197 void BMI_Clut_AddColor( BMIClut* clut, const RGBQUAD* rgb );
00198 size_t BMI_Clut_Make( BMIClut* clut, RGBQUAD* dstlut, size_t cClut );
00199 
00200 bool BMI_Save( const char* path, const BITMAPINFO* bmi, const void* bits );
00201 bool BMI_Load( const char* path, BITMAPINFO** bmi, void** bits );
00202 /*
00203  * Bind BMP to OPC library
00204  */
00205 void BMI_OPC( OPC* opc, const BITMAPINFO* bmi, void* bits );
00206 

Generated on Fri Jan 2 15:28:34 2009 for Squat by  doxygen 1.5.6