Differenze tra le versioni di "Creare un file immagine da dati immagine grezzi mediante le funzioni del API di Imlib2"
(Creata pagina con '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 d...') |
|||
(9 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
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''. | 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 | + | 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: "''libImlib2.so.1.12.2'' ". |
Mostriamo un semplice esempio attinente al presente argomento: | Mostriamo un semplice esempio attinente al presente argomento: | ||
− | Library "libImlib2:1. | + | Library "libImlib2:1.12.2" |
<FONT Color=gray>' ''Imlib_Image imlib_create_image_using_data(int width, int height, DATA32 * data)'' | <FONT Color=gray>' ''Imlib_Image imlib_create_image_using_data(int width, int height, DATA32 * data)'' | ||
Riga 12: | Riga 12: | ||
<FONT Color=gray>' ''void imlib_context_set_image(Imlib_Image image)'' | <FONT Color=gray>' ''void imlib_context_set_image(Imlib_Image image)'' | ||
− | ' Sets the current image Imlib2 will be using with its function calls.''</font> | + | ' ''Sets the current image Imlib2 will be using with its function calls.''</font> |
Private Extern imlib_context_set_image(Imlib_Image As Pointer) | Private Extern imlib_context_set_image(Imlib_Image As Pointer) | ||
+ | |||
+ | <FONT Color=gray>' ''void imlib_image_set_has_alpha(char has_alpha)'' | ||
+ | ' ''Sets the alpha flag for the current image.''</font> | ||
+ | Private Extern imlib_image_set_has_alpha(has_alpha As Boolean) | ||
<FONT Color=gray>' ''void imlib_save_image(const char *filename)'' | <FONT Color=gray>' ''void imlib_save_image(const char *filename)'' | ||
− | ' ''Saves the current image in the format specified by the current image's format settings to the | + | ' ''Saves the current image in the format specified by the current image's format settings to the filename.''</font> |
Private Extern imlib_save_image(filename As String) | Private Extern imlib_save_image(filename As String) | ||
− | + | Public Sub Form_Open() | |
Dim im As Image | Dim im As Image | ||
− | Dim | + | Dim imago As Pointer |
− | + | im = Image.Load("<FONT Color=darkgreen>''/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi''</font>") | |
<FONT Color=gray>' ''Viene creata l'immagine dai dati grezzi:''</font> | <FONT Color=gray>' ''Viene creata l'immagine dai dati grezzi:''</font> | ||
− | + | imago = imlib_create_image_using_data(im.W, im.H, <FONT Color=#B22222>im.Data</font>) | |
+ | If imago == 0 Then Error.Raise("Errore !") | ||
− | + | imlib_context_set_image(imago) | |
+ | |||
+ | <FONT Color=gray>' ''<SPAN Style="text-decoration:underline">Volendo</span>, può essere assegnato all'immagine il canale "alfa" per la trasparenza (se la supporta) dello sfondo:''</font> | ||
+ | imlib_image_set_has_alpha(True) | ||
<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("<FONT Color=darkgreen>''/percorso/del/file/immagine/creato''</font>") | |
− | + | End | |
+ | Si potrà creare un'immagine anche da dati grezzi ottenuti da una specifica funzione esterna della libreria ''Imlib2''. | ||
+ | <BR>In quest'altro esempio l'applicazione sarà ''a riga di comando'', al di fuori quindi di ambiente grafico. | ||
+ | Library "libImlib2:1.12.2" | ||
+ | |||
+ | <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 | ||
+ | |||
+ | <FONT Color=gray>' ''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.''</font> | ||
+ | Private Extern imlib_create_image_using_data(width As Integer, height As Integer, data As Pointer) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''void imlib_image_set_has_alpha(char has_alpha)'' | ||
+ | ' ''Sets the alpha flag for the current image.''</font> | ||
+ | Private Extern imlib_image_set_has_alpha(has_alpha As Boolean) | ||
+ | |||
+ | <FONT Color=gray>' ''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.''</font> | ||
+ | Private Extern imlib_save_image(filename As String) | ||
+ | |||
+ | |||
+ | Public Sub Main() | ||
+ | |||
+ | Dim ImlibImage, dati, imago As Pointer | ||
+ | Dim w, h, i As Integer | ||
+ | |||
+ | ImlibImage = imlib_load_image_immediately("<FONT Color=darkgreen>''/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi''</font>") | ||
+ | If ImlibImage == 0 Then Error.Raise("Errore !") | ||
+ | |||
+ | imlib_context_set_image(ImlibImage) | ||
+ | |||
+ | <FONT Color=gray>' ''Viene creato un puntatore ai dati grezzi immagine:''</font> | ||
+ | dati = imlib_image_get_data() | ||
+ | If dati == 0 Then Error.Raise("Errore !") | ||
+ | |||
+ | w = imlib_image_get_width() | ||
+ | h = imlib_image_get_height() | ||
+ | |||
+ | <FONT Color=gray>' ''Viene creata l'immagine dai dati grezzi:''</font> | ||
+ | imago = imlib_create_image_using_data(w, h, dati) | ||
+ | If imago == 0 Then Error.Raise("Errore !") | ||
+ | |||
+ | imlib_context_set_image(imago) | ||
+ | |||
+ | <FONT Color=gray>' ''<SPAN Style="text-decoration:underline">Volendo</span>, può essere assegnato all'immagine il canale "Alfa" per la trasparenza (se la supporta) dello sfondo:''</font> | ||
+ | imlib_image_set_has_alpha(True) | ||
+ | |||
+ | <FONT Color=gray>' ''L'immagine creata viene salvata nel nuovo file immagine:''</font> | ||
+ | imlib_save_image("<FONT Color=darkgreen>''/percorso/del/file/immagine/creato''</font>") | ||
+ | |||
+ | End | ||
=Riferimenti= | =Riferimenti= | ||
− | * | + | * https://docs.enlightenment.org/api/imlib2/html/ |
Versione attuale delle 16:29, 21 mar 2024
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: "libImlib2.so.1.12.2 ".
Mostriamo un semplice esempio attinente al presente argomento:
Library "libImlib2:1.12.2"
' 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 imago As Pointer
im = Image.Load("/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi")
' Viene creata l'immagine dai dati grezzi:
imago = imlib_create_image_using_data(im.W, im.H, im.Data)
If imago == 0 Then Error.Raise("Errore !")
imlib_context_set_image(imago)
' 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.
In quest'altro esempio l'applicazione sarà a riga di comando, al di fuori quindi di ambiente grafico.
Library "libImlib2:1.12.2"
' 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
' 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_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 Main()
Dim ImlibImage, dati, imago As Pointer
Dim w, h, i As Integer
ImlibImage = imlib_load_image_immediately("/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi")
If ImlibImage == 0 Then Error.Raise("Errore !")
imlib_context_set_image(ImlibImage)
' Viene creato un puntatore ai dati grezzi immagine:
dati = imlib_image_get_data()
If dati == 0 Then Error.Raise("Errore !")
w = imlib_image_get_width()
h = imlib_image_get_height()
' Viene creata l'immagine dai dati grezzi:
imago = imlib_create_image_using_data(w, h, dati)
If imago == 0 Then Error.Raise("Errore !")
imlib_context_set_image(imago)
' 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