Blitz3D+ Command Reference

ReadPixelFast ( x,y[,buffer] )

Parameters

x - x coordinate of the pixel; must be inside the buffer

y - y coordinate of the pixel; must be inside the buffer

buffer (optional) - buffer to read from, e.g. BackBuffer(); the current buffer set with SetBuffer (default)

Description

Returns the colour of a pixel as an ARGB integer, with all safety checks skipped for speed.

This is the raw-speed version of ReadPixel, made for tight loops that touch thousands of pixels - image filters, fire and plasma effects, terrain sampling. Use it between LockBuffer and UnlockBuffer, and it flies. The result unpacks the same way as ReadPixel's: (argb Shr 16) And 255 for red, and so on.

The speed comes from checking nothing. Coordinates are not clipped and Origin is ignored: reading outside the buffer's 0 to width-1, 0 to height-1 range reads memory that is not yours - garbage values at best, a crash at worst. Validate your loop bounds yourself.

Keep the classic rules: lock the buffer first (LockBuffer), read, unlock. On the modern runtime the lock is instant, but the no-bounds-checking danger is fully intact.

See also: ReadPixel, WritePixelFast, LockBuffer, UnlockBuffer.

Example

; ReadPixelFast Example
; ---------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

t#=0

While Not KeyDown(1)

    Cls
    t=t+4

    ; The fast pixel commands require a locked buffer
    LockBuffer BackBuffer()

    ; Write a plasma pattern pixel by pixel into a small panel
    For y=0 To 95
        For x=0 To 127
            v#=Sin(x*4+t)+Sin(y*5-t)+Sin((x+y)*3+t*2)
            r=128+v*42
            g=128+Sin(v*60+t)*127
            b=255-r
            WritePixelFast 256+x,192+y,$FF000000 Or (r Shl 16) Or (g Shl 8) Or b
        Next
    Next

    ; Clamp the mouse position into the plasma panel...
    mx=MouseX()
    my=MouseY()
    If mx<256 Then mx=256
    If mx>383 Then mx=383
    If my<192 Then my=192
    If my>287 Then my=287

    ; ...and read the ARGB value there back with ReadPixelFast
    argb=ReadPixelFast(mx,my)

    ; Unlock before using any other drawing command
    UnlockBuffer BackBuffer()

    ; Split the packed value into its colour components
    r=(argb Shr 16) And 255
    g=(argb Shr 8) And 255
    b=argb And 255

    ; Show the colour we read as a swatch
    Color r,g,b
    Rect 20,60,40,40,1
    Color 255,255,255
    Rect 255,191,130,98,0
    Text 0,0,"Move the mouse over the plasma panel   Esc: exit"
    Text 0,20,"ReadPixelFast("+mx+","+my+") = RGB "+r+","+g+","+b

    Flip

Wend

End

Index