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("<FONT Color=gray>''/percorso/del/file/immagine/creato''</font>")
+
   imlib_save_image("<FONT Color=darkgreen>''/percorso/del/file/immagine/creato''</font>")
 
   
 
   
 
  End
 
  End
 
 
  
 
Si potrà creare un'immagine anche da dati grezzi ottenuti da una specifica funzione esterna della libreria ''Imlib2''.
 
Si potrà creare un'immagine anche da dati grezzi ottenuti da una specifica funzione esterna della libreria ''Imlib2''.
Riga 91: Riga 89:
 
   Dim w, h, i As Integer
 
   Dim w, h, i As Integer
 
    
 
    
   ImlibImage = imlib_load_image_immediately("<FONT Color=gray>''/percorso/del/file/immagine/di/cui/verranno/estratti/i dati/grezzi''</font>")
+
   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 !")
 
   If ImlibImage == 0 Then Error.Raise("Errore !")
 
   
 
   
Riga 113: Riga 111:
 
    
 
    
 
  <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=gray>''/percorso/del/file/immagine/creato''</font>")
+
   imlib_save_image("<FONT Color=darkgreen>''/percorso/del/file/immagine/creato''</font>")
 
   
 
   
 
  End
 
  End

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


Riferimenti