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.
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 3: | Riga 3: | ||
Alcune sue funzioni ci consentono di ottenere un ''Puntatore'' ai dati grezzi dei file immagine, e così di poterli estrarre. | 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: "''libgdk_pixbuf-2.0.so.0.4000.0'' " | |
− | Sarà necessario avere installata nel sistema la libreria: '' | ||
− | |||
Mostriamo un semplice esempio pratico: | Mostriamo un semplice esempio pratico: | ||
− | Library " | + | Library "libgdk_pixbuf-2.0:0.4000.0" |
<FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_new_from_file (const char *filename, GError **error)'' | <FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_new_from_file (const char *filename, GError **error)'' | ||
Riga 33: | Riga 31: | ||
' ''Queries whether a pixbuf has an alpha channel (opacity information).''</font> | ' ''Queries whether a pixbuf has an alpha channel (opacity information).''</font> | ||
Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf As Pointer) As Boolean | Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf 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() | '''Public''' Sub Main() | ||
− | Dim nomefile As String | + | Dim nomefile As String |
− | Dim w, h As Integer | + | Dim w, h, i, n As Integer |
− | Dim | + | Dim pixbuf, dati As Pointer |
− | Dim b | + | Dim b As Byte |
Dim bo As Boolean | Dim bo As Boolean | ||
Dim st As Stream | Dim st As Stream | ||
− | + | ||
+ | nomeFile = "<FONT Color=gray>''/percorso/del/file/immagine''</font>" | ||
− | + | pixbuf = gdk_pixbuf_new_from_file(nomefile, 0) | |
− | + | If pixbuf == 0 Then Error.Raise("Errore !") | |
− | + | ||
− | + | Print "File immagine: "; nomeFile | |
− | + | w = gdk_pixbuf_get_width(pixbuf) | |
− | + | Print "Larghezza: "; w; " pixel" | |
− | + | h = gdk_pixbuf_get_height(pixbuf) | |
− | + | Print "Altezza: "; h; " pixel" | |
− | + | i = gdk_pixbuf_get_n_channels(pixbuf) | |
− | + | Print "Canali: "; i | |
− | + | bo = gdk_pixbuf_get_has_alpha(pixbuf) | |
− | + | Print "Canale Alfa: "; bo | |
+ | dati = gdk_pixbuf_get_pixels(pixbuf) | ||
+ | If dati == 0 Then Error.Raise("Errore !") | ||
+ | |||
<FONT Color=gray>' ''Dereferenziamo il Puntatore con i "Memory Stream", e ne leggiamo i valori contenuti (i dati/byte grezzi dei pixel) dell'immagine:''</font> | <FONT Color=gray>' ''Dereferenziamo il Puntatore con i "Memory Stream", e ne leggiamo i valori contenuti (i dati/byte grezzi dei pixel) dell'immagine:''</font> | ||
− | + | st = Memory dati For Read | |
− | + | For n = 0 To (w * h * i) - 1 | |
− | + | Read #st, b | |
− | + | Print n, Hex(b) | |
− | + | Next | |
− | + | st.Close | |
+ | g_object_unref(pixbuf) | ||
'''End''' | '''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: | 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 " | + | Library "libgdk_pixbuf-2.0:0.4000.0" |
<FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_new_from_file (const char *filename, GError **error)'' | <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> | ' ''Creates a new pixbuf by loading an image from a file.''</font> | ||
Private Extern gdk_pixbuf_new_from_file(filename As String, GError As Pointer) As Pointer | Private Extern gdk_pixbuf_new_from_file(filename As String, GError 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 97: | Riga 103: | ||
' ''Queries whether a pixbuf has an alpha channel (opacity information).''</font> | ' ''Queries whether a pixbuf has an alpha channel (opacity information).''</font> | ||
Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf As Pointer) As Boolean | Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf 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 Form_Open() | '''Public''' Sub Form_Open() | ||
− | + | ||
− | Dim | + | Dim pixbuf, dati As Pointer |
− | Dim | + | Dim bb As Byte[] |
− | Dim w, h, i As Integer | + | Dim w, h, i, n As Integer |
− | |||
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 | ||
+ | |||
+ | <FONT Color=gray>' ''Caricando il file immagine e ri-salvandolo, garantisce che l'immagine sia a 32-bit:''</font> | ||
+ | imago = Image.Load(''/percorso/del/file/immagine'') | ||
+ | imago.Save("/tmp/im.png", 100) | ||
+ | <FONT Color=gray>' ''Per sicurezza distrugge l'Oggetto "Image":''</font> | ||
+ | imago = Null | ||
+ | |||
+ | pixbuf = gdk_pixbuf_new_from_file("/tmp/im.png", 0) | ||
+ | If pixbuf == 0 Then Error.Raise("Errore !") | ||
+ | |||
+ | <FONT Color=gray>' ''Ottiene e mostra alcune informazioni sull'immagine caricata:''</font> | ||
+ | w = gdk_pixbuf_get_width(pixbuf) | ||
+ | Print "Larghezza: "; w; " pixel" | ||
+ | h = gdk_pixbuf_get_height(pixbuf) | ||
+ | Print "Altezza: "; h; " pixel" | ||
+ | i = gdk_pixbuf_get_n_channels(pixbuf) | ||
+ | Print "Canali: "; i | ||
+ | bo = gdk_pixbuf_get_has_alpha(pixbuf) | ||
+ | Print "Canale Alfa: "; bo | ||
+ | |||
+ | dati = gdk_pixbuf_get_pixels(pixbuf) | ||
+ | If dati == 0 Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dei pixel dell'immagine !") | ||
+ | |||
+ | <FONT Color=gray>' ''Carica i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel:''</font> | ||
+ | st = Memory dati For Read | ||
+ | With bb = New Byte[w * h * i] | ||
+ | .Read(st, 0, .count) | ||
+ | End With | ||
+ | st.Close | ||
− | + | <FONT Color=gray>' ''Viene preparata la variabile di tipo Image per la gestione dei dati del futuro nuovo file immagine:''</font> | |
− | + | With imago = New Image(w, h, Color.Transparent, Image.Standard) | |
− | + | st = Memory .Data For Write | |
− | + | End With | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <FONT Color=gray>' ''Corregge la disomogeneità della posizione dei byte dei colori dei pixel tra il formato Immagine standard e quello di Gambas:''</font> | |
− | + | For n = 0 To bb.Max Step 4 | |
− | + | b = bb[n] | |
− | <FONT Color=gray>' '' | + | c = bb[n + 2] |
− | + | bb[n] = c | |
− | + | bb[n + 2] = b | |
− | + | Next | |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<FONT Color=gray>' ''Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image":''</font> | <FONT Color=gray>' ''Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image":''</font> | ||
− | + | bb.Write(st, 0, bb.Count) | |
− | + | st.Close | |
− | + | g_object_unref(pixbuf) | |
− | + | ||
− | + | PictureBox1.Image = imago | |
− | + | imago.Save("/tmp/imago.png", 100) | |
'''End''' | '''End''' | ||
− | |||
Versione attuale delle 21:38, 23 nov 2021
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: "libgdk_pixbuf-2.0.so.0.4000.0 "
Mostriamo un semplice esempio pratico:
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 ' 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 ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(gobject As Pointer) Public Sub Main() Dim nomefile As String Dim w, h, i, n As Integer Dim pixbuf, dati As Pointer Dim b As Byte Dim bo As Boolean Dim st As Stream nomeFile = "/percorso/del/file/immagine" pixbuf = gdk_pixbuf_new_from_file(nomefile, 0) If pixbuf == 0 Then Error.Raise("Errore !") Print "File immagine: "; nomeFile w = gdk_pixbuf_get_width(pixbuf) Print "Larghezza: "; w; " pixel" h = gdk_pixbuf_get_height(pixbuf) Print "Altezza: "; h; " pixel" i = gdk_pixbuf_get_n_channels(pixbuf) Print "Canali: "; i bo = gdk_pixbuf_get_has_alpha(pixbuf) Print "Canale Alfa: "; bo dati = gdk_pixbuf_get_pixels(pixbuf) If dati == 0 Then Error.Raise("Errore !") ' 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 n = 0 To (w * h * i) - 1 Read #st, b Print n, Hex(b) Next st.Close g_object_unref(pixbuf) 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 "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 ' 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 ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(gobject As Pointer) Public Sub Form_Open() Dim pixbuf, dati As Pointer Dim bb As Byte[] Dim w, h, i, n As Integer Dim b, c As Byte Dim bo As Boolean Dim st As Stream Dim imago As Image ' Caricando il file immagine e ri-salvandolo, garantisce che l'immagine sia a 32-bit: imago = Image.Load(/percorso/del/file/immagine) imago.Save("/tmp/im.png", 100) ' Per sicurezza distrugge l'Oggetto "Image": imago = Null pixbuf = gdk_pixbuf_new_from_file("/tmp/im.png", 0) If pixbuf == 0 Then Error.Raise("Errore !") ' Ottiene e mostra alcune informazioni sull'immagine caricata: w = gdk_pixbuf_get_width(pixbuf) Print "Larghezza: "; w; " pixel" h = gdk_pixbuf_get_height(pixbuf) Print "Altezza: "; h; " pixel" i = gdk_pixbuf_get_n_channels(pixbuf) Print "Canali: "; i bo = gdk_pixbuf_get_has_alpha(pixbuf) Print "Canale Alfa: "; bo dati = gdk_pixbuf_get_pixels(pixbuf) If dati == 0 Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dei pixel dell'immagine !") ' Carica i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel: st = Memory dati For Read With bb = New Byte[w * h * i] .Read(st, 0, .count) End With st.Close ' Viene preparata la variabile di tipo Image per la gestione dei dati del futuro nuovo file immagine: With imago = New Image(w, h, Color.Transparent, Image.Standard) st = Memory .Data For Write End With ' Corregge la disomogeneità della posizione dei byte dei colori dei pixel tra il formato Immagine standard e quello di Gambas: For n = 0 To bb.Max Step 4 b = bb[n] c = bb[n + 2] bb[n] = c bb[n + 2] = b Next ' Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image": bb.Write(st, 0, bb.Count) st.Close g_object_unref(pixbuf) PictureBox1.Image = imago imago.Save("/tmp/imago.png", 100) End