Recent

Author Topic: About the method of generating 8-bit indexed bitmaps.  (Read 2757 times)

TRon

  • Hero Member
  • *****
  • Posts: 2540
Re: About the method of generating 8-bit indexed bitmaps.
« Reply #15 on: April 27, 2023, 10:49:27 pm »
Thank you, too. Amazing how powerful the fcl-image library is.
you're welcome. And it is indeed very powerful. Shame that nobody added more functionality (as done f.e. for  bgrabitmap).

Quote
And almost nobody knows about it...
It was my little secret... now you've opened Pandora's box  ;D (but there will still be people insisting on expanding MS$ bound TBitmap which is ofc fine but it beats me why).

elioenaishalom

  • New Member
  • *
  • Posts: 28
Re: About the method of generating 8-bit indexed bitmaps.
« Reply #16 on: January 16, 2024, 02:05:27 pm »
Color quantization and dithering is in this old forum post: https://forum.lazarus.freepascal.org/index.php/topic,13468.msg70495.

Adapted the code in the attached small demo which converts the famous Lenna test image (24bpp png) to a paletted 8bpp bmp.

Windows 10 latest update and Lazarus 2.04 used.
I adapted the code and in fact "lenna-8bpp.bmp" was recorded as 8 bits, but with 7643 RGB colors.
My intention is to reduce 24-bit palettes to 256 colors - I have seen many discussions too complicated for my skills. Can anyone help?
Attachment complete project used to process "Lenna.png".

Thank you very much for some attention.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: About the method of generating 8-bit indexed bitmaps.
« Reply #17 on: January 17, 2024, 11:05:26 pm »
The problem is that TBitmap always seems to expand the pixelformat to 24 or 32 bits per pixel. You can see this when you query the Pixelformat after loading the image:
Code: Pascal  [Select][+][-]
  1. uses
  2.   TypInfo;
  3. ...
  4.   BitMap := TBitMap.Create;
  5.   BitMap.LoadFromFile('lenna-8bpp.bmp');
  6.   Caption := 'Pixelformat = ' + GetEnumName(TypeInfo(TPixelFormat), ord(BitMap.PixelFormat));
  7.  
  8.   Lista := TStringList.Create;
  9. ...

If you process the color-reduced image by means of the more primitive fcl-image classes, the pixel format is retained:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.RecoverPallete;
  2. var
  3.   img: TFpMemoryImage;
  4.   ListA: TStringList;
  5.   x, y: Integer;
  6.   R, G, B: Byte;
  7. begin
  8.   img := TFPMemoryImage.Create(0,0);
  9.   img.LoadFromFile('lenna-8bpp.bmp');
  10.   Lista := TStringList.Create;
  11.   try
  12.     Lista.Sorted := True;
  13.     for y := 0 to img.Height-1 do
  14.     begin
  15.       for x := 0 to img.Width-1 do
  16.       begin
  17.         R := img.Colors[x, y].Red shr 8;
  18.         G := img.Colors[x, y].Green shr 8;
  19.         B := img.Colors[x, y].Blue shr 8;
  20.         Lista.Add(IntToStr(R) + ' ' + IntToStr(G) + ' ' + IntToStr(B));
  21.       end;
  22.     end;
  23.     Lista.SaveToFile('Lenna.bmp-colors.txt');
  24.   finally
  25.     Lista.Free;
  26.     img.Free;
  27.   end;
  28. end;

elioenaishalom

  • New Member
  • *
  • Posts: 28
Re: About the method of generating 8-bit indexed bitmaps.
« Reply #18 on: January 19, 2024, 02:32:02 am »
Dear attentive and dedicated sir,
thank you for the detailed feedback.

Quote
Lista.SaveToFile('Lenna.bmp-colors.txt');
......still with 7643 colors

I imagine that 8-bit bmp is something like the attachment
(saved 8-bit bitmap with Delphi 7, note that Delphi was replaced by Lazarus a long time ago)
I give up on this question - don't bother yourself anymore.

TRon

  • Hero Member
  • *****
  • Posts: 2540
Re: About the method of generating 8-bit indexed bitmaps.
« Reply #19 on: January 19, 2024, 05:07:20 am »
I give up on this question - don't bother yourself anymore.
I won't bother but there is something fundamentally wrong with the used (coding) logic and so with my response try to prevent others from making the same 'mistake'.

As explained by wp a TBitmap internally keeps track of the image-data as either 24- or 32-bit depending on the platform.

A true 8-bit bitmap does not have any rgb pixels stored. For the pixels it uses indices to the palette that is part of the image. The palette (actual colors) might be stored in a variety of different ways but the most one used is 32-bit ARGB/BGRA (but that does not have to be the case).

Both images in your example project are 256-color bitmaps existing of .. well ... 256 colors  :)


@wp:
The problem is that TBitmap always seems to expand the pixelformat to 24 or 32 bits per pixel. You can see this when you query the Pixelformat after loading the image:
If I do make use of TBitmap then I normally use the raw image description depth in order to determine the (real) pixel depth. At least in the past the use of the PixelFormat property was mentioned to be not (always) reliable.

Do you happen to know if the use of PixelFormat property to determine the internal stored format is still not recommended and RawImage.Description should be preferred instead ?
« Last Edit: January 19, 2024, 06:22:13 am by TRon »

 

TinyPortal © 2005-2018