Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

delphi - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...