Recent

Author Topic: Animated gif  (Read 1484 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Animated gif
« on: November 19, 2022, 10:46:27 pm »
Is there an example project for creating and saving an animated gif?

Many thanks.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Animated gif
« Reply #1 on: November 20, 2022, 07:30:55 am »
Hi,
sample in attachment. BGRABitmap required
« Last Edit: November 20, 2022, 11:55:11 am by paweld »
Best regards / Pozdrawiam
paweld

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Animated gif
« Reply #2 on: November 20, 2022, 06:35:33 pm »
Excellent! Thank you very much. :)
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Animated gif
« Reply #3 on: November 26, 2022, 11:38:10 pm »
The animated gif works nicely in many cases. However there seems to be a bug in the conversion of colors to gif. Maybe in the color quantization, but that is just a guess. Attached are a png image from an animation, and the corresponding frame in the gif animation.

My workaround is to allow the saving of animated frames as png files. These can be opened as layers in GIMP, and an animated gif can be exported.



“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Animated gif
« Reply #4 on: November 27, 2022, 07:23:21 am »
This problem is known by @circular (author): https://www.lazarusforum.de/viewtopic.php?p=131469&sid=d7dd21c194dcb043d1a77f1e0a233657#p131469
You can still try FPWriteGIF from: https://www.gocher.me/FPWriteGIF - conversion sample (from "png frame.png") in attachment.
Code: Pascal  [Select][+][-]
  1. uses
  2.   FPWriteGIF;
  3.  
  4. procedure TForm1.Button3Click(Sender: TObject);
  5. var
  6.   img: TBGRABitmap;
  7.   gif: TBGRAAnimatedGif;
  8.   i, delayms: integer;
  9.   ms: TMemoryStream;
  10.   gifw: TFPWriterGIF;
  11. begin
  12.   delayms := 100;
  13.   gif := TBGRAAnimatedGif.Create;
  14.   gif.SetSize(709, 709);
  15.   gif.LoopCount := 0;  //endless loop
  16.   ms := TMemoryStream.Create;
  17.   gifw := TFPWriterGIF.Create;
  18.   for i := 0 to 9 do
  19.   begin
  20.     img := TBGRABitmap.Create(Format('frame%d.png', [i]));
  21.     //save as gif with fpwritergif
  22.     img.SaveToStream(ms, gifw);
  23.     ms.Position := 0;
  24.     img.Free;
  25.     img := TBGRABitmap.Create(ms);
  26.     //add frame
  27.     gif.AddFullFrame(img, delayms);
  28.     img.Free;
  29.     ms.Clear;
  30.   end;
  31.   gifw.Free;
  32.   ms.Free;
  33.   gif.OptimizeFrames;
  34.   gif.SaveToFile('sample-ani-2.gif'); //save to file
  35.   gif.Free; //clean
  36. end;            
  37.  
Best regards / Pozdrawiam
paweld

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Animated gif
« Reply #5 on: November 27, 2022, 02:58:45 pm »
Many thanks paweld. I will give that a try.

Attached is the png image converted to gif by GIMP, apparently using an "optimum palette". I can't find any documentation on how that is done.

https://docs.gimp.org/en/gimp-image-convert-indexed.html

EDIT: I just realized that GIMP can also export webp.



« Last Edit: November 27, 2022, 04:23:57 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Boleeman

  • Sr. Member
  • ****
  • Posts: 403
Re: Animated gif
« Reply #6 on: January 27, 2023, 09:57:36 am »
Can anyone help to tweek the fpwritegif unit? Get Access Violation Error

I tried using the fpwritegif.pas unit provided to try to make a 3 frame animation test but I get 2 errors when compiling and a Access Violation Error when running the exe.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, BGRAAnimatedGif,
  9.   BGRAGraphicControl, FPWriteGIF, BGRABitmap, BGRABitmapTypes;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BGRAGraphicControl1: TBGRAGraphicControl;
  17.     bnConvert: TButton;
  18.     procedure bnConvertClick(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.bnConvertClick(Sender: TObject);
  35.   var
  36.   img: TBGRABitmap;
  37.   gif: TBGRAAnimatedGif;
  38.   i, delayms: integer;
  39.   ms: TMemoryStream;
  40.   gifw: TFPWriterGIF;
  41. begin
  42.   delayms := 100;
  43.   gif := TBGRAAnimatedGif.Create;
  44.   gif.SetSize(709, 709);
  45.   gif.LoopCount := 0;  //endless loop
  46.   ms := TMemoryStream.Create;
  47.   gifw := TFPWriterGIF.Create;
  48.  
  49.   for i := 0 to 2 do
  50.   begin
  51.     img := TBGRABitmap.Create(Format('frame%d.png', [i]));
  52.     //save as gif with fpwritergif
  53.     img.SaveToStream(ms, gifw);
  54.     ms.Position := 0;
  55.     img.Free;
  56.     img := TBGRABitmap.Create(ms);
  57.     //add frame
  58.     gif.AddFullFrame(img, delayms);
  59.     img.Free;
  60.     ms.Clear;
  61.   end;
  62.   gifw.Free;
  63.   ms.Free;
  64.   gif.OptimizeFrames;
  65.   gif.SaveToFile('sample-ani-2.gif'); //save to file
  66.   gif.Free; //clean
  67. end;
  68.  
  69. end.      

QArr and CT are not initialsed are the 2 errors
QArr is a QuadArray and CT is a Color Table
They are both declared in the var section but I am struggling to find how to initialize them.

Tried doing      QArr := nil;   but as      QArr is TRGBQuadArray256   I get another message saying got Pointer expecting.
 
Tried doing    QArr[brackets i].r := Nil ;   then got i was not initialised
I put brackets word here as web page gets rid of brackets i

Going round in circles.

Thanks in advance.


« Last Edit: January 27, 2023, 10:13:11 am by Boleeman »

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Animated gif
« Reply #7 on: January 27, 2023, 11:44:28 am »
It seems that FPWriteGIF has a problem if the color palette of the image is less than 256.
Your images have only 2 colors, so you don't need to save to gif, you can add such an image directly to the animation
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, BGRAAnimatedGif,
  9.   BGRAGraphicControl, BGRABitmap, BGRABitmapTypes;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BGRAGraphicControl1: TBGRAGraphicControl;
  17.     bnConvert: TButton;
  18.     procedure bnConvertClick(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.bnConvertClick(Sender: TObject);
  35.   var
  36.   img: TBGRABitmap;
  37.   gif: TBGRAAnimatedGif;
  38.   i, delayms: integer;
  39. begin
  40.   delayms := 100;
  41.   gif := TBGRAAnimatedGif.Create;
  42.   gif.SetSize(355, 355);
  43.   gif.LoopCount := 0;  //endless loop
  44.   for i := 0 to 2 do
  45.   begin
  46.     img := TBGRABitmap.Create(Format('frame%d.png', [i]));
  47.     //add frame
  48.     gif.AddFullFrame(img, delayms);
  49.     img.Free;
  50.   end;
  51.   gif.OptimizeFrames;
  52.   gif.SaveToFile('sample-ani-2.gif'); //save to file
  53.   gif.Free; //clean
  54. end;
  55.  
  56. end.
  57.  
Best regards / Pozdrawiam
paweld

Boleeman

  • Sr. Member
  • ****
  • Posts: 403
Re: Animated gif
« Reply #8 on: January 27, 2023, 01:23:35 pm »
Thanks paweld.

Looks like you were correct.

Tested new ADD code with 2 color pictures = working
Tested greater than 256 colors original code= working


Although 2 color mode did not need gif.SetSize(355, 355);  where as the older code I had to set the exact size of the png source files.

So no need to initialze QArr and CT in the fpwritegif.pas unit

 

TinyPortal © 2005-2018