Skip to main content

split_run

Function split_run 

Source
pub fn split_run(written: u32, count: u32) -> (u32, u32, u32)
Expand description

Split a run of count pixels into the part that finishes the current byte, the whole bytes after it, and the remainder.

Upstream replaced push_pixel and push_pixel_chunk with a single push_pixels, which is the better shape: the implementor sees the whole run and decides how to write it. But every implementor that writes bytes rather than bits then needs the same three-way split, and an off-by-one in that arithmetic corrupts image data silently rather than failing.

written is how many bits the implementor has already put into its current byte, so written % 8 == 0 means it is aligned and can start writing bytes immediately. Returns (prefix_pixels, whole_bytes, tail_pixels), where prefix_pixels + whole_bytes * 8 + tail_pixels == count.

assert_eq!(split_run(0, 24), (0, 3, 0));   // aligned, three whole bytes
assert_eq!(split_run(3, 24), (5, 2, 3));   // three bits in: 5 + 16 + 3
assert_eq!(split_run(0, 5), (0, 0, 5));    // too short for a byte