Reading a pixel

BlitzMax Forums/BlitzMax Beginners Area/Reading a pixel

nawi(Posted 2005) [#1]
How can I read a pixel from TImage to return its R,G and B value?


klepto2(Posted 2005) [#2]
You have to convert the Image to TPixmap and then you can read the Pixel.


'Pseudo

Local Image:Timage = LoadImage("Test.jpg")

Function ReadPixel:Int(_Image:Timage,_X,_Y)
local Temp:Tpixmap = Lockimage(_Image)
Color = Temp.ReadPixel(_X,_Y)
UnlockImage(_Image)
Return Color
End Function




JazzieB(Posted 2005) [#3]
Carrying on from the above, if you would like the colour components returned into separate variables then the following function will be useful to you.
Function GetRGB(argb:Int,a Var,r Var, g Var, b Var)
	a=(argb Shr 24) & $ff
	r=(argb Shr 16) & $ff
	g=(argb Shr 8) & $ff
	b=argb & $ff
End Function

Global alpha:Int,red:Int,green:Int,blue:Int

GetRGB($f0ff800f,alpha,red,green,blue)

Simply pass the function the value returned from ReadPixel and the four variables you want the results returned in.


ImaginaryHuman(Posted 2005) [#4]
It would be a LOT faster to use glReadPixels, and only read off to main memory the area the size of the actual pixels you want to know about, otherwise you are doing a heck of a lot of unnecessary conversion and transfer.

All you need to do is glReadPixels() the pixel(s) you want to know, read it into some kind of buffer or bank or whatever, then use `Peek` or something to access the values.


xMicky(Posted 2005) [#5]
Similar to read a pixel from an image, I want to read a pixel from the backbuffer.
For anyone who wants to do the same here the compilation of the code samples posted above are put together:

Strict

Type appColor
  Field red:Int
  Field green:Int
  Field blue:Int 
  Field alpha:Int
End Type
   
Graphics 800, 600, 0
SetClsColor 0, 0, 0

Local pixColor:appColor =New appColor
Local y:Int =100' y-coordinate we want to read

Repeat

  Cls
  SetColor 255, 0, 0
  DrawLine 300, 0, 300, 600 ' can we detect this line through reading the backbuffer ?

  For Local x:Int =298 To 304 'x-coordinates we want to read
    pixColor =GetPixel(x, y)
    DebugLog "x :"+x+",y :"+y
    DebugLog "red  :" +pixColor.red
    DebugLog "green:" +pixColor.green
    DebugLog "blue :" +pixColor.blue
    DebugLog "alpha:" +pixColor.alpha
  Next
  DebugLog "----------------------------------------"  

  Flip
  FlushMem

Until KeyHit(KEY_ESCAPE)

End 

'----------------------------------------------------------------------------------------------------------------------------------
Function GetPixel:appColor(x:Int, y:Int)
  ' should read the backbuffer at x, y and return a Type-variable containing r, g, b, a
  Local result:appColor =New appColor

  Local tmp:TImage =CreateImage(1, 1, DYNAMICIMAGE)
  GrabImage tmp, x, y
  Local temp:Tpixmap =LockImage(tmp)
  Local argb:Int =Temp.ReadPixel(0, 0)
  UnlockImage(tmp)
  result.alpha =(argb Shr 24) & $ff
  result.red   =(argb Shr 16) & $ff
  result.green =(argb Shr 8) & $ff
  result.blue  =argb & $ff

  Return result

End Function
'----------------------------------------------------------------------------------------------------------------------------------



Suco-X(Posted 2005) [#6]
Hi
@Xmicky: Why you dont use GrabPixmap direct?
My Solution:


Strict

Graphics 800,600,0




Repeat
   Cls

   SetColor 255,0,0
   Plot 60,60

   Local r,g,b
   ColorOnPosition(60,60,r,g,b)

   SetColor 255,255,255
   DrawText "RED: "+r, 10,10
   DrawText "GREEN: "+g,10,30
   DrawText "BLUE: "+b,10,50


   Flip
   FlushMem()
Until KeyHit(KEY_ESCAPE)


Function ColorOnPosition(x:Int, y:Int, r:Int Var, g:Int Var, b:Int Var)
   Local TempPixmap:TPixmap = GrabPixmap(x,y,1,1)
   Local TempPtr:Byte Ptr = TempPixmap.PixelPtr(0,0)
   r = TempPtr[2]
   g = TempPtr[1]
   b = TempPtr[0]
End Function

Mfg suco


xMicky(Posted 2005) [#7]
Yes thats even better, thank you