Convertire un file immagine SVG in un file immagine di altro formato con le funzioni esterne delle librerie librsvg e libgdk pixbuf

Da Gambas-it.org - Wikipedia.

Utilizzando alcune funzioni esterne delle librerie librsvg e libgdk pixbuf di GNOME è possibile convertire un file immagine SVG in un file immagine di altro formato. Tali formati possibili sono di base "jpeg", "png", "ico" e "bmp", ma possono essere installati altri formati.

In particolare la libreria "librsvg" è utilizzata per effettuare il rendering di immagini di formato SVG, mentre la libreria "libgdk pixbuf" è utilizzata per caricare e gestire immagini.

E' necessario avere installate nel sistema e richiamare in Gambas le librerie condivise: "libgdk_pixbuf-2.0.so.0.4200.8 " e "librsvg-2.so.2.48.0 "

Mostriamo di seguito un esempio, nel quale sarà caricato un file immagine SVG che sarà convertito in formato PNG:

Library "librsvg-2:2.48.0"

' RsvgHandle * rsvg_handle_new_from_file (const gchar *file_name, GError **error)
' Loads the SVG specified by file_name.
Private Extern rsvg_handle_new_from_file(file_name As String, gerror As Pointer) As Pointer

' GdkPixbuf * rsvg_handle_get_pixbuf (RsvgHandle *handle)
' Returns the pixbuf loaded by handle.
Private Extern rsvg_handle_get_pixbuf(handle As Pointer) As Pointer

' gboolean rsvg_handle_close (RsvgHandle *handle, GError **error)
' Closes handle , to indicate that loading the image is complete.
Private Extern rsvg_handle_close(handle As Pointer, gerror As Pointer) As Boolean


Library "libgdk_pixbuf-2.0:0.4200.8"

' 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, altro 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 rsvg, pbf As Pointer
 Dim bo As Boolean
  
' Carica il file immagine SGV da convertire:
 rsvg = rsvg_handle_new_from_file("/percorso/del/file/immagine.svg", 0)
 If rsvg == 0 Then Error.Raise("Errore !")
  
 rsvg_handle_close(rsvg, 0)
  
 pbf = rsvg_handle_get_pixbuf(rsvg)
 If pbf == 0 Then Error.Raise("Errore !")
  
' Converte il file immagine SGV nel nuovo file immagine di formato PNG:
 bo = gdk_pixbuf_save(pbf, "/percorso/del/file/immagine.png", "png", 0, 0)
 If Not bo Then Error.Raise("ERRORE !")
   
' In fine libera la memoria occupata dalle due librerie esterne utilizzate:
 g_object_unref(pbf)
 g_object_unref(rsvg)
  
End


Un'altra modalità può essere la seguente:

Library "librsvg-2:2.48.0"

Private Enum RSVG_HANDLE_FLAGS_NONE = 0, RSVG_HANDLE_FLAG_UNLIMITED, RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA

' RsvgHandle * rsvg_handle_new_from_gfile_sync (GFile *file, RsvgHandleFlags flags, GCancellable *cancellable, GError **error)
' Creates a new RsvgHandle for file.
Private Extern rsvg_handle_new_from_gfile_sync(gfile As Pointer, flags As Integer, cancellable As Pointer, gerror As Pointer) As Pointer

' gboolean rsvg_handle_close (RsvgHandle *handle, GError **error)
' Closes handle , to indicate that loading the image is complete.
Private Extern rsvg_handle_close(handle As Pointer, gerror As Pointer) As Boolean

' GdkPixbuf * rsvg_handle_get_pixbuf (RsvgHandle *handle)
' Returns the pixbuf loaded by handle.
Private Extern rsvg_handle_get_pixbuf(handle As Pointer) As Pointer


Library "libgdk_pixbuf-2.0:0.4200.8"

' GFile * g_file_new_for_path (const char *path)
' Constructs a GFile for a given path.
Private Extern g_file_new_for_path(path As String) 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, altro 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 p, svg, pbf As Pointer
 Dim bo As Boolean
  
' Carica il file immagine SGV da convertire:
 p = g_file_new_for_path("/percorso/del/file/immagine.svg", 0)
 If p == 0 Then Error.Raise("Errore !")
  
 svg = rsvg_handle_new_from_gfile_sync(p, 0, 0, 0)
 If svg == 0 Then Error.Raise("Errore !")
  
 bo = rsvg_handle_close(svg, 0)
 If Not bo Then Error.Raise("Errore !")
  
 pbf = rsvg_handle_get_pixbuf(svg)   
 If pbf == 0 Then Error.Raise("Errore !")
  
' Converte il file immagine SGV nel nuovo file immagine di formato PNG:
 bo = gdk_pixbuf_save(pbf, "/percorso/del/file/immagine.png", "png", 0, 0)
 If Not bo Then Error.Raise("Errore !")
   
' In fine libera la memoria occupata dalle due librerie esterne utilizzate:
 g_object_unref(pbf)
 g_object_unref(svg)
 g_object_unref(p)
  
End


Riferimenti