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 27 feb 2015 alle 17:52 di Vuott (Discussione | contributi) (Creata pagina con '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 è ne...')

(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)

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 e dinamica: "libgdk_pixbuf-2.0.so.0.3000.7"


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:

Library "libgdk_pixbuf-2.0:0.3000.7"

' 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


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 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 err <> 0 Then Error.Raise("Impossibile salvare l'immagine in scala di grigi!")
    
End


Private Function grayscale_pixbuf(pixbuf As Pointer) As Pointer
 
 Dim gray As Pointer
 Dim rowstride, pixstride, row, n_rows, width As Integer
 Dim pixels, endp, p As Pointer
 Dim v As Float
 Dim p1, p2, p3 As Pointer
 Dim st As Stream
  
  gray = gdk_pixbuf_copy(pixbuf)
 
  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)
  n_rows = gdk_pixbuf_get_height(gray)
  width = gdk_pixbuf_get_width(gray)
 
  Print "Larghezza: "; Null, width; " pixel"
  Print "Altezza: "; Null, 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



Riferimenti