|
-
November 5th, 2007, 10:24 AM
#1
convert bmp to monochrome
Hi,
all i basically want to do is convert an already existing bitmap to greyscale.
to do this i assume i will have to create a new bimapeaderinfo, fill in the needed details and then look at and convert each pixel in the exisiting image.
can anyone help me to do this? also am i calculating the width size of the destination bmp correctly?
Code:
BOOL CBitmapOCR::ConvertBitmap(LPTSTR path, LPTSTR dest, DWORD wBpp, int rVal, int gVal, int bVal)
{
// Make sure bits per pixel is valid
if (wBpp <= 1) wBpp = 1;
else if (wBpp <= 4) wBpp = 4;
else if (wBpp <= 8) wBpp = 8;
else if (wBpp <= 16)wBpp = 16;
else if (wBpp <= 24)wBpp = 24;
else wBpp = 32;
//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);
//create the new bitmap
BITMAPINFOHEADER head;
// set the correct bpp value
switch (wBpp)
{
case 1:
head.biClrUsed = 2;
break;
case 4:
head.biClrUsed = 16;
break;
case 8:
head.biClrUsed = 256;
break;
default:
head.biClrUsed = 0;
}
int dActualWidth = (WorkBmp.bmWidth * WorkBmp.bmBitsPixel + 3)&(~3);
head.biSize = sizeof(BITMAPINFOHEADER);
head.biWidth = WorkBmp.bmWidth / WorkBmp.bmBitsPixel ;
head.biHeight = WorkBmp.bmHeight;
head.biPlanes = 1;
head.biBitCount = (WORD)wBpp;
head.biCompression = BI_RGB;
head.biSizeImage = WorkBmp.bmHeight * WorkBmp.bmWidth;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;
head.biClrImportant = 0;
BITMAPINFO * pBmiSrc = ( BITMAPINFO * ) new BYTE[ WorkBmp.bmHeight * WorkBmp.bmWidth ];
int x, y;
int r,g,b;
for(x=0;x<WorkBmp.bmWidth;x++)
{
for (y=0; y<WorkBmp.bmHeight;y++)
{
r = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * WorkBmp.bmBitsPixel + 2);
g = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * WorkBmp.bmBitsPixel + 1);
b = *((BYTE*)WorkBmp.bmBits + y * dActualWidth + x * WorkBmp.bmBitsPixel + 0);
if(r==rVal && g==gVal && b==bVal)
{
//set pixel to be black
}
else
{
//set pixel to be white
}
}
}
return true;
}
many thanks,
Matt.
Last edited by flynny1st; November 6th, 2007 at 11:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|