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];
00055 buf[(i+j*width)*3+1] = gray_data[i+j*width];
00056 buf[(i+j*width)*3+2] = gray_data[i+j*width];
00057
00058 }
00059 }
00060
00061 WriteRgbBufferToBitmap(buf,width,height, filename);
00062 delete []buf;
00063 }
00064
00065
00066 void
00067 WriteRgbBufferToBitmap(BYTE *buf, const int Width, const int Height, const char* filename)
00068 {
00069
00070
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;
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
00084
00085
00086
00087
00088
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
00119
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
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 }
00143 }
00144 }
00145
00146 #endif