Recent

Author Topic: Fastest way for fill pixels of TBitMap from array of Bytes of RGB colors  (Read 8162 times)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
You should use "packed" for TBitmapPixel. Otherwise it will mean 32-bit, or even 64-bit memory alignment on 64-bit compilers. Scanline specifically means continuous series of pixel data with no empty gaps between records.
Code: Pascal  [Select][+][-]
  1.   TBitmapPixel = packed record
  2.     B, G, R {$IFDEF UNIX}, A {$ENDIF}: UInt8;
  3.   end;
  4.  
  5. type
  6.   // row of pixels
  7.   PBitmapLine = ^TBitmapLine;
  8.   TBitmapLine = array [UInt16] of TBitmapPixel;
« Last Edit: April 11, 2019, 07:56:54 am by User137 »

flowCRANE

  • Hero Member
  • *****
  • Posts: 937
You must use "packed" for TBitmapPixel. Otherwise it will mean 32-bit, or even 64-bit memory alignment on 64-bit compilers.

Well, unfortunately, but you are wrong.

These data types are used in my game and ensure correct bitmap processing with default data alignment settings on Windows systems. Everything is fine, I do not need to use extra packing for record and array, just see the attachment.

Quote
Scanline specifically means continuous series of pixel data with no empty gaps between records.

If I did not know such basics, I would not be able to write any subroutine that processes bitmaps using ScanLine. And as my game shows, it is quite the opposite. 8-)
« Last Edit: April 11, 2019, 04:36:19 am by furious programming »
Lazarus 4.2 with FPC 3.2.2, Windows 11 — all 64-bit

Working solo on a top-down retro-style action/adventure game (pixel art), programming the engine from scratch, using Free Pascal and SDL3.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Sure, got a bit lucky in this case i guess because all of the types are the same. I didn't know compiler's special rules.

This would sizeof() result 4 bytes:
Code: Pascal  [Select][+][-]
  1.   TTest = Record
  2.     a: UInt8;
  3.     b: UInt16;
  4.   end;

Thausand

  • Sr. Member
  • ****
  • Posts: 499
I think User137 agree

Is lucky because depend platform/arch. if work windows then not work other. Not have think endian so some platform record BGR pixel is around RGB. align is other problem platform. And DDB confusing if some platform. Example 5-5-6 pixel. I think many video-buffer now 64 bit pixel and more confuse.

Maybe furious programmer read createcompatibledc ? I think is also LCL widget function.

Problem yurkad i think is other problem. If move palette then is fast but depend show device. if device 24 or 32 bit pixel then bitmap convert palette to pixel and slow. Maybe can do one time for screen then fast and any other move palette and then still fast. Depend and not tell if faster always.

Palette is no fast when to many color. That why bitmap not have palette > 16 bit pixel. Move palette or move pixel then make same (or more) time and use same (or more) memory.
« Last Edit: April 11, 2019, 11:04:38 am by Thausand »

Thaddy

  • Hero Member
  • *****
  • Posts: 18950
  • Glad to be alive.
This would sizeof() result 4 bytes:
Code: Pascal  [Select][+][-]
  1.   TTest = Record
  2.     a: UInt8;
  3.     b: UInt16;
  4.   end;
Yes, but as indicated before
Code: Pascal  [Select][+][-]
  1.   TTest = Packed Record
  2.     a: UInt8;
  3.     b: UInt16;
  4.   end;
That will result in three bytes! It is better to expand such a record to 32 bit, though, to store alpha channel as well.
« Last Edit: April 11, 2019, 11:38:47 am by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12768
  • FPC developer.
No, ScanLine itself is portable between platforms, but the pixel format between platforms varies. Under Windows, the TBitmap instance created in memory stores 24-bit pixels in BGR order. Under Unixes, in-memory bitmap stores 32-bit pixels in BGRA order. In addition, the pixel format may change eg when a bitmap is loaded from a file or from a stream.

Isn't this simply a default pixelformat property ?   pf8bit  etc.

Best is if code either simply adapts to the pixelformat property OR enforces the format of all tbitmaps.




 

TinyPortal © 2005-2018