Reading a pixel

BlitzMax Forums/BlitzMax Beginners Area/Reading a pixel

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
'----------------------------------------------------------------------------------------------------------------------------------