Recent

Author Topic: Grayscale bitmap - image palette is too big or absent  (Read 9636 times)

nilpointer

  • New member
  • *
  • Posts: 7
Grayscale bitmap - image palette is too big or absent
« on: July 12, 2014, 05:47:17 pm »
I use TDelphiTwain from kluug.net to acquire a grayscale (8 bits, 256 colors) image from a scanner. TDelphiTwain delivers a the image as TBitmap. The problem is when I try to save it with Image.SaveToFile, I get an  FPImageException stating that "Image palette is too big or absent".
Assigning the Image bitmap to a TImage component works and the image shows up on the Form, however it's not possible to save it.
Edit: Forgot to mention that I am using Lazarus 1.2.4, FPC 2.6.4 32 bit on Win7 64.
« Last Edit: July 12, 2014, 06:34:05 pm by nilpointer »
Lazarus 1.2.4, FPC 2.6.4 32 bit on Win7 64 bit

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Grayscale bitmap - image palette is too big or absent
« Reply #1 on: July 12, 2014, 07:11:51 pm »
Could you post at least a part of the image in order to be tested by others? Thanks.

nilpointer

  • New member
  • *
  • Posts: 7
Re: Grayscale bitmap - image palette is too big or absent
« Reply #2 on: July 12, 2014, 08:02:36 pm »
Well, there's a little problem about this, because I can't save it. In the OnTwainAcquire I get the TBitMap like this:
Code: [Select]
procedure TForm1.TwainTwainAcquire(Sender: TObject; const Index: integer;
  Image: TBitmap; var Cancel: boolean);
begin
  // Handle scanned images
  Image1.Picture.Bitmap.Assign(Image); // This works, Image1 is a TImage component
  Image.SaveToFile('test.bmp'); // here exception is raised
end;

I use resolution 200x200 dpi and grayscale in the scan dialog.
However, I don't know how to save the data delivered by the scanner (that is actually the problem).
Using another program like IrfanView to do the scan and save the image as bmp, I have no problem loading it into a TBitmap and then save it to another file. It seems that the delivered TBitmap lacks a proper palette.
Lazarus 1.2.4, FPC 2.6.4 32 bit on Win7 64 bit

tk

  • Sr. Member
  • ****
  • Posts: 361
Re: Grayscale bitmap - image palette is too big or absent
« Reply #3 on: July 12, 2014, 08:06:32 pm »
TPicture should normally have the possibility to save to more formats, did you try e.g. png?
Or try converting the delivered bitmap e.g. to a 32 bpp bitmap like that
var
  BM: TBitmap;

BM := TBitmap.Create;
BM.SetSize(Image.Width, Image.Height);
BM.PixelFormat := pf32bit;
BM.Canvas.Draw(0, 0, Image);
BM.Savetofile('test.bmp');

Maybe there is some problem with saving the palette.
15 and higher bpp bitmaps have no palette.
« Last Edit: July 12, 2014, 08:18:52 pm by tk »

nilpointer

  • New member
  • *
  • Posts: 7
Re: Grayscale bitmap - image palette is too big or absent
« Reply #4 on: July 12, 2014, 09:43:48 pm »
Saving as PNG with TImage.Picture.SaveToFile raises the same exception.
However, your second solution works, it results in a 32 bpp bitmap (according to the IrfanView image properties).
Using pf24bit, pf8bit, pf4bit results in the same file (all 24 bpp). Strange. When I use pf1bit instead, it raises the known exception.

So your solution is kind of a workaround, but it doesn't solve the problem itself.
Maybe that needs to be investigated further.
Lazarus 1.2.4, FPC 2.6.4 32 bit on Win7 64 bit

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Grayscale bitmap - image palette is too big or absent
« Reply #5 on: July 13, 2014, 04:49:19 am »
as far as I can see the bitmap returned in image might not be initialized correctly if you remove the line image.savetofile does image1 show the bitmap on screen correctly? if yes then try to use the image1.picture.bitmap.savetofile instead.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tk

  • Sr. Member
  • ****
  • Posts: 361
Re: Grayscale bitmap - image palette is too big or absent
« Reply #6 on: July 13, 2014, 09:34:01 am »
So your solution is kind of a workaround, but it doesn't solve the problem itself.
Maybe that needs to be investigated further.

The problem might be bug in LCL, it is very likely that monochrome and 256bpp bitmaps are not used anymore by most ppl.
If you have time and energy for that you can try to download svn version and locate the bug.
You can then even make a patch and send it to Lazarus authors.

nilpointer

  • New member
  • *
  • Posts: 7
Re: Grayscale bitmap - image palette is too big or absent
« Reply #7 on: July 13, 2014, 11:58:39 am »
@taazz The image is shown correctly on the Form, but trying to save it with Image1.Picture.Bitmap.SaveToFile or Image1.Picture.SaveToFile again throws the exception.
@tk Maybe it's a bug, but I doubt that finding and fixing it lies within my capabilities.

Anyway, thanks for your help.
Lazarus 1.2.4, FPC 2.6.4 32 bit on Win7 64 bit

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: Grayscale bitmap - image palette is too big or absent
« Reply #8 on: July 13, 2014, 08:38:18 pm »
Try to set the usepalette property to false? Maybe the default is different from Delphi, since palette is less common nowadays.

PizzaProgram

  • Jr. Member
  • **
  • Posts: 53
  • ...developing Delphi apps since 25 years.
Re: Grayscale bitmap - image palette is too big or absent
« Reply #9 on: August 04, 2020, 08:54:31 pm »
This bug still exists. :-(

If I set different pixel format, it does not allow to save the file.
(Trying to generate a QR code with 1 bit Black&White images, and it's 3MB instead of 0.1)  :o

'FPImageException'
  Image palette is too big or absent

fpimagebitmap.inc  Line 174:         IntfImg.SaveToStream(AStream, ImgWriter); 
« Last Edit: August 04, 2020, 09:02:17 pm by PizzaProgram »
x86_64-win64 --Win7 PRO 64bit HUN

 

TinyPortal © 2005-2018