Click to See Complete Forum and Search --> : error check question


celtics
December 7th, 2003, 04:02 PM
I need to map colors to gray scale such that i get a BW image.
any errors in this mfc / sdi code?.

void CMyView::OnDraw(CDC* dc)
{
CMemDC pDC(dc);//to remore flickers
CPalette pal;
CPalette* pOldPalette = NULL;
float MyV;
LPBITMAPINFO bInfo = (LPBITMAPINFO)new BYTE[sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD)];
bInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bInfo->bmiHeader.biWidth = Width;
bInfo->bmiHeader.biHeight = Height;
bInfo->bmiHeader.biPlanes = 1;
bInfo->bmiHeader.biBitCount = 8;
bInfo->bmiHeader.biCompression = 0;
bInfo->bmiHeader.biSizeImage = 0;
bInfo->bmiHeader.biXPelsPerMeter = 0;
bInfo->bmiHeader.biYPelsPerMeter = 0;
bInfo->bmiHeader.biClrUsed = 0;
bInfo->bmiHeader.biClrImportant = 0;

int nColors = bInfo->bmiHeader.biClrUsed ? bInfo->bmiHeader.biClrUsed :
1 << bInfo->bmiHeader.biBitCount;

if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && nColors <= 256 )
{
// The device supports a palette and bitmap has color table

// Allocate memory for a palette
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];

pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;

for( int i=0; i < nColors; i++)
{
MyV = (float)((0.3 * bInfo->bmiColors[i].rgbRed) +
(0.59 * bInfo->bmiColors[i].rgbGreen) +
(0.11 * bInfo->bmiColors[i].rgbBlue));
pLP->palPalEntry[i].peRed = (int)MyV;
pLP->palPalEntry[i].peGreen = (int)MyV;
pLP->palPalEntry[i].peBlue = (int)MyV;
pLP->palPalEntry[i].peFlags = 0;
}

pal.CreatePalette( pLP );

delete[] pLP;

// Select the palette
pOldPalette = pDC->SelectPalette(&pal, FALSE);
pDC->RealizePalette();
}
else if((pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE) == 0 && nColors <= 256 )
{
// Device does not supports palettes but bitmap has a color table
// Modify the bitmaps color table directly
// Note : This ends up changing the DIB. If that is not acceptable then
// copy the DIB and then change the copy rather than the original

for( int i=0; i < nColors; i++)
{
MyV = (float)((0.3 * bInfo->bmiColors[i].rgbRed) +
(0.59 * bInfo->bmiColors[i].rgbGreen) +
(0.11 * bInfo->bmiColors[i].rgbBlue));
bInfo->bmiColors[i].rgbRed = (int)MyV;
bInfo->bmiColors[i].rgbGreen = (int)MyV;
bInfo->bmiColors[i].rgbBlue = (int)MyV;
}
}
HDC hdc = pDC->GetSafeHdc();
StretchDIBits( hdc, 0, 0,
Wt,
Ht,
0, 0,
Width,
Height,
MyDataBuf,
bInfo,
DIB_RGB_COLORS,
SRCCOPY );
delete [] bInfo;
ReleaseDC(pDC);
}

Marc G
December 12th, 2003, 12:43 PM
I'm not going to read that code. Please use the CODE tags when posting code, then it's a lot easier to read!

Now, what is the problem you'r having? Could you post a screenshot of the color version and a screenshot of the BW version?