问题描述
我在寻找如何将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函数这样做。最简单的方法,如大卫在他的评论中提到的,就是扫描每一行,并计算像素颜色。你所寻找的可能是,看到的部分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通过像素强度为灰度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!