Recent

Author Topic: Unable to save image in Gif  (Read 4204 times)

artem101

  • Jr. Member
  • **
  • Posts: 84
Unable to save image in Gif
« on: August 13, 2021, 05:42:48 pm »
I try to draw image and write it to GIF file. There is my sample code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   bitmap: TBitmap;
  4.   gif: TGIFImage;
  5. begin
  6.   bitmap := TBitmap.Create;
  7.  
  8.   bitmap.Width:=100;
  9.   bitmap.Height:=100;
  10.   bitmap.Canvas.Brush.Color := clWhite;
  11.   bitmap.Canvas.FillRect(Rect(0, 0, bitmap.Width, bitmap.Height));
  12.   bitmap.Canvas.Brush.Color := clBlue;
  13.   bitmap.Canvas.Line(10, 20, 60, 90);
  14.  
  15.   gif := TGIFImage.Create;
  16.   gif.Assign(bitmap);
  17.   gif.SaveToFile('d:\test.gif');
  18.   gif.Free;
  19.   bitmap.Free;
  20. end;  

When I run this, program fails with "Abstract method called" exception. What is wrong?

Windows 7 64bit, Lazarus 1.6.0
« Last Edit: August 13, 2021, 06:40:59 pm by artem101 »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Unable to save image in Gif
« Reply #1 on: August 13, 2021, 06:34:27 pm »
Hi!

Searching in this forum helps.

This leads you to

https://forum.lazarus.freepascal.org/index.php?topic=47214.0

Winni

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: Unable to save image in Gif
« Reply #2 on: August 13, 2021, 06:45:27 pm »
It`s a pity that Lazarus doesn`t have built-in support for writing GIFs. I will try to use additional libs. Thank you for reply.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Unable to save image in Gif
« Reply #3 on: August 13, 2021, 07:46:43 pm »
Hi!

Some remarks to GIF files:

* They are "out of time" and rarely used now
* Because harddisks and RAM is cheap there is no need to use only 256 colors
* They are widely replaced by PNG
* Even animated GIFs can be replaced by animated PNGs

Perhaps this is the reason why there is no GIF-Writer in Lazarus.

Another reason might be that before 2004 there was a patent on LZW compression used by GIF. But that is now history.

Winni




wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Unable to save image in Gif
« Reply #4 on: August 13, 2021, 07:50:34 pm »
Found this fpwritegif unit in the internet: https://www.gocher.me/FPWriteGIF ; it does not support animated gifs, but maybe this is enough for you.

In order to use this unit you must register the new writer. I don't know how to hook into the existing TGifImage - but this is not necessary: simply create a new gif image class inherited from TGifImage, and this new class will use this writer unit. Put the following code into a separate unit, or put it to the top of the unit in which you want to write a gif image:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., graphics, fpimage, fpwritegif;
  3.  
  4. type
  5.   TMyGifImage = class(TGifImage)
  6.   protected
  7.     class function GetWriterClass: TFPCustomImageWriterClass; override;
  8.   end;
  9.  
  10. class function TMyGifImage.GetWriterClass: TFPCustomImageWriterClass;
  11. begin
  12.   Result := TFPWriterGIF;
  13. end;

In the initialization section of this unit register the new image class:
Code: Pascal  [Select][+][-]
  1. ....
  2. initialization
  3.   TPicture.RegisterFileFormat('.gif', 'gif', TMyGifImage);  

Simply check the attached demo.

The gif writer unit seems to have an issue with memory management because the demo crashes in the local ClearMappings procedure - knowing that this could create a memory leak I simply bypassed the contents of this procedure, just to get a working example; but you are warned.
« Last Edit: August 13, 2021, 10:41:14 pm by wp »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Unable to save image in Gif
« Reply #5 on: August 13, 2021, 08:39:42 pm »
Found this fpwritegif unit in the internet: https://www.gocher.me/FPWriteGIF; it does not support animated gifs, but maybe this is enough for you.


Please exclude the seicolon from the link. Now you get 404.

Winni

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Unable to save image in Gif
« Reply #6 on: August 13, 2021, 09:05:28 pm »
Some remarks to GIF files:

* They are "out of time" and rarely used now
* Because harddisks and RAM is cheap there is no need to use only 256 colors
* They are widely replaced by PNG
* Even animated GIFs can be replaced by animated PNGs

Perhaps this is the reason why there is no GIF-Writer in Lazarus.

Another reason might be that before 2004 there was a patent on LZW compression used by GIF. But that is now history.

I think the dominant GIF handler during Delphi's heyday was similarly constrained, so Lazarus (possibly FPC) merely inherited the same functionality if not the same code. Back in the day I used to write GIFs without compression specifically for customers whose software (e.g. based on Lotus/IBM Notes/Domino) didn't yet handle PNGs.

I did need to do some bitmap creation a few months ago, I ended up creating either .bmp or .pbm (I forget) and converting to the required final format by piping through scripts.

Postel's law ("Be liberal in what you accept, and conservative in what you send") suggests that it would be worth clearing this loose end up, even if it was expected to be rarely used.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Unable to save image in Gif
« Reply #7 on: August 13, 2021, 09:59:14 pm »
Hi!

Despite that GIFs are nearly forgotten they are still used in the animated version. Useful, nice, awful and kiddie stuff.

BGRAControls  implemented the component TBGRASpriteAnimation. For animated GIFs.

And The BGRAbitmapPack added in one of the last versions GIF support.

If GIFs are desperatly needed you could use BGRA,

Winni

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Unable to save image in Gif
« Reply #8 on: August 13, 2021, 10:15:12 pm »
Hi

Simple demo with  TBGRASpriteAnimation;

Cheeta is running back and forth on a form.

Project attached.

Winni

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Unable to save image in Gif
« Reply #9 on: August 14, 2021, 12:27:32 am »
hello,
to save bitmap in gif format you can use  vampyreImaging library (available on package manager).

Example :
Code: Pascal  [Select][+][-]
  1. uses  Imaging, ImagingTypes, ImagingClasses, ImagingComponents;        
  2. procedure TForm1.Button8Click(Sender: TObject);
  3. var
  4.   bitmap: TBitmap;
  5.   myGIF : TImagingGif;
  6.   I: LongInt;
  7. begin
  8.   bitmap := TBitmap.Create;
  9.  
  10.   bitmap.Width:=100;
  11.   bitmap.Height:=100;
  12.   bitmap.Canvas.Brush.Color := clWhite;
  13.   bitmap.Canvas.FillRect(Rect(0, 0, bitmap.Width, bitmap.Height));
  14.   bitmap.Canvas.Brush.Color := clBlue;
  15.   bitmap.Canvas.Line(10, 20, 60, 90);
  16.   myGIF := TImagingGif.Create;
  17.   myGIF.Assign(Bitmap);
  18.   myGIF.SaveToFile('d:\temp\test.gif');
  19.   myGIF.Free;
  20.   bitmap.Free;
  21. end;    

Friendly, J.P
« Last Edit: August 14, 2021, 12:35:24 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018