Inserire un'immagine all'interno di un'altra immagine mediante le funzioni esterne del API di Libgdk pixbuf

Da Gambas-it.org - Wikipedia.

La funzione esterna "gdk_pixbuf_composite( )" del API di gdk pixbuf consente di inserire un'immagine o parte di essa all'interno di un'altra immagine.

In particolare i parametri della predetta funzione esterna "gdk_pixbuf_composite( )" dovranno essere così gestiti:

  • il 1° parametro formale "const GdkPixbuf *src" è un Puntatore all'oggetto Gdkpixbuf, che rappresenta l'immagine da copiare;
  • il 2° parametro formale "GdkPixbuf *dest" è un Puntatore all'oggetto Gdkpixbuf, che rappresenta l'immagine all'interno della quale sarà copiata l'immagine puntata dal 1° parametro formale;
  • il 3° parametro formale "int dest_x" è un intero, che rappresenta il punto x sia dell'immagine (o della sua parte) da copiare, sia dell'immagine ospitante ed in particoalre il punto ove sarà copiata l'immagine (o sua parte);
  • il 4° parametro formale "int dest_y" è un intero, che rappresenta il punto y sia dell'immagine (o della sua parte) da copiare, sia dell'immagine ospitante ed in particoalre il punto ove sarà copiata l'immagine (o sua parte);
  • il 5° parametro formale "int dest_width" è un intero, che rappresenta la larghezza in pixel dell'area dell'immagine da copiare;
  • il 6° parametro formale "int dest_height" è un intero, che rappresenta l'altezza in pixel dell'area dell'immagine da copiare;
  • il 7° parametro formale "double offset_x" è un tipo corrispondente al Float di Gambas, che rappresenta l'eventuale valore (positivo, uguale a zero o negativo) di spostamento in orizzontale rispetto al valore precedentemente impostato nel 3° parametro formale;
  • il 8° parametro formale "double offset_y" è un tipo corrispondente al Float di Gambas, che rappresenta l'eventuale valore (positivo, uguale a zero o negativo) di spostamento in verticale rispetto al valore precedentemente impostato nel 3° parametro formale.

E' necessario avere installata e richiamare in Gambas la libreria dinamica condivisa: "libgdk pixbuf-2.0"


Mostriamo un semplice esempio pratico:

Library "libgdk_pixbuf-2.0"

' 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

Private Const GDK_COLORSPACE_RGB As Integer = 0

' 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

' 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

' void gdk_pixbuf_composite (const GdkPixbuf *src, GdkPixbuf *dest, int dest_x, int dest_y, int dest_width, int dest_height, ' double offset_x, double offset_y, double scale_x, double scale_y, GdkInterpType interp_type, int overall_alpha) ' Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y. Private Extern gdk_pixbuf_composite(src As Pointer, dest As Pointer, dest_x As Integer, dest_y As Integer, dest_width As Integer, dest_height As Integer, offset_x As Float, offset_y As Float, scale_x As Float, scale_y As Float, interp_type As Integer, overall_alpha 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 pb1, pb2 As Pointer
 Dim im1, im2 As String
 Dim w, h As Integer
 Dim bo As Boolean
 
  im1 = "/home/ploppo/Scrivania/IMMAGINI/luna.jpg"
 
  pb1 = gdk_pixbuf_new_from_file(im1, 0)
  If pb1 = 0 Then Error.Raise("ERRORE !")
  
  w = gdk_pixbuf_get_width(pb1)
  h = gdk_pixbuf_get_height(pb1)
  
  Print "Immagine destinazione: "; im1
  Print "Dimensioni:            "; w; "x"; h; " pixel\n"
  
  im2 = "/home/ploppo/Scrivania/IMMAGINI/Ultima-cena.jpg"
  pb2 = gdk_pixbuf_new_from_file(im2, 0)
  If pb2 = 0 Then Error.Raise("ERRORE !")
  
  w = gdk_pixbuf_get_width(pb2)
  h = gdk_pixbuf_get_height(pb2)
  
  Print "Immagine da copiare:   "; im2
  Print "Dimensioni:            "; w; "x"; h; " pixel"
  

'

  gdk_pixbuf_composite(pb2, pb1, 300, 300, 300, 150, 200, -200, 1, 1, GDK_INTERP_HYPER, 255)
  
  bo = gdk_pixbuf_save(pb1, "/tmp/test.png", "png", 0, 0)
  If bo = False Then Error.Raise("ERRORE !")
  g_object_unref(pb2)
  g_object_unref(pb1)

End




Riferimenti



Pagina in costruzione !