Am i creating the Bitmap Byte array correctly?
basically i want to create a monochrome images from another image i pass to the method, here i will set the selected colour to be black and all other pixels to be white.
i'm getting a bitmap being output of the same size of the original, however when i try and open the bitmap i get the message drawing failed, which make sme think i'm not creating the byte array correctly??
the code i'm using is as follows
BOOL CBitmapOCR::HighlightImage(LPTSTR path, int rVal, int gVal, int bVal)
{
//load the bitmap
CString szFilename(path);
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
BITMAP WorkBmp;
GetObject(hBmp, sizeof(WorkBmp), &WorkBmp);
//pad so multiple of 4
int bitsPerPixel = WorkBmp.bmBitsPixel/8;
int dActualWidth = WorkBmp.bmWidth * bitsPerPixel + WorkBmp.bmWidth % bitsPerPixel;
ocrMsg.Format("dActualWidth :%d: Wid:%d; planes:%d: BitsPixel:%d: what is mod4 :%d:",dActualWidth, WorkBmp.bmWidth, WorkBmp.bmPlanes, WorkBmp.bmBitsPixel, WorkBmp.bmWidth % 4);
logMsg(ocrMsg);
BYTE* pBmiSrc = new BYTE[ WorkBmp.bmHeight * WorkBmp.bmWidth ];
int x, y;
int count = 0;
int r,g,b;
for (y=0; y<WorkBmp.bmHeight;y++)
{
for(x=0;x<WorkBmp.bmWidth;x++)
{
r = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * bitsPerPixel + 2);
g = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * bitsPerPixel + 1);
b = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * bitsPerPixel + 0);
if(r==rVal && g==gVal && b==bVal)
{
ocrMsg.Format("black [x%dy%d]r%dg%db%d.count:%d:",x,y,r,g,b,count);
logMsg(ocrMsg);
pBmiSrc[count] = 1;
//set pixel to be black
}
else
{
ocrMsg.Format("white [x%dy%d]r%dg%db%d.count:%d:",x,y,r,g,b,count);
logMsg(ocrMsg);
pBmiSrc[count] = 0;
//set pixel to be white
}
count++;
}
}
//now write new bitmap to file
//create the new bitmap
BITMAPFILEHEADER bfh;
bfh.bfType=('M'<<8)+'B';
bfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); //offset to bitmap pixel data
bfh.bfSize= (WorkBmp.bmHeight * WorkBmp.bmWidth / 8) + bfh.bfOffBits;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
//next the bitmap info
BITMAPINFO Bmi = {0};
Bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Bmi.bmiHeader.biWidth = WorkBmp.bmWidth;
Bmi.bmiHeader.biHeight = WorkBmp.bmHeight;
Bmi.bmiHeader.biPlanes = 1;
Bmi.bmiHeader.biBitCount = 1;
Bmi.bmiHeader.biCompression = BI_RGB;
Bmi.bmiHeader.biSizeImage = WorkBmp.bmHeight * WorkBmp.bmWidth /8; //should ust be ths as only one bit er pixel
Bmi.bmiHeader.biXPelsPerMeter = 0;
Bmi.bmiHeader.biYPelsPerMeter = 0;
Bmi.bmiHeader.biClrUsed = 0;
Bmi.bmiHeader.biClrImportant = 0;
Bmi.bmiColors[0].rgbBlue = Bmi.bmiColors[0].rgbGreen = Bmi.bmiColors[0].rgbRed = 255;
Bmi.bmiColors[1].rgbBlue = Bmi.bmiColors[1].rgbGreen = Bmi.bmiColors[1].rgbRed = 1;
//now create the file
CFile file;
if( !file.Open( "C:\\temp111.bmp", CFile::modeWrite|CFile::modeCreate) )
return FALSE;
file.Write(&bfh, sizeof(bfh));
file.Write(&Bmi.bmiHeader,sizeof(BITMAPINFOHEADER));
file.Write((int*)pBmiSrc,Bmi.bmiHeader.biSizeImage);
return true;
}
many thanks,
Matt.

