program test2;
{$MODE OBJFPC}{$H+}
Uses
FPImage, FPCanvas, FPImgCanv,
FPReadPNG, FPWritePNG;
procedure CopyPartOfImage(Origine, Destinazione: TFPCustomCanvas; Top, Left, Height, Width: integer);
var
x,y : integer;
i,j : integer;
CheckForColor : TFPColor; // The colour that we want to search for in order to replace
ChangeIntoColor : TFPColor; // The 'replacement' colour
ThisPixelColor : TFPColor; // To temporary store current pixel (x,y) color
begin
i := 0;
{
BIG NOTE: Each individual R,G,B and Alpha channel inside TFPCColor is
expressed in 16 bits = 1 word
you: $aa $rr $gg $bb
me: $aaaa $rrrr $gggg $bbbb
Where each letter represents a hexadecimal nibble.
So, instead of a colour occupying the usual widely used 4 bytes, TFPColor
stores a single colorvalue into 8 bytes.
}
// Intialize rgb values for that colour that needs to be searched for and
// that we want to replace
// For this example we search for a white colour
CheckForColor.Red := $FFFF;
CheckForColor.Green := $FFFF;
CheckForColor.Blue := $FFFF;
// Initialize rgb values for the replacement colour
// For this example we want to replace all green pixels to blue
ChangeIntoColor.Red := $0000;
ChangeIntoColor.Green := $0000;
ChangeIntoColor.Blue := $FFFF; // Blue
//Destinazione.clear;
for y := Top to (Top + Height) do
begin
j:=0;
for x := Left to (Left + Width) do
begin
// Temp. store coloor of curent pixel into ThisPixelColor
ThisPixelColor := Origine.Colors[x, y];
// Now check if ThisPixelColor matches the colour that we want to replace
// Note: No comparison for the alpha channel (change accordingly to your wishes)
If (
( ThisPixelColor.Red = CheckForColor.Red ) and
( ThisPixelColor.Green = CheckForColor.Green ) and
( ThisPixelColor.Blue = CheckForColor.Blue )
) then
// Here it is pretty sure that current pixel matches the rgb values
// of the color that need to change so, let's accomodate the 'destination'
// color
begin
// First we copy the alpha channel, as we didn't checked for that value
// (in order to alllow all possible alpha values -> change according to
// your wishes)
ChangeIntoColor.Alpha := ThisPixelColor.Alpha;
Destinazione.Colors[j, i] := ChangeIntoColor;
end
else
// just copy the pixel colour from source to destination for all other
// pixels that did not match the color that we wanted to change.
Destinazione.Colors[j, i] := Origine.Colors[x, y];
inc(j);
end;
inc(i);
end;
end;
procedure ElaboraSingolaImmagine(AFileName, ADestFileName: string);
var
SourceImage : TFPCustomImage;
SourceCanvas : TFPCustomCanvas;
DestinationImage : TFPCustomImage;
DestinationCanvas : TFPCustomCanvas;
reader : TFPCustomImageReader;
writer : TFPCustomImageWriter;
begin
// Create in-memory image
SourceImage := TFPMemoryImage.Create(300, 273);
// Create appropiate reader
Reader := TFPReaderPNG.Create;
// Read imagedata from file
SourceImage.LoadFromFile(AFileName, Reader);
// Create a canvas for the image
SourceCanvas := TFPImageCanvas.Create(SourceImage);
// Prepare destination
DestinationImage := TFPMemoryImage.Create(300, 273);
DestinationCanvas := TFPImageCanvas.Create(DestinationImage);
CopyPartOfImage(SourceCanvas, DestinationCanvas, 0, 0, 50, 50);
Writer := TFPWriterPNG.Create;
DestinationImage.SaveToFile(ADestFileName, Writer);
// Clean up
SourceImage.Free;
DestinationImage.Free;
SourceCanvas.Free;
DestinationCanvas.Free;
Reader.Free;
Writer.Free;
end;
//
// additive-colors.png (300 x 273)
//
begin
ElaboraSingolaImmagine('additive-colors.png', 'result1.png');
end.