Creare un file immagine GIF con sfondo colorato mediante le funzioni del API di GIFLIB

Da Gambas-it.org - Wikipedia.

Come si sa, salvare un'immagine in un file di formato .GIF (Graphics Interchange Format), in Gambas non è attualmente possibile. Gambas possiede, sì, risorse per caricare file immagine di tipo .gif, ma non per salvarle.

Un aiuto, però, per la generazione di file immagine di tipo GIF può giungerci dalle risorse della libreria GIFLIB, la quale consente di caricare, gestire, manipolare, creare ex novo e salvare file immagine in fomato GIF.

Per poter utilizzare al meglio le risorse della libreria GifLib con Gambas, suggeriamo di utilizzare la relativa libreria dinamica condivisa nella seguente versione: libgif.so.7.0.0


Mostriamo di seguito un breve esempio per generare ex novo un semplice file immagine di tipo GIF, di dimensioni 400 pixel * 200 pixel e sfondo arancione.

Library "libgif:7.0.0"

Private Enum GIF_ERROR = 0, GIF_OK

' GifFileType * EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error)
' Open a new GIF file for write, specified by name.
Private Extern EGifOpenFileName(FileName As String, TestExistence As Boolean, ErrorP As Pointer) As Pointer

' ColorMapObject * GifMakeMapObject (int ColorCount, const GifColorType * ColorMap)
' Allocate a color map of given size.
Private Extern GifMakeMapObject(ColorCount As Integer, ColorMap As Pointer) As Pointer

' int EGifPutScreenDesc(GifFileType *GifFile, const int Width, const int Height, const int ColorRes, const int BackGround, const ColorMapObject *ColorMap)
' This routine should be called immediately following the GIF file opening.
Private Extern EGifPutScreenDesc(GifFileType As Pointer, wI As Integer, hI As Integer, ColorRes As Integer, BackGround As Integer, ColorMap As Pointer) As Integer

' int EGifPutImageDesc(GifFileType *GifFile, const int Left, const int Top, const int Width, const int Height, const bool Interlace, const ColorMapObject *ColorMap)
' This routine should be called before any attempt to dump an image - any call to any of the pixel dump routines.
Private Extern EGifPutImageDesc(GifFileType As Pointer, LeftI As Integer, Top As Integer, wI As Integer, hI As Integer, Interlace As Boolean, ColorMap As Pointer) As Integer

' int EGifPutLine(GifFileType * GifFile, GifPixelType *Line, int LineLen)
' Put one full scanned line (Line) of length LineLen into GIF file.
Private Extern EGifPutLine(GifFileType As Pointer, gpt As Pointer, LineLen As Integer) As Integer

' int EGifCloseFile(GifFileType *GifFile, int *ErrorCode)
' Closes the GIF file.
Private Extern EGifCloseFile(GifFileType As Pointer, ErrorCode As Pointer) As Integer
 

Public Sub Main()

 Dim fileGIF, MappaColori, p As Pointer
 Dim Width, Height As Integer
 Dim b As Byte
 Dim st, rgb As Stream
 

' Vengono impostate le dimensioni in pixel dell'immagine "gif":
  Width = 400
  Height = 200
 
' Apre il file .gif in uscita:
   fileGIF = EGifOpenFileName("/percorso/del/nuovo/file.gif", 0, 0)
   If IsNull(fileGIF) Then Error.Raise("Impossibile creare il file GIF !")

  MappaColori = GifMakeMapObject(256, 0)
  If IsNull(MappaColori) Then Error.Raise("Impossibile allocare memoria !")

' Scrive nella mappa dei colori impostando il colore di sfondo dell'immagine:
  st = Memory MappaColori For Read
  Seek #st, 16
  Read #st, p
    rgb = Memory p For Write
      Write #rgb, 255 As Byte
      Write #rgb, 180 As Byte
      Write #rgb, 0 As Byte
    rgb.Close
  st.Close
  
  If EGifPutScreenDesc(fileGIF, Width, Height, 8, 0, MappaColori) = GIF_ERROR Then Error.Raise("Errore alla funzione 'EGifPutScreenDesc()' !")
 
  If EGifPutImageDesc(fileGIF, 0, 0, Width, Height, False, 0) = GIF_ERROR Then Error.Raise("Errore alla funzione 'EGifPutImageDesc()' !")
  
  p = Alloc(Width)

' Scrive le linee orizzontali colorate dell'immagine "gif":
  For b = 1 To Height
    If EGifPutLine(fileGIF, p, Width) = GIF_ERROR Then Error.Raise("Errore alla funzione 'EGifPutLine()' !")
  Next


' Va in chiusura:
  If EGifCloseFile(fileGIF, 0) = GIF_ERROR Then Error.Raise("Errore nella chiusura del file GIF !")  
  Free(p)
   
End



Riferimenti