Olá um colega de outro fórum, mas também é usuário daqui, me passou esse código:
uRasterImageHelper.pas
unit uRasterImageHelper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, GraphType, LCLType;
type
{ TRasterImageHeper }
TRasterImageHeper = class helper for TRasterImage
public
function GetRawPtr(): PRawImage;
procedure ToGrayScale();
end;
procedure ToGrayScale(aImg : TRasterImage); //pq o helper ta bugado
implementation
procedure ToGrayScale(aImg : TRasterImage);
begin
aImg.ToGrayScale();
end;
function RGBToGray(R, G, B : Byte) : Byte;
var
i : Integer;
begin
i := Round(0.2989 * R + 0.5870 * G + 0.1140 * B);
if i > 255 then
Result := 255
else
Result := i;
end;
{ TRasterImageHeper }
function TRasterImageHeper.GetRawPtr : PRawImage;
begin
Result := GetRawImagePtr;
end;
procedure TRasterImageHeper.ToGrayScale;
var
x, y : Integer;
pRawPtr : PRawImage;
Bpp : Integer;
pRow, pPixel : PByte;
pix : PRGBAQuad absolute pPixel;
begin
Self.BeginUpdate(False);
try
pRawPtr := Self.GetRawPtr();
Bpp := pRawPtr^.Description.BitsPerPixel div 8;
pRow := pRawPtr^.Data;
for y := 0 to Height - 1 do
begin
pPixel := pRow;
for x := 0 to Width - 1 do
begin
//if pix^.Alpha <> 0 then
begin
pix^.Red := RGBToGray(pix^.Red, pix^.Green, pix^.Blue);
pix^.Green := pix^.Red;
pix^.Blue := pix^.Red;
end;
Inc(pPixel, Bpp);
end;
Inc(pRow, pRawPtr^.Description.BytesPerLine);
end;
finally
Self.EndUpdate(False);
end;
end;
end.
Muito mais rápido e que resolveu o meu problema.
Muito Obrigado Gilson Nunes Rodrigues