如何使用 GDI 通过像素强度将位图转换为灰度? [英] How to convert bitmap to grayscale by pixel intensity using GDI?

查看:25
本文介绍了如何使用 GDI 通过像素强度将位图转换为灰度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何使用 GDI(而非 GDI+)将 32 位位图转换为灰度的简单解决方案.有没有可能,例如通过更改位图的调色板或其他什么?

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 ?

当然,Delphi 中有很多例子,比如这个,但我正在寻找一个 WinAPI 函数,它可以在没有迭代的情况下执行此操作.

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.

推荐答案

我没有发现任何单个 GDI 函数执行此操作.正如大卫在评论中提到的,最简单的方法是扫描每一行并计算像素颜色.您正在寻找的可能是 luminance 公式.

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.

此公式几乎没有变体,在以下示例中,我使用了ITU,见 本文档 2.5.1 节.正如我在某处发现的那样,使用了这个公式,例如即使是著名的 Adob​​e Photoshop.以下代码示例仅支持并期望将 24 位像素格式位图作为输入:

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;

这篇关于如何使用 GDI 通过像素强度将位图转换为灰度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆