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

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

问题描述

我正在寻找如何使用GDI(而不是GDI +)将32位位图转换为灰度的简单解决方案。有可能吗当然,Delphi中有很多例子,如这一个,但是我正在寻找一个WinAPI函数,这样就可以通过这一行进行迭代。

解决方案

我没有发现任何一个GDI函数这样做。像David在评论中提到的最简单的方法是扫描每一行并计算像素颜色。您正在寻找的可能是 亮度 公式。



这个公式有很少的变体,在下面的例子中,我使用了 国际电联 ,请参阅 本文档 a>第2.5.1节。当我在某处找到这个公式时,即使是众所周知的Adobe Photoshop。以下代码示例支持并期望只有24位像素格式位图作为输入:

 程序BitmapGrayscale(ABitmap:TBitmap); 
type
PPixelRec = ^ TPixelRec;
TPixelRec =打包记录
B:字节;
G:字节;
R:字节;
结束
var
X:整数;
Y:整数;
灰色:字节;
像素:PPixelRec;
begin
for Y:= 0 to ABitmap.Height - 1 do
begin
像素:= ABitmap.ScanLine [Y];
for X:= 0 to ABitmap.Width - 1 do
begin
Gray:= Round((0.299 * Pixel.R)+(0.587 * Pixel.G)+(0.114 *像素.B));
Pixel.R:=灰色
Pixel.G:= Gray;
Pixel.B:=灰色
Inc(Pixel);
结束
结束
结束


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, 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天全站免登陆