function TForm1.GetPixelSafe(bmp: TBGRABitmap; x, y: integer): TBGRAPixel;
begin
if (x >= 0) and (x < bmp.Width) and (y >= 0) and (y < bmp.Height) then
Result := bmp.GetPixel(x, y)
else
Result := BGRA(0, 0, 0, 0); // Transparent pixels hors limites !!!
end;
function TForm1.ColorsEqual(c1, c2: TBGRAPixel): boolean;
begin
Result := (c1.red = c2.red) and (c1.green = c2.green) and (c1.blue = c2.blue);
end;
function TForm1.Scale2xFilter(source: TBGRABitmap): TBGRABitmap;
var
x, y: integer;
A, B, C, D, E, F, G, H, I: TBGRAPixel;
E0, E1, E2, E3: TBGRAPixel;
begin
Result := TBGRABitmap.Create(source.Width * 2, source.Height * 2);
for y := 0 to source.Height - 1 do
for x := 0 to source.Width - 1 do
begin
// recupe 9 pixels voisins en x,y -1 +1 etc
A := GetPixelSafe(source, x-1, y-1);
B := GetPixelSafe(source, x, y-1);
C := GetPixelSafe(source, x+1, y-1);
D := GetPixelSafe(source, x-1, y);
E := GetPixelSafe(source, x, y); // Pixel au milieu
F := GetPixelSafe(source, x+1, y);
G := GetPixelSafe(source, x-1, y+1);
H := GetPixelSafe(source, x, y+1);
I := GetPixelSafe(source, x+1, y+1);
// Algorithme Scale2x de m*rde !
if (ColorsEqual(D, B) and not ColorsEqual(D, H) and not ColorsEqual(B, F)) then
E0 := D
else
E0 := E;
if (ColorsEqual(B, F) and not ColorsEqual(B, D) and not ColorsEqual(F, H)) then
E1 := F
else
E1 := E;
if (ColorsEqual(D, H) and not ColorsEqual(D, B) and not ColorsEqual(H, F)) then
E2 := D
else
E2 := E;
if (ColorsEqual(H, F) and not ColorsEqual(H, D) and not ColorsEqual(F, B)) then
E3 := F
else
E3 := E;
// replace les 4 pixel
Result.SetPixel(x*2, y*2, E0);
Result.SetPixel(x*2+1, y*2, E1);
Result.SetPixel(x*2, y*2+1, E2);
Result.SetPixel(x*2+1, y*2+1, E3);
end;
end;