Recent

Author Topic: "PixelFormat:=pf24bit" cannot work?  (Read 459 times)

lijinjie

  • New member
  • *
  • Posts: 6
"PixelFormat:=pf24bit" cannot work?
« on: January 22, 2021, 04:05:33 am »
 :oAfter I ran this code, the whole picture turned to black- -.  Are there any differences between Delphi and Lazarus when we use PixelFormat?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.     image1.Picture.Bitmap.PixelFormat:=pf24bit;
  4. end;

Handoko

  • Hero Member
  • *****
  • Posts: 4070
  • My goal: build my own game engine using Lazarus
Re: "PixelFormat:=pf24bit" cannot work?
« Reply #1 on: January 22, 2021, 05:10:37 am »
Why did you need to change the pixel format value?

Pixel format is used to inform programmer how the data being stored so one knows how to deal with it. You cannot change it directly and hope some 'magical' things to happen.

You may have better understanding how to use pixel format by studying the code here:
https://forum.lazarus.freepascal.org/index.php/topic,37242.msg253349.html#msg253349
« Last Edit: January 22, 2021, 05:19:10 am by Handoko »

lijinjie

  • New member
  • *
  • Posts: 6
Re: "PixelFormat:=pf24bit" cannot work?
« Reply #2 on: January 22, 2021, 07:51:51 am »
Why did you need to change the pixel format value?

Pixel format is used to inform programmer how the data being stored so one knows how to deal with it. You cannot change it directly and hope some 'magical' things to happen.

You may have better understanding how to use pixel format by studying the code here:
https://forum.lazarus.freepascal.org/index.php/topic,37242.msg253349.html#msg253349

Thank you for your reply.

I just moved from Delphi to Lazarus this week, So I see there are some differences between Delphi and Lazarus.

my project is to capture photos from the camera or from other devices, then analyze the image to do something.

so the picture pixel format some are 24bits, some are 32bit.  even 16 bits.
 
In Delphi, If we wanna change a bitmap pixel bits, just set Pixelformat to what we want.
------------------------
In Lazarus, seems we cannot do that.  :)
--------------------------------------------
So follow your information, I will check the pixel format of the image at first, then process them defferently.

Handoko

  • Hero Member
  • *****
  • Posts: 4070
  • My goal: build my own game engine using Lazarus
Re: "PixelFormat:=pf24bit" cannot work?
« Reply #3 on: January 22, 2021, 09:35:09 am »
In Delphi, If we wanna change a bitmap pixel bits, just set Pixelformat to what we want.

That's interesting. I do not use Delphi so I don't know it.

circular

  • Hero Member
  • *****
  • Posts: 3666
    • Personal webpage
Re: "PixelFormat:=pf24bit" cannot work?
« Reply #4 on: January 22, 2021, 10:21:51 pm »
In Lazarus, the PixelFormat does not work well. Basically, better not change it nor rely on it. Part of the problem is that bitmap data for display is not the same depending on the platform.

The easiest way, but not necessarily the fastest, is to convert your TBitmap into a TLazIntfImage, and then access its Colors property. See: https://wiki.freepascal.org/Fast_direct_pixel_access#TLazIntfImage

There are faster ways. In my view, the easiest is with my library TBGRABitmap, though I may be a bit biased. Anyway, you can load your image directly in TBGRABitmap and then access its pixels via ScanLine and it is always in TBGRAPixel format (a record with 8-bit RGBA values). See : https://wiki.freepascal.org/BGRABitmap_tutorial_4

There is also Graphics32 which is similar. See: https://documentation.help/Graphics32/ScanLine.htm

Another way, which is more complicated, is to access the raw image of the TBitmap. When you create a TBitmap, as far as I know, it is always 24-bit or 32-bit. You can access its content by looking at the raw image description. See: https://forum.lazarus.freepascal.org/index.php/topic,43001.msg300308.html#msg300308

If you load an TBitmap from a file, it may be in another format. So you could convert it to 24/32 bit by drawing it on a newly created TBitmap.
« Last Edit: January 22, 2021, 10:34:39 pm by circular »
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018