Differenze tra le versioni di "Individuare il colore di un determinato pixel"

Da Gambas-it.org - Wikipedia.
Riga 113: Riga 113:
 
   gdImageDestroy(imago)
 
   gdImageDestroy(imago)
 
      
 
      
 +
'''End'''
 +
 +
 +
==Uso delle risorse della libreria SDL2==
 +
Si potrà fare uso anche delle risorse del API del sistema SDL
 +
 +
Per poter fruire in Gambas delle risorse di ''SDL'', è necessario installare e richiamare le seguenti librerie dinamiche condivise:
 +
* "''libSDL2-2.0.so.0.2.0''"
 +
* "''libSDL2_image-2.0.o.0.0.0''"
 +
 +
 +
Mostriamo un semplice esempio:
 +
Library "libSDL2-2.0:0.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 = 32
 +
 +
<FONT Color=gray>' ''int SDL_Init(Uint32 flags)''
 +
' ''Initializes the SDL library.''</font>
 +
Private Extern SDL_Init(flags As Integer) As Integer
 +
 +
<FONT Color=gray>' ''void SDL_Quit(void)''
 +
' ''Clean up all initialized subsystems.''</font>
 +
Private Extern SDL_Quit()
 +
 +
 +
Library "libSDL2_image-2.0:0.0.0"
 +
 +
<FONT Color=gray>' ''SDL_Surface * IMG_Load(const char *file)''
 +
' ''Load an image from an SDL data source.''</font>
 +
Private Extern IMG_Load(_file As String) As SDL_Surface
 +
 +
 +
'''Public''' Sub Main()
 +
 
 +
  Dim x, y As Integer
 +
  Dim imago As SDL_Surface
 +
  Dim pxfmt As SDL_PixelFormat
 +
  Dim dati As Pointer
 +
  Dim r, g, b, a As Byte
 +
 
 +
  SDL_Init(SDL_INIT_VIDEO)
 +
 
 +
  imago = IMG_Load("/home/ploppo/Scrivania/IMMAGINI/imago.png")
 +
  If IsNull(imago) Then Error.Raise("Errore nel caricamento del file immagine!")
 +
 
 +
  pxfmt = imago.format
 +
  Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
 +
 
 +
<FONT Color=gray>' ''Invividua il pixel di cui conoscere i colori RGBA costituenti:''</font>
 +
  x = 100
 +
  y = 39
 +
 
 +
  dati = imago.pixels
 +
  dati += 4 * (x + imago.w * y)
 +
 
 +
  r = Byte@(dati)
 +
  g = Byte@(dati + 1)
 +
  b = Byte@(dati + 2)
 +
  a = Byte@(dati + 3)
 +
  Print "r: "; Hex(r, 2); "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2);
 +
 
 +
  SDL_Quit()
 +
 
 
  '''End'''
 
  '''End'''
  

Versione delle 08:16, 16 mag 2017

Mostriamo di seguito un paio di modalità per individuare il colore di un determinato pixel di un'immagine.


Uso delle sole risorse di Gambas

Volendo utilizzare le sole riorse di Gambas, si potrà utilizzare semplicemente il seguente codice:

Public Sub Button1_Click()

 Dim im As Image
 Dim nCol As Integer 

  im = Image.Load("/percorso/del/file/immagine")

' Individueremo il pixel dell'immagine mediante le sue coordinate x ed y (che ovviamente dovranno essere specificate):
   nCol = im[x, y]

   Print "Il numero decimale del colore del pixel è: "; nCol, "&h"; Hex(nCol, 8)

End

Verrà restituito il numero decimale del colore del pixel, determinato dalle coordinate x ed y all'interno dell'immagine.


Individuare il colore di un pixel di un'immagine cliccandoci sopra con il mouse

Mostriamo un esempio nel quale, cliccando su un'immagine inserita in una PictureBox, viene mostrato in una TextLabel il colore del pixel, nonché in console il corrispondente numero:

Private im As Image
 

Public Sub Form_Open()

 Dim pc As Picture

' Carichiamo l'immagine in una variabile di tipo "Image"...
  im = Image.Load("/percorso/del/file/immagine")

' ...e la convertiamo in una "Picture":
  pc = im.Picture

' Rendiamo la "PictureBox" delle medesime dimensioni dell'immagine caricata:
  With PictureBox1
    .W = im.W
    .H = im.H
' Quindi carichiamo l'immagine, contenuta nella variabile di tipo "Picture", nella "PictureBox":
    .Picture = pc
  End With

End


Public Sub PictureBox1_MouseDown()
 
 Dim nCol As Integer

' Individuiamo le coordinate x ed y del puntatore del mouse, quando clicchiamo nella "PictureBox" sull'immagine:
  With Mouse
     nCol = im[.X, .Y]
  End With

' Quindi otteniamo il numero del colore del pixel cliccato con il mouse...
  Print "Il numero del colore del pixel cliccato è: "; nCol, "&h"; Hex(nCol, 8)

' ...nonché mostriamo il suo colore nella "TextLabel" posta sul Form:
  TextLabel1.Background = nCol
 
End


Uso delle risorse della libreria Libgd

Si potranno anche utilizzare le risorse del API di GD.

Per poter fruire in Gambas delle risorse della libreria GD, è necessario installare e richiamare la libreria dinamica e condivisa: "libgd.so.3.0.0"


Mostriamo un semplice esempio:

Library "libgd:3.0.0"

' gdImagePtr gdImageCreateFromPng (FILE * infile)
' Creates an image.
Private Extern gdImageCreateFromPng(infile As Pointer) As Pointer

' int gdImageGetPixel (gdImagePtr im, int x, int y)
Private Extern gdImageGetPixel(im As Pointer, x As Integer, y As Integer) As Integer

' void gdImageDestroy (gdImagePtr im)
Private Extern gdImageDestroy(im As Pointer)


Library "libc:6"

' FILE *fopen(const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(__filename As String, __mode As String) As Integer

' int fclose(FILE *__stream)
' Closes the stream. All buffers are flushed.
Private Extern fclose(flstream As Pointer) As Integer


Public Sub Main()
 
 Dim fl, imago as Pointer
 Dim i As Integer
 
  fl = fopen("/percorso/del/file.png", "r")
  
  imago = gdImageCreateFromPng(fl)
  
' Individua il pixel ad esempio alle coordinate x=20, y=20 dell'immagine:
  i = gdImageGetPixel(imago, 20, 20)
  
  Print "Il colore del pixel è: "; Hex(i, 6)
  
' Va in chiusura:
  fclose(fl)    
  gdImageDestroy(imago)
   
End


Uso delle risorse della libreria SDL2

Si potrà fare uso anche delle risorse del API del sistema SDL

Per poter fruire in Gambas delle risorse di SDL, è necessario installare e richiamare le seguenti librerie dinamiche condivise:

  • "libSDL2-2.0.so.0.2.0"
  • "libSDL2_image-2.0.o.0.0.0"


Mostriamo un semplice esempio:

Library "libSDL2-2.0:0.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 = 32

' int SDL_Init(Uint32 flags)
' Initializes 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.0.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 x, y As Integer
 Dim imago As SDL_Surface
 Dim pxfmt As SDL_PixelFormat
 Dim dati As Pointer
 Dim r, g, b, a As Byte
 
  SDL_Init(SDL_INIT_VIDEO)
  
  imago = IMG_Load("/home/ploppo/Scrivania/IMMAGINI/imago.png")
  If IsNull(imago) Then Error.Raise("Errore nel caricamento del file immagine!")
  
  pxfmt = imago.format
  Print "Formato immagine: "; pxfmt.BitsPerPixel; " bit per pixel"
  
' Invividua il pixel di cui conoscere i colori RGBA costituenti:
  x = 100
  y = 39
  
  dati = imago.pixels
  dati += 4 * (x + imago.w * y)
  
  r = Byte@(dati)
  g = Byte@(dati + 1)
  b = Byte@(dati + 2)
  a = Byte@(dati + 3)
  Print "r: "; Hex(r, 2); "\ng: "; Hex(g, 2); "\nb: "; Hex(b, 2); "\na: "; Hex(a, 2); 
  
  SDL_Quit()
  
End



Note

[1] Per analogia di argomento si segnala anche questa pagina della Wiki.