Recent

Author Topic: Scroll an image with Rect  (Read 8869 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #15 on: January 28, 2016, 11:02:33 pm »
I thank you really nice. But for now what I needed I was able to do it with your help.

I still have a thing to understand. I have to be able to go to change the color of a part of the image and put it white. But this code is not working. It puts the background all black and do not know why

Code: Pascal  [Select][+][-]
  1.    MyColor: TFPColor;
  2.    MyRed, MyBlue, MyGreen: byte;
  3.   MyCanvas: TFPCustomCanvas;
  4. .
  5. .
  6. .
  7.  
  8.                    MyColor.blue:=255;
  9.                    MyColor.green:=255;
  10.                    MyColor.red:=255;
  11.                    MyCanvas.Colors[i,j]:=MyColor;
  12.  

Solved with                    MyCanvas.Colors[i,j]:=FPColor(65535, 65535, 65535, 65535);

But I do not understand. The colors should not go from 0 to 255?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Scroll an image with Rect
« Reply #16 on: January 28, 2016, 11:20:30 pm »
But I do not understand. The colors should not go from 0 to 255?
Ah, you found that already  :-X

Nevermind, i post an updated example, which explained that in the comments. It searches for a particular color on the sourcecanvas and replaces it with another on the destination canvas while copying the pixels (and when the color matches).
Code: Pascal  [Select][+][-]
  1. program test2;
  2.  
  3.  
  4. {$MODE OBJFPC}{$H+}
  5.  
  6. Uses
  7.   FPImage, FPCanvas, FPImgCanv,
  8.   FPReadPNG, FPWritePNG;
  9.  
  10.  
  11. procedure CopyPartOfImage(Origine, Destinazione: TFPCustomCanvas; Top, Left, Height, Width: integer);
  12. var
  13.   x,y   : integer;
  14.   i,j   : integer;
  15.  
  16.   CheckForColor     : TFPColor; // The colour that we want to search for in order to replace
  17.   ChangeIntoColor   : TFPColor; // The 'replacement' colour
  18.   ThisPixelColor    : TFPColor; // To temporary store current pixel (x,y) color
  19. begin
  20.   i := 0;
  21.  
  22.   {
  23.    BIG NOTE: Each individual R,G,B and Alpha channel inside TFPCColor is
  24.              expressed in 16 bits = 1 word
  25.    
  26.    you: $aa $rr $gg $bb
  27.    me: $aaaa $rrrr $gggg $bbbb
  28.  
  29.    Where each letter represents a hexadecimal nibble.
  30.    So, instead of a colour occupying the usual widely used 4 bytes, TFPColor
  31.    stores a single colorvalue into 8 bytes.
  32.  }
  33.  
  34.  
  35.  
  36.   // Intialize rgb values for that colour that needs to be searched for and
  37.   // that we want to replace
  38.   // For this example we search for a white colour
  39.   CheckForColor.Red   := $FFFF;
  40.   CheckForColor.Green := $FFFF;
  41.   CheckForColor.Blue  := $FFFF;
  42.  
  43.   // Initialize rgb values for the replacement colour
  44.   // For this example we want to replace all green pixels to blue
  45.   ChangeIntoColor.Red   := $0000;
  46.   ChangeIntoColor.Green := $0000;
  47.   ChangeIntoColor.Blue  := $FFFF;   // Blue
  48.  
  49.  
  50.   //Destinazione.clear;
  51.   for y := Top to (Top + Height) do
  52.   begin
  53.     j:=0;
  54.  
  55.     for x := Left to (Left + Width) do
  56.     begin
  57.       // Temp. store coloor of curent pixel into ThisPixelColor
  58.       ThisPixelColor := Origine.Colors[x, y];
  59.  
  60.       // Now check if ThisPixelColor matches the colour that we want to replace
  61.       // Note: No comparison for the alpha channel (change accordingly to your wishes)
  62.       If (
  63.             ( ThisPixelColor.Red   = CheckForColor.Red   ) and
  64.             ( ThisPixelColor.Green = CheckForColor.Green ) and
  65.             ( ThisPixelColor.Blue  = CheckForColor.Blue  )
  66.          ) then
  67.       // Here it is pretty sure that current pixel matches the rgb values
  68.       // of the color that need to change so, let's accomodate the 'destination'
  69.       // color
  70.       begin
  71.         // First we copy the alpha channel, as we didn't checked for that value
  72.         // (in order to alllow all possible alpha values -> change according to
  73.         // your wishes)
  74.         ChangeIntoColor.Alpha := ThisPixelColor.Alpha;
  75.         Destinazione.Colors[j, i] := ChangeIntoColor;
  76.       end
  77.       else
  78.         // just copy the pixel colour from source to destination for all other
  79.         // pixels that did not match the color that we wanted to change.
  80.         Destinazione.Colors[j, i] := Origine.Colors[x, y];
  81.       inc(j);
  82.     end;
  83.     inc(i);
  84.  
  85.   end;
  86. end;
  87.  
  88.  
  89.  
  90. procedure ElaboraSingolaImmagine(AFileName, ADestFileName: string);
  91. var
  92.   SourceImage       : TFPCustomImage;
  93.   SourceCanvas      : TFPCustomCanvas;
  94.  
  95.   DestinationImage  : TFPCustomImage;
  96.   DestinationCanvas : TFPCustomCanvas;
  97.  
  98.   reader            : TFPCustomImageReader;
  99.   writer            : TFPCustomImageWriter;
  100. begin
  101.   // Create in-memory image
  102.   SourceImage       := TFPMemoryImage.Create(300, 273);
  103.   // Create appropiate reader
  104.   Reader            := TFPReaderPNG.Create;
  105.   // Read imagedata from file
  106.   SourceImage.LoadFromFile(AFileName, Reader);
  107.   // Create a canvas for the image
  108.   SourceCanvas := TFPImageCanvas.Create(SourceImage);
  109.  
  110.   // Prepare destination
  111.   DestinationImage  := TFPMemoryImage.Create(300, 273);
  112.   DestinationCanvas := TFPImageCanvas.Create(DestinationImage);
  113.  
  114.   CopyPartOfImage(SourceCanvas, DestinationCanvas, 0, 0, 50, 50);
  115.  
  116.   Writer := TFPWriterPNG.Create;
  117.   DestinationImage.SaveToFile(ADestFileName, Writer);
  118.  
  119.   // Clean up
  120.   SourceImage.Free;
  121.   DestinationImage.Free;
  122.  
  123.   SourceCanvas.Free;
  124.   DestinationCanvas.Free;
  125.  
  126.   Reader.Free;
  127.   Writer.Free;
  128. end;
  129.  
  130.  
  131. //
  132. // additive-colors.png (300 x 273)
  133. //
  134.  
  135. begin
  136.   ElaboraSingolaImmagine('additive-colors.png', 'result1.png');
  137. end.
  138.  

Yes, the TFPcolor is 16 bits per gun, see documentation.
Quote
The TFPColor record contains the Red, Green, Blue and Alpha channel values as left adjusted 16 bit words, with increasing intensity from 0 to $FFFF


edit: sorry for my terrible typo's in English comments inside the source-code -> i've made some improvements to them so you are (hopefully) able to translate better.
« Last Edit: January 28, 2016, 11:48:09 pm by molly »

 

TinyPortal © 2005-2018