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.
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
, seethis 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: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).