Differenze tra le versioni di "Inserire in un'immagine uno sfondo colorato mediante le funzioni esterne del API di Libgdk pixbuf"
Da Gambas-it.org - Wikipedia.
(5 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
Utilizzando la funzione esterna "''gdk_pixbuf_composite_color_simple( )''" della libreria ''Libgdk_pixbuf'', è possibile trasformare un'immagine inserendovi uno sfondo colorato, ed eventualmente ridimensionandola e cambiando il valore del suo canale alfa. | Utilizzando la funzione esterna "''gdk_pixbuf_composite_color_simple( )''" della libreria ''Libgdk_pixbuf'', è possibile trasformare un'immagine inserendovi uno sfondo colorato, ed eventualmente ridimensionandola e cambiando il valore del suo canale alfa. | ||
− | E' necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "''libgdk_pixbuf-2.0''" | + | E' necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "''libgdk_pixbuf-2.0.so.0.4200.8'' ". |
− | |||
Mostriamo un semplice esempio, nel quale inseriremo uno sfondo arancione: | Mostriamo un semplice esempio, nel quale inseriremo uno sfondo arancione: | ||
− | + | Library "libgdk_pixbuf-2.0:0.4200.8" | |
+ | |||
+ | <FONT Color=gray>' ''Questa enumerazione descrive le diverse modalità di interpolazione che possono essere utilizzate con le funzioni di ridimensionamento.''</font> | ||
+ | Private Enum GDK_INTERP_NEAREST = 0, GDK_INTERP_TILES, GDK_INTERP_BILINEAR, GDK_INTERP_HYPER | ||
+ | |||
+ | <FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_new_from_file (const char *filename, GError **error)'' | ||
+ | ' ''Creates a new pixbuf by loading an image from a file.''</font> | ||
+ | Private Extern gdk_pixbuf_new_from_file(filename As String, gerro As Pointer) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_composite_color_simple (const GdkPixbuf *src, int dest_width, int dest_height, GdkInterpType interp_type, int overall_alpha, int check_size, guint32 color1, guint32 color2)'' | ||
+ | ' ''Creates a new GdkPixbuf by scaling src to dest_width x dest_height and compositing the result with a checkboard of colors color1 and color2.''</font> | ||
+ | Private Extern gdk_pixbuf_composite_color_simple(pixbuf As Pointer, dest_width As Integer, dest_height As Integer, interp_type As Integer, overall_alpha As Integer, check_size As Integer, color1 As Integer, color2 As Integer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''int gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)'' | ||
+ | ' ''Queries the width of a pixbuf.''</font> | ||
+ | Private Extern gdk_pixbuf_get_width(pixbuf As Pointer) As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''int gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)'' | ||
+ | ' ''Queries the height of a pixbuf.''</font> | ||
+ | Private Extern gdk_pixbuf_get_height(pixbuf As Pointer) As Integer | ||
+ | |||
+ | <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.''</font> | ||
+ | Private Extern gdk_pixbuf_save(pixbuf As Pointer, filename As String, type As String, gerror As Pointer, ptr 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 pb As Pointer | ||
+ | Dim im As String | ||
+ | Dim w, h As Integer | ||
+ | Dim bo As Boolean | ||
+ | |||
+ | im = "<FONT Color=gray>''/percorso/del/file/immagine/originario.png''</font>" | ||
+ | |||
+ | pb = gdk_pixbuf_new_from_file(im, 0) | ||
+ | If pb == 0 Then Error.Raise("ERRORE !") | ||
+ | |||
+ | w = gdk_pixbuf_get_width(pb) | ||
+ | h = gdk_pixbuf_get_height(pb) | ||
+ | |||
+ | Print "File immagine: "; im | ||
+ | Print "Dimensioni : "; w; "x"; h; " pixel" | ||
+ | |||
+ | <FONT Color=gray>' ''il 2° e il 3° argomento modificano la dimensione in pixel dell'immagine originaria.'' | ||
+ | ' ''Il 5° argomento imposta il canale alpha dell'immagine originaria.'' | ||
+ | ' ''Il 6° argomento imposta la dimensione dei controlli nel quadro di controllo (a scacchiera) dei colori (deve essere una potenza di 2).'' | ||
+ | ' ''Imposta infine il colore da inserire agendo sui valori degli ultimi due argomenti che fanno da quadro di controllo (a scacchiera) dei colori.''</font> | ||
+ | pb = gdk_pixbuf_composite_color_simple(pb, w, h, GDK_INTERP_HYPER, 150, 1, &FFFF00, &FF0000) | ||
+ | |||
+ | bo = gdk_pixbuf_save(pb, "<FONT Color=gray>''/percorso/del/file/immagine/modificato.png''</font>", "png", 0, 0) | ||
+ | If bo == False Then Error.Raise("ERRORE !") | ||
+ | |||
+ | <FONT Color=gray>' ''Libera la memoria precedentemente allocata:''</font> | ||
+ | g_object_unref(pb) | ||
+ | |||
+ | '''End''' | ||
Riga 11: | Riga 70: | ||
=Riferimenti= | =Riferimenti= | ||
* https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-Scaling.html#gdk-pixbuf-composite-color-simple | * https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-Scaling.html#gdk-pixbuf-composite-color-simple | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Versione attuale delle 15:27, 1 mag 2023
Utilizzando la funzione esterna "gdk_pixbuf_composite_color_simple( )" della libreria Libgdk_pixbuf, è possibile trasformare un'immagine inserendovi uno sfondo colorato, ed eventualmente ridimensionandola e cambiando il valore del suo canale alfa.
E' necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "libgdk_pixbuf-2.0.so.0.4200.8 ".
Mostriamo un semplice esempio, nel quale inseriremo uno sfondo arancione:
Library "libgdk_pixbuf-2.0:0.4200.8" ' Questa enumerazione descrive le diverse modalità di interpolazione che possono essere utilizzate con le funzioni di ridimensionamento. Private Enum GDK_INTERP_NEAREST = 0, GDK_INTERP_TILES, GDK_INTERP_BILINEAR, GDK_INTERP_HYPER ' GdkPixbuf * gdk_pixbuf_new_from_file (const char *filename, GError **error) ' Creates a new pixbuf by loading an image from a file. Private Extern gdk_pixbuf_new_from_file(filename As String, gerro As Pointer) As Pointer ' GdkPixbuf * gdk_pixbuf_composite_color_simple (const GdkPixbuf *src, int dest_width, int dest_height, GdkInterpType interp_type, int overall_alpha, int check_size, guint32 color1, guint32 color2) ' Creates a new GdkPixbuf by scaling src to dest_width x dest_height and compositing the result with a checkboard of colors color1 and color2. Private Extern gdk_pixbuf_composite_color_simple(pixbuf As Pointer, dest_width As Integer, dest_height As Integer, interp_type As Integer, overall_alpha As Integer, check_size As Integer, color1 As Integer, color2 As Integer) As Integer ' int gdk_pixbuf_get_width (const GdkPixbuf *pixbuf) ' Queries the width of a pixbuf. Private Extern gdk_pixbuf_get_width(pixbuf As Pointer) As Integer ' int gdk_pixbuf_get_height (const GdkPixbuf *pixbuf) ' Queries the height of a pixbuf. Private Extern gdk_pixbuf_get_height(pixbuf As Pointer) As Integer ' gboolean gdk_pixbuf_save (GdkPixbuf *pixbuf, const char *filename, const char *type, GError **error, ...) ' Saves pixbuf to a file in format type. 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 pb As Pointer Dim im As String Dim w, h As Integer Dim bo As Boolean im = "/percorso/del/file/immagine/originario.png" pb = gdk_pixbuf_new_from_file(im, 0) If pb == 0 Then Error.Raise("ERRORE !") w = gdk_pixbuf_get_width(pb) h = gdk_pixbuf_get_height(pb) Print "File immagine: "; im Print "Dimensioni : "; w; "x"; h; " pixel" ' il 2° e il 3° argomento modificano la dimensione in pixel dell'immagine originaria. ' Il 5° argomento imposta il canale alpha dell'immagine originaria. ' Il 6° argomento imposta la dimensione dei controlli nel quadro di controllo (a scacchiera) dei colori (deve essere una potenza di 2). ' Imposta infine il colore da inserire agendo sui valori degli ultimi due argomenti che fanno da quadro di controllo (a scacchiera) dei colori. pb = gdk_pixbuf_composite_color_simple(pb, w, h, GDK_INTERP_HYPER, 150, 1, &FFFF00, &FF0000) bo = gdk_pixbuf_save(pb, "/percorso/del/file/immagine/modificato.png", "png", 0, 0) If bo == False Then Error.Raise("ERRORE !") ' Libera la memoria precedentemente allocata: g_object_unref(pb) End