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

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

问题描述

我在寻找如何将32位位图转换使用GDI(而不是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 ?

当然还有很多像这个在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函数这样做。最简单的方法,如大卫在他的评论中提到的,就是扫描每一行,并计算像素颜色。你所寻找的可能是 亮度 配方。

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。正如我在什么地方找到这个公式例如使用即使是由著名的Adobe Photoshop。下面code例如支持,预计只有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天全站免登陆