Jump to content
  • Advertisement
Sign in to follow this  
davidcoleman

StretchDIBits funny colors

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using StretchDIBits to paint a graph in a dialog but instead black and windowColor I get green and windowColor!!! Function looks like this CDC *pDC = m_container->GetDC(); pDC->SetStretchBltMode(HALFTONE); // tried other also BITMAPINFOHEADER bmiHeader; bmiHeader.biWidth=320; bmiHeader.biHeight=240; bmiHeader.biPlanes=1; bmiHeader.biBitCount=8; bmiHeader.biCompression=BI_RGB; bmiHeader.biSizeImage=320*240; byte MyBWImage[320*240]; for (i=1;i<320*100;i++) MyBWImage=200; //graph drawing // depending on above number I get different colors: "200" green, "3" pale yellow... what are these colors and how do I set some normal ones? int aaa=StretchDIBits (pDC->GetSafeHdc(), 0,0,320,240, 0,0,320,240, MyBWImage, reinterpret_cast<LPBITMAPINFO>( &bmiHeader), DIB_RGB_COLORS, SRCCOPY); m_container->ReleaseDC();

Share this post


Link to post
Share on other sites
Advertisement
An 8 bit BMP has a palette associated with it, palette index 200 will be green, index 3 is pale yellow, etc. The BITMAPINFO struct has a palette in it, and you're supposed to pass one to StretchDIBits. However, because of the cast, you're reading into random memory - StretchDIBits expects an array of 256 RGBQUADs after the BITMAPINFOHEADER, so it tries to read that and happens to read green and pale yellow colours.

Use something like the following instead:

BYTE* pbyBuffer = new BYTE[sizeof(BITMAPINFO) + sizeof(RGBQUAD)*256];
BITMAPINFO* pBitmap = (BITMAPINFO*)pbyBuffer;

// Setup header
pBitmap->bmiHeader.biWidth=320;
pBitmap->bmiHeader.biHeight=240;
pBitmap->bmiHeader.biPlanes=1;
pBitmap->bmiHeader.biBitCount=8;
pBitmap->bmiHeader.biCompression=BI_RGB;
pBitmap->bmiHeader.biSizeImage=320*240;

// Zero out palette
memset(pBitmap->bmiColors, 0, sizeof(RGBQUAD)*256);

// Set entry 200 to red
pBitmap->bmiColors[200].rgbRed = 255;

BYTE MyBWImage[320*240];
for (i=1;i<320*100;i++)
MyBWImage=200;

int aaa=StretchDIBits (pDC->GetSafeHdc(),
0,0,320,240,
0,0,320,240,
MyBWImage,
pBitmap,
DIB_RGB_COLORS,
SRCCOPY);

delete[] pbyBuffer;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!