Fastest Editable Bitmap For Internal Use Only (Not displayed on screen) lazarus / 39-Graphics ---------------------------------------------------------------------- Frog · 2018-10-12 19:14 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. ---------------------------------------------------------------------- dogriz · 2018-10-12 19:47 For bitmap manipulation you can check this link: http://www.efg2.com/Lab/index.html Old but great stuff ---------------------------------------------------------------------- Frog · 2018-10-12 22:14 Quote from: dogriz on October 12, 2018, 09:47:34 pmFor 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. ---------------------------------------------------------------------- circular · 2018-10-12 23:49 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? ---------------------------------------------------------------------- Frog · 2018-10-13 01:29 Quote from: circular on October 13, 2018, 01:49:11 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. ---------------------------------------------------------------------- guest58172 · 2018-10-13 03:00 You can use https://github.com/graphics32/graphics32/blob/master/Sourc e/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. ---------------------------------------------------------------------- BeanzMaster · 2018-10-13 11:23 Hi, for acessing to scanline in TBItmap with Lazarus Quick sample from scratch : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   TColorRGBA = Record  // becarefull under windows format is BGRA     r,g,b,a : Byte;   end;  PColorRGBA = ^TColorRGBA ; var  bmp: TBitmap;  // Take care bitmap is a 32bits  x,y : integer;  Pixel : PColorRGBA;  newColor : TColorRGBA;   With NewColor do // RED  begin    r:= 255;  g := 0;    b := 0;   a := 255;  end;  for y:=0 to bmp.Height-1 do  begin      Pixel :=  PColorRGBA(bmp.RawImage.GetLineStart(y));      for x:=0 to bmp.Width-1 do      begin        Pixel^ := newcolor;      inc(Pixel);      end;   end; if you want add scanline like in delphi you can implement a quick helper Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Type    TBitmapHelper = class helper for TBitmap   private      function getscanline(index : LongWord):PColorRGBA;   public      property Scanline[index:Integer]:PColorRGA read getScanline;   end;   function TBitmapHelper.GetScanLine(Index:LongWord) : PColorRGBA;  begin      result := PColorRGBA(Self.RawImage.GetLineStart(index));  end;    // usage :  Pixel :=  Bmp.ScanLine[0];  for y:=0 to bmp.Heighth-1 do  begin            for x:=0 to bmp.Width-1 do      begin                     Pixel^ := newcolor;         inc(Pixel);     end;   end; cheers ---------------------------------------------------------------------- marcov · 2018-10-13 13:12 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 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. ---------------------------------------------------------------------- BeanzMaster · 2018-10-13 14:43 Quote from: marcov on October 13, 2018, 03:12:58 pmBeanzMaster: 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  . 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  Thanks Marcov ---------------------------------------------------------------------- BeanzMaster · 2018-10-13 15:14 Hi Marcov can you share the fastcode unit movejohunit10 i not found anywhere on web. it's an update of http://fastcode.sourceforge.net/Fas tcodeFileDownloads/FastcodeMoveUnit.pas i'm right ? Thanks in advance best regards ---------------------------------------------------------------------- circular · 2018-10-17 09:50 Quote from: Frog on October 13, 2018, 03:29:32 amConvert 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function GetLightness(c: TBGRAPixel): word;  function BGRAToGrayscale(c: TBGRAPixel): TBGRAPixel; //if color channels are in sRGB (usually the case)  function BGRAToGrayscaleLinear(c: TBGRAPixel): TBGRAPixel; //if color channels are in linear RGB  function BGRAToColor(c: TBGRAPixel): TColor;  function BGRADiff(c1, c2: TBGRAPixel): byte; //color difference Note that it does not matter much if you don't diplay the image. ---------------------------------------------------------------------- marcov · 2018-10-17 11:40 Quote from: circular on October 17, 2018, 11:50:32 amNote 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. ---------------------------------------------------------------------- marcov · 2018-10-17 11:40 Quote from: BeanzMaster on October 13, 2018, 05:14:51 pmHi Marcov can you share the fastcode unit movejohunit10 i not found anywhere on web. it's an update of http://fastcode.sourceforge.net/FastcodeFileDownload s/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) ---------------------------------------------------------------------- Thaddy · 2018-10-17 17:50 Quote from: marcov on October 17, 2018, 01:40:52 pmBut 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. ---------------------------------------------------------------------- argb32 · 2018-10-17 19:43 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). ---------------------------------------------------------------------- Thaddy · 2018-10-17 19:47 Quote from: argb32 on October 17, 2018, 09:43:14 pmFrog, 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). of course...