Recent

Author Topic: Confusion with image formats  (Read 5278 times)

Tomi

  • Full Member
  • ***
  • Posts: 125
Confusion with image formats
« on: April 03, 2024, 03:56:56 pm »
Hello!

I use a TOpenPictureDialog in my program to open different images. With this dialog box possible to open different image formats, e.g. .BMP, .JPG, .GIF, etc.
But what if I want use these image formats in my program, not only the .BMPs? So, I would like open it and then perform various actions on it, e.g. draw on a certain coordinate or copy a part of it onto another image.
Now, all of my images are TBitmap in my program, therefore when I try to open a different format (.JPG, .GIF, etc.) with TOpenPictureDialog, I get an error message about wrong image format.
This solution is good:
Code: Pascal  [Select][+][-]
  1. if fileOpener.Execute then
  2.    begin
  3.      sourceimage:=TBitmap.Create;
  4.      sourceimage.LoadFromFile(fileOpener.FileName);
and later I can use this sourceimage with drawing and with copyrect() function, for example.
But which type do I need to use other image formats? TImage? TGraphic? TPicture? I tried these, but the result is the total confusion in my program, e.g. because I just could draw only TBitmaps with Form1.canvas.draw().
So, can only .BMPs be used or is there a solution to use all supported image formats? For example, transform the opened file type to .BMP within the program? Or can I solely use TBitmap and BMPs?

lainz

  • Hero Member
  • *****
  • Posts: 4517
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Confusion with image formats
« Reply #1 on: April 03, 2024, 04:26:04 pm »
Use BGRABitmap, you can use more than BMP with that...

Code: Pascal  [Select][+][-]
  1. bgra := TBGRABitmap.Create('file.jpg')

wp

  • Hero Member
  • *****
  • Posts: 12013
Re: Confusion with image formats
« Reply #2 on: April 03, 2024, 05:58:47 pm »
When you create an image as TBitmap - yes, you can only load bmp files into it. But the LCL has a multi-format class, named TPicture. You can load any format and access the data as its Graphic property:

Untested:
Code: Pascal  [Select][+][-]
  1. var
  2.   pic: TPicture;
  3. ...
  4.   if FileOpener.Execute then
  5.   begin
  6.     pic := TPicture.Create;
  7.     pic.LoadFromFile(fileOpener.FileName);
  8.     // Example: crop the center 100x100 pixels into a bitmap
  9.     bmp := TBitmap.Create;
  10.     bmp.SetSize(100, 100);
  11.     bmp.Canvas.Draw((pic.Width - bmp.Width) div 2, pic.Height - bmp.Height) div 2, pic.Graphic);
  12.     bmp.SaveToFile('cropped.bmp');
  13.     bmp.Free;

circular

  • Hero Member
  • *****
  • Posts: 4240
    • Personal webpage
Re: Confusion with image formats
« Reply #3 on: April 03, 2024, 06:03:05 pm »
Indeed, TPicture handles multiple formats. You may need to add FPRead*/FPWrite* to the uses clause.
Conscience is the debugger of the mind

cdbc

  • Hero Member
  • *****
  • Posts: 1162
    • http://www.cdbc.dk
Re: Confusion with image formats
« Reply #4 on: April 03, 2024, 06:03:25 pm »
Hi
Have you tried:
Code: Pascal  [Select][+][-]
  1. if fileOpener.Execute then
  2.   Image1.Picture.LoadFromFile(fileOpener.FileName);
  3.  
The 3 answers above are better, but I think this should work too...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

wp

  • Hero Member
  • *****
  • Posts: 12013
Re: Confusion with image formats
« Reply #5 on: April 03, 2024, 06:09:31 pm »
Indeed, TPicture handles multiple formats. You may need to add FPRead*/FPWrite* to the uses clause.
Usually not. Because they are already mentioned in the uses clause of the Graphics unit:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ...
  3.   FPWriteBMP,              // bmp support
  4.   FPWritePNG, PNGComn,     // png support
  5.   {$IFNDEF DisableLCLPNM}
  6.   FPReadPNM, FPWritePNM,   // PNM (Portable aNyMap) support
  7.   {$ENDIF}
  8.   {$IFNDEF DisableLCLJPEG}
  9.   FPReadJpeg, FPWriteJpeg, // jpg support
  10.   {$ENDIF}
  11.   {$IFNDEF DisableLCLTIFF}
  12.   FPReadTiff, FPTiffCmn,   // tiff support
  13.   {$ENDIF}
  14.   {$IFNDEF DisableLCLGIF}
  15.   FPReadGif,
  16.   {$ENDIF}
  17. ...
« Last Edit: April 03, 2024, 07:45:06 pm by wp »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2241
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Confusion with image formats
« Reply #6 on: April 03, 2024, 06:25:46 pm »
It strong depend about what formats you want to support, some generic formats are supported by LCL for loading or displaying but that does not mean you can change all context however you liking it.
Some more formats you can get by using the FreePascal sources.
Best practice is to convert internal to bitmap format, modify it and convert back to source format.
FPC/LCL also offers a special kind of internal storage format for images where I am not that skilled yet.
A third party like BGRABitmap or the new PV_Bitmap may assist to support some more formats where the LCL has no support yet and both can manipulate the image on the fly.
From my point of view, most formats having a fixed binary signature, pre-check a file for possible signatures and load/decode to bitmap, draw or modify pixels, offer via savedialog the destination format conversion.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Tomi

  • Full Member
  • ***
  • Posts: 125
Re: Confusion with image formats
« Reply #7 on: April 05, 2024, 03:16:12 pm »
Thank you for your answers, experts! Then, will TPicture be the solution? I will tried it, but I got an error message at CopyRect command, because it seems, TPicture has no canvas property.
Can I use CopyRect command with TPicture? I would like perform copy action in my program with a piece of image.

wp

  • Hero Member
  • *****
  • Posts: 12013
Re: Confusion with image formats
« Reply #8 on: April 05, 2024, 03:45:17 pm »
TPicture has no canvas, yes. But it can provide access to the loaded images as TGraphic, TBitmap, TPortableNetworkGraphic, or as TJpegImage: Picture.Graphic, Picture.Bitmap, Picture.PNG, Picture.JPEG

Untested (cuts the inner quarter of an image and saves that to a bitmap file):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ButtonClick(Sender: TObject);
  2. var
  3.   pic: TPicture;
  4.   bmp: TBitmap;
  5.   Rsrc, Rdest: TRect;
  6. begin
  7.   pic := TPicture.Create;
  8.   try
  9.     pic.LoadfromFile(NAME_OF_THE_PICTURE_FILE);
  10.     bmp := TBitmap.Create;
  11.     try
  12.       bmp.SetSize(pic.Width div 2, pic.Height div 2);
  13.       Rdest := Rect(0, 0, bmp.Width, bmp.Height);
  14.       Rsrc := Rect(0, 0, bmp.Width, bmp.Height);
  15.       OffsetRect(Rsrc, (pic.Width - bmp.Width) div 2, (pic.Height - bmp.Height) div 2);
  16.       bmp.Canvas.CopyRect(Rdest, pic.Graphic, Rsrc);
  17.       bmp.SaveToFile(NAME_OF_THE_OUTPUT_FILE);
  18.     finally
  19.       bmp.Free;
  20.     end;
  21.   finally
  22.     pic.Free;
  23.   end;
  24. end;

Tomi

  • Full Member
  • ***
  • Posts: 125
Re: Confusion with image formats
« Reply #9 on: April 05, 2024, 06:42:45 pm »
Something is wrong with the second parameter of this line:
Code: Pascal  [Select][+][-]
  1. bmp.Canvas.CopyRect(Rdest, pic.Graphic, Rsrc);
"Incompatible type for arg no. 2: Got "TGraphic", expected "TCanvas""
Therefore I tried using TImage instead of TPicture, but in this case my program can use only BMPs...  %)

wp

  • Hero Member
  • *****
  • Posts: 12013
Re: Confusion with image formats
« Reply #10 on: April 05, 2024, 07:30:52 pm »
Code: Pascal  [Select][+][-]
  1. bmp.Canvas.CopyRect(Rdest, pic.Bitmap.Canvas, Rsrc);  

Tomi

  • Full Member
  • ***
  • Posts: 125
Re: Confusion with image formats
« Reply #11 on: April 06, 2024, 10:39:32 am »
Code: Pascal  [Select][+][-]
  1. bmp.Canvas.CopyRect(Rdest, pic.Bitmap.Canvas, Rsrc);  
Thank you, wp; it works! Now the program can use not only BMPs, but e.g. JPGs too, exactly as I want.  :D

 

TinyPortal © 2005-2018