Recent

Author Topic: Fast pixel access  (Read 1741 times)

user5

  • Sr. Member
  • ****
  • Posts: 357
Fast pixel access
« on: November 28, 2019, 02:00:15 pm »
    In the code below lines 1 and 2 together do the same thing as line 3 except of course line 3 would be much slower if I used it in the code instead of lines 1 and 2. Line 1 uses a raw image fast pixel access method that is similar to BGRABitmap.
    Line 5 accesses pixels next to x and y and I was wondering if anyone knows how I could use the fast pixel method to accomplish the same thing but at a faster speed.
    Thanks and happy Thanksgiving morning!

   
Code: Pascal  [Select][+][-]
  1. for y := starty to (mybitmap.height - 1) do
  2.   begin
  3.    PixelPtr := PixelRowPtr;
  4.    for x := startx to (mybitmap.width - 1) do
  5.     begin
  6.      tempcolor := PixelPtr^; //line 1
  7.      tempcolor := SwapRedBlue(tempcolor); //line 2
  8.  
  9.      tempcolor := mybitmap.canvas.pixels[x,y]; //line 3
  10.  
  11.      newcolor := mybitmap.canvas.pixels[x + 1,y + 1]; //line 5
  12.     end;
  13.   end;


user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Fast pixel access
« Reply #1 on: November 28, 2019, 02:04:31 pm »
    Oops. Line 5 should have been: newcolor := mybitmap.canvas.pixels[x + 1,y]; //to access the pixel next to x

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Fast pixel access
« Reply #2 on: November 28, 2019, 03:44:11 pm »
Hi!

There is a page about fast direct pixel access in Lazarus with a lot of examples:

https://wiki.freepascal.org/Fast_direct_pixel_access

No thanksgiving here tomorrow - just another Friday for Future.

Winni

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Fast pixel access
« Reply #3 on: November 28, 2019, 07:31:32 pm »
    I checked out the page you mentioned and it led me to some other sites but it seems like not enough information is provided. For example, I found the following code but I can't get it to work. "colGreen" from what I understand is supposed to be a TFPColor but the compiler wouldn't let me define something like var newcolor:TFPColor. I put in all the correct headers.

 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. var
  3.   b: TBitmap;
  4.   t: TLazIntfImage;
  5. begin
  6.   b := TBitmap.Create;
  7.   try
  8.     b.LoadFromFile('test.bmp');
  9.     t := b.CreateIntfImage;
  10.  
  11.     // Read and/or write to the pixels
  12.     t.Colors[10,20] := colGreen; //This won't compile.
  13.  
  14.     b.LoadFromIntfImage(t);
  15.   finally
  16.     t.Free;
  17.     b.Free;
  18.   end;
  19. end;

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Fast pixel access
« Reply #4 on: November 28, 2019, 07:40:32 pm »
Just missing the "uses":

Code: Pascal  [Select][+][-]
  1. uses
  2.   fpImage,        // colGreen, TFPColor
  3.   IntfGraphics;   // TLazIntfImage
 

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Fast pixel access
« Reply #5 on: November 28, 2019, 07:48:58 pm »
    You are absolutely correct. I was missing FPImage in the "uses" clause. It's working now. The old slow pixel method took 7 seconds to fill a 1053x655 image with the color red but the TLazIntfImage method did it almost instantaneously. Wow! I wonder if there's anything even faster. Thanks a lot.

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Fast pixel access
« Reply #6 on: November 28, 2019, 07:52:33 pm »
    Does the TFPColor "colRed" equal "clRed" or do I need a conversion of some kind?

user5

  • Sr. Member
  • ****
  • Posts: 357
Re: Fast pixel access
« Reply #7 on: November 28, 2019, 08:08:13 pm »
    Okay... I found the conversion function which converts a TColor to a TFPColor so that: newcolor := TColorToFPColor(tempcolor); newcolor is a TFPColor and tempcolor is a TColor. I also found the function which converts a TFPColor to a TColor. Thanks again for taking the time to help me. I really appreciate it. This seems to be what I need.

Soner

  • Sr. Member
  • ****
  • Posts: 305
Re: Fast pixel access
« Reply #8 on: November 28, 2019, 08:52:23 pm »
Don't forget, TBitmap has Scanline property now. As someone the wikipage wrote, then tbitmap has not this property.
NOw, you can manipulate Tbitmap without TLazIntfImage faster.

 

TinyPortal © 2005-2018