Differenze tra le versioni di "Individuare i valori RGBA dei pixel di una immagine"

Da Gambas-it.org - Wikipedia.
 
(6 versioni intermedie di uno stesso utente non sono mostrate)
Riga 3: Riga 3:
  
 
==Uso delle sole risorse di Gambas==
 
==Uso delle sole risorse di Gambas==
Mediante la Classe ''Color'':    <SUP>&#091;[[#Note|nota 1]]&#093;</sup>
+
===Mediante la Classe ''ColorInfo''===
  '''Public''' Sub Button1_Click()
+
Si rinvia alla seguente pagina: [[Uso della classe ColorInfo]]
 +
 
 +
===Mediante la Classe ''Color''===
 +
<SUP>&#091;[[#Note|nota 1]]&#093;</sup>
 +
  Public Sub Button1_Click()
 
    
 
    
  Dim im As Image
+
  Dim im As Image
  Dim px, n As Integer
+
  Dim px, n As Integer
 
    
 
    
   im = Image.Load("<FONT Color=gray>' ''/percorso/del/file/immagine''</font>")
+
   im = Image.Load("<FONT Color=darkgreen>''/percorso/del/file/immagine''</font>")
 
    
 
    
 
   For n = 0 To im.Pixels.Max
 
   For n = 0 To im.Pixels.Max
Riga 18: Riga 22:
 
   Next
 
   Next
 
    
 
    
  '''End'''
+
  End
 
 
 
oppure distinguendo i quattro valori che compongono un pixel di formato RGBA:
 
oppure distinguendo i quattro valori che compongono un pixel di formato RGBA:
  '''Public''' Sub Button1_Click()
+
  Public Sub Button1_Click()
 
    
 
    
  Dim im As Image
+
  Dim im As Image
  Dim px, n As Integer
+
  Dim px, n As Integer
 
    
 
    
   im = Image.Load("<FONT Color=gray>' ''/percorso/del/file/immagine''</font>")
+
   im = Image.Load("<FONT Color=darkgreen>''/percorso/del/file/immagine''</font>")
 
    
 
    
 
   For n = 0 To im.Pixels.Max
 
   For n = 0 To im.Pixels.Max
Riga 35: Riga 38:
 
   Next
 
   Next
 
    
 
    
  '''End'''
+
  End
  
o anche penetrando nell'area di memoria dell'Oggetto ''Image'', dereferenziando la sua proprietà " ''.Data'' ", e leggendovi ogni byte-dato:
+
===Penetrando nell'area di memoria dell'Oggetto ''Image''===
  '''Public''' Sub Button1_Click()
+
Penetrando nell'area di memoria dell'Oggetto ''Image'', si dereferenzia la sua Proprietà ".Data" e si legge così ogni byte-dato:
 +
  Public Sub Button1_Click()
 
    
 
    
  Dim im As Image
+
  Dim im As Image
  Dim i As Integer
+
  Dim i, n As Integer
 +
 
 +
  im = Image.Load("<FONT Color=darkgreen>''/percorso/del/file/immagine''</font>")
 
    
 
    
   im = Image.Load("<FONT Color=gray>' ''/percorso/del/file/immagine''</font>")
+
   n = Len(bmp.Format)
 
    
 
    
   For i = 0 To (im.W * im.H * Len(im.Format)) - 1
+
   For i = 0 To (im.W * im.H * n) - 1
     If i Mod 4 = 0 Then Print
+
     If i Mod n = 0 Then Print
 
     Print i, Hex(Byte@(im.Data + i), 2)
 
     Print i, Hex(Byte@(im.Data + i), 2)
     Sleep 0.3
+
     Wait 0.3
 
   Next
 
   Next
 
    
 
    
  '''End'''
+
  End
  
  
Riga 58: Riga 64:
  
 
Per poter fruire in Gambas delle risorse di ''SDL'', è necessario installare e richiamare le seguenti librerie dinamiche condivise:
 
Per poter fruire in Gambas delle risorse di ''SDL'', è necessario installare e richiamare le seguenti librerie dinamiche condivise:
* "''libSDL2-2.0.so''"
+
* "''libSDL2-2.0.so.0.3000.3'' "
* "''libSDL2_image-2.0.so''"
+
* "''libSDL2_image-2.0.so.0.800.2'' "
  
  
 
Mostriamo un semplice esempio pratico:
 
Mostriamo un semplice esempio pratico:
  Library "libSDL2-2.0"
+
  Library "<FONT Color=blue>libSDL2-2.0:0.3000.3</font>"
 
    
 
    
 
  Public Struct SDL_Rect
 
  Public Struct SDL_Rect
Riga 119: Riga 125:
 
   
 
   
 
   
 
   
  Library "libSDL2_image-2.0"
+
  Library "<FONT Color=red>libSDL2_image-2.0:0.800.2</font>"
 
   
 
   
 
  <FONT Color=gray>' ''SDL_Surface * IMG_Load(const char *file)''
 
  <FONT Color=gray>' ''SDL_Surface * IMG_Load(const char *file)''
Riga 126: Riga 132:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
  Dim immagine As String
+
  Dim immagine As String
  Dim imago As SDL_Surface
+
  Dim immago As SDL_Surface
  Dim pxfmt As SDL_PixelFormat
+
  Dim pxfmt As SDL_PixelFormat
  Dim dati As Pointer
+
  Dim dati As Pointer
  Dim i As Integer
+
  Dim i As Integer
  Dim r, g, b, a As Byte
+
  Dim r, g, b, a As Byte
 
    
 
    
   immagine = "<FONT Color=gray>''/percorso/del/file/immagine''</font>"
+
   immagine = "<FONT Color=darkgreen>''/percorso/del/file/immagine''</font>"
 
    
 
    
 
   SDL_Init(SDL_INIT_VIDEO)
 
   SDL_Init(SDL_INIT_VIDEO)
 
    
 
    
 
  <FONT Color=gray>' ''Carica un'immagine:''</font>
 
  <FONT Color=gray>' ''Carica un'immagine:''</font>
   imago = IMG_Load(immagine)
+
   immago = IMG_Load(immagine)
   If IsNull(imago) Then Error.Raise("Impossibile caricare un'immagine !")
+
   If IsNull(immago) Then Error.Raise("Impossibile caricare un'immagine !")
 
    
 
    
  <FONT Color=gray>' ''Assegna il membro di tipo Puntatore della Struttura "SDL_Surface"  alla variabile di tipo della Struttura "SDL_PixelFormat",''
+
  <FONT Color=gray>' ''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":''</font>
' ''affinché possa essere utilizzato comodamente il membro ".BytesPerPixel" della predetta Struttura "SDL_PixelFormat":''</font>
+
   pxfmt = immago.format
   pxfmt = imago.format
 
 
   Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
 
   Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
 
    
 
    
   dati = imago.pixels
+
   dati = immago.pixels
 
    
 
    
   For i = 0 To (imago.w * imago.h * 4) - 1 Step 4
+
   For i = 0 To (imago.w * immago.h * (pxfmt.BitsPerPixel / 8)) - 1 Step pxfmt.BitsPerPixel / 8
 
     r = Byte@(dati + i)
 
     r = Byte@(dati + i)
 
     g = Byte@(dati + i + 1)
 
     g = Byte@(dati + i + 1)
 
     b = Byte@(dati + i + 2)
 
     b = Byte@(dati + i + 2)
     a = Byte@(dati + i + 3)
+
     Select Case pxfmt.BitsPerPixel
   
+
      Case 32
    Print "r: "; Hex(r, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2)
+
        a = Byte@(dati + i + 3)
 +
        Print "r: "; Hex(r, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2)
 +
      Case 24
 +
        Print "r: "; Hex(b, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(r, 2)
 +
    End Select
 
     Print "\n"
 
     Print "\n"
     Sleep 0.3
+
     Wait 0.3
 
   Next
 
   Next
 
        
 
        
Riga 164: Riga 173:
 
   SDL_Quit()
 
   SDL_Quit()
 
    
 
    
  '''End'''
+
  End
 
 
  
  

Versione attuale delle 15:05, 3 lug 2024

Per individuare i valori RGBA dei pixel di una immagine, possiamo adottare alcune modalità.


Uso delle sole risorse di Gambas

Mediante la Classe ColorInfo

Si rinvia alla seguente pagina: Uso della classe ColorInfo

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

Penetrando nell'area di memoria dell'Oggetto Image

Penetrando nell'area di memoria dell'Oggetto Image, si dereferenzia la sua Proprietà ".Data" e si legge così ogni byte-dato:

Public Sub Button1_Click()
 
  Dim im As Image
  Dim i, n As Integer
  
  im = Image.Load("/percorso/del/file/immagine")
  
  n = Len(bmp.Format)
  
  For i = 0 To (im.W * im.H * n) - 1
    If i Mod n = 0 Then Print
    Print i, Hex(Byte@(im.Data + i), 2)
    Wait 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.0.3000.3 "
  • "libSDL2_image-2.0.so.0.800.2 "


Mostriamo un semplice esempio pratico:

Library "libSDL2-2.0:0.3000.3"
 
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:0.800.2"

' 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 immago 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:
  immago = IMG_Load(immagine)
  If IsNull(immago) 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 = immago.format
  Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
  
  dati = immago.pixels
  
  For i = 0 To (imago.w * immago.h * (pxfmt.BitsPerPixel / 8)) - 1 Step pxfmt.BitsPerPixel / 8
    r = Byte@(dati + i)
    g = Byte@(dati + i + 1)
    b = Byte@(dati + i + 2)
    Select Case pxfmt.BitsPerPixel
      Case 32
        a = Byte@(dati + i + 3)
        Print "r: "; Hex(r, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2)
      Case 24
        Print "r: "; Hex(b, 2), i; "\ng: "; Hex(g, 2); "\nb: "; Hex(r, 2)
    End Select
    Print "\n"
    Wait 0.3
  Next
     
' Libera la libreria "SDL2" e va in chiusura:
  SDL_Quit()
  
End


Note

[1] La modalità è stata suggerita dal membro Gianluigi