[英]How create shadow effect in pf32bit bitmap?
I found this code that works fine with pf24bit
.我发现这段代码适用于pf24bit
。 How change to work with pf32bit
(or any other PixelFormat
)?如何更改以使用pf32bit
(或任何其他PixelFormat
)?
Edit :编辑:
For example if you get a screenshot, this code doesn't apply effect in full bitmap width.例如,如果您获得屏幕截图,则此代码不适用于完整的 bitmap 宽度。 To bitmap height it's ok.到 bitmap 高度没问题。
This is straightforward.这很简单。
A 24-bpp image has format BGR and a 32-bpp image has format BGRA, where the alpha component is typically not used for anything. 24-bpp 图像具有 BGR 格式,32-bpp 图像具有 BGRA 格式,其中 alpha 分量通常不用于任何事情。 To blacken the bitmap, we simply ignore the A component.为了使 bitmap 变黑,我们只需忽略 A 分量。
So, 24 bits per pixel:因此,每像素 24 位:
procedure FadeBitmap24(ABitmap: TBitmap);
type
PRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[Word] of TRGBTriple;
var
SL: PRGBTripleArray;
y: Integer;
x: Integer;
begin
ABitmap.PixelFormat := pf24bit;
for y := 0 to ABitmap.Height - 1 do
begin
SL := ABitmap.ScanLine[y];
for x := 0 to ABitmap.Width - 1 do
with SL[x] do
begin
rgbtRed := rgbtRed div 2;
rgbtGreen := rgbtGreen div 2;
rgbtBlue := rgbtBlue div 2;
end;
end;
end;
32 bits per pixel:每像素 32 位:
procedure FadeBitmap32(ABitmap: TBitmap);
type
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array[Word] of TRGBQuad;
var
SL: PRGBQuadArray;
y: Integer;
x: Integer;
begin
ABitmap.PixelFormat := pf32bit;
for y := 0 to ABitmap.Height - 1 do
begin
SL := ABitmap.ScanLine[y];
for x := 0 to ABitmap.Width - 1 do
with SL[x] do
begin
rgbRed := rgbRed div 2;
rgbGreen := rgbGreen div 2;
rgbBlue := rgbBlue div 2;
end;
end;
end;
This is a direct translation.这是直接翻译。
But both versions can be written more elegantly:但是这两个版本都可以写得更优雅:
procedure FadeBitmap24(ABitmap: TBitmap);
begin
ABitmap.PixelFormat := pf24bit;
for var y := 0 to ABitmap.Height - 1 do
begin
var SL := PRGBTriple(ABitmap.ScanLine[y]);
for var x := 0 to ABitmap.Width - 1 do
begin
SL.rgbtRed := SL.rgbtRed div 2;
SL.rgbtGreen := SL.rgbtGreen div 2;
SL.rgbtBlue := SL.rgbtBlue div 2;
Inc(SL);
end;
end;
end;
procedure FadeBitmap32(ABitmap: TBitmap);
begin
ABitmap.PixelFormat := pf32bit;
for var y := 0 to ABitmap.Height - 1 do
begin
var SL := PRGBQuad(ABitmap.ScanLine[y]);
for var x := 0 to ABitmap.Width - 1 do
begin
SL.rgbRed := SL.rgbRed div 2;
SL.rgbGreen := SL.rgbGreen div 2;
SL.rgbBlue := SL.rgbBlue div 2;
Inc(SL);
end;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.