目前,我循环Canvas.Pixels[]属性并读取画布上的每个像素,以交换红色/蓝色字节(出于特定原因)。但是,平均每幅图片需要2秒,我需要转换的图片超过8,000张(一夜之间)。我知道我可以使用ScanLine的一种方法来更快地完成这个任务,但是我对ScanLine一无所知--它的编码水平比我所习惯的要低得多。实现这一目标的最快方法是什么?我愿意等一段时间才能完成这一切,但如果我能把这段时间切成两半或更多,那就太好了。
现在,这是我使用的程序:
procedure SwapBytes(var Bmp: TBitmap);
var
X, Y: Integer;
R, G, B: Byte;
C: TColor;
begin
for Y := 0 to Bmp.Height - 1 do begin
for X := 0 to Bmp.Width - 1 do begin
C:= Bmp.Canvas.Pixels[X,Y];
R:= GetRValue(C);
G:= GetGValue(C);
B:= GetBValue(C);
Bmp.Canvas.Pixels[X,Y]:= RGB(B, G, R)
end;
end;
end;添加注意:最初转换超过8,000张图片是我为什么需要这个的第一步。然而,我也将使用相同的东西在我们的软件自动转换任何图像现场,视需要。所以第三方转换器不能工作,因为我不能把这个分发给客户。
发布于 2012-01-19 19:32:50
我会尝试下面这样的方法。此版本仅适用于24位位图:
procedure SwapRedBluePixels(ABitmap: TBitmap);
var
X: Integer;
Y: Integer;
Red: Byte;
Pixel: PRGBTriple;
begin
// check for the bit depth, it must be 24-bit if you use PRGBTriple pointer
// for line scan; if it wouldn't the iterated line pointers would point to
// another place in the memory
if ABitmap.PixelFormat <> pf24bit then
begin
ShowMessage('Your bitmap has color depth different from 24-bit');
Exit;
end;
// iterate through the image vertically
for Y := 0 to (ABitmap.Height - 1) do
begin
// access the line of pixels and get the pointer to the first pixel of
// that line
Pixel := ABitmap.ScanLine[Y];
// iterate through the scanned line pixels horizontally
for X := 0 to (ABitmap.Width - 1) do
begin
// store the pixel's red channel value
Red := Pixel.rgbtRed;
// modify the pixel's red channel value
Pixel.rgbtRed := Pixel.rgbtBlue;
// modify the pixel's blue channel value
Pixel.rgbtBlue := Red;
// increment to get the next pixel pointer of the scanned line
Inc(Pixel);
end;
end;
end;更新2:
此版本适用于24位和32位位图:
procedure SwapRedBluePixels(ABitmap: TBitmap);
var
X: Integer;
Y: Integer;
Red: Byte;
Size: Integer;
Pixels: PByteArray;
begin
// check the color depth and set the size of the pixel arrangement
case ABitmap.PixelFormat of
pf24bit: Size := SizeOf(TRGBTriple);
pf32bit: Size := SizeOf(TRGBQuad);
else
// if the image is not 24-bit or 32-bit go away
begin
ShowMessage('Your bitmap has unsupported color depth!');
Exit;
end;
end;
// iterate through the image vertically
for Y := 0 to (ABitmap.Height - 1) do
begin
// access the line of pixels and get the pointer to the first pixel of
// that line
Pixels := ABitmap.ScanLine[Y];
// iterate through the scanned line pixels horizontally
// for 24-bit images the pixels are stored like
// B -> G -> R -> B -> G -> R etc.
// for 32-bit images the pixels are stored like
// B -> G -> R -> A -> B -> G -> R -> A etc.
// so we can simply use e.g. byte array and iterate through
// it, if we have 24-bit image, we have to read each element,
// if 32-bit we have to skip the alpha (reserved) channel
for X := 0 to (ABitmap.Width - 1) do
begin
// store the pixel's red channel value
Red := Pixels^[(X * Size) + 2];
// modify the pixel's red channel value
Pixels^[(X * Size) + 2] := Pixels^[(X * Size)];
// modify the pixel's blue channel value
Pixels^[(X * Size)] := Red;
end;
end;
end;https://stackoverflow.com/questions/8931784
复制相似问题