Differenze tra le versioni di "Creare un file immagine da dati immagine grezzi mediante le funzioni del API di Imlib2"
Da Gambas-it.org - Wikipedia.
Riga 41: | Riga 41: | ||
<FONT Color=gray>' ''L'immagine creata viene salvata nel nuovo file immagine:''</font> | <FONT Color=gray>' ''L'immagine creata viene salvata nel nuovo file immagine:''</font> | ||
imlib_save_image("''/percorso/del/file/immagine/creato''") | imlib_save_image("''/percorso/del/file/immagine/creato''") | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | |||
+ | Si potrà creare un'immagine anche da dati grezzi ottenuti da una specifica funzione esterna della libreria ''Imlib2''. | ||
+ | Library "libgtk-3:0.1000.8" | ||
+ | |||
+ | <FONT Color=gray>' ''Imlib_Image imlib_load_image_immediately(const char *file)'' | ||
+ | ' ''Loads an image from disk located at the path specified by file.''</font> | ||
+ | Private Extern imlib_load_image_immediately(filename As String) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''void imlib_context_set_image(Imlib_Image image)'' | ||
+ | ' ''Sets the current image Imlib2 will be using with its function calls.''</font> | ||
+ | Private Extern imlib_context_set_image(Imlib_Image As Pointer) | ||
+ | |||
+ | <FONT Color=gray>' ''DATA32* imlib_image_get_data(void)'' | ||
+ | ' ''Returns a pointer to the image data in the image set as the image for the current context.'' | ||
+ | ' ''The image data is returned in the format of a DATA32 (32 bits) per pixel in a linear array ordered'' | ||
+ | ' ''from the top left of the image to the bottom right going from left to right each line.''</font> | ||
+ | Private Extern imlib_image_get_data() As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''int imlib_image_get_width(void)'' | ||
+ | ' ''Returns the width in pixels of the current image in Imlib2's context.''</font> | ||
+ | Private Extern imlib_image_get_width() As Integer | ||
+ | |||
+ | <FONT Color=gray>' ''int imlib_image_get_height(void)'' | ||
+ | ' ''Returns the height in pixels of the current image in Imlib2's context.''</font> | ||
+ | Private Extern imlib_image_get_height() As Integer | ||
+ | |||
+ | |||
+ | '''Public''' Sub Form_Open() | ||
+ | |||
+ | Dim ImlibImage, dati As Pointer | ||
+ | Dim w, h, i As Integer | ||
+ | Dim st As Stream | ||
+ | Dim b As Byte | ||
+ | |||
+ | ImlibImage = imlib_load_image_immediately("''/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi''") | ||
+ | |||
+ | imlib_context_set_image(ImlibImage) | ||
+ | |||
+ | <FONT Color=gray>' ''Viene creato un puntatore ai dati grezzi immagine:''</font> | ||
+ | dati = imlib_image_get_data() | ||
+ | |||
+ | w = imlib_image_get_width() | ||
+ | h = imlib_image_get_height() | ||
+ | |||
+ | <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 i = 0 To (w * h * 4) - 1 | ||
+ | Read #st, b | ||
+ | Print i, Hex(b) | ||
+ | Next | ||
+ | |||
+ | st.Close | ||
'''End''' | '''End''' |
Versione delle 17:33, 6 dic 2014
Avendo una variabile di tipo Puntatore che punta a dati immagine grezzi in formato 32 bit per pixel (come nei formati: "RGBA", "ARGB", "BGRA", etc), è possibile da tali dati creare la relativa immagine e salvarla in un file immagine mediante alcune funzioni della libreria Imlib2.
La libreria Imlib2 è in grado di caricare, gestire, elaborare e salvare file immagini. Per poter fruire delle sue funzionalità in Gambas, è necessario richiamare la libreria (nella sua attuale versione): "libImlib2.so.1.4.6"
Mostriamo un semplice esempio attinente al presente argomento:
Library "libImlib2:1.4.6"
' Imlib_Image imlib_create_image_using_data(int width, int height, DATA32 * data)
' Creates an image from the image data - in the format of a DATA32 (32 bits) per pixel in a linear array - specified with the width and the height specified.
Private Extern imlib_create_image_using_data(width As Integer, height As Integer, data As Pointer) As Pointer
' void imlib_context_set_image(Imlib_Image image)
' Sets the current image Imlib2 will be using with its function calls.
Private Extern imlib_context_set_image(Imlib_Image As Pointer)
' void imlib_image_set_has_alpha(char has_alpha)
' Sets the alpha flag for the current image.
Private Extern imlib_image_set_has_alpha(has_alpha As Boolean)
' void imlib_save_image(const char *filename)
' Saves the current image in the format specified by the current image's format settings to the filename.
Private Extern imlib_save_image(filename As String)
Public Sub Form_Open()
Dim im As Image
Dim immago As Pointer
im = Image.Load("/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi")
' Viene creata l'immagine dai dati grezzi:
immago = imlib_create_image_using_data(im.W, im.H, im.Data)
imlib_context_set_image(immago)
' Volendo, può essere assegnato all'immagine il canale "alfa" per la trasparenza (se la supporta) dello sfondo:
imlib_image_set_has_alpha(True)
' L'immagine creata viene salvata nel nuovo file immagine:
imlib_save_image("/percorso/del/file/immagine/creato")
End
Si potrà creare un'immagine anche da dati grezzi ottenuti da una specifica funzione esterna della libreria Imlib2.
Library "libgtk-3:0.1000.8" ' Imlib_Image imlib_load_image_immediately(const char *file) ' Loads an image from disk located at the path specified by file. Private Extern imlib_load_image_immediately(filename As String) As Pointer ' void imlib_context_set_image(Imlib_Image image) ' Sets the current image Imlib2 will be using with its function calls. Private Extern imlib_context_set_image(Imlib_Image As Pointer) ' DATA32* imlib_image_get_data(void) ' Returns a pointer to the image data in the image set as the image for the current context. ' The image data is returned in the format of a DATA32 (32 bits) per pixel in a linear array ordered ' from the top left of the image to the bottom right going from left to right each line. Private Extern imlib_image_get_data() As Pointer ' int imlib_image_get_width(void) ' Returns the width in pixels of the current image in Imlib2's context. Private Extern imlib_image_get_width() As Integer ' int imlib_image_get_height(void) ' Returns the height in pixels of the current image in Imlib2's context. Private Extern imlib_image_get_height() As Integer Public Sub Form_Open() Dim ImlibImage, dati As Pointer Dim w, h, i As Integer Dim st As Stream Dim b As Byte ImlibImage = imlib_load_image_immediately("/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi") imlib_context_set_image(ImlibImage) ' Viene creato un puntatore ai dati grezzi immagine: dati = imlib_image_get_data() w = imlib_image_get_width() h = imlib_image_get_height() ' 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 * 4) - 1 Read #st, b Print i, Hex(b) Next st.Close End