Individuare i valori RGBA dei pixel di una immagine
Vai alla navigazione
Vai alla ricerca
Per individuare i valori RGBA dei pixel di una immagine, possiamo adottare alcune modalità.
Uso delle sole risorse di Gambas
Mediante la Classe Color: [nota 1]
Public Sub Button1_Click()
Dim im As Image
Dim px, n As Integer
im = Image.Load("' /percorso/del/file/immagine")
For n = 0 To im.Pixels.Max
px = im.Pixels[n]
Print n, "Pixel: "; Hex(px, 8)
Print Null, Hex(Color[px].Alpha, 2), Hex(Color[px].Red, 2), Hex(Color[px].Green, 2), Hex(Color[px].Blue, 2)
Print
Next
End
oppure distinguendo i quattro valori che compongono un pixel di formato RGBA:
Public Sub Button1_Click()
Dim im As Image
Dim px, n As Integer
im = Image.Load("' /percorso/del/file/immagine")
For n = 0 To im.Pixels.Max
px = im.Pixels[n]
Print n, "Pixel: "; Hex(px, 8)
Print Null, Hex(Shr(px, 24) And &FF, 2), Hex(Shr(px, 16) And &FF, 2), Hex(Shr(px, 8) And &FF, 2), Hex(px And &FF, 2)
Print
Next
End
o anche penetrando nell'area di memoria dell'Oggetto Image, dereferenziando la sua proprietà " .Data ", e leggendovi ogni byte-dato:
Public Sub Button1_Click()
Dim im As Image
Dim i As Integer
im = Image.Load("' /percorso/del/file/immagine")
For i = 0 To im.W * im.H * Len(im.Format)
If i Mod 4 = 0 Then Print
Print i, Hex(Byte@(im.Data + i), 2)
Sleep 0.3
Next
End
Individuare i valori RGBA dei pixel di una immagine mediante le risorse del API di SDL2
Con alcune risorse del API di SDL2 è possibile conoscere i valori RGBA dei pixel di una immagine.
Per poter fruire in Gambas delle risorse di SDL, è necessario installare e richiamare le seguenti librerie dinamiche condivise:
- "libSDL2-2.0.so"
- "libSDL2_image-2.0.so"
Mostriamo un semplice esempio pratico:
Library "libSDL2-2.0"
Public Struct SDL_Rect
x As Integer
y As Integer
w As Integer
h As Integer
End Struct
Public Struct SDL_Surface
flags As Integer
format As Pointer
w As Integer
h As Integer
pitch As Integer
pixels As Pointer
userdata As Pointer
locked As Integer
lock_data As Pointer
clip_rect As Struct SDL_Rect
map As Pointer
refcount As Integer
End Struct
Public Struct SDL_PixelFormat
format As Integer
palette As Pointer
BitsPerPixel As Byte
BytesPerPixel As Byte
Rmask As Integer
Gmask As Integer
BMask As Integer
AMask As Integer
Rloss As Byte
Gloss As Byte
Bloss As Byte
Aloss As Byte
Rshift As Byte
Gshift As Byte
Bshift As Byte
Ashift As Byte
refcount As Integer
next_ As Pointer
End Struct
Private Const SDL_INIT_VIDEO As Integer = &20
' int SDL_Init(Uint32 flags)
' Initialize the SDL library.
Private Extern SDL_Init(flags As Integer) As Integer
' void SDL_Quit(void)
' Clean up all initialized subsystems.
Private Extern SDL_Quit()
Library "libSDL2_image-2.0"
' SDL_Surface * IMG_Load(const char *file)
' Load an image from an SDL data source.
Private Extern IMG_Load(_file As String) As SDL_Surface
Public Sub Main()
Dim immagine As String
Dim imago As SDL_Surface
Dim pxfmt As SDL_PixelFormat
Dim dati As Pointer
Dim i As Integer
Dim r, g, b, a As Byte
immagine = "/percorso/del/file/immagine"
SDL_Init(SDL_INIT_VIDEO)
' Carica un'immagine:
imago = IMG_Load(immagine)
If IsNull(imago) Then Error.Raise("Impossibile caricare un'immagine !")
' Assegna il membro di tipo Puntatore della Struttura "SDL_Surface" alla variabile di tipo della Struttura "SDL_PixelFormat",
' affinché possa essere utilizzato comodamente il membro ".BytesPerPixel" della predetta Struttura "SDL_PixelFormat":
pxfmt = imago.format
Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
dati = imago.pixels
For i = 0 To (imago.w * imago.h * 4) - 1 Step 4
r = Byte@(dati + i)
g = Byte@(dati + i + 1)
b = Byte@(dati + i + 2)
a = Byte@(dati + i + 3)
Print "r: "; Hex(r, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2)
Print "\n"
Sleep 0.3
Next
' Libera la libreria "SDL2" e va in chiusura:
SDL_Quit()
End
Note
[1] La modalità è stata suggerita dal membro Gianluigi