Blitz3D Docs -> 2D - Category -> Graphics -> ReadPixelFast
ReadPixelFast (x,y,[buffer])
Parameters:
x - y-coordinate of pixely - y-coordinate of pixel
buffer (optional) - name of buffer to read colour value from, e.g. BackBuffer()
Description:
Reads a color value from either the current buffer or the specified buffer, and returns it.The returned colour value is in the form of an integer than contains the alpha, red, green and blue values of the pixel.
IMPORTANT:
You *must* use this command on a locked buffer, otherwise the command will fail. See LockBuffer.
Also, you must make sure that the coordinates that you are reading from are valid, otherwise you will end up reading garbage values.
WARNING:
By not following the above advice, you may cause your computer to crash.
See also: GetColor, ReadPixel.
Example:
; ReadPixelFast/WritePixeFast Example
; -----------------------------------
Graphics 640,480,16
Print "Press a key to read color values"
WaitKey()
; Load and draw an image on to the screen - can be anything
pic=LoadImage("media/blitz_pic.bmp")
DrawImage pic,0,0
; Initialise an array big enough to fit all the color information of the screen
Dim pix(GraphicsWidth(),GraphicsHeight())
; Lock buffer before using ReadPixelFast
LockBuffer
; Use ReadPixel to get all the color information of the screen
For y=0 To GraphicsHeight()
For x=0 To GraphicsWidth()
pix(x,y)=ReadPixelFast(x,y)
Next
Next
; Lock buffer after using ReadPixelFast
UnlockBuffer
Cls
Locate 0,0
Print "Press a key to write pixels"
Print "Once this has finished, you can then press a key to end the program"
WaitKey()
; Lock buffer before using WritePixelFast
LockBuffer
; Use WritePixel to redraw the screen using the color information we got earlier
For y=0 To GraphicsHeight()
For x=0 To GraphicsWidth()
WritePixelFast x,y,pix(x,GraphicsHeight()-y) ; get y array value in backwards order, to flip screen
Next
Next
; Unlock buffer after using WritePixelFast
UnlockBuffer
WaitKey()
Comments
| ||
| The example code gives an MAV in windowed mode after the first key press, but works ok in fullscreen. I'm using Win7. |
| ||
| If you are using a bank to create or parse the integer color value used with ReadPixel, WritePixel etc, then the bytes that make up the pixel color data are arranged in the following order. Bank slot:- Byte 0 = Blue Byte 1 = Green Byte 2 = Red Byte 3 = Alpha If you are creating or parsing the pixel color data mathematically then the least significant byte (blue_color_value * 2 ^ 0) holds the color blue. The next higher byte (green_color_value * 2 ^ 8) holds the color green. The next higher byte (red_color_value * 2 ^ 16) holds the color red. The most significant byte (alpha_value * 2 ^ 24) holds the alpha value. Color byte values range from zero (darkest, or least color) to 255 (lightest, or most color). The alpha value determines how opaque (visible) the pixel is. An alpha value of zero means that the pixel is invisible. An alpha value of 255 means that the pixel is fully visible. Anything in-between means that the pixel is transparent. You can find some code which shows the byte ordering for each color component at: http://www.blitzbasic.com/codearcs/codearcs.php?code=2999 You can also learn more about RGBA color encoding at: http://en.wikipedia.org/wiki/RGBA |
Blitz3D Manual Forum
BlitzPlus Equivalent Command




