Recent

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

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Scroll an image with Rect
« on: January 27, 2016, 09:49:27 pm »
Hello guys, I'm playing a little 'with the pictures and I wanted to figure out how to scroll an image, with the rect. That is, I'm trying to read the box to box the whole image.Just I do not understand where I'm wrong, instead of slide shows me always the same memory area. I do not understand. Who tells me where I'm wrong?

View attachment

Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #1 on: January 27, 2016, 10:15:00 pm »
So in the second image I'm going to put the part of image1 interests me. But in image2 going to put it on a white background in the same position as the original. I need to put the extracted in a TImage running from Top = 0 and Left = 0.

I do not understand where I'm wrong

Code: Pascal  [Select][+][-]
  1.             Self.Edit1.Text:=IntToStr(DaLeft) + ' - ' + IntToStr(DaTop) + ' - ' + IntToStr(LargRect) + ' - ' + IntToStr(AltezRect);
  2.             MyRect := Rect(DaLeft,DaTop,(LargRect),(AltezRect));
  3.             MyOther := Rect(DaLeft,DaTop,(LargRect),(AltezRect));
  4.  
  5.             Form1.Image2.Canvas.Clear;
  6.             Form1.Image2.Canvas.CopyRect(MyOther,Self.Image1.Canvas,MyRect);                                                            
  7.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #2 on: January 28, 2016, 12:13:46 am »
I solved with

            MyRect := Rect(DaLeft,DaTop,(LargRect+DaLeft),(AltezRect+DaTop));
            //MyOther := Rect(DaLeft,DaTop,(LargRect+DaLeft),(AltezRect+DaTop));
            MyOther := Rect(0,0,(LargRect),(AltezRect));


But another problem arose. I used to do the manipulations of TImage image to scan images. If I wanted to do it in bacground? With a console program? TImage not good because if it is not drawn on the form must be in error. Tips?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Scroll an image with Rect
« Reply #3 on: January 28, 2016, 12:37:54 am »
But another problem arose. I used to do the manipulations of TImage image to scan images. If I wanted to do it in bacground? With a console program? TImage not good because if it is not drawn on the form must be in error. Tips?
TBitmap or TPicture ?

You mentioned console mode,in case only compiling with FPC you can perhaps take a look at fcl-image package.

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: Scroll an image with Rect
« Reply #4 on: January 28, 2016, 01:37:23 am »
what sort of manipulations do you plan to do

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #5 on: January 28, 2016, 08:32:43 am »
I have to pull out of parts of the image that has a certain height and width and that has only in orange and black
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #6 on: January 28, 2016, 08:48:42 am »
And then a curiosity, if I use a TImage.CopyRect in a cycle works only if you use Application.ProcessMessage each cycle. I get it off not working. As if he was drawing within the TImage. Who explains to me why and how to fix?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Scroll an image with Rect
« Reply #7 on: January 28, 2016, 09:14:59 am »
And then a curiosity, if I use a TImage.CopyRect in a cycle works only if you use Application.ProcessMessage each cycle. I get it off not working. As if he was drawing within the TImage. Who explains to me why and how to fix?
If you 'loop' the copyrect in order to ''simulate' scrolling, it is most probably that the image is not updated when done with a copyrect. Processmessages 'allows' for the (re)paint messages to be send to the control.

You can probably 'force' that without using processmessages when doing either an .update, .repaint or .refresh

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #8 on: January 28, 2016, 12:43:48 pm »
Hello, I'm trying to do as I had suggested and use fcl-image.
But I have some questions, as shown by the code below required width TFPCustomImage with fixed values. To do that you change the loading LoadFromFile how? Does this automatically?

Then I would understand, I can not put the contents of the image in the canvas, I change the canvas, but if I want to reset the image to the canvas changed and then save it as I do?

Thanks

Code: Pascal  [Select][+][-]
  1.  
  2. procedure ElaboraSingolaImmagine(AFileName, ADestFileName: string);
  3.   procedure CopyPartOfImage(Origine, Destinazione: TCanvas; Top,Left,Height,Width: integer);
  4.   var
  5.      x, y,i,j: integer;
  6.   begin
  7.        i:=0;
  8.        //Destinazione.clear;
  9.        for y:=Top to (Top+Height) do
  10.        begin
  11.             j:=0;
  12.             for x:=Left to (Left+Width) do
  13.             begin
  14.                  Destinazione.Pixels[j,i]:=Origine.Pixels[x,y];
  15.                  Inc(j);
  16.             end;
  17.             Inc(i);
  18.        end;
  19.   end;
  20. var
  21.   image: TFPCustomImage;
  22.   reader: TFPCustomImageReader;
  23.   writer: TFPCustomImageWriter;
  24.   canvasOrig, canvasDest : TFPCustomCanvas;
  25. begin
  26.   Image := TFPMemoryImage.Create(450, 300);
  27.   Image.LoadFromFile(AFileName, Reader);
  28.  
  29.   CanvasOrig := TFPImageCanvas.Create (image);
  30.   CanvasDest := TFPImageCanvas.Create (image);
  31.  
  32.   CopyPartOfImage(CanvasOrig, CanvasDest, 0,0,100,200);
  33.  
  34.   Image.
  35.   Image.SaveToFile(ADestFileName);
  36.  
  37. end;
  38.  
  39.  
  40.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Scroll an image with Rect
« Reply #9 on: January 28, 2016, 05:09:54 pm »
Hi xinyiman,

Unfortunately it seems like i am a bit lost with my English skills, as i did not completely understood what you asked for exactly
(so please accept my apologies, in case i answered wrongly).

Quote
To do that you change the loading LoadFromFile how? Does this automatically?
Yes, FPimage works a bit differently from what we are used to with Lazarus.

AFAIK you need to define the dimensions yourself and this can't be done automatically (please, someone correct me when wrong).

Quote
Then I would understand, I can not put the contents of the image in the canvas, I change the canvas, but if I want to reset the image to the canvas changed and then save it as I do?
If i understood correctly (also by having looked at your code), then you seem to be unable to save your copied rectangle to a new file on disk ?

That is probably because you are using two canvases on the same image...
Code: [Select]
  CanvasOrig := TFPImageCanvas.Create (image);
  CanvasDest := TFPImageCanvas.Create (image);

I have modified your code a little, and the following seems to work for me:

Code: Pascal  [Select][+][-]
  1. program test1;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. Uses
  6.   FPImage, FPCanvas, FPImgCanv,
  7.   FPReadPNG, FPWritePNG;
  8.  
  9.  
  10. procedure CopyPartOfImage(Origine, Destinazione: TFPCustomCanvas; Top, Left, Height, Width: integer);
  11. var
  12.   x,y   : integer;
  13.   i,j   : integer;
  14. begin
  15.   i := 0;
  16.  
  17.   //Destinazione.clear;
  18.   for y := Top to (Top + Height) do
  19.   begin
  20.     j:=0;
  21.  
  22.     for x := Left to (Left + Width) do
  23.     begin
  24.       Destinazione.Colors[j, i] := Origine.Colors[x, y];
  25.       inc(j);
  26.     end;
  27.     inc(i);
  28.  
  29.   end;
  30. end;
  31.  
  32.  
  33.  
  34. procedure ElaboraSingolaImmagine(AFileName, ADestFileName: string);
  35. var
  36.   SourceImage       : TFPCustomImage;
  37.   SourceCanvas      : TFPCustomCanvas;
  38.  
  39.   DestinationImage  : TFPCustomImage;
  40.   DestinationCanvas : TFPCustomCanvas;
  41.  
  42.   reader            : TFPCustomImageReader;
  43.   writer            : TFPCustomImageWriter;
  44. begin
  45.   // Create in memory image
  46.   SourceImage       := TFPMemoryImage.Create(144, 144);
  47.   // Create appropiate reader
  48.   Reader            := TFPReaderPNG.Create;
  49.   // Read imagedata from file
  50.   SourceImage.LoadFromFile(AFileName, Reader);
  51.   // Create canvas for the image
  52.   SourceCanvas := TFPImageCanvas.Create(SourceImage);
  53.  
  54.   // Prepare destination
  55.   DestinationImage  := TFPMemoryImage.Create(144, 144);
  56.   DestinationCanvas := TFPImageCanvas.Create(DestinationImage);
  57.  
  58.   CopyPartOfImage(SourceCanvas, DestinationCanvas, 0, 0, 50, 50);
  59.  
  60.   Writer := TFPWriterPNG.Create;
  61.   DestinationImage.SaveToFile(ADestFileName, Writer);
  62.  
  63.   // Clean up
  64.   SourceImage.Free;
  65.   DestinationImage.Free;
  66.  
  67.   SourceCanvas.Free;
  68.   DestinationCanvas.Free;
  69.  
  70.   Reader.Free;
  71.   Writer.Free;
  72. end;
  73.  
  74.  
  75. //
  76. // GreenSwirl.png (144 x 144)
  77. //
  78.  
  79. begin
  80.   ElaboraSingolaImmagine('greenswirl.png', 'result1.png');
  81. end.
  82.  

It might perhaps not be entirely what you are looking for, but hopefully can aid you with your questions.

BTW: i noticed some other postings from you concerning images.
In case these questions all belong to the same project then it would perhaps be an idea to do some research on what would be the best option (read: graphics package or other helpful related functionality) that are better suited for helping you out with your project.

edit: a couple of "perhaps" too many -> rephrased and removed
« Last Edit: January 28, 2016, 05:27:00 pm by molly »

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #10 on: January 28, 2016, 08:54:48 pm »
Hello, thank you, I tested your code, but not working. The result is a png file completely black.

Are you sure that you work? I am attaching the program to fill in with the image. You tell me if it works for you?

Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #11 on: January 28, 2016, 09:08:19 pm »
I apologize, using another image png works.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #12 on: January 28, 2016, 09:26:51 pm »
Now I just have to figure out how to read the size of the images to read!

fcl-image as it runs? If you do not run it the only way it is through a TImage?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Scroll an image with Rect
« Reply #13 on: January 28, 2016, 09:46:12 pm »
Are you sure that you work?
Yes, it works for me.

I apologize, using another image png works.
Hmz, no idea why it works with some png images while not with other. Might perhaps be related to the colourdepth in the png.

fcl-image as it runs? If you do not run it the only way it is through a TImage?
No, as i pointed out you can also use a TPicture to load a graphics file and use TBitmap to manipulate your image (or use TBitmap class directly in case working with BMP files).

I'm sure there are lot of other approaches possible (*)

There is a distinct difference between not using a visible canvas and creating a program for FPC (console) only, hence why i mentioned fcl-image.

Besides the 'default' solutions provided by FPC/Lazarus there are a lot of other 3th party libraries that can aid you in working with graphics.

Here you can find some of those graphics libraries being listed.

Unfortunately, only you can decide if any of these libraries will be helpful for your project or not (or that it is perhaps better to use standard FCL/LCL components).

I don't have many experience with any of them to be able to give any advise on that topic.

If you are able to share with us what kind of functionality you are looking for then i'm confident someone else reading might be able to give you some direction. In case you are only toying around, then please try any of the libraries in the wiki, i'm sure some of those libraries will impress the living daylights out of you (*) ;-)

(*) Some of those libraries are very impressive and can take a while to get yourself familiar with.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Scroll an image with Rect
« Reply #14 on: January 28, 2016, 10:21:56 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.  
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018