Recent

Author Topic: TBitmap in RGBA also under Windows  (Read 1107 times)

Mathias

  • Jr. Member
  • **
  • Posts: 93
TBitmap in RGBA also under Windows
« on: June 21, 2024, 04:25:21 pm »
If you create a simple bitmap as follows, you get a 32-bit image under Linux and a 24-bit image under Windows.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var
  3.   bit: TBitmap;
  4. begin
  5.   bit := TBitmap.Create;
  6.   bit.SetSize(256, 256);
  7.   bit.Canvas.Brush.Color := clRed;
  8.   bit.Canvas.Rectangle(10, 10, 50, 50);
  9.  
  10.   WriteLn('OS: ', {$I %FPCTARGETOS%});
  11.   WriteLn('Pixel format: ', bit.PixelFormat);
  12.   WriteLn('bits per Pixel: ', bit.RawImage.Description.BitsPerPixel);
  13.   WriteLn('Red Shift: ', bit.RawImage.Description.RedShift);
  14.  
  15.   Canvas.Draw(50, 50, bit);
  16.   bit.Free;
  17. end;    
Output:
Code: Text  [Select][+][-]
  1. $ wine project1.exe
  2. OS: Win64
  3. Pixel format: pf24bit
  4. bits per Pixel: 24
  5. Red Shift: 16
  6. $ ./project1
  7. OS: Linux
  8. Pixel format: pf24bit
  9. bits per Pixel: 32
  10. Red Shift: 16
  11.  

So now my question, can you force the format when creating it to be RGA, BGR, RGBA, ABGR, etc.

It has to work somehow, if you load something with TImage.LoadFromFile, it is different depending on whether it is BMP or PNG with or without alpha.

Does anyone have an idea how to do this?

I have already tried
Bit.RawImage.Description.Init_BPP32_R8G8B8A8_BIO_TTB(w,h)

And another thing, why is the PixelFormat <> bitPerPixel in my example code under Windows?


Leledumbo

  • Hero Member
  • *****
  • Posts: 8776
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TBitmap in RGBA also under Windows
« Reply #1 on: June 25, 2024, 10:24:01 am »
So now my question, can you force the format when creating it to be RGA, BGR, RGBA, ABGR, etc.
Yes, PixelFormat property is read-write, not read only, so set that. The description also answers why the default is different.

Mathias

  • Jr. Member
  • **
  • Posts: 93
Re: TBitmap in RGBA also under Windows
« Reply #2 on: June 29, 2024, 03:18:31 pm »
Can you show me an example of how I can create an RGBA32 TBitmap?
What do I have to change in the following code to make it work?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var
  3.   bit: TBitmap;
  4. begin
  5.   bit := TBitmap.Create;
  6.   bit.SetSize(128, 128);
  7.  
  8.   bit.Canvas.Rectangle(16, 16, 128 - 16, 128 - 16);
  9.   Canvas.Draw(10, 10, bit);
  10.  
  11.   bit.Free;
  12. end;    

lainz

  • Hero Member
  • *****
  • Posts: 4621
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TBitmap in RGBA also under Windows
« Reply #3 on: June 29, 2024, 04:03:38 pm »
Can you show me an example of how I can create an RGBA32 TBitmap?
What do I have to change in the following code to make it work?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var
  3.   bit: TBitmap;
  4. begin
  5.   bit := TBitmap.Create;
  6.   bit.SetSize(128, 128);
  7.  
  8.   bit.Canvas.Rectangle(16, 16, 128 - 16, 128 - 16);
  9.   Canvas.Draw(10, 10, bit);
  10.  
  11.   bit.Free;
  12. end;    

Using BGRABitmap?

Code: Pascal  [Select][+][-]
  1. var
  2.   bit: TBGRABitmap;
  3.  
  4. bit := TBGRABitmap.Create(128, 128);
  5. bit.CanvasBGRA.Rectangle(16, 16, 128 - 16, 128 - 16);
  6. bit.SaveToFile('file_32_bit.png');
  7. bit.Free;
  8.  

 

TinyPortal © 2005-2018