Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: Frog on October 12, 2018, 09:14:57 pm

Title: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: Frog on October 12, 2018, 09:14:57 pm
Hello, I need some advice.

My application needs a fast editable pixel level bitmap that will never be displayed on the screen. The bitmap will be loaded from a *.bmp file then manipulated under program control. I have read through the wiki documents, but it isn't really clear which is the fastest when screen writes will not happen.

I expect to have to process 50 - 100 ARGB8888 bitmaps per second. The entire bitmap will be rewritten, with the exception of the alpha channels.

This is a Linux application. The bmp file will be loaded from /dev/shm, not disk.

Edit: Forgot to mention that Bitmaps will be 1920 x1080.

Thank you.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not dispalyed on screen)
Post by: dogriz on October 12, 2018, 09:47:34 pm
For bitmap manipulation you can check this link: http://www.efg2.com/Lab/index.html
Old but great stuff :)
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not dispalyed on screen)
Post by: Frog on October 13, 2018, 12:14:35 am
For bitmap manipulation you can check this link: http://www.efg2.com/Lab/index.html
Old but great stuff :)

Thanks, I had forgotten about that site. I'll see if it helps.


Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: circular on October 13, 2018, 01:49:11 am
This link seems more like Delphi scanline.

In Lazarus, it is not available in TBitmap objects.

There is my library BGRABitmap that adds ScanLine support, however is it the fastest for what you want to do, I don't know.

What do you want to do with the image?
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: Frog on October 13, 2018, 03:29:32 am

What do you want to do with the image?


Convert it to gray scale, process it for motion against a reference image, then incorporate it into the reference image.

I should note that this image is a copy of the original. The original will be stored and/or displayed if motion is detected, hence no need to display this image.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: guest58172 on October 13, 2018, 05:00:29 am
You can use https://github.com/graphics32/graphics32/blob/master/Source/GR32.pas#L660 Bitmap object (which however must be very similar to default LCL TBitmap since there are no many ways to implement a bitmap). Note that a bitmap is still data only. i.e nothing is displayed until you paint it on a canvas.

For speed you can divide the task in threads but that depends on wether the bmps come frame per frame or in bulk in the shared mem.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: BeanzMaster on October 13, 2018, 01:23:38 pm
Hi, for acessing to scanline in TBItmap with Lazarus

Quick sample from scratch :

Code: Pascal  [Select][+][-]
  1. type
  2.    TColorRGBA = Record  // becarefull under windows format is BGRA
  3.      r,g,b,a : Byte;
  4.    end;
  5.   PColorRGBA = ^TColorRGBA ;
  6.  
  7. var
  8.   bmp: TBitmap;  // Take care bitmap is a 32bits
  9.   x,y : integer;
  10.   Pixel : PColorRGBA;
  11.   newColor : TColorRGBA;
  12.  
  13.   With NewColor do // RED
  14.   begin
  15.     r:= 255;
  16.     g := 0;
  17.     b := 0;
  18.    a := 255;
  19.   end;
  20.   for y:=0 to bmp.Height-1 do
  21.   begin
  22.       Pixel :=  PColorRGBA(bmp.RawImage.GetLineStart(y));
  23.       for x:=0 to bmp.Width-1 do
  24.       begin
  25.         Pixel^ := newcolor;
  26.         inc(Pixel);
  27.      end;
  28.   end;

if you want add scanline like in delphi you can implement a quick helper

Code: Pascal  [Select][+][-]
  1. Type
  2.    TBitmapHelper = class helper for TBitmap
  3.    private
  4.       function getscanline(index : LongWord):PColorRGBA;
  5.    public
  6.       property Scanline[index:Integer]:PColorRGA read getScanline;
  7.    end;
  8.  
  9.   function TBitmapHelper.GetScanLine(Index:LongWord) : PColorRGBA;
  10.   begin
  11.       result := PColorRGBA(Self.RawImage.GetLineStart(index));
  12.   end;
  13.  
  14.   // usage :
  15.   Pixel :=  Bmp.ScanLine[0];
  16.   for y:=0 to bmp.Heighth-1 do
  17.   begin      
  18.       for x:=0 to bmp.Width-1 do
  19.       begin              
  20.         Pixel^ := newcolor;
  21.          inc(Pixel);
  22.      end;
  23.   end;

cheers



   
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: marcov on October 13, 2018, 03:12:58 pm
BeanzMaster: nice trick that helper, filling with an uniform color is better done with a filldword per scanline though. (but I assume that was just a demonstration ).

I made my own simple type for machine vision purposes, and later upgraded it to generics. Using generics I specialize it for 8,16,32 bit.

Some older source is in
here (http://www.stack.nl/~marcov/standalonegllaz.zip)

Note that the code might require generics.collections to run. (part of trunk, separate download for fpc 3.0.4)

Shortcuts is that I avoid a lot of corner cases (like imagewidths not dividable by 4), but on the plus side many simple operations are inlined, and the pixel data block is 32/64 byte aligned which keeps SSE routines simple.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: BeanzMaster on October 13, 2018, 04:43:40 pm
BeanzMaster: nice trick that helper, filling with an uniform color is better done with a filldword per scanline though. (but I assume that was just a demonstration ).

Shortcuts is that I avoid a lot of corner cases (like imagewidths not dividable by 4), but on the plus side many simple operations are inlined, and the pixel data block is 32/64 byte aligned which keeps SSE routines simple.

Yes it's from scratch  :D. I'm often using this trick for TBitmap when i need to do some things quickly. It's not the fastest way but work very well.
One other thing is to check the rawimage descritption to say what exactly the right pixel format (rgba, rgb, bgra argb....)

For another project i manage image data directly with a memory Pointer as DWord (instead of PBYte. I'm only manipulate bitmap as 32, it's fastest no need conversion when bitmap must be displayed, just at loading from bmp 8bit for example) I have some functions for converting pixel formats. I prefer using and advanced record rather than juste a DWord (like you do with RGBA record) for manage color it's more easy and it have no impact if you want to code some functions with SSE. I take a look quickly to you code, using generic it's cool, (i'm not use generics fluently). Using OpenGL, really improve display and you not need to take care of the color component position following the os (RGBA<>BGRA). I take a look more deeply in your code, it seem very interesting  8-)

Thanks Marcov  ;)

Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: BeanzMaster on October 13, 2018, 05:14:51 pm
Hi Marcov can you share the fastcode unit movejohunit10 i not found anywhere on web. it's an update of http://fastcode.sourceforge.net/FastcodeFileDownloads/FastcodeMoveUnit.pas i'm right ?

Thanks in advance

best regards
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: circular on October 17, 2018, 11:50:32 am
Convert it to gray scale, process it for motion against a reference image, then incorporate it into the reference image.

I should note that this image is a copy of the original. The original will be stored and/or displayed if motion is detected, hence no need to display this image.
As others have mention, there are some tricks to access color components with TBitmap.

With BGRABitmap, no trick needed and you cannot confuse channels. See for example:
http://wiki.lazarus.freepascal.org/BGRABitmap_tutorial_4

Also there are color functions that you might find useful :
Code: Delphi  [Select][+][-]
  1.   function GetLightness(c: TBGRAPixel): word;
  2.   function BGRAToGrayscale(c: TBGRAPixel): TBGRAPixel; //if color channels are in sRGB (usually the case)
  3.   function BGRAToGrayscaleLinear(c: TBGRAPixel): TBGRAPixel; //if color channels are in linear RGB
  4.   function BGRAToColor(c: TBGRAPixel): TColor;
  5.   function BGRADiff(c1, c2: TBGRAPixel): byte; //color difference

Note that it does not matter much if you don't diplay the image.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: marcov on October 17, 2018, 01:40:06 pm
Note that it does not matter much if you don't diplay the image.

Or do such things in a shader.  I do have a false color version of that code that shows a 8-bit grayscale texture using   false color in opengl.

And a shader that interprets a 32-bit texture as cmyk.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: marcov on October 17, 2018, 01:40:52 pm
Hi Marcov can you share the fastcode unit movejohunit10 i not found anywhere on web. it's an update of http://fastcode.sourceforge.net/FastcodeFileDownloads/FastcodeMoveUnit.pas i'm right ?

Afaik for FPC you can just use the native move(). For Delphi, the native move used to be very slow, which is why I use the fastcode unit.

But that is probably a bit outdated, and never fixed, because my current framework is mostly zero copy. (the topdown property)

Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: Thaddy on October 17, 2018, 07:50:34 pm
But that is probably a bit outdated, and never fixed, because my current framework is mostly zero copy. (the topdown property)
Yes, not probably, but outdated. Anything after SSE2 is not used. That doesn't mean the move is slow. MMX allowed for pretty fast moves and is used in fastcode.
It is also not a cross platform solution because fastcode is Intel only.
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: argb32 on October 17, 2018, 09:43:14 pm
Frog, why are you need a bitmap class? Just load your data into an array and manipulate as you wish. The array can be of dword or of some argb record. This will be fastest (potentially).
Title: Re: Fastest Editable Bitmap For Internal Use Only (Not displayed on screen)
Post by: Thaddy on October 17, 2018, 09:47:22 pm
Frog, why are you need a bitmap class? Just load your data into an array and manipulate as you wish. The array can be of dword or of some argb record. This will be fastest (potentially).
:D ;D of course...
TinyPortal © 2005-2018