Creare una GIF animata con le funzioni esterne del API di Libgd

Da Gambas-it.org - Wikipedia.

La libreria GD consente anche di creare un file di formato GIF contenente una immagine animata.

Per poter fruire in Gambas delle risorse della libreria GD, è necessario installare e richiamare la libreria condivisa: "libgd.so.3.0.8 ".

Mostriamo di seguito un semplice esempio, nel quale da quattro file immagine di formato PNG si genererà una GIF animata:

Library "libgd:3.0.8"

' gdImagePtr gdImageCreateFromPng (FILE * fd)
Private Extern gdImageCreateFromPng(fd As Pointer) As Pointer

' void gdImageGifAnimBegin(gdImagePtr im, FILE *outFile, int GlobalCM, int Loops)
Private Extern gdImageGifAnimBegin(im As Pointer, outFile As Pointer, GCM As Integer, Loops As Integer)

' void gdImageGifAnimAdd(gdImagePtr im, FILE *outFile, int LocalCM, int LeftOfs, int TopOfs, int Delay, int Disposal, gdImagePtr previm)
Private Extern gdImageGifAnimAdd(im As Pointer, outFile As Pointer, LCM As Integer, LeftOfs As Integer, TopOfs As Integer, Delay As Integer, Disposal As Integer, previm As Pointer)

' void gdImageGifAnimEnd(FILE *outFile)
Private Extern gdImageGifAnimEnd(outFile As Pointer)

' void gdImageDestroy (gdImagePtr im)
Private Extern gdImageDestroy(gdImage As Pointer)


Library "libc:6"

' FILE *fopen(const char *filename, const char *mode)
' Opens the filename pointed to by filename using the given mode.
Private Extern fopen(filename As String, mode As String) As Integer

' int fclose(FILE *stream)
' Closes the stream. All buffers are flushed.
Private Extern fclose(flstream As Pointer) As Integer


Public Sub Main()
 
 Dim ex, imago, prev As Pointer
 Dim i As Integer

 ex = fopen("/tmp/file.gif", "wb")
 If ex == 0 Then Error.Raise("Impossibile creare il file di formato GIF !")
   
' Inserisce la prima immagine:
 imago = gdImageCreateFromPng(fopen("/percorso/del/primo/file.png", "rb"))
 If imago == 0 Then Error.Raise("Impossibile creare la GIF animata!")
 
 gdImageGifAnimBegin(imago, ex, 1, -1)
 
 prev = imago
 
 For i = 0 To 47  
' Inserisce le altre immagini:
   imago = gdImageCreateFromPng(fopen("/percorso/del/secondo/file.png", "rb"))
   If imago == 0 Then Error.Raise("Impossibile creare la GIF animata!")
   gdImageGifAnimAdd(imago, ex, 1, 0, 0, 10, 1, prev)
   prev = imago
   imago = gdImageCreateFromPng(fopen("/percorso/del/terzo/file.png", "rb"))
   If imago == 0 Then Error.Raise("Impossibile creare la GIF animata!")
   gdImageGifAnimAdd(imago, ex, 1, 0, 0, 10, 1, prev)
   prev = imago
   imago = gdImageCreateFromPng(fopen("/percorso/del/quarto/file.png", "rb"))
   If imago == 0 Then Error.Raise("Impossibile creare la GIF animata!")
   gdImageGifAnimAdd(imago, ex, 1, 0, 0, 10, 1, prev)
   prev = imago
   imago = gdImageCreateFromPng(fopen("/percorso/del/primo/file.png", "rb"))
   If imago == 0 Then Error.Raise("Impossibile creare la GIF animata!")
   gdImageGifAnimAdd(imago, ex, 1, 0, 0, 10, 1, prev)
   prev = imago
   
 Next  

' Genera in fine il file della GIF animata:
 gdImageGifAnimEnd(ex)
  
' Va in chiusura:
 gdImageDestroy(imago)
 fclose(ex)
   
End


Riferimenti