expression
Locate an RGB pixel in a flat byte buffer
An RGB8 image stores three unsigned bytes per pixel in this tightly packed, row-major format. Coordinates start at the upper-left; x is the column and y the row. Validate dimensions, coordinates and buffer length before slicing. Padded rows and planar formats need different indexing. This example has no alpha channel. Expected result (NOT RUN): bottom left: [0, 0, 255] outside: true Common mistake: Using a pixel index directly as a byte offset or trusting unchecked dimension multiplication. Static Rust example; not a verified ByteMind executable assessment.
Formal Syntax & Structure
rust syntaxrust
offset = (y * width + x) * 3Working Example
Example Snippetrust
fn pixel(data: &[u8], w: usize, h: usize, x: usize, y: usize) -> Option<&[u8]> { if x >= w || y >= h || w.checked_mul(h)?.checked_mul(3)? != data.len() { return None; } let i = y.checked_mul(w)?.checked_add(x)?.checked_mul(3)?; data.get(i..i.checked_add(3)?)}fn main() { let data = [255,0,0, 0,255,0, 0,0,255, 255,255,255]; println!("bottom left: {:?}", pixel(&data,2,2,0,1).unwrap()); println!("outside: {}", pixel(&data,2,2,2,1).is_none());}