Convertire un'immagine a colori in un'immagine a scala di grigi mediante le funzioni esterne del API di Libgdk pixbuf
Da Gambas-it.org - Wikipedia.
Versione del 26 dic 2021 alle 16:37 di Vuott (Discussione | contributi)
La libreria GDK-PixBuf consente il caricamento delle immagini e la manipolazione del buffer dei pixel.
Per poter fruire in Gambas delle risorse di questa libreria è necessario installare e richiamare la libreria condivisa: "libgdk_pixbuf-2.0.so.0.4000.0 ".
Con le risorse della libreria GDK-PixBuf è possibile anche convertire un'immagine a colori in un'immagine a scala di grigi.
Mostriamo un semplice esempio, nel quale un'immagine a colori di formato .jpg viene convertita in immagine .png a scala di grigi:
Library "libgdk_pixbuf-2.0:0.4000.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, GError As Pointer) As Pointer ' GdkPixbuf * gdk_pixbuf_copy (const GdkPixbuf *pixbuf) ' Creates a new GdkPixbuf with a copy of the information in the specified pixbuf. Private Extern gdk_pixbuf_copy(GdkPixbuf As Pointer) 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(GdkPixbuf As Pointer, filename As String, type As String, GErr As Pointer, altro As String) As Boolean ' int gdk_pixbuf_get_rowstride (const GdkPixbuf *pixbuf) ' Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row and the start of the next row. Private Extern gdk_pixbuf_get_rowstride(GdkPixbuf As Pointer) As Integer ' gboolean gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf) ' Queries whether a pixbuf has an alpha channel (opacity information). Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf As Pointer) As Boolean ' guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf) ' Queries a pointer to the pixel data of a pixbuf. Private Extern gdk_pixbuf_get_pixels(GdkPixbuf As Pointer) As Pointer ' int gdk_pixbuf_get_height (const GdkPixbuf *pixbuf) ' Queries the height of a pixbuf. Private Extern gdk_pixbuf_get_height(GdkPixbuf As Pointer) As Integer ' int gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer ' Queries the width of a pixbuf. Private Extern gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(gobject As Pointer) Public Sub Main() Dim pixbuf, err As Pointer Dim imm As String = "/percorso/della/immagine/da/convertire" Print "File immagine: "; imm pixbuf = gdk_pixbuf_new_from_file(imm, VarPtr(err)) If Val(String@(err)) <> 0 Then Error.Raise("Impossibile caricare l'immagine !") gray = grayscale_pixbuf(pixbuf) gdk_pixbuf_save(gray, "/tmp/icona.png", "png", VarPtr(err), Null) If Val(String@(err)) <> 0 Then Error.Raise("Impossibile salvare l'immagine in scala di grigi!") g_object_unref(pixbuf) End Private Function grayscale_pixbuf(pixbuf As Pointer) As Pointer Dim gray, pixels, endp, p As Pointer Dim rowstride, pixstride, row, n_rows, width As Integer Dim v As Float Dim p1, p2, p3 As Pointer Dim st As Stream gray = gdk_pixbuf_copy(pixbuf) If gray == 0 Then Error.Raise("Errore !") rowstride = gdk_pixbuf_get_rowstride(gray) If gdk_pixbuf_get_has_alpha(gray) Then pixstride = 4 Else pixstride = 3 Endif pixels = gdk_pixbuf_get_pixels(gray) If pixels == 0 Then Error.Raise("Errore !") n_rows = gdk_pixbuf_get_height(gray) width = gdk_pixbuf_get_width(gray) Print "Larghezza: "; width; " pixel" Print "Altezza: "; n_rows; " pixel" While (row < n_rows) p = pixels + (row * rowstride) endp = p + (pixstride * width) st = Memory p For Write While p <> endp p1 = p p2 = p + 1 p3 = p + 2 v = CFloat(Byte@(p1) * 0.30) + (Byte@(p2) * 0.59) + (Byte@(p3) * 0.11) Write #st, CByte(v) As Byte Write #st, CByte(v) As Byte Write #st, CByte(v) As Byte p += pixstride Wend Inc row Wend st.Close Return gray End