use super::*;
impl Framebuffer {
#[must_use]
pub fn allocate(width: u32, height: u32) -> Self {
let w: u32 = width.max(1);
let h: u32 = height.max(1);
let byte_len: usize = (w as usize).saturating_mul(h as usize).saturating_mul(4);
Self {
width: w,
height: h,
pixels: vec![0; byte_len],
}
}
pub fn resize(&mut self, width: u32, height: u32) -> bool {
let w: u32 = width.max(1);
let h: u32 = height.max(1);
if self.get_width() == w && self.get_height() == h {
return false;
}
self.set_width(w);
self.set_height(h);
let byte_len: usize = (w as usize).saturating_mul(h as usize).saturating_mul(4);
self.get_mut_pixels().resize(byte_len, 0);
true
}
pub fn clear(&mut self, color: Color) {
fill_rgba_span_32(self.get_mut_pixels().as_mut_slice(), color.to_rgba_bytes());
}
pub fn fill_span(&mut self, x0: u32, x1: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
let width: u32 = self.get_width();
let height: u32 = self.get_height();
if sa == 0 || y >= height || x0 >= x1 || x0 >= width {
return;
}
let end_x: u32 = x1.min(width);
let row_base: usize = (y as usize) * (width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = self.get_mut_pixels().get_mut(start_byte..end_byte) else {
return;
};
if sa == 255 {
fill_rgba_span_32(span, [color.get_r(), color.get_g(), color.get_b(), 255]);
return;
}
let sr: u32 = color.get_r() as u32;
let sg: u32 = color.get_g() as u32;
let sb: u32 = color.get_b() as u32;
let sa_u32: u32 = sa as u32;
let inv_a: u32 = 255 - sa_u32;
for px in span.as_chunks_mut::<4>().0.iter_mut() {
let dr: u32 = px[0] as u32;
let dg: u32 = px[1] as u32;
let db: u32 = px[2] as u32;
let da: u32 = px[3] as u32;
let vr: u32 = sr * sa_u32 + dr * inv_a + 128;
let vg: u32 = sg * sa_u32 + dg * inv_a + 128;
let vb: u32 = sb * sa_u32 + db * inv_a + 128;
let va: u32 = da * inv_a + 128;
px[0] = ((vr + (vr >> 8)) >> 8) as u8;
px[1] = ((vg + (vg >> 8)) >> 8) as u8;
px[2] = ((vb + (vb >> 8)) >> 8) as u8;
px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
pub fn fill_span_linear(&mut self, x0: u32, x1: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
let width: u32 = self.get_width();
let height: u32 = self.get_height();
if sa == 0 || y >= height || x0 >= x1 || x0 >= width {
return;
}
if sa == 255 {
self.fill_span(x0, x1, y, color);
return;
}
let end_x: u32 = x1.min(width);
let row_base: usize = (y as usize) * (width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = self.get_mut_pixels().get_mut(start_byte..end_byte) else {
return;
};
let a: f32 = (sa as f32) * (1.0 / 255.0);
let inv_a: f32 = 1.0 - a;
let sr_a: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize] * a;
let sg_a: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize] * a;
let sb_a: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize] * a;
let sa_u32: u32 = sa as u32;
let inv_a_u32: u32 = 255 - sa_u32;
for px in span.as_chunks_mut::<4>().0.iter_mut() {
let dr: f32 = SRGB_TO_LIN_LUT[px[0] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[px[1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[px[2] as usize];
let ir: usize = ((sr_a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg_a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb_a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let va: u32 = (px[3] as u32) * inv_a_u32 + 128;
px[0] = ((LIN_TO_SRGB_Q4_LUT[ir] + 8) >> 4).min(255) as u8;
px[1] = ((LIN_TO_SRGB_Q4_LUT[ig] + 8) >> 4).min(255) as u8;
px[2] = ((LIN_TO_SRGB_Q4_LUT[ib] + 8) >> 4).min(255) as u8;
px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
pub fn blend_glyph_row(&mut self, dx: u32, dy: u32, coverage: &[u8], color: Color) -> usize {
let width: u32 = self.get_width();
let height: u32 = self.get_height();
if color.get_a() == 0 || dy >= height || dx >= width || coverage.is_empty() {
return 0;
}
let max_len: usize = ((width - dx) as usize).min(coverage.len());
let start_byte: usize = ((dy as usize) * (width as usize) + (dx as usize)) * 4;
let end_byte: usize = start_byte + max_len * 4;
let Some(dst_span) = self.get_mut_pixels().get_mut(start_byte..end_byte) else {
return 0;
};
blend_glyph_row_span(dst_span, &coverage[..max_len], color)
}
#[must_use]
pub fn get_pixel(&self, x: u32, y: u32) -> Option<Color> {
if x >= self.get_width() || y >= self.get_height() {
return None;
}
let offset: usize = ((y as usize) * (self.get_width() as usize) + (x as usize)) * 4;
let slice: &[u8] = self.get_pixels().get(offset..offset + 4)?;
Some(Color::new(slice[0], slice[1], slice[2], slice[3]))
}
pub fn blend_pixel_linear_dithered(&mut self, x: u32, y: u32, color: Color, dither: f32) {
let sa: u8 = color.get_a();
if sa == 0 || x >= self.get_width() || y >= self.get_height() {
return;
}
let offset: usize = ((y as usize) * (self.get_width() as usize) + (x as usize)) * 4;
let pixels: &mut [u8] = self.get_mut_pixels().as_mut_slice();
if offset + 4 > pixels.len() {
return;
}
if sa == 255 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let a: f32 = (sa as f32) * (1.0 / 255.0);
let inv_a: f32 = 1.0 - a;
let sr: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize];
let sg: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize];
let sb: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize];
let dr: f32 = SRGB_TO_LIN_LUT[pixels[offset] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[pixels[offset + 1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[pixels[offset + 2] as usize];
let ir: usize = ((sr * a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg * a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb * a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let dq: i32 = (dither * 16.0) as i32 + 8;
pixels[offset] = (((LIN_TO_SRGB_Q4_LUT[ir] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 1] = (((LIN_TO_SRGB_Q4_LUT[ig] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 2] = (((LIN_TO_SRGB_Q4_LUT[ib] as i32) + dq) >> 4).clamp(0, 255) as u8;
let sa_u32: u32 = sa as u32;
let va: u32 = (pixels[offset + 3] as u32) * (255 - sa_u32) + 128;
pixels[offset + 3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
pub fn blend_pixel_f32_dithered(
&mut self,
x: u32,
y: u32,
color: Color,
alpha_f: f32,
dither: f32,
) {
if alpha_f <= 0.0002 || x >= self.get_width() || y >= self.get_height() {
return;
}
let offset: usize = ((y as usize) * (self.get_width() as usize) + (x as usize)) * 4;
let pixels: &mut [u8] = self.get_mut_pixels().as_mut_slice();
if offset + 4 > pixels.len() {
return;
}
let a: f32 = alpha_f.clamp(0.0, 1.0);
if a >= 0.9995 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let inv_a: f32 = 1.0 - a;
let sr: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize];
let sg: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize];
let sb: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize];
let dr: f32 = SRGB_TO_LIN_LUT[pixels[offset] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[pixels[offset + 1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[pixels[offset + 2] as usize];
let ir: usize = ((sr * a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg * a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb * a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let dq: i32 = (dither * 16.0) as i32 + 8;
pixels[offset] = (((LIN_TO_SRGB_Q4_LUT[ir] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 1] = (((LIN_TO_SRGB_Q4_LUT[ig] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 2] = (((LIN_TO_SRGB_Q4_LUT[ib] as i32) + dq) >> 4).clamp(0, 255) as u8;
let sa_u32: u32 = (a * 255.0) as u32;
let va: u32 = (pixels[offset + 3] as u32) * (255 - sa_u32) + 128;
pixels[offset + 3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
pub fn blend_pixel(&mut self, x: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
if sa == 0 || x >= self.get_width() || y >= self.get_height() {
return;
}
let offset: usize = ((y as usize) * (self.get_width() as usize) + (x as usize)) * 4;
let pixels: &mut [u8] = self.get_mut_pixels().as_mut_slice();
if offset + 4 > pixels.len() {
return;
}
if sa == 255 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let dst: Color = Color::new(
pixels[offset],
pixels[offset + 1],
pixels[offset + 2],
pixels[offset + 3],
);
let blended: Color = color.blend_over(dst);
pixels[offset] = blended.get_r();
pixels[offset + 1] = blended.get_g();
pixels[offset + 2] = blended.get_b();
pixels[offset + 3] = blended.get_a();
}
#[must_use]
pub fn to_bgra_bytes(&self) -> Vec<u8> {
let src: &[u8] = self.get_pixels().as_slice();
let len: usize = src.len() & !3usize;
let mut out: Vec<u8> = vec![0u8; len];
let (src_wide, src_tail): (&[[u8; 16]], &[u8]) = src[..len].as_chunks::<16>();
let (dst_wide, dst_tail): (&mut [[u8; 16]], &mut [u8]) = out.as_chunks_mut::<16>();
for (s, d) in src_wide.iter().zip(dst_wide.iter_mut()) {
*d = [
s[2], s[1], s[0], s[3], s[6], s[5], s[4], s[7], s[10], s[9], s[8], s[11], s[14],
s[13], s[12], s[15],
];
}
for (s, d) in src_tail
.as_chunks::<4>()
.0
.iter()
.zip(dst_tail.as_chunks_mut::<4>().0.iter_mut())
{
*d = [s[2], s[1], s[0], s[3]];
}
out
}
#[must_use]
pub fn fnv1a_hash(&self) -> u64 {
let bytes: &[u8] = self.get_pixels().as_slice();
let mut lanes: [u64; 4] = [
FNV1A_OFFSET_BASIS,
FNV1A_OFFSET_BASIS ^ 0x9E37_79B9_7F4A_7C15,
FNV1A_OFFSET_BASIS ^ 0xBF58_476D_1CE4_E5B9,
FNV1A_OFFSET_BASIS ^ 0x94D0_49BB_1331_11EB,
];
let (blocks32, tail): (&[[u8; 32]], &[u8]) = bytes.as_chunks::<32>();
let (lines128, rem32): (&[[[u8; 32]; 4]], &[[u8; 32]]) = blocks32.as_chunks::<4>();
for line in lines128.iter() {
fold_fnv1a_32(&mut lanes, &line[0]);
fold_fnv1a_32(&mut lanes, &line[1]);
fold_fnv1a_32(&mut lanes, &line[2]);
fold_fnv1a_32(&mut lanes, &line[3]);
}
for blk in rem32.iter() {
fold_fnv1a_32(&mut lanes, blk);
}
let mut hash: u64 = lanes[0]
^ lanes[1].rotate_left(13)
^ lanes[2].rotate_left(29)
^ lanes[3].rotate_left(47);
hash = hash.wrapping_mul(FNV1A_PRIME);
for byte in tail.iter().copied() {
hash ^= byte as u64;
hash = hash.wrapping_mul(FNV1A_PRIME);
}
hash
}
pub fn as_band_mut(&mut self) -> PixelBand<'_> {
let Self {
width,
height,
pixels,
} = self;
PixelBand {
width: *width,
y_offset: 0,
height: *height,
pixels: pixels.as_mut_slice(),
}
}
pub fn split_bands_mut(&mut self, y0: u32, y1: u32, band_count: usize) -> Vec<PixelBand<'_>> {
let Self {
width,
height,
pixels,
} = self;
let fb_w: u32 = *width;
let fb_h: u32 = *height;
let start_y: u32 = y0.min(fb_h);
let end_y: u32 = y1.clamp(start_y, fb_h);
let total_rows: u32 = end_y - start_y;
if total_rows == 0 || fb_w == 0 {
return Vec::new();
}
let count: usize = band_count.max(1).min(total_rows as usize);
let rows_per: usize = (total_rows as usize).div_ceil(count);
let row_stride: usize = (fb_w as usize) * 4;
let start_byte: usize = (start_y as usize) * row_stride;
let end_byte: usize = (end_y as usize) * row_stride;
let Some(slice) = pixels.get_mut(start_byte..end_byte) else {
return Vec::new();
};
let mut bands: Vec<PixelBand<'_>> = Vec::with_capacity(count);
let mut cur_y: u32 = start_y;
for chunk in slice.chunks_mut(rows_per * row_stride) {
let chunk_rows: u32 = (chunk.len() / row_stride) as u32;
bands.push(PixelBand {
width: fb_w,
y_offset: cur_y,
height: chunk_rows,
pixels: chunk,
});
cur_y += chunk_rows;
}
bands
}
}
impl<'a> PixelBand<'a> {
#[must_use]
pub fn width(&self) -> u32 {
let Self { width, .. } = self;
*width
}
#[must_use]
pub fn y_offset(&self) -> u32 {
let Self { y_offset, .. } = self;
*y_offset
}
#[must_use]
pub fn y_end(&self) -> u32 {
let Self {
y_offset, height, ..
} = self;
*y_offset + *height
}
pub fn row_slice_mut(&mut self, y: u32) -> Option<&mut [u8]> {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if y < *y_offset || y >= *y_offset + *height {
return None;
}
let local_y: usize = (y - *y_offset) as usize;
let stride: usize = (*width as usize) * 4;
let start: usize = local_y * stride;
pixels.get_mut(start..start + stride)
}
pub fn two_row_slices_mut(&mut self, y_a: u32, y_b: u32) -> Option<(&mut [u8], &mut [u8])> {
let Self {
width,
y_offset,
height,
pixels,
} = self;
let y_end: u32 = *y_offset + *height;
if y_a >= y_b || y_a < *y_offset || y_b >= y_end {
return None;
}
let stride: usize = (*width as usize) * 4;
let start_a: usize = ((y_a - *y_offset) as usize) * stride;
let start_b: usize = ((y_b - *y_offset) as usize) * stride;
if start_b + stride > pixels.len() {
return None;
}
let (head, tail): (&mut [u8], &mut [u8]) = pixels.split_at_mut(start_b);
Some((&mut head[start_a..start_a + stride], &mut tail[..stride]))
}
#[inline(always)]
pub(crate) fn rows_slice_mut(&mut self, y0: u32, y1: u32) -> Option<(&mut [u8], usize)> {
let Self {
width,
y_offset,
height,
pixels,
} = self;
let y_end: u32 = *y_offset + *height;
if y0 >= y1 || y0 < *y_offset || y1 > y_end {
return None;
}
let stride: usize = (*width as usize) * 4;
let start: usize = ((y0 - *y_offset) as usize) * stride;
let end: usize = ((y1 - *y_offset) as usize) * stride;
pixels.get_mut(start..end).map(|s: &mut [u8]| (s, stride))
}
#[inline(always)]
pub(crate) fn fill_rect_opaque_preclipped(
&mut self,
x0: u32,
x1: u32,
y0: u32,
y1: u32,
rgba: [u8; 4],
) {
let Self {
width,
y_offset,
height,
pixels,
} = self;
let fb_w: u32 = *width;
let y_end: u32 = *y_offset + *height;
let valid_x: bool = x0 < x1 && x1 <= fb_w;
let valid_y: bool = y0 < y1 && y0 >= *y_offset && y1 <= y_end;
if !valid_x || !valid_y {
return;
}
let stride: usize = (fb_w as usize) * 4;
let start: usize = ((y0 - *y_offset) as usize) * stride;
let end: usize = ((y1 - *y_offset) as usize) * stride;
let Some(rows_buf) = pixels.get_mut(start..end) else {
return;
};
if x0 == 0 && x1 == fb_w {
fill_rgba_span_32(rows_buf, rgba);
return;
}
let bx0: usize = (x0 as usize) * 4;
let bx1: usize = (x1 as usize) * 4;
for row in rows_buf.chunks_exact_mut(stride) {
fill_rgba_span_32(&mut row[bx0..bx1], rgba);
}
}
pub fn clear(&mut self, color: Color) {
let Self { pixels, .. } = self;
fill_rgba_span_32(pixels, color.to_rgba_bytes());
}
pub fn fill_span(&mut self, x0: u32, x1: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
let Self {
width,
y_offset,
height,
pixels,
} = self;
if sa == 0 || y < *y_offset || y >= *y_offset + *height || x0 >= x1 || x0 >= *width {
return;
}
let end_x: u32 = x1.min(*width);
let row_base: usize = ((y - *y_offset) as usize) * (*width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = pixels.get_mut(start_byte..end_byte) else {
return;
};
if sa == 255 {
fill_rgba_span_32(span, [color.get_r(), color.get_g(), color.get_b(), 255]);
return;
}
let sr: u32 = color.get_r() as u32;
let sg: u32 = color.get_g() as u32;
let sb: u32 = color.get_b() as u32;
let sa_u32: u32 = sa as u32;
let inv_a: u32 = 255 - sa_u32;
for px in span.as_chunks_mut::<4>().0.iter_mut() {
let dr: u32 = px[0] as u32;
let dg: u32 = px[1] as u32;
let db: u32 = px[2] as u32;
let da: u32 = px[3] as u32;
let vr: u32 = sr * sa_u32 + dr * inv_a + 128;
let vg: u32 = sg * sa_u32 + dg * inv_a + 128;
let vb: u32 = sb * sa_u32 + db * inv_a + 128;
let va: u32 = da * inv_a + 128;
px[0] = ((vr + (vr >> 8)) >> 8) as u8;
px[1] = ((vg + (vg >> 8)) >> 8) as u8;
px[2] = ((vb + (vb >> 8)) >> 8) as u8;
px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
pub fn fill_span_linear(&mut self, x0: u32, x1: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
if sa == 255 {
self.fill_span(x0, x1, y, color);
return;
}
let Self {
width,
y_offset,
height,
pixels,
} = self;
if sa == 0 || y < *y_offset || y >= *y_offset + *height || x0 >= x1 || x0 >= *width {
return;
}
let end_x: u32 = x1.min(*width);
let row_base: usize = ((y - *y_offset) as usize) * (*width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = pixels.get_mut(start_byte..end_byte) else {
return;
};
fill_span_linear_slice(span, color);
}
pub fn fill_span_linear_luts(
&mut self,
x0: u32,
x1: u32,
y: u32,
luts: &([u8; 256], [u8; 256], [u8; 256], [u8; 256]),
) {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if y < *y_offset || y >= *y_offset + *height || x0 >= x1 || x0 >= *width {
return;
}
let end_x: u32 = x1.min(*width);
let row_base: usize = ((y - *y_offset) as usize) * (*width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = pixels.get_mut(start_byte..end_byte) else {
return;
};
fill_span_linear_luts_slice(span, luts);
}
pub fn blend_shadow_span_const_alpha(
&mut self,
x0: u32,
x1: u32,
y: u32,
color: Color,
alpha_f: f32,
) {
if alpha_f <= 0.0005 {
return;
}
let Self {
width,
y_offset,
height,
pixels,
} = self;
if y < *y_offset || y >= *y_offset + *height || x0 >= x1 || x0 >= *width {
return;
}
let end_x: u32 = x1.min(*width);
let row_base: usize = ((y - *y_offset) as usize) * (*width as usize);
let start_byte: usize = (row_base + (x0 as usize)) * 4;
let end_byte: usize = (row_base + (end_x as usize)) * 4;
let Some(span) = pixels.get_mut(start_byte..end_byte) else {
return;
};
let a: f32 = alpha_f.clamp(0.0, 1.0);
let cr: u8 = color.get_r();
let cg: u8 = color.get_g();
let cb: u8 = color.get_b();
let (chunks, _): (&mut [[u8; 4]], &mut [u8]) = span.as_chunks_mut::<4>();
if a >= 0.9995 {
let opaque: [u8; 4] = [cr, cg, cb, 255];
for px in chunks.iter_mut() {
*px = opaque;
}
return;
}
let Some(first_px) = chunks.first().copied() else {
return;
};
let inv_a: f32 = 1.0 - a;
let sr_a: f32 = SRGB_TO_LIN_LUT[cr as usize] * a;
let sg_a: f32 = SRGB_TO_LIN_LUT[cg as usize] * a;
let sb_a: f32 = SRGB_TO_LIN_LUT[cb as usize] * a;
let sa_u32: u32 = (a * 255.0) as u32;
let inv_a_u32: u32 = 255 - sa_u32;
let mut phase_u32: u32 = x0
.wrapping_mul(IGN_X_STEP_U32)
.wrapping_add(y.wrapping_mul(IGN_Y_STEP_U32));
let mut prev_px: [u8; 4] = first_px;
let dr0: f32 = SRGB_TO_LIN_LUT[first_px[0] as usize];
let dg0: f32 = SRGB_TO_LIN_LUT[first_px[1] as usize];
let db0: f32 = SRGB_TO_LIN_LUT[first_px[2] as usize];
let ir0: usize = ((sr_a + dr0 * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig0: usize = ((sg_a + dg0 * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib0: usize = ((sb_a + db0 * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let mut q4_r: u16 = LIN_TO_SRGB_Q4_LUT[ir0];
let mut q4_g: u16 = LIN_TO_SRGB_Q4_LUT[ig0];
let mut q4_b: u16 = LIN_TO_SRGB_Q4_LUT[ib0];
let va0: u32 = (first_px[3] as u32) * inv_a_u32 + 128;
let mut out_a: u8 = (sa_u32 + ((va0 + (va0 >> 8)) >> 8)).min(255) as u8;
let total_len: usize = chunks.len();
let mut miss_count: u32 = 0;
let mut split_idx: usize = total_len;
for (idx, px) in chunks.iter_mut().enumerate() {
if *px != prev_px {
miss_count += 1;
if miss_count >= 10 && total_len.saturating_sub(idx) >= 64 {
split_idx = idx;
break;
}
prev_px = *px;
let dr: f32 = SRGB_TO_LIN_LUT[px[0] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[px[1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[px[2] as usize];
let ir: usize = ((sr_a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg_a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb_a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
q4_r = LIN_TO_SRGB_Q4_LUT[ir];
q4_g = LIN_TO_SRGB_Q4_LUT[ig];
q4_b = LIN_TO_SRGB_Q4_LUT[ib];
if px[3] == 255 {
out_a = 255;
} else {
let va: u32 = (px[3] as u32) * inv_a_u32 + 128;
out_a = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
let dq: u16 = (((phase_u32 as u64).wrapping_mul(IGN_MUL_Q26) >> 54) & 15) as u16;
phase_u32 = phase_u32.wrapping_add(IGN_X_STEP_U32);
px[0] = ((q4_r + dq) >> 4) as u8;
px[1] = ((q4_g + dq) >> 4) as u8;
px[2] = ((q4_b + dq) >> 4) as u8;
px[3] = out_a;
}
if split_idx < total_len {
let tail: &mut [[u8; 4]] = &mut chunks[split_idx..];
let same_rg: bool = cr == cg;
let same_rb: bool = cr == cb;
let lut_r: [u16; 256] = build_shadow_q4_lut_channel(cr, a);
if same_rg && same_rb {
for px in tail.iter_mut() {
let dq: u16 =
(((phase_u32 as u64).wrapping_mul(IGN_MUL_Q26) >> 54) & 15) as u16;
phase_u32 = phase_u32.wrapping_add(IGN_X_STEP_U32);
px[0] = ((lut_r[px[0] as usize] + dq) >> 4) as u8;
px[1] = ((lut_r[px[1] as usize] + dq) >> 4) as u8;
px[2] = ((lut_r[px[2] as usize] + dq) >> 4) as u8;
if px[3] != 255 {
let va: u32 = (px[3] as u32) * inv_a_u32 + 128;
px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
} else {
let lut_g: [u16; 256] = if same_rg {
lut_r
} else {
build_shadow_q4_lut_channel(cg, a)
};
let lut_b: [u16; 256] = if same_rb {
lut_r
} else {
build_shadow_q4_lut_channel(cb, a)
};
for px in tail.iter_mut() {
let dq: u16 =
(((phase_u32 as u64).wrapping_mul(IGN_MUL_Q26) >> 54) & 15) as u16;
phase_u32 = phase_u32.wrapping_add(IGN_X_STEP_U32);
px[0] = ((lut_r[px[0] as usize] + dq) >> 4) as u8;
px[1] = ((lut_g[px[1] as usize] + dq) >> 4) as u8;
px[2] = ((lut_b[px[2] as usize] + dq) >> 4) as u8;
if px[3] != 255 {
let va: u32 = (px[3] as u32) * inv_a_u32 + 128;
px[3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
}
}
}
}
pub fn blend_glyph_row(&mut self, dx: u32, dy: u32, coverage: &[u8], color: Color) -> usize {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if color.get_a() == 0
|| dy < *y_offset
|| dy >= *y_offset + *height
|| dx >= *width
|| coverage.is_empty()
{
return 0;
}
let max_len: usize = ((*width - dx) as usize).min(coverage.len());
let start_byte: usize =
(((dy - *y_offset) as usize) * (*width as usize) + (dx as usize)) * 4;
let end_byte: usize = start_byte + max_len * 4;
let Some(dst_span) = pixels.get_mut(start_byte..end_byte) else {
return 0;
};
blend_glyph_row_span(dst_span, &coverage[..max_len], color)
}
pub fn blend_pixel(&mut self, x: u32, y: u32, color: Color) {
let sa: u8 = color.get_a();
let Self {
width,
y_offset,
height,
pixels,
} = self;
if sa == 0 || x >= *width || y < *y_offset || y >= *y_offset + *height {
return;
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return;
}
if sa == 255 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let dst: Color = Color::new(
pixels[offset],
pixels[offset + 1],
pixels[offset + 2],
pixels[offset + 3],
);
let blended: Color = color.blend_over(dst);
pixels[offset] = blended.get_r();
pixels[offset + 1] = blended.get_g();
pixels[offset + 2] = blended.get_b();
pixels[offset + 3] = blended.get_a();
}
pub fn blend_pixel_linear_dithered(&mut self, x: u32, y: u32, color: Color, dither: f32) {
let sa: u8 = color.get_a();
let Self {
width,
y_offset,
height,
pixels,
} = self;
if sa == 0 || x >= *width || y < *y_offset || y >= *y_offset + *height {
return;
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return;
}
if sa == 255 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let a: f32 = (sa as f32) * (1.0 / 255.0);
let inv_a: f32 = 1.0 - a;
let sr: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize];
let sg: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize];
let sb: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize];
let dr: f32 = SRGB_TO_LIN_LUT[pixels[offset] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[pixels[offset + 1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[pixels[offset + 2] as usize];
let ir: usize = ((sr * a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg * a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb * a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let dq: i32 = (dither * 16.0) as i32 + 8;
pixels[offset] = (((LIN_TO_SRGB_Q4_LUT[ir] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 1] = (((LIN_TO_SRGB_Q4_LUT[ig] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 2] = (((LIN_TO_SRGB_Q4_LUT[ib] as i32) + dq) >> 4).clamp(0, 255) as u8;
let sa_u32: u32 = sa as u32;
let va: u32 = (pixels[offset + 3] as u32) * (255 - sa_u32) + 128;
pixels[offset + 3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
pub fn blend_pixel_f32_dithered(
&mut self,
x: u32,
y: u32,
color: Color,
alpha_f: f32,
dither: f32,
) {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if alpha_f <= 0.0002 || x >= *width || y < *y_offset || y >= *y_offset + *height {
return;
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return;
}
let a: f32 = alpha_f.clamp(0.0, 1.0);
if a >= 0.9995 {
pixels[offset] = color.get_r();
pixels[offset + 1] = color.get_g();
pixels[offset + 2] = color.get_b();
pixels[offset + 3] = 255;
return;
}
let inv_a: f32 = 1.0 - a;
let sr: f32 = SRGB_TO_LIN_LUT[color.get_r() as usize];
let sg: f32 = SRGB_TO_LIN_LUT[color.get_g() as usize];
let sb: f32 = SRGB_TO_LIN_LUT[color.get_b() as usize];
let dr: f32 = SRGB_TO_LIN_LUT[pixels[offset] as usize];
let dg: f32 = SRGB_TO_LIN_LUT[pixels[offset + 1] as usize];
let db: f32 = SRGB_TO_LIN_LUT[pixels[offset + 2] as usize];
let ir: usize = ((sr * a + dr * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((sg * a + dg * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((sb * a + db * inv_a) * 4095.0).clamp(0.0, 4095.0) as usize;
let dq: i32 = (dither * 16.0) as i32 + 8;
pixels[offset] = (((LIN_TO_SRGB_Q4_LUT[ir] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 1] = (((LIN_TO_SRGB_Q4_LUT[ig] as i32) + dq) >> 4).clamp(0, 255) as u8;
pixels[offset + 2] = (((LIN_TO_SRGB_Q4_LUT[ib] as i32) + dq) >> 4).clamp(0, 255) as u8;
let sa_u32: u32 = (a * 255.0) as u32;
let va: u32 = (pixels[offset + 3] as u32) * (255 - sa_u32) + 128;
pixels[offset + 3] = (sa_u32 + ((va + (va >> 8)) >> 8)).min(255) as u8;
}
#[must_use]
pub fn get_pixel_rgba(&self, x: u32, y: u32) -> [u8; 4] {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if x >= *width || y < *y_offset || y >= *y_offset + *height {
return [0, 0, 0, 0];
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return [0, 0, 0, 0];
}
[
pixels[offset],
pixels[offset + 1],
pixels[offset + 2],
pixels[offset + 3],
]
}
pub fn set_pixel_rgba(&mut self, x: u32, y: u32, rgba: [u8; 4]) {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if x >= *width || y < *y_offset || y >= *y_offset + *height {
return;
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return;
}
pixels[offset] = rgba[0];
pixels[offset + 1] = rgba[1];
pixels[offset + 2] = rgba[2];
pixels[offset + 3] = rgba[3];
}
pub fn lerp_pixel_from_saved_linear(&mut self, x: u32, y: u32, saved_rgba: [u8; 4], cov: f32) {
let Self {
width,
y_offset,
height,
pixels,
} = self;
if x >= *width || y < *y_offset || y >= *y_offset + *height {
return;
}
let offset: usize = (((y - *y_offset) as usize) * (*width as usize) + (x as usize)) * 4;
if offset + 4 > pixels.len() {
return;
}
let cur: [u8; 4] = [
pixels[offset],
pixels[offset + 1],
pixels[offset + 2],
pixels[offset + 3],
];
if cur == saved_rgba {
return;
}
let c: f32 = cov.clamp(0.0, 1.0);
let inv_c: f32 = 1.0 - c;
let nr: f32 = SRGB_TO_LIN_LUT[cur[0] as usize];
let ng: f32 = SRGB_TO_LIN_LUT[cur[1] as usize];
let nb: f32 = SRGB_TO_LIN_LUT[cur[2] as usize];
let or: f32 = SRGB_TO_LIN_LUT[saved_rgba[0] as usize];
let og: f32 = SRGB_TO_LIN_LUT[saved_rgba[1] as usize];
let ob: f32 = SRGB_TO_LIN_LUT[saved_rgba[2] as usize];
let ir: usize = ((nr * c + or * inv_c) * 4095.0).clamp(0.0, 4095.0) as usize;
let ig: usize = ((ng * c + og * inv_c) * 4095.0).clamp(0.0, 4095.0) as usize;
let ib: usize = ((nb * c + ob * inv_c) * 4095.0).clamp(0.0, 4095.0) as usize;
pixels[offset] = ((LIN_TO_SRGB_Q4_LUT[ir] + 8) >> 4).min(255) as u8;
pixels[offset + 1] = ((LIN_TO_SRGB_Q4_LUT[ig] + 8) >> 4).min(255) as u8;
pixels[offset + 2] = ((LIN_TO_SRGB_Q4_LUT[ib] + 8) >> 4).min(255) as u8;
let na: f32 = (cur[3] as f32) * c + (saved_rgba[3] as f32) * inv_c;
pixels[offset + 3] = na.round().clamp(0.0, 255.0) as u8;
}
}
impl DamageRegion {
#[must_use]
pub fn full() -> Self {
Self {
full_damage: true,
rect: None,
rects: [Rect::default(); 8],
rect_count: 0,
}
}
pub fn add_rect(&mut self, rect: Rect) {
let Self {
full_damage,
rect: bounds,
rects,
rect_count,
} = self;
if *full_damage {
return;
}
let next_bounds: Rect = match *bounds {
Some(existing) => existing.union(rect),
None => rect,
};
*bounds = Some(next_bounds);
if rect.get_width() <= 0.0 || rect.get_height() <= 0.0 {
return;
}
let mut merged: Rect = rect;
let mut count: usize = (*rect_count as usize).min(rects.len());
let mut changed: bool = true;
while changed {
changed = false;
let mut i: usize = 0;
while i < count {
let cur: Rect = rects[i];
let touches: bool = merged.get_x() - 2.0 <= cur.right()
&& cur.get_x() - 2.0 <= merged.right()
&& merged.get_y() - 2.0 <= cur.bottom()
&& cur.get_y() - 2.0 <= merged.bottom();
if touches {
merged = merged.union(cur);
count -= 1;
let mut j: usize = i;
while j < count {
rects[j] = rects[j + 1];
j += 1;
}
rects[count] = Rect::default();
changed = true;
} else {
i += 1;
}
}
}
if count < rects.len() {
let mut insert_at: usize = count;
while insert_at > 0 {
let prev: Rect = rects[insert_at - 1];
let goes_before: bool = merged.get_y() < prev.get_y()
|| (merged.get_y() == prev.get_y() && merged.get_x() < prev.get_x());
if goes_before {
rects[insert_at] = prev;
insert_at -= 1;
} else {
break;
}
}
rects[insert_at] = merged;
*rect_count = (count + 1) as u8;
} else {
*rects = [Rect::default(); 8];
rects[0] = next_bounds;
*rect_count = 1;
}
}
#[must_use]
pub(crate) fn normalized_rects(&self, width: u32, height: u32) -> ([Rect; 8], usize) {
let Self {
full_damage,
rect: bounds,
rects,
rect_count,
} = *self;
let mut out: [Rect; 8] = [Rect::default(); 8];
if width == 0 || height == 0 {
return (out, 0);
}
let viewport: Rect = Rect::new(0.0, 0.0, width as f32, height as f32);
if full_damage {
out[0] = viewport;
return (out, 1);
}
let Some(bound_rect) = bounds else {
return (out, 0);
};
let active: usize = (rect_count as usize).min(out.len());
if active == 0 {
if let Some(clamped) = bound_rect.intersect(viewport)
&& clamped.get_width() > 0.0
&& clamped.get_height() > 0.0
{
out[0] = clamped;
return (out, 1);
}
return (out, 0);
}
let mut out_len: usize = 0;
for item in rects.iter().take(active) {
if let Some(clamped) = item.intersect(viewport)
&& clamped.get_width() > 0.0
&& clamped.get_height() > 0.0
{
out[out_len] = clamped;
out_len += 1;
}
}
(out, out_len)
}
#[must_use]
pub fn resolve(&self, width: u32, height: u32) -> Option<Rect> {
if self.get_full_damage() {
return Some(Rect::new(0.0, 0.0, width as f32, height as f32));
}
self.bounds_rect()
}
#[must_use]
pub fn bounds_rect(&self) -> Option<Rect> {
let Self { rect, .. } = *self;
rect
}
pub fn optimize_for_viewport(
&mut self,
width: u32,
height: u32,
full_fallback_ratio: f32,
) -> bool {
if self.get_full_damage() {
return true;
}
let total_area: f32 = (width as f32) * (height as f32);
if total_area <= 0.0 {
return false;
}
let threshold: f32 = full_fallback_ratio.clamp(0.1, 1.0);
let (mut list, mut count): ([Rect; 8], usize) = self.normalized_rects(width, height);
if count == 0 {
return false;
}
let mut dirty_area: f32 = 0.0;
for item in list.iter().take(count) {
dirty_area += item.get_width().max(0.0) * item.get_height().max(0.0);
}
if dirty_area / total_area >= threshold {
*self = Self::full();
return true;
}
let mut merged_any: bool = true;
while merged_any && count > 1 {
merged_any = false;
let mut best_pair: Option<(usize, usize, Rect, f32)> = None;
for i in 0..count {
let area_i: f32 = list[i].get_width().max(0.0) * list[i].get_height().max(0.0);
for j in (i + 1)..count {
let area_j: f32 = list[j].get_width().max(0.0) * list[j].get_height().max(0.0);
let u: Rect = list[i].union(list[j]);
let union_area: f32 = u.get_width().max(0.0) * u.get_height().max(0.0);
let waste: f32 = (union_area - (area_i + area_j)).max(0.0);
let acceptable: bool = waste <= (area_i + area_j) * 0.25 || count > 4;
if acceptable {
let better: bool = match best_pair {
Some((_, _, _, best_w)) => waste < best_w,
None => true,
};
if better {
best_pair = Some((i, j, u, waste));
}
}
}
}
if let Some((i, j, u, _)) = best_pair {
list[i] = u;
let mut k: usize = j;
while k + 1 < count {
list[k] = list[k + 1];
k += 1;
}
list[count - 1] = Rect::default();
count -= 1;
merged_any = true;
}
}
let mut post_area: f32 = 0.0;
let mut union_all: Rect = list[0];
for item in list.iter().take(count) {
post_area += item.get_width().max(0.0) * item.get_height().max(0.0);
union_all = union_all.union(*item);
}
if post_area / total_area >= threshold {
*self = Self::full();
return true;
}
let Self {
rect,
rects,
rect_count,
..
} = self;
*rect = Some(union_all);
*rects = list;
*rect_count = count as u8;
false
}
#[must_use]
pub fn active_rect_count(&self) -> usize {
if self.get_full_damage() {
1
} else {
let Self { rect_count, .. } = *self;
rect_count as usize
}
}
pub fn clear(&mut self) {
let Self {
full_damage,
rect,
rects,
rect_count,
} = self;
*full_damage = false;
*rect = None;
*rects = [Rect::default(); 8];
*rect_count = 0;
}
}