Differenze tra le versioni di "Ottenere dati grezzi di un file immagine mediante le funzioni del API di GDK-PixBuf"
Da Gambas-it.org - Wikipedia.
Riga 37: | Riga 37: | ||
'''Public''' Sub Main() | '''Public''' Sub Main() | ||
− | Dim nomefile As String | + | Dim nomefile As String |
Dim w, h As Integer | Dim w, h As Integer | ||
Dim Pixbuf, dati As Pointer | Dim Pixbuf, dati As Pointer | ||
Riga 44: | Riga 44: | ||
Dim st As Stream | Dim st As Stream | ||
Dim i As Integer | Dim i As Integer | ||
+ | |||
+ | nomeFile = "<FONT Color=gray>''/percorso/del/file/immagine''</font>" | ||
Pixbuf = gdk_pixbuf_new_from_file(nomefile, 0) | Pixbuf = gdk_pixbuf_new_from_file(nomefile, 0) | ||
Riga 74: | Riga 76: | ||
Library "libgtk-3:0.1000.8" | Library "libgtk-3:0.1000.8" | ||
− | <FONT Color=gray>' '' | + | <FONT Color=gray>' ''GdkPixbufLoader * gdk_pixbuf_loader_new(void)'' |
− | ' ''Creates a new pixbuf | + | ' ''Creates a new pixbuf loader object.''</font> |
− | Private Extern | + | Private Extern gdk_pixbuf_loader_new() As Pointer |
+ | <FONT Color=gray>' ''gboolean gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, gsize count, GError **error)'' | ||
+ | ' ''Cause a pixbuf loader to parse the next count bytes of an image.''</font> | ||
+ | Private Extern gdk_pixbuf_loader_write(lo As Pointer, buf As Pointer, i As Integer, po As Pointer) | ||
+ | |||
+ | <FONT Color=gray>' ''gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader, GError **error)'' | ||
+ | ' ''Informs a pixbuf loader that no further writes.''</font> | ||
+ | Private Extern gdk_pixbuf_loader_close(lo As Pointer, po As Pointer) | ||
+ | |||
+ | <FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)'' | ||
+ | ' ''Queries the GdkPixbuf that a pixbuf loader is currently creating.''</font> | ||
+ | Private Extern gdk_pixbuf_loader_get_pixbuf(lo As Pointer) As Pointer | ||
+ | |||
<FONT Color=gray>' ''guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)'' | <FONT Color=gray>' ''guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)'' | ||
' ''Queries a pointer to the pixel data of a pixbuf.''</font> | ' ''Queries a pointer to the pixel data of a pixbuf.''</font> | ||
Riga 100: | Riga 114: | ||
'''Public''' Sub Form_Open() | '''Public''' Sub Form_Open() | ||
− | + | ||
− | |||
Dim fl As File | Dim fl As File | ||
+ | Dim bb As Byte[] | ||
Dim w, h, i As Integer | Dim w, h, i As Integer | ||
− | Dim Pixbuf, dati As Pointer | + | Dim loader, Pixbuf, dati As Pointer |
Dim b, c As Byte | Dim b, c As Byte | ||
Dim bo As Boolean | Dim bo As Boolean | ||
Dim st As Stream | Dim st As Stream | ||
Dim imago As Image | Dim imago As Image | ||
+ | |||
+ | fl = Open "<FONT Color=gray>''/percorso/del/file/immagine''</font>" For Read | ||
+ | |||
+ | With bb = New Byte[Lof(fl)] | ||
+ | .Read(fl, 0, bb.Count) | ||
+ | End With | ||
+ | |||
+ | fl.Close | ||
+ | |||
+ | loader = gdk_pixbuf_loader_new() | ||
+ | |||
+ | gdk_pixbuf_loader_write(loader, bb.data, bb.count, 0) | ||
+ | |||
+ | gdk_pixbuf_loader_close(loader, 0) | ||
+ | |||
+ | pixbuf = gdk_pixbuf_loader_get_pixbuf(loader) | ||
− | |||
− | |||
− | |||
− | |||
w = gdk_pixbuf_get_width(Pixbuf) | w = gdk_pixbuf_get_width(Pixbuf) | ||
Print "Larghezza: "; Null, w; " pixel" | Print "Larghezza: "; Null, w; " pixel" | ||
Riga 124: | Riga 150: | ||
dati = gdk_pixbuf_get_pixels(Pixbuf) | dati = gdk_pixbuf_get_pixels(Pixbuf) | ||
− | If IsNull(dati) Then Error.Raise("Impossibile | + | If IsNull(dati) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dei pixel dell'immagine !") |
<FONT Color=gray>' ''Carichiamo i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel:''</font> | <FONT Color=gray>' ''Carichiamo i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel:''</font> | ||
st = Memory dati For Read | st = Memory dati For Read | ||
− | + | ||
+ | With bb = New Byte[w * h * c] | ||
.Read(st, 0, .count) | .Read(st, 0, .count) | ||
− | + | End With | |
− | + | ||
st.Close | st.Close | ||
Versione delle 10:46, 14 nov 2015
GDK-PixBuf è una libreria grafica per il caricamento e la manipolazione delle immagini.
Alcune sue funzioni ci consentono di ottenere un Puntatore ai dati grezzi dei file immagine, e così di poterli estrarre.
Sarà necessario avere installata nel sistema la libreria: libgtk-3.so.0.1000.8
Mostriamo un semplice esempio pratico:
Library "libgtk-3:0.1000.8" ' 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 ' 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_width (const GdkPixbuf *pixbuf) ' Queries the width of a pixbuf. Private Extern gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer ' 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_n_channels (const GdkPixbuf *pixbuf) ' Queries the number of channels of a pixbuf. Private Extern gdk_pixbuf_get_n_channels(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 Public Sub Main() Dim nomefile As String Dim w, h As Integer Dim Pixbuf, dati As Pointer Dim b, c As Byte Dim bo As Boolean Dim st As Stream Dim i As Integer nomeFile = "/percorso/del/file/immagine" Pixbuf = gdk_pixbuf_new_from_file(nomefile, 0) w = gdk_pixbuf_get_width(Pixbuf) Print "Larghezza: "; Null, w; " pixel" h = gdk_pixbuf_get_height(Pixbuf) Print "Altezza: "; Null, h; " pixel" c = gdk_pixbuf_get_n_channels(Pixbuf) Print "Canali: "; Null, c bo = gdk_pixbuf_get_has_alpha(Pixbuf) Print "Canale Alfa: "; Null, bo dati = gdk_pixbuf_get_pixels(Pixbuf) ' Dereferenziamo il Puntatore con i "Memory Stream", e ne leggiamo i valori contenuti (i dati/byte grezzi dei pixel) dell'immagine: st = Memory dati For Read For i = 0 To (w * h * c) - 1 Read #st, b Print i, Hex(b) Next st.Close End
In quest'altro esempio caricata un'immagine, utilizzando le risorse di Gambas, ed infine i dati ottenuti dei pixel saranno riutilizzati ricreando una nuova immagine identica all'immagine originaria:
Library "libgtk-3:0.1000.8" ' GdkPixbufLoader * gdk_pixbuf_loader_new(void) ' Creates a new pixbuf loader object. Private Extern gdk_pixbuf_loader_new() As Pointer ' gboolean gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, gsize count, GError **error) ' Cause a pixbuf loader to parse the next count bytes of an image. Private Extern gdk_pixbuf_loader_write(lo As Pointer, buf As Pointer, i As Integer, po As Pointer) ' gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader, GError **error) ' Informs a pixbuf loader that no further writes. Private Extern gdk_pixbuf_loader_close(lo As Pointer, po As Pointer) ' GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader) ' Queries the GdkPixbuf that a pixbuf loader is currently creating. Private Extern gdk_pixbuf_loader_get_pixbuf(lo As Pointer) As Pointer ' 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_width (const GdkPixbuf *pixbuf) ' Queries the width of a pixbuf. Private Extern gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer ' 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_n_channels (const GdkPixbuf *pixbuf) ' Queries the number of channels of a pixbuf. Private Extern gdk_pixbuf_get_n_channels(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 Public Sub Form_Open() Dim fl As File Dim bb As Byte[] Dim w, h, i As Integer Dim loader, Pixbuf, dati As Pointer Dim b, c As Byte Dim bo As Boolean Dim st As Stream Dim imago As Image fl = Open "/percorso/del/file/immagine" For Read With bb = New Byte[Lof(fl)] .Read(fl, 0, bb.Count) End With fl.Close loader = gdk_pixbuf_loader_new() gdk_pixbuf_loader_write(loader, bb.data, bb.count, 0) gdk_pixbuf_loader_close(loader, 0) pixbuf = gdk_pixbuf_loader_get_pixbuf(loader) w = gdk_pixbuf_get_width(Pixbuf) Print "Larghezza: "; Null, w; " pixel" h = gdk_pixbuf_get_height(Pixbuf) Print "Altezza: "; Null, h; " pixel" c = gdk_pixbuf_get_n_channels(Pixbuf) Print "Canali: "; Null, c bo = gdk_pixbuf_get_has_alpha(Pixbuf) Print "Canale Alfa: "; Null, bo dati = gdk_pixbuf_get_pixels(Pixbuf) If IsNull(dati) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dei pixel dell'immagine !") ' Carichiamo i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel: st = Memory dati For Read With bb = New Byte[w * h * c] .Read(st, 0, .count) End With st.Close ' Viene preparata la variabile di tipo immagine per la gestione dei dati del futuro nuovo file immagine: With imago = New Image(w, h, 0, 0) st = Memory .Data For Write End With ' Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image": bb.Write(st, 0, bb.Count) st.Close PictureBox1.Picture = imago.Picture imago.Save("/tmp/imago.png", 100) End