Hello!
It is required to calculate the illumination of a point in a TPNGImage object, taking into account transparency. I wrote the following algorithm:
function GetBrightness(const AImage: TPNGImage; AX, AY: Integer): Integer;
var
LColor, LTransparentColor: TColor;
r, g, b, alpha: Byte;
tr, tg, tb: Byte;
LDstAlpha: pByteArray;
begin
LColor := AImage.Pixels[AX, AY];
r := Byte(LColor);
g := Byte(LColor shr 8);
b := Byte(LColor shr 16);
if AImage.Transparent and (LColor <> 0) then
begin
if (AImage.Header.ColorType in [COLOR_RGBALPHA, COLOR_GRAYSCALEALPHA]) then
begin
LDstAlpha := AImage.AlphaScanline[AY];
if Assigned(LDstAlpha) then
begin
alpha := LDstAlpha[AX];
r := Byte(Round(r * alpha/255));
g := Byte(Round(g * alpha/255));
b := Byte(Round(b * alpha/255));
end;
end else
if AImage.Header.ColorType = COLOR_PALETTE then
begin
LTransparentColor := AImage.TransparentColor;
tr := Byte(LTransparentColor);
tg := Byte(LTransparentColor shr 8);
tb := Byte(LTransparentColor shr 16);
r := Byte(Round(r * tr/255));
g := Byte(Round(g * tg/255));
b := Byte(Round(b * tb/255));
end;
end;
Result := Round(0.3*r + 0.59*g + 0.11*b);
end;
Please analyze my algorithm for errors.
P.S. Unfortunately, in my test base there are no images with TransparentColor - only with alpha channel.
Alexander.