问题描述
我正在寻找如何使用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);
结束
结束
结束
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将位图转换为灰度像素强度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!