Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Bitmap.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Feature_Bitmap_h
00002 #define Impala_Core_Feature_Bitmap_h
00003 
00004 #include "Basis/String.h"
00005 #include "Core/Database/RawDataSet.h"
00006 #include "Core/Feature/FeatureDefinition.h"
00007 
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace Feature
00013 {
00014 
00015 class Bitmap
00016 {
00017 public:
00018 
00019     Bitmap()
00020     {
00021     }
00022    
00023     ~Bitmap()
00024     {
00025     }
00026 
00027     void
00028     SaveRgb2BitmapFile(const char* file, BYTE* rgb_data, int width, int height)
00029     {
00030         char filename[256];
00031         strcpy(filename,file);
00032         int bufsize = sizeof(BYTE)* width * height * 3;
00033         BYTE* buf = new BYTE [bufsize];
00034         memset(buf,0,bufsize);
00035         RGB2BGR(rgb_data,buf,width,height);
00036 
00037         WriteRgbBufferToBitmap(buf,width,height, filename);
00038         delete []buf;
00039     }
00040 
00041     void 
00042     SaveGray2BitmapFile(char* file, BYTE* gray_data, int width, int height)
00043     {
00044         char filename[256];
00045         strcpy(filename,file);
00046         int bufsize = sizeof(BYTE)* width * height * 3;
00047         BYTE* buf = new BYTE [bufsize];
00048         memset(buf,0,bufsize);
00049         
00050         for (int i=0; i<width; i++)
00051         {
00052             for (int j=0; j<height; j++)
00053             {
00054                 buf[(i+j*width)*3+0] = gray_data[i+j*width]; // Blue
00055                 buf[(i+j*width)*3+1] = gray_data[i+j*width]; // Green
00056                 buf[(i+j*width)*3+2] = gray_data[i+j*width]; // Red
00057 
00058             }
00059         }
00060         
00061         WriteRgbBufferToBitmap(buf,width,height, filename);
00062         delete []buf;
00063     }
00064 
00065     //  This buffer *buf should be bottom-up mode.
00066     void  
00067     WriteRgbBufferToBitmap(BYTE *buf, const int Width, const int Height, const char* filename)
00068     {
00069         
00070         // if it is NOT the multiple of 4,just full it to.
00071         int    byteWidth=(3*Width+3)/4*4; 
00072         long   m_ImageSize = byteWidth*Height;
00073 
00074         BITMAPFILEHEADER  bmfHdr; 
00075         BITMAPINFO *pInfo;
00076 
00077         bmfHdr.bfType=0x4d42; //bmfHdr.bfType='MB';
00078         bmfHdr.bfSize=m_ImageSize+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
00079         bmfHdr.bfReserved1=0; 
00080         bmfHdr.bfReserved2=0; 
00081         bmfHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
00082 
00083         // note:
00084         // ------------------------------------------------------------
00085         // If biHeight is positive, the bitmap is a bottom-up DIB and
00086         //           its origin is the lower-left corner. 
00087         // If biHeight is negative, the bitmap is a top-down DIB and 
00088         //           its origin is the upper-left corner. 
00089         // ------------------------------------------------------------
00090         pInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER));
00091         pInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
00092         pInfo->bmiHeader.biWidth=Width; 
00093         pInfo->bmiHeader.biHeight=Height; 
00094         pInfo->bmiHeader.biPlanes=1; 
00095         pInfo->bmiHeader.biBitCount=24; 
00096         pInfo->bmiHeader.biCompression=BI_RGB; 
00097         pInfo->bmiHeader.biSizeImage=m_ImageSize; 
00098         pInfo->bmiHeader.biXPelsPerMeter=0; 
00099         pInfo->bmiHeader.biYPelsPerMeter=0; 
00100         pInfo->bmiHeader.biClrUsed=0; 
00101         pInfo->bmiHeader.biClrImportant=0;
00102 
00103         FILE* fp = fopen(filename, "wb");
00104         if (fp==NULL)
00105         {
00106             printf("Can not open the file: %s\n",filename);
00107             return;
00108         }
00109         
00110         fwrite((LPSTR)&bmfHdr,   sizeof(BITMAPFILEHEADER), 1, fp);
00111         fwrite(pInfo,    sizeof(BITMAPINFOHEADER), 1, fp);
00112         fwrite(buf, sizeof(BYTE), m_ImageSize,   fp);
00113 
00114         fclose(fp);
00115         free(pInfo);
00116     }
00117 
00118     // Array2dScalarUInt8 is the order of RGBRGBRGB...
00119     // The Bitmap buffer is the order of BGRBGRBGR...
00120     void 
00121     RGB2BGR(BYTE *src, BYTE *dst, const int Width, const int Height)
00122     {
00123         for (int i=0; i<Width; i++)
00124         {
00125             for (int j=0; j<Height; j++)
00126             {
00127                 // R G B --> B G R
00128                 dst[(j*Width+i)*3+2] = src[(j*Width+i)*3+0];
00129                 dst[(j*Width+i)*3+1] = src[(j*Width+i)*3+1];
00130                 dst[(j*Width+i)*3+0] = src[(j*Width+i)*3+2];
00131             }
00132         }
00133     }
00134 
00135 private:
00136 
00137     int mWidth;
00138     int mHeight;
00139 
00140 };
00141 
00142 } // namespace Feature
00143 } // namespace Core
00144 } // namespace Impala
00145 
00146 #endif

Generated on Fri Mar 19 09:31:05 2010 for ImpalaSrc by  doxygen 1.5.1