Differenze tra le versioni di "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.
(Creata pagina con "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....")
 
 
(6 versioni intermedie di uno stesso utente non sono mostrate)
Riga 3: Riga 3:
 
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.
 
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 dinamiche condivise: "''libgdk_pixbuf-2.0''" e "''librsvg-2.so.2.40.13''"
+
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:
 
Mostriamo di seguito un esempio, nel quale sarà caricato un file immagine SVG che sarà convertito in formato PNG:
  Library "librsvg-2:2.40.13"
+
  Library "librsvg-2:2.48.0"
 
   
 
   
  <FONT Color=gray>'' 'RsvgHandle * rsvg_handle_new_from_file (const gchar *file_name, GError **error)''
+
  <FONT Color=gray>' ''RsvgHandle * rsvg_handle_new_from_file (const gchar *file_name, GError **error)''
 
  ' ''Loads the SVG specified by file_name.''</font>
 
  ' ''Loads the SVG specified by file_name.''</font>
 
  Private Extern rsvg_handle_new_from_file(file_name As String, gerror As Pointer) As Pointer
 
  Private Extern rsvg_handle_new_from_file(file_name As String, gerror As Pointer) As Pointer
 
   
 
   
  <FONT Color=gray>'' 'GdkPixbuf * rsvg_handle_get_pixbuf (RsvgHandle *handle)''
+
  <FONT Color=gray>' ''GdkPixbuf * rsvg_handle_get_pixbuf (RsvgHandle *handle)''
 
  ' Returns the pixbuf loaded by handle.''</font>
 
  ' Returns the pixbuf loaded by handle.''</font>
 
  Private Extern rsvg_handle_get_pixbuf(handle As Pointer) As Pointer
 
  Private Extern rsvg_handle_get_pixbuf(handle As Pointer) As Pointer
 +
 +
<FONT Color=gray>' ''gboolean rsvg_handle_close (RsvgHandle *handle, GError **error)''
 +
' ''Closes handle , to indicate that loading the image is complete.''</font>
 +
Private Extern rsvg_handle_close(handle As Pointer, gerror As Pointer) As Boolean
 
   
 
   
 
   
 
   
  Library "libgdk_pixbuf-2.0"
+
  Library "libgdk_pixbuf-2.0:0.4200.8"
 
   
 
   
  <FONT Color=gray>'' 'gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf, const char *filename, const char *type, GError **error, ...)''
+
  <FONT Color=gray>' ''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.''</font>
 
  ' Saves pixbuf to a file in format type. By default, "jpeg", "png", "ico" and "bmp" are possible file formats to save in.''</font>
 
  Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, altro As Pointer) As Boolean
 
  Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, altro As Pointer) As Boolean
 
   
 
   
  <FONT Color=gray>'' 'void g_object_unref (gpointer object)''
+
  <FONT Color=gray>' ''void g_object_unref (gpointer object)''
 
  ' Decreases the reference count of object.''</font>
 
  ' Decreases the reference count of object.''</font>
 
  Private Extern g_object_unref(gobject As Pointer)
 
  Private Extern g_object_unref(gobject As Pointer)
Riga 34: Riga 37:
 
   Dim bo As Boolean
 
   Dim bo As Boolean
 
    
 
    
  <FONT Color=gray>'' 'Carica il file immagine SGV da convertire:''</font>
+
  <FONT Color=gray>' ''Carica il file immagine SGV da convertire:''</font>
   rsvg = rsvg_handle_new_from_file("<FONT Color=gray>''/percorso/del/file/immagine.svg''</font>", 0)
+
  rsvg = rsvg_handle_new_from_file("<FONT Color=gray>''/percorso/del/file/immagine.svg''</font>", 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 !")
 +
    
 +
<FONT Color=gray>' ''Converte il file immagine SGV nel nuovo file immagine di formato PNG:''</font>
 +
  bo = gdk_pixbuf_save(pbf, "<FONT Color=gray>''/percorso/del/file/immagine.png''</font>", "png", 0, 0)
 +
  If Not bo Then Error.Raise("ERRORE !")
 +
   
 +
<FONT Color=gray>' ''In fine libera la memoria occupata dalle due librerie esterne utilizzate:''</font>
 +
  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
 +
 +
<FONT Color=gray>' ''RsvgHandle * rsvg_handle_new_from_gfile_sync (GFile *file, RsvgHandleFlags flags, GCancellable *cancellable, GError **error)''
 +
' ''Creates a new RsvgHandle for file.''</font>
 +
Private Extern rsvg_handle_new_from_gfile_sync(gfile As Pointer, flags As Integer, cancellable As Pointer, gerror As Pointer) As Pointer
 +
 +
<FONT Color=gray>' ''gboolean rsvg_handle_close (RsvgHandle *handle, GError **error)''
 +
' ''Closes handle , to indicate that loading the image is complete.''</font>
 +
Private Extern rsvg_handle_close(handle As Pointer, gerror As Pointer) As Boolean
 +
 +
<FONT Color=gray>' ''GdkPixbuf * rsvg_handle_get_pixbuf (RsvgHandle *handle)''
 +
' Returns the pixbuf loaded by handle.''</font>
 +
Private Extern rsvg_handle_get_pixbuf(handle As Pointer) As Pointer
 +
 +
 +
Library "libgdk_pixbuf-2.0:0.4200.8"
 +
 +
<FONT Color=gray>' ''GFile * g_file_new_for_path (const char *path)''
 +
' ''Constructs a GFile for a given path.''</font>
 +
Private Extern g_file_new_for_path(path As String) As Pointer
 +
 +
<FONT Color=gray>' ''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.''</font>
 +
Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, altro As Pointer) As Boolean
 +
 +
<FONT Color=gray>' ''void g_object_unref (gpointer object)''
 +
' Decreases the reference count of object.''</font>
 +
Private Extern g_object_unref(gobject As Pointer)
 +
 +
 +
'''Public''' Sub Main()
 +
 
 +
  Dim p, svg, pbf As Pointer
 +
  Dim bo As Boolean
 +
 
 +
<FONT Color=gray>' ''Carica il file immagine SGV da convertire:''</font>
 +
  p = g_file_new_for_path("<FONT Color=gray>''/percorso/del/file/immagine.svg''</font>", 0)
 +
  If p == 0 Then Error.Raise("Errore !")
 
    
 
    
  pbf = rsvg_handle_get_pixbuf(rsvg)
+
  svg = rsvg_handle_new_from_gfile_sync(p, 0, 0, 0)
 +
  If svg == 0 Then Error.Raise("Errore !")
 
    
 
    
<FONT Color=gray>'' 'Converte il file immagine SGV nel nuovo file immagine di formato PNG:''</font>
+
  bo = rsvg_handle_close(svg, 0)
  bo = gdk_pixbuf_save(pbf, "<FONT Color=gray>''/percorso/del/file/immagine.png''</font>", "png", 0, 0)
+
  If Not bo Then Error.Raise("Errore !")
  If Not bo Then Error.Raise("ERRORE !")
 
 
    
 
    
 +
  pbf = rsvg_handle_get_pixbuf(svg) 
 +
  If pbf == 0 Then Error.Raise("Errore !")
 
    
 
    
  <FONT Color=gray>'' 'In fine libera la memoria occupata dalle due librerie esterne utilizzate:''</font>
+
<FONT Color=gray>' ''Converte il file immagine SGV nel nuovo file immagine di formato PNG:''</font>
  g_object_unref(pbf)
+
  bo = gdk_pixbuf_save(pbf, "<FONT Color=gray>''/percorso/del/file/immagine.png''</font>", "png", 0, 0)
  g_object_unref(rsvg)
+
  If Not bo Then Error.Raise("Errore !")
 +
   
 +
  <FONT Color=gray>' ''In fine libera la memoria occupata dalle due librerie esterne utilizzate:''</font>
 +
  g_object_unref(pbf)
 +
  g_object_unref(svg)
 +
  g_object_unref(p)
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione attuale delle 15:29, 1 mag 2023

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