Estrarre informazioni da una GIF animata relative al tempo di esecuzione dei fotogrammi

Da Gambas-it.org - Wikipedia.

Per conoscere il tempo di esecuzione fra un fotogramma e l'altro di una GIF animata, è necessario considerare che i dati di ciascun fotogramma sono anticipati dal blocco chiamato "Graphics Control Extension", il quale è composto come mostrato in queste due immagini:

  • graphic_control_ext.gif

I byte del blocco che interessano sono il 5° e il 6°e si riferiscono al Delay time: essi impostano il ritardo espresso in centesimi di secondo di esecuzione fra un fotogramma (frame) e il successivo. Va precisato e va ricordato che l'ordine di questi due byte è in little-endian !
Ovviamente questa risorsa consente anche di diversificare l'esecuzione dei fotogrammi all'interno del file gif animato.

Estrarre il numero di millisecondi per il quale un fotogramma viene mostrato

Per estrarre il numero di millisecondi per il quale un fotogramma di una GIF animata viene mostrato, possiamo utilizzare alcune funzioni esterne della libreria di GLIB-2.0.

E' necessario avere installata nel sistema e richiamare in Gambas la libreria: "libgdk_pixbuf-2.0 ".

Mostriamo un esempio pratico:

Library "libgdk_pixbuf-2.0"

Public Struct GTimeVal
  tv_sec As Long
  tv_usec As Long
End Struct

' GdkPixbufAnimation * gdk_pixbuf_animation_new_from_file (const char *filename, GError **error)
' Creates a new animation by loading it from a file.
Private Extern gdk_pixbuf_animation_new_from_file(filename As String, gerror As Pointer) As Pointer

' GdkPixbufAnimationIter * gdk_pixbuf_animation_get_iter (GdkPixbufAnimation *animation, const GTimeVal *start_time)
' Get an iterator for displaying an animation.
Private Extern gdk_pixbuf_animation_get_iter(animation As Pointer, start_time As GTimeVal) As Pointer

' int gdk_pixbuf_animation_iter_get_delay_time (GdkPixbufAnimationIter *iter)
' Gets the number of milliseconds the current pixbuf should be displayed, or -1 if the current pixbuf should be displayed forever.
Private Extern gdk_pixbuf_animation_iter_get_delay_time(iter As Pointer) As Integer

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Public Sub Main()
 
 Dim an, it As Pointer
 Dim i As Integer
  
  an = gdk_pixbuf_animation_new_from_file("/percorso/del/file/immagine.gif", 0)
  If an == 0 Then Error.Raise("Errore !")
  
  it = gdk_pixbuf_animation_get_iter(an, Null)
  If it == 0 Then Error.Raise("Errore !")
  
  i = gdk_pixbuf_animation_iter_get_delay_time(it)
  Print i; " millisecondi"
  
  g_object_unref(it)
  g_object_unref(an)
  
End



Riferimenti