Recent

Author Topic: Setting TBitmap.PixelFormat is broken?  (Read 377 times)

domasz

  • Full Member
  • ***
  • Posts: 125
Setting TBitmap.PixelFormat is broken?
« on: November 30, 2022, 01:05:11 pm »
Very simple code. Reading a 24bit BMP, converting to 32bit and saving:

Code: Pascal  [Select][+][-]
  1.   Bmp := TBitmap.Create;
  2.   Bmp.LoadFromStream(Str);
  3.   Bmp.PixelFormat := pf32bit;
  4.   Bmp.SaveToFile('hejka.bmp');

Saved image is still 24bpp.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10485
  • FPC developer.
Re: Setting TBitmap.PixelFormat is broken?
« Reply #1 on: November 30, 2022, 04:05:46 pm »
Setting the pixel format afaik doesn't convert. It just prepares the buffer for the next (write to the image) action to be 32-bit sized.

domasz

  • Full Member
  • ***
  • Posts: 125
Re: Setting TBitmap.PixelFormat is broken?
« Reply #2 on: November 30, 2022, 04:08:23 pm »
So this needs to be changed because in Delphi this does convert and is used by a lot of graphic libraries and functions.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1917
Re: Setting TBitmap.PixelFormat is broken?
« Reply #3 on: November 30, 2022, 04:40:05 pm »
You could try this:

Code: Pascal  [Select][+][-]
  1. uses FPReadBMP, FPImage, FPWriteBMP;
  2. ....
  3. var
  4.   Image: TFPCustomImage;
  5.   Reader: TFPCustomImageReader;
  6.   Writer: TFPWriterBMP;
  7. begin
  8.   Image := TFPMemoryImage.Create(10, 10);
  9.   Reader := TFPReaderBMP.Create;
  10.   Writer := TFPWriterBMP.Create;
  11.   Image.LoadFromFile('test.bmp', Reader);
  12.   Writer.BitsPerPixel:=32;
  13.   Image.SaveToFile('test32.bmp', Writer);
  14.   Image.free;
  15.   Reader.free;
  16.   Writer.free;
  17. end;    

KodeZwerg

  • Hero Member
  • *****
  • Posts: 903
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting TBitmap.PixelFormat is broken?
« Reply #4 on: November 30, 2022, 05:16:05 pm »
Hello theo, thank you for your snippet, I just tried it, partially I can transform BPP from-to but now I run into an error where the created file is 0 byte.
Is there also a method to convert/adjust palette for destination BPP?

My try was to convert from 32bpp to 8bpp.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 903
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting TBitmap.PixelFormat is broken?
« Reply #5 on: November 30, 2022, 05:20:08 pm »
Setting the pixel format afaik doesn't convert. It just prepares the buffer for the next (write to the image) action to be 32-bit sized.
I did try what you was writing, unsuccessful.
I loaded 32bpp Bitmap and create a second TBitmap with 16bits PixelFormat, draw the source on and saved = 32bpp  %)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1917
Re: Setting TBitmap.PixelFormat is broken?
« Reply #6 on: November 30, 2022, 05:30:59 pm »
My try was to convert from 32bpp to 8bpp.
For 8bpp you need a palette (Color Quantization).
I don't know how to do this with fpImage.
It seems to work with BGRABitmap though.
See: https://forum.lazarus.freepascal.org/index.php/topic,61252.0.html

KodeZwerg

  • Hero Member
  • *****
  • Posts: 903
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Setting TBitmap.PixelFormat is broken?
« Reply #7 on: December 01, 2022, 10:24:54 am »
My try was to convert from 32bpp to 8bpp.
For 8bpp you need a palette (Color Quantization).
I don't know how to do this with fpImage.
It seems to work with BGRABitmap though.
See: https://forum.lazarus.freepascal.org/index.php/topic,61252.0.html
Thanks! That was what I needed for my little project to easy reduce/slim very big pictures! (yes, with loss of colors...)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

 

TinyPortal © 2005-2018