Convertire un oggetto di tipo "Image" in un file di formato TIFF o ICO con le funzioni esterne del API di Libgdk pixbuf

Da Gambas-it.org - Wikipedia.

Attualmente Gambas non è in grado di salvare un Oggetto del tipo nativo "Image" nei formati "TIFF" e "ICO".

E' possibile, però, ottenere questi due formati, partendo da una "Image", usando alcune funzioni esterne del API di GDK_pixbuf.

E' necessario avere installata nel sistema e richiamare con Gambas la libreria condivisa: "libgdk_pixbuf-2.0.so.0.4200.8 ".

Mostriamo un esempio (poiché l'ambiente non è grafico, bisognerà attivare il Componenti gb.image e gb.image.io).

Library "libgdk_pixbuf-2.0:0.4200.8"

Private Const GDK_COLORSPACE_RGB As Integer = 0

' GdkPixbuf * gdk_pixbuf_new_from_data (const guchar *data, GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample, int width,
' int height, int rowstride, GdkPixbufDestroyNotify destroy_fn, gpointer destroy_fn_data)
' Creates a new GdkPixbuf out of in-memory image data.
Private Extern gdk_pixbuf_new_from_data(data As Pointer, colorspace As Long, has_alpha As Boolean, bits_per_sample As Integer, width As Integer, height As Integer, rowstride As Integer, fn As Pointer, fn_data As Pointer) As Pointer

' gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf, const char *filename, const char *type, GError **error, ...)
' Saves pixbuf to a file in format type. By default, "jpeg", "png", "ico" and "bmp" are possible file formats to save in.
Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, ptr As Pointer) As Boolean

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


Public Sub Main()
 
 Dim im As Image
 Dim pb As Pointer
 
 im = Image.Load("/percorso/file/immagine/originario")
  
 pb = gdk_pixbuf_new_from_data(im.Data, GDK_COLORSPACE_RGB, True, 8, im.W, im.H, im.W * (im.Depth / 8), 0, 0)
 If pb == 0 Then Error.Raise("Errore !")
  
 gdk_pixbuf_save(pb, "/percorso/file.tif", "tiff", 0, 0)
  
 g_object_unref(pb)
  
End