#[cfg(feature = "nostd")]
use alloc::{vec, vec::Vec};
#[cfg(not(feature = "nostd"))]
use std::vec::Vec;
use crate::pipeline::IntermediateLayer;
use crate::utils::RenderError;
#[derive(Debug, Clone, Copy)]
pub enum CompositeMode {
Normal,
Add,
Multiply,
Xor,
}
pub fn composite_layers(
layers: &[IntermediateLayer],
width: u32,
height: u32,
) -> Result<Vec<u8>, RenderError> {
let mut output = vec![0u8; (width * height * 4) as usize];
for layer in layers {
composite_layer(layer, &mut output, width, height)?;
}
Ok(output)
}
fn composite_layer(
layer: &IntermediateLayer,
output: &mut [u8],
width: u32,
height: u32,
) -> Result<(), RenderError> {
let _ = (layer, output, width, height);
Ok(())
}
pub fn alpha_blend(src: [u8; 4], dst: [u8; 4]) -> [u8; 4] {
let src_alpha = src[3] as u32;
if src_alpha == 0 {
return dst;
}
if src_alpha == 255 && dst[3] == 0 {
return src;
}
let inv_src_alpha = 255 - src_alpha;
let out_r = ((src[0] as u32 * src_alpha + dst[0] as u32 * inv_src_alpha + 255) >> 8) as u8;
let out_g = ((src[1] as u32 * src_alpha + dst[1] as u32 * inv_src_alpha + 255) >> 8) as u8;
let out_b = ((src[2] as u32 * src_alpha + dst[2] as u32 * inv_src_alpha + 255) >> 8) as u8;
let dst_alpha = dst[3] as u32;
let out_alpha = ((src_alpha * 255 + dst_alpha * inv_src_alpha + 255) >> 8) as u8;
[out_r, out_g, out_b, out_alpha]
}