|
-
August 24th, 2010, 08:46 AM
#1
[RESOLVED] Read grayscale bitmap into array
Hello,
I need to read in an 8 bit grayscale bmp and I want the value of its pixels stored in an array (can do that myself). I can't seem to get the value between 0 and 255, I always get zero with the code below. What's wrong?
Code:
HANDLE hBitmap; //Holds the Handle of the Bitmap once it is loaded.
HDC hDC; //Our temporary Device Context, which will be used to hold our Bitmap later on.
hDC = CreateCompatibleDC(NULL); //Create's a compatible Device Context(DC) and stores its value in 'hDC'.
hBitmap = LoadImage(0,"E:\\Test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); //This loads our Image into memory and stores the handle in the variable "hBitmap". You may change the Directory from this API call to one containing your Image.
//Get info about the bitmap
BITMAP Bitmap;
SIZE ImageSize;
//Get info about the bitmap
GetObject(hBitmap, sizeof(Bitmap), &Bitmap);
ImageSize.cx=Bitmap.bmWidth;
ImageSize.cy=Bitmap.bmHeight;
SelectObject(hDC,hBitmap); //Puts our loaded Image into our temporary Device Context.
for(int x=0;x<ImageSize.cx;++x)
for(int y=0;y<ImageSize.cy;++y)
{
COLORREF color=GetPixel(hDC,x,y);
// Problem here : color is always zero, I want the correct value and put it in an array
}
// Clean up
DeleteDC(hDC);
Last edited by Simon666; August 26th, 2010 at 07:16 AM.
Reason: Managed to find something that works for me
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
-
August 24th, 2010, 12:04 PM
#2
Re: Read grayscale bitmap into array
 Originally Posted by Simon666
I need to read in an 8 bit grayscale bmp and I want the value of its pixels stored in an array (can do that myself). I can't seem to get the value between 0 and 255, I always get zero with the code below. What's wrong?
Could you post your test bitmap here?
How do you "always get zero"? What's your test?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 25th, 2010, 02:13 AM
#3
Re: Read grayscale bitmap into array
As test I use a 256 color bitmap that has a grayscale palette. I change some pixels in all four corners to various grayscales but always get zero no matter what.
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
-
August 25th, 2010, 03:11 AM
#4
Re: Read grayscale bitmap into array
 Originally Posted by Simon666
As test I use a 256 color bitmap that has a grayscale palette. I change some pixels in all four corners to various grayscales but always get zero no matter what.
I am sorry, but you have NOT answered Vlad's question:
 Originally Posted by VladimirF
Could you post your test bitmap here?
How do you "always get zero"? What's your test?
Since we still don't know how and what exactly you checked that "always get zero" (did you debug your code, BTW?) we cannot help you more.
Victor Nijegorodov
-
August 25th, 2010, 03:54 AM
#5
Re: Read grayscale bitmap into array
 Originally Posted by VictorN
I am sorry, but you have NOT answered Vlad's question:
Since we still don't know how and what exactly you checked that "always get zero" (did you debug your code, BTW?) we cannot help you more.
Fine, I didn't want to upload it as attachment as it is several hundred kilobyte, so I put it on my own website. If you want it, it's here:
http://users.ugent.be/~skdmeule/Test.bmp
With regards to the second part, if I do:
CString Test; Test.Format("%d",color);
AfxMessageBox(Test);
in the for-loop I always get zero. So I am pretty sure of that.
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
-
August 25th, 2010, 04:00 AM
#6
Re: Read grayscale bitmap into array
1. Get the right dimensions
I use a more complex code than yours:
Code:
SIZE hbmp_size;
GetBitmapDimensionEx(hBitmap, &hbmp_size);
ImageSize.cx = (UINT) hbmp_size.cx;
ImageSize.cy = (UINT) hbmp_size.cy;
if (ImageSize.cx == 0 || ImageSize.cy == 0) {
BITMAP bm;
if (GetObject(hBitmap, sizeof(BITMAP), &Bitmap, (LPSTR)&Bitmap) != 0) {
ImageSize.cx = (UINT) Bitmap.bmWidth;
ImageSize.cy = (UINT) Bitmap.bmHeight;
}
}
2. Have a buffer for your bitmap bits
I always allocate a buffer for the bitmap bits. Maybe this is not useful some situations. Anyway it works for me.
3. Copy the grid to your buffer with GetDIBits()
The code is quite long. But you will find examples with gloogloo.
-
August 25th, 2010, 08:11 PM
#7
Re: Read grayscale bitmap into array
 Originally Posted by Simon666
With regards to the second part, if I do:
CString Test; Test.Format("%d",color);
AfxMessageBox(Test);
in the for-loop I always get zero. So I am pretty sure of that.
What do you mean - always? Your bitmap has 900,000 pixels. Are you saying that you read and OK'ed 900,000 message boxes???
Anyway, your loops scan the bitmap vertically, and your first vertical line is all black. The white dots are not really at the corners.
Note that for better performance (which is not really an issue with the very slow GetPixel), you should scan your bitmap horizontally.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 26th, 2010, 07:15 AM
#8
Re: Read grayscale bitmap into array
 Originally Posted by VladimirF
What do you mean - always? Your bitmap has 900,000 pixels. Are you saying that you read and OK'ed 900,000 message boxes???
Ofcourse not, but I know some values in all 4 corners. Anyway, I managed to solve it myself looking at some related code on the net. For posterity reasons:
Code:
// Additional functions needed
int GetCBitmapWidth(const CBitmap & cbm)
{
BITMAP bm;
cbm.GetObject(sizeof(BITMAP),&bm);
return bm.bmWidth;
}
int GetCBitmapHeight(const CBitmap & cbm)
{
BITMAP bm;
cbm.GetObject(sizeof(BITMAP),&bm);
return bm.bmHeight;
}
// Actual code
CDC *dc=new CDC;
dc->CreateCompatibleDC(NULL);
CBitmap bitmap;
bitmap.m_hObject=(HBITMAP)::LoadImage(0,"E:\\Test.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
dc->SelectObject(bitmap);
int cx = GetCBitmapWidth(bitmap);
int cy = GetCBitmapHeight(bitmap);
for(int x=0;x<ImageSize.cx;++x)
for(int y=0;y<ImageSize.cy;++y)
{
COLORREF color=dc->GetPixel(x,y);
// At least this crap works, so now I can do my image processing
}
delete dc;
CBitmap doesn't have member functions to determine its width and height as on MSDN, that's why you need those helper functions.
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
-
August 26th, 2010, 12:03 PM
#9
Re: Read grayscale bitmap into array
 Originally Posted by Simon666
Ofcourse not, but I know some values in all 4 corners. Anyway, I managed to solve it myself looking at some related code on the net. For posterity reasons:...
CBitmap doesn't have member functions to determine its width and height as on MSDN, that's why you need those helper functions.
As I mentioned before, the corners of your image are black.
Glad that you solved your problem.
However, for clarity, I have to say that:
1. There are no significant differences between this code and the fragment from your first post.
2. The use of CBitmap is unjustified: you do not use any of its methods and simply store HGDIOBJ in it.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 26th, 2010, 12:17 PM
#10
Re: Read grayscale bitmap into array
I agree with Vlad with one slight exception: using CBitmap rather than HBITMAP helps to forget about deleting HBITMAP object. 
And I'd like to also add, dear Simon666, that there is no any advantage in creating CDC object in the HEAP. A very simple must work the same way and doesn't need to call delete.
Victor Nijegorodov
-
August 26th, 2010, 01:37 PM
#11
Re: Read grayscale bitmap into array
It's probably also worth noting that using LoadImage() without the LR_CREATEDIBSECTION flag will result in mapping the bitmap to the display colors of the default device. I haven't checked the code, but this may result in a monochrome bitmap since the DC is initialized with CreateCompatibleDC(NULL). This might be why it's "all black".
-
August 27th, 2010, 02:09 AM
#12
Re: Read grayscale bitmap into array
I know things can be done better. That's why I'm asking help here, I'm not a programmer by education, but I need something that does the job. I managed to concoct something myself using related googled code and it seems it can be done better, but hey, I have my results now and that's what matters.
 Originally Posted by VladimirF
As I mentioned before, the corners of your image are black.
Of course I tested it editing the corners to some variations in shade.
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
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
|