Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert bitmap to grayscale by pixel intensity using GDI?

I'm looking for the simple solution of how to convert the 32-bit bitmap to grayscale using GDI (not GDI+). Is there a possibility e.g. by changing the bitmap's pallete or something ?

Of course there is plenty of examples in Delphi like this one, but I'm looking for a WinAPI function which would do this without iteration through the lines.

like image 1000
Martin Reiner Avatar asked Dec 19 '11 09:12

Martin Reiner


2 Answers

I haven't found any single GDI function doing this. The easiest way, as David mentioned in his comment, is to scan each line and compute the pixel colors. What you are looking for is probably the luminance formula.

There are few variations of this formula and in the following example I've used the one recommended by the ITU, see this document section 2.5.1. As I found somewhere, this formula is used e.g. even by well known Adobe Photoshop. The following code example supports and expects only 24-bit pixel format bitmaps as an input:

procedure BitmapGrayscale(ABitmap: TBitmap);
type
  PPixelRec = ^TPixelRec;
  TPixelRec = packed record
    B: Byte;
    G: Byte;
    R: Byte;
  end;
var
  X: Integer;
  Y: Integer;
  Gray: Byte;
  Pixel: PPixelRec;
begin
  for Y := 0 to ABitmap.Height - 1 do
  begin
    Pixel := ABitmap.ScanLine[Y];
    for X := 0 to ABitmap.Width - 1 do
    begin
      Gray := Round((0.299 * Pixel.R) + (0.587 * Pixel.G) + (0.114 * Pixel.B));
      Pixel.R := Gray;
      Pixel.G := Gray;
      Pixel.B := Gray;
      Inc(Pixel);
    end;
  end;
end;
like image 70
TLama Avatar answered Nov 08 '22 23:11

TLama


You can create a paletted DIB Section, 8 bits per pixel and 256 colors, and initialize palette to shades of grey { 0, 0, 0 }, { 1, 1, 1 }, ... { 255, 255, 255 }.

A single GDI BitBlt into this bitmap will grey your original image out. Here is the code snippet (in C++, ATL and WTL - but you should get the idea).

CWindowDC DesktopDc(NULL);
CDC BitmapDc;
ATLVERIFY(BitmapDc.CreateCompatibleDC(DesktopDc));
CBitmap Bitmap;
CTempBuffer<BITMAPINFO> pBitmapInfo;
const SIZE_T nBitmapInfoSize = sizeof (BITMAPINFO) + 256 * sizeof (RGBQUAD);
pBitmapInfo.AllocateBytes(nBitmapInfoSize);
ZeroMemory(pBitmapInfo, nBitmapInfoSize);
pBitmapInfo->bmiHeader.biSize = sizeof pBitmapInfo->bmiHeader;
pBitmapInfo->bmiHeader.biWidth = 320;
pBitmapInfo->bmiHeader.biHeight = 240;
pBitmapInfo->bmiHeader.biPlanes = 1;
pBitmapInfo->bmiHeader.biBitCount = 8;
pBitmapInfo->bmiHeader.biCompression = BI_RGB;
pBitmapInfo->bmiHeader.biSizeImage = 240 * 320;
pBitmapInfo->bmiHeader.biClrUsed = 256;
pBitmapInfo->bmiHeader.biClrImportant = 256;
for(SIZE_T nIndex = 0; nIndex < 256; nIndex++)
{
    pBitmapInfo->bmiColors[nIndex].rgbRed = (BYTE) nIndex;
    pBitmapInfo->bmiColors[nIndex].rgbGreen = (BYTE) nIndex;
    pBitmapInfo->bmiColors[nIndex].rgbBlue = (BYTE) nIndex;
}
Bitmap.Attach(CreateDIBSection(DesktopDc, pBitmapInfo, 0, DIB_RGB_COLORS, NULL, 0));
ATLVERIFY(Bitmap);
BitmapDc.SelectBitmap(Bitmap);
////////////////////////////////////////////////
// This is what greys it out:
ATLVERIFY(BitBlt(BitmapDc, 0, 0, 320, 240, DesktopDc, 0, 0, SRCCOPY));
////////////////////////////////////////////////
ATLVERIFY(BitBlt(DesktopDc, 0, 240, 320, 240, BitmapDc, 0, 0, SRCCOPY));
like image 28
Roman R. Avatar answered Nov 08 '22 22:11

Roman R.