Reads red channel from locked image (requires b2dLockImage first, fast CPU buffer access, 0-255).
Takes imageHandle (locked image handle, positive integer, must have isLocked=true), x (horizontal pixel coordinate, integer, 0=left edge), y (vertical pixel coordinate, integer, 0=top edge).
Returns red channel value (0-255 integer, 0=no red, 255=full red), returns 0 on error (not locked, invalid handle, or out-of-bounds coordinates).
2D Overlay
Parameters & Returns
Parameters
imageHandleInt
xInt
yInt
Returns
Int
Quick Summary
Reads red channel from locked image (requires b2dLockImage first, fast CPU buffer access, 0-255).
Takes imageHandle (locked image handle, positive integer, must have isLocked=true), x (horizontal pixel coordinate, integer, 0=left edge), y (vertical pixel coordinate, integer, 0=top edge).
Returns red channel value (0-255 integer, 0=no red, 255=full red), returns 0 on error (not locked, invalid handle, or out-of-bounds coordinates).
Technical Exegesis...
Reads red channel from locked image (requires b2dLockImage first, fast CPU buffer access, 0-255). Takes imageHandle (locked image handle, positive integer, must have isLocked=true), x (horizontal pixel coordinate, integer, 0=left edge), y (vertical pixel coordinate, integer, 0=top edge). Returns red channel value (0-255 integer, 0=no red, 255=full red), returns 0 on error (not locked, invalid handle, or out-of-bounds coordinates). Reads from CPU buffer (img.
Reads red channel from locked image (requires b2dLockImage first, fast CPU buffer access, 0-255). Takes imageHandle (locked image handle, positive integer, must have isLocked=true), x (horizontal pixel coordinate, integer, 0=left edge), y (vertical pixel coordinate, integer, 0=top edge). Returns red channel value (0-255 integer, 0=no red, 255=full red), returns 0 on error (not locked, invalid handle, or out-of-bounds coordinates). Reads from CPU buffer (img.cpuPixelBuffer, direct memory access), calculates pixel index (index = (yxwidth+x)x4 row-major RGBA layout), returns R byte (buffer[index+0] red channel at offset 0). Use to sample color (read pixel colors for analysis), detect features (find edges, patterns, specific colors), implement collision (check alpha or color at overlap), or process image data (filters, effects, statistics). Lock requirement: MUST call b2dLockImage first (downloads texture to CPU buffer, enables pixel access), fast access (direct CPU memory read, no GPU overhead per pixel), many reads efficient (one lock enables many GetPixel calls without GPU round-trips). Use cases: (1) Color sampling (b2dLockImage(img): r=b2dGetPixelR(img,x,y): g=b2dGetPixelG(img,x,y): b=b2dGetPixelB(img,x,y): analyze color: b2dUnlockImage(img)), (2) Find specific color (For y=0 To h: For x=0 To w: If b2dGetPixelR(img,x,y)>200 Then found red pixel), (3) Edge detection (compare r at x,y vs x+1,y for gradient), (4) Color statistics (sum=0: For each pixel: sum+=b2dGetPixelR(img,x,y): avg=sum/count compute average red), (5) Pixel-perfect collision (If b2dGetPixelR(img1,x,y)>0 And b2dGetPixelA(img2,x,y)>0 Then collision). Common patterns: read RGB = r=b2dGetPixelR(img,x,y): g=b2dGetPixelG(img,x,y): b=b2dGetPixelB(img,x,y) full color; scan image = For y=0 To h-1: For x=0 To w-1: r=b2dGetPixelR(img,x,y) iterate all pixels. RGBA format: R at offset+0 (red channel first byte), G at offset+1 (green channel second byte), B at offset+2 (blue channel third byte), A at offset+3 (alpha channel fourth byte), 4 bytes per pixel (RGBA tightly packed). Coordinate system: origin top-left (0,0 = top-left corner of image), x rightward (0=left edge, width-1=right edge), y downward (0=top edge, height-1=bottom edge), row-major (pixels stored left-to-right, top-to-bottom). Index calculation: pixel index = (y x width + x) x 4 (offset to pixel's first byte), R byte = buffer[index+0] (add 0 for red channel), bounds check (x must be 0 to width-1, y must be 0 to height-1), out-of-bounds returns 0 (no error, silently returns 0 for invalid coordinates). Performance: access cost zero (direct CPU memory read, single array index and return), no GPU overhead (all reads from CPU buffer, no GPU round-trips), amortized lock cost (one lock GPU->CPU copy amortizes over many GetPixel calls), efficient for batch (reading many pixels much faster than GPU-based per-pixel access). Validation: prints error if not locked (img.isLocked=false or cpuPixelBuffer=nullptr, returns 0), returns 0 if invalid handle (imageHandle not found in g_images map), returns 0 if out-of-bounds (x<0 or x>=width or y<0 or y>=height, silently clamps to 0), no bounds clamping (only check, doesn't adjust coordinates). Related: b2dGetPixelG reads green channel (same usage pattern, offset+1 in buffer), b2dGetPixelB reads blue channel (same usage pattern, offset+2 in buffer), b2dGetPixelA reads alpha channel (same usage pattern, offset+3 in buffer), b2dLockImage enables pixel access (required before this function, downloads texture to CPU), b2dUnlockImage completes cycle (required after done reading, frees CPU buffer), b2dSetPixel writes pixel (modifies locked image, pair with GetPixel for read-modify-write).