-
May 16th, 2013, 12:44 PM
#1
Trouble with GetDIBits()
Hello guys, I'm new to C++ and I couldn't figure out the proper way to store pixel data of a device context into an array using GetDIBits(). Currently I am trying to get the RGB values of a single pixel of a bitmap file:
Code:
#include <windows.h>
#include <iostream>
using namespace std;
int main() {HDC MemDC=CreateCompatibleDC(NULL);
SelectObject(MemDC,(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\gBit.bmp",IMAGE_BITMAP,1366,768,LR_LOADFROMFILE));
HBITMAP hBit=(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\gBit.bmp",IMAGE_BITMAP,1366,768,LR_LOADFROMFILE);
BITMAPINFO bmi;
BYTE p[3];
bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth=1;
bmi.bmiHeader.biHeight=-1;
bmi.bmiHeader.biPlanes=1;
bmi.bmiHeader.biBitCount=24;
bmi.bmiHeader.biCompression=BI_RGB;
bmi.bmiHeader.biSizeImage=0;
bmi.bmiHeader.biXPelsPerMeter=0;
bmi.bmiHeader.biYPelsPerMeter=0;
bmi.bmiHeader.biClrUsed=0;
bmi.bmiHeader.biClrImportant=0;
GetDIBits(MemDC,hBit,0,0,p,&bmi,DIB_RGB_COLORS);
cout<<p[0]<<endl<<p[1]<<endl<<p[2];
ReleaseDC(NULL,MemDC); DeleteDC(MemDC);
DeleteObject(hBit); while (1) {}
}
gBit.bmp, the bitmap file selected into MemDC, is an entirely white 24-bit bitmap image, so I thought cout should display 255, but for some reason it displays some weird characters. Most probably p is not initialized since the same values are returned when I remove the line containing GetDIBits(). Am I using this function properly? What could I possibly be doing wrong here?
-
May 16th, 2013, 02:18 PM
#2
Re: Trouble with GetDIBits()
What does the GetDIBits return? And did you red the GetDIBits documentation?
Victor Nijegorodov
-
May 16th, 2013, 11:42 PM
#3
Re: Trouble with GetDIBits()
Well, in my case the return value is 1, so I guess the pixel is successfully scanned. But whether or not I call GetDIBits(), the same characters are stored in the array. I did read the documentation of the function but I didn't find anything that could cause the error. What do you expect me to know from the documentation?
-
May 17th, 2013, 05:16 AM
#4
Re: Trouble with GetDIBits()
BYTE is a char.
cout << (BYTE)32;
outputs a character space (ascii value 32), not the value 32.
to display char's (BYTE's) as numeric values, typecast to int.
-
May 18th, 2013, 01:38 AM
#5
Re: Trouble with GetDIBits()
First, in your other thread COLORREF is 4-bytes, as it stores 32-bit values RGBA (RGB and Alpha for transparency). Both your examples declare 24-bit colour values, so you would need only 3-bytes per pixel. It's ok to use 4-bytes like COLORREF, but you would ignore the 4th byte each time as it would be 0. Though, that is a waste of memory.
In this example you are loading an image the size 1366x768. So you would need (1366 * 768 * 3) bytes to store it 3147264. Fortunately the width (768) is a multiple of 4, otherwise you would need to add some padding.
Should be more like
Code:
BYTE p[1366 * 768 * 3] // Can't actually be done
This would need to be done with new in the heap otherwise it will crash.
Code:
bmi.bmiHeader.biSizeImage=0;
This also needs to be the value of 1366 * 768 * 3.
-
May 18th, 2013, 01:44 AM
#6
Re: Trouble with GetDIBits()
Also, the fourth variable needs to be the image height (scanlines). So instead of:
Code:
GetDIBits(MemDC,hBit,0,0,p,&bmi,DIB_RGB_COLORS); // Incorrect
It should be more like:
Code:
GetDIBits(MemDC,hBit,0,768,p,&bmi,DIB_RGB_COLORS);
Tags for this Thread
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
|