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.
 
(2 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 condivise: "''libgdk_pixbuf-2.0'' " e "''librsvg-2.so.2.47.0'' "
+
E' necessario avere installate nel sistema e richiamare in Gambas le librerie condivise: "''librsvg-2.so.2.50.0'' " e "''libgdk_pixbuf-2.0.so.0.4200.12'' ".
 
 
  
 
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.47.0"
+
  Library "librsvg-2:2.50.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)''
Riga 22: Riga 21:
 
   
 
   
 
   
 
   
  Library "libgdk_pixbuf-2.0"
+
  Library "libgdk_pixbuf-2.0:0.4200.12"
 
   
 
   
 
  <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, ...)''
Riga 33: Riga 32:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
 
   Dim rsvg, pbf As Pointer
 
   Dim rsvg, pbf As Pointer
Riga 39: Riga 38:
 
    
 
    
 
  <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=darkgreen>''/percorso/del/file/immagine.svg''</font>", 0)
 
   If rsvg == 0 Then Error.Raise("Errore !")
 
   If rsvg == 0 Then Error.Raise("Errore !")
 
    
 
    
Riga 55: Riga 54:
 
   g_object_unref(rsvg)
 
   g_object_unref(rsvg)
 
    
 
    
  '''End'''
+
  End
  
  
 
Un'altra modalità può essere la seguente:
 
Un'altra modalità può essere la seguente:
  Library "librsvg-2:2.47.0"
+
  Library "librsvg-2:2.50.0"
 
   
 
   
 
  Private Enum RSVG_HANDLE_FLAGS_NONE = 0, RSVG_HANDLE_FLAG_UNLIMITED, RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA
 
  Private Enum RSVG_HANDLE_FLAGS_NONE = 0, RSVG_HANDLE_FLAG_UNLIMITED, RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA
Riga 76: Riga 75:
 
   
 
   
 
   
 
   
  Library "libgdk_pixbuf-2.0"
+
  Library "libgdk_pixbuf-2.0:0.4200.12"
 
   
 
   
 
  <FONT Color=gray>' ''GFile * g_file_new_for_path (const char *path)''
 
  <FONT Color=gray>' ''GFile * g_file_new_for_path (const char *path)''
Riga 91: Riga 90:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
 
   Dim p, svg, pbf As Pointer
 
   Dim p, svg, pbf As Pointer
Riga 97: Riga 96:
 
    
 
    
 
  <FONT Color=gray>' ''Carica il file immagine SGV da convertire:''</font>
 
  <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)
+
   p = g_file_new_for_path("<FONT Color=darkgreen>''/percorso/del/file/immagine.svg''</font>", 0)
 
   If p == 0 Then Error.Raise("Errore !")
 
   If p == 0 Then Error.Raise("Errore !")
 
    
 
    
Riga 110: Riga 109:
 
    
 
    
 
  <FONT Color=gray>' ''Converte il file immagine SGV nel nuovo file immagine di formato PNG:''</font>
 
  <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)
+
   bo = gdk_pixbuf_save(pbf, "<FONT Color=darkgreen>''/percorso/del/file/immagine.png''</font>", "png", 0, 0)
 
   If Not bo Then Error.Raise("Errore !")
 
   If Not bo Then Error.Raise("Errore !")
 
      
 
      
Riga 118: Riga 117:
 
   g_object_unref(p)
 
   g_object_unref(p)
 
    
 
    
  '''End'''
+
  End
  
  

Versione attuale delle 04:26, 6 giu 2024

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: "librsvg-2.so.2.50.0 " e "libgdk_pixbuf-2.0.so.0.4200.12 ".

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

Library "librsvg-2:2.50.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.12"

' 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.50.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.12"

' 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