# Command: ReadPixel

ReadPixel (x,y,[buffer]) Definition:

Quickly reads the value of a single pixel on the screen.

Parameter Description:

x = x location of the pixel to read y = y location of the pixel to read buffer = any valid screen/image buffer (optional)

Command Description:

This command will allow you fast access to a specific pixel in the buffer selected. While this command reads the pixel quickly (and it can be written back quickly with WritePixel, it is still not fast enough for real-time screen effects.
You are not required to lock the buffer with LockBuffer and subsequently unlock the buffer with UnlockBuffer. However, the operations will be a bit faster if you do.
V1.52 and above: It has been necessary to make ReadPixel and ReadPixelFast "alpha-aware".
This means that the high 8 bits of the color returned ReadPixel/ReadPixelFast now contain valid alpha information.
However, in the case of Blitz2D apps, there is no alpha information in images!
Mark decided that in the abscence of any alpha information, a pixel is assumed to have 'full' alpha.
Therefore, values returned by ReadPixel/ReadPixelFast will now (usually) have their high 8 bits sets. The exception will be when reading pixels from Blitz3D textures created with an alpha channel.
To get the old behaviour of v1.50 (and below) of ReadPixel/ReadPixelFast, use the following:
rgb=ReadPixel( x,y ) And $FFFFFF
This will 'mask out' the 8 high bits of alpha and return just the red, green and blue components of the pixel.

Example:

; High Speed Graphics Commands
Graphics 640,480,16
; Draw a bunch of crap on the screen
For t= 1 To 1000
Color Rnd(255),Rnd(255),Rnd(255)
Rect Rnd(640),Rnd(480),Rnd(150),Rnd(150),Rnd(1)
Next
Delay 3000
; Copy the top half of the screen over the bottom half
; using fast pixels and locked buffers
LockBuffer FrontBuffer()
For x = 1 To 640
For y = 1 To 240
WritePixelFast x,y+241,ReadPixelFast(x,y)
Next
Next
UnlockBuffer FrontBuffer()
Delay 3000
; Draw the left half of the screen over the right half
; using the slower direct pixel access
For x = 1 To 320
For y = 1 To 480
WritePixel x+320,y,ReadPixel(x,y)
Next
Next

Index