Recent

Author Topic: [Solved] TImage - FPImageException: after converting .webp to .png  (Read 1473 times)

re_Chained

  • New Member
  • *
  • Posts: 12
Hello all,

I have a simple app that grabs a random image from a folder and displays it using a TImage upon pressing a button.

I have a group of images I would like to use, but they were all .webp. I used a batch script (listed below for anyone curious) to just replace all the extensions to .png, perhaps a bit naively.

Code: Bash  [Select][+][-]
  1. @ECHO OFF
  2. PUSHD .
  3. FOR /R %%d IN (.) DO (
  4. cd "%%d"
  5. IF EXIST *.webp (
  6. REN *.webp *.png
  7. )
  8. )
  9. POPD

That worked fine, but whenever my app tries to assign one of the .pngs to the TImage via Picture.LoadFromFile, I get an FPImageException exception, which is actually a different exception than if I try and load an actual .webp, which throws an EInvalidGraphic exception.

Is there some leftover .webp metadeta that's causing this? Is there a better way I should be converting the image extensions to work better with TImage?
« Last Edit: July 22, 2022, 03:26:46 pm by re_Chained »

Handoko

  • Hero Member
  • *****
  • Posts: 5507
  • My goal: build my own game engine using Lazarus
Hello re_Chained,
Welcome to the forum.

WebP is not PNG. WebP format uses intra-frame compression but PNG uses DEFLATE compression algorithm. Simply changing the file extension will not change the compression the file uses. Many programs will open image files not based on the file extension but they read the signature of the files:
https://en.wikipedia.org/wiki/List_of_file_signatures

As far as I know TImage does not support WebP. You can try BGRABitmap, which should support WebP:
https://github.com/bgrabitmap/webp

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
That worked fine, but whenever my app tries to assign one of the .pngs to the TImage via Picture.LoadFromFile, I get an FPImageException exception, which is actually a different exception than if I try and load an actual .webp, which throws an EInvalidGraphic exception.

Is there some leftover .webp metadeta that's causing this? Is there a better way I should be converting the image extensions to work better with TImage?

You can't just rename a webp file to png and expect things to work, they are different formats as Handoko mentioned.

The reason for the different exceptions is this: if you supply it a webp file then there is no format handler found, thus the EInvalidGraphic exception. If you supply it the same file with a png extension then the PNG handler will be used and that will then detect an invalid format and thus raise a FPImage related exception.

re_Chained

  • New Member
  • *
  • Posts: 12
I see, thank you very much for the answers and explanations Handoko and PascalDragon. I assumed that was the case but I was interested in what the underlying issue actually was.

I've seen BGRABitmap around and I may switch to that. I'd like to be able to just use images without worrying about myself or any users having to properly convert them.

Cheers!

EDIT:
Wanted to give a small update to anyone stumbling across this in the future with a similar problem, BGRABitmap worked well. All I did was have a TGRABitmap object create a bitmap of the image first, then assign the TBGRABitmap.Bitmap to the TImage.Picture.Bitmap so I could still use the controls of TImage.

Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2.  
  3.     { ... }
  4.  
  5. private
  6.     ImgConvert: TBGRABitmap;
  7.  
  8.     { ... }
  9.  
  10. implementation
  11.  
  12.     { ... }
  13.  
  14.     ImgConvert := TBGRABitmap.Create('image.webp');
  15.     Img.Picture.Bitmap := ImgConvert.Bitmap;
« Last Edit: July 23, 2022, 05:55:07 pm by re_Chained »

 

TinyPortal © 2005-2018