?
Solved

Hbitmap to RGB Buffer help

Posted on 2014-02-22
7
Medium Priority
?
963 Views
Last Modified: 2014-02-26
Hello,

inside an external DLL I have a function to set a RGB Buffer for a logo to render. This function is:
 void       SetLogo(unsigned char *RGBBuf, int width, int strideLen, int height); // sets the actual logo 

Open in new window


The description of this function is:
RGBBuf is a bitmap buffer, which contains a large bitmap width * height. Because Windows bitmaps still have padding inside, one must also specify strideLen in bytes, that is the line size. Without padding the line size is width * 3, with padding it varies between width * 3 + 0 and width * 3 + 3

I use now following code:
void CDVDConverterWork::GetImageForLogo()
{
	HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_OVLLOGO), IMAGE_BITMAP, 0, 0, 0); 

	BITMAP bm;
	::GetObject( hBitmap , sizeof(bm) , &bm );

	/* Omitting error checks for brevity */
	HDC dcBitmap = CreateCompatibleDC ( NULL );
	SelectObject( dcBitmap, hBitmap );

	BITMAPINFO bmpInfo;
	bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = bm.bmWidth;
	bmpInfo.bmiHeader.biHeight = -bm.bmHeight;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 24;
	bmpInfo.bmiHeader.biCompression = BI_RGB;        
	bmpInfo.bmiHeader.biSizeImage = 0;  

	 
	unsigned char* retArray = new unsigned char[bmpInfo.bmiHeader.biSizeImage];

	//LPVOID retArray = malloc(bmpInfo.bmiHeader.biSizeImage);
	GetDIBits( dcBitmap , hBitmap , 0 , bm.bmHeight , retArray , &bmpInfo , DIB_RGB_COLORS );

	
	m_pConverter->SetLogo(retArray,bm.bmWidth, bmpInfo.bmiHeader.biSizeImage, bm.bmHeight);
	m_pConverter->EnableLogo(true);
	
}

Open in new window


But I get now crashes inside the DLL. It seems that my function submit wrong data to the DLL.  Any idea what is wrong there?
0
Comment
Question by:Ingo Foerster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39879446
>>Any idea what is wrong there?

You're  allocating an array of size 0:
bmpInfo.bmiHeader.biSizeImage = 0;  

unsigned char* retArray = new unsigned char[bmpInfo.bmiHeader.biSizeImage];

Open in new window


That can't work.
0
 
LVL 10

Expert Comment

by:Korbus
ID: 39879594
Agree with jkr-  perhaps you meant to use: bmpInfo.bmiHeader.biSize  in your new statement?
0
 

Author Comment

by:Ingo Foerster
ID: 39879998
Hi,,
ok, I changed the code:
	HBITMAP hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_OVLLOGO), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_SHARED); 

	BITMAP bm;
	::GetObject( hBitmap , sizeof(bm) , &bm );

	CDC mem_dc;  
	mem_dc.CreateCompatibleDC(NULL);

	/* Omitting error checks for brevity */
	BITMAPINFO bmpInfo;
	bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = bm.bmWidth;
	bmpInfo.bmiHeader.biHeight = bm.bmHeight;
	bmpInfo.bmiHeader.biPlanes = bm.bmPlanes;
	bmpInfo.bmiHeader.biBitCount = bm.bmBitsPixel;
	bmpInfo.bmiHeader.biCompression = BI_RGB;        
	bmpInfo.bmiHeader.biSizeImage = 0;  
	bmpInfo.bmiHeader.biClrImportant = 0;
	bmpInfo.bmiHeader.biClrUsed = 0;

	unsigned char *Pixels = (unsigned char *)malloc(bm.bmWidth * bm.bmHeight * 4); //sizeof(unsigned char)

	::GetDIBits(mem_dc.m_hDC, hBitmap, 0, bm.bmHeight, Pixels, &bmpInfo, DIB_RGB_COLORS);
	//GetDIBits( hDC , hBitmap , 0 , bm.bmHeight , Pixels , &bmpInfo , DIB_RGB_COLORS );

	::DeleteObject(hBitmap);
	
	SetLogo(Pixels, bm.bmWidth, bm.bmWidth*3, bm.bmHeight);

Open in new window


But now I have a gray image that is mirrored and is slanted. Any idea what is wrong with my buffer?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 39880854
Does the bitmap you are using have padding or not? Multiplying by three seems a bit weird to me. How does it look if you leave out that multiplication?
0
 
LVL 10

Expert Comment

by:Korbus
ID: 39883137
Not sure, but I suspect the params to the LoadImage function:

try LR_VGACOLOR:  "Uses true VGA colors."
rather than LR_CREATEDIBSECTION  : "When the uType parameter specifies IMAGE_BITMAP, causes the function to return a DIB section bitmap rather than a compatible bitmap."

(from : http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045(v=vs.85).aspx)
0
 

Author Closing Comment

by:Ingo Foerster
ID: 39887405
Was the passing issue. Thanks.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 39888109
unsigned char *Pixels = (unsigned char *)malloc(bm.bmWidth * bm.bmHeight * 4);

Open in new window

instead of multiplying times 4 you better should use (bm.bmBitsPixel/8) and instead of a separate pointer Pixels you better have used the bmiColors member array of structure BITMAPINFO.

in case of BI_RGB you have 32 bits per pixel what is 4 bytes what is the size of a RGBQUAD what is 3 bytes for red, green, blue + 1 byte reserved which could be used for specials like alpha blending. an RGBQUAD which has size of int on most systems is more handy than a RGBTRIPLE and the 4th byte could be well used for additional purposes.

nevertheless, when working with bitmaps you never should rely on assumptions but use the information given in the appropriate structures, here in the Bitmap structure.

The BITMAPINFO should have dynamic size such that the bmiColors array has appropriate size:

size_t sizePixels = 0;
BITMAPINFO * pBmpInfo = NULL;
...
sizePixels = bm.bmWidth * bm.bmHeight * (bm.bmBitsPixel/8);
// the structure was created with a size such that the 
// bmiColors member could take the required colors per pixel.
pBmpInfo = (unsigned char *)malloc( (sizeof(BITMAPINFOHEADER) + sizePixels);

pBmpInfo->bmiHeader.biWidth = bm.bmWidth;
pBmpInfo->bmiHeader.biHeight = bm.bmHeight;
...
::GetDIBits(mem_dc.m_hDC, hBitmap, 0, bm.bmHeight, pBmpInfo->bmiColors, &bmpInfo, DIB_RGB_COLORS);

Open in new window


btw, you seem to use the c++ compiler and not the c compiler. if so, you should use new/delete rather than malloc/free.

Sara
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question