use crate::error::HwpError;
use crate::renderer::layer_renderer::{LayerRenderResult, RasterColorSpace};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RasterPixelFormat {
Rgba8Premultiplied,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RasterPixels {
pub bytes: Vec<u8>,
pub width: i32,
pub height: i32,
pub stride: usize,
pub format: RasterPixelFormat,
pub color_space: RasterColorSpace,
pub dpi: Option<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RasterRegion {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
pub(crate) const MAX_RASTER_PIXEL_COORDINATE: i32 = 16_777_216;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct RasterRegionWindow {
pub origin_x: i32,
pub origin_y: i32,
pub width: i32,
pub height: i32,
pub stride: usize,
pub output_bytes: usize,
}
impl RasterRegion {
pub(crate) fn pixel_window(
self,
scale: f64,
max_dimension: i32,
max_pixels: u64,
) -> LayerRenderResult<RasterRegionWindow> {
let invalid = || HwpError::RenderError("invalid raster region geometry".into());
if !scale.is_finite() || scale <= 0.0 || !(scale as f32).is_finite() || scale as f32 <= 0.0
{
return Err(invalid());
}
let right = self.x + self.width;
let bottom = self.y + self.height;
if [self.x, self.y, self.width, self.height, right, bottom]
.into_iter()
.any(|value| !value.is_finite())
|| self.width <= 0.0
|| self.height <= 0.0
|| right <= self.x
|| bottom <= self.y
{
return Err(invalid());
}
let endpoints = [
(self.x * scale).floor(),
(self.y * scale).floor(),
(right * scale).ceil(),
(bottom * scale).ceil(),
];
let exact = f64::from(MAX_RASTER_PIXEL_COORDINATE);
if endpoints
.iter()
.any(|value| !value.is_finite() || !(-exact..=exact).contains(value))
{
return Err(invalid());
}
let [origin_x, origin_y, right, bottom] = endpoints.map(|value| value as i32);
let width = right.checked_sub(origin_x).ok_or_else(invalid)?;
let height = bottom.checked_sub(origin_y).ok_or_else(invalid)?;
if width <= 0 || height <= 0 || width > max_dimension || height > max_dimension {
return Err(HwpError::RenderError(
"raster window dimensions out of range".into(),
));
}
let pixels = (width as u64)
.checked_mul(height as u64)
.ok_or_else(|| HwpError::RenderError("raster pixel count overflow".into()))?;
if pixels > max_pixels {
return Err(HwpError::RenderError(format!(
"raster pixel count out of range: {pixels}"
)));
}
let byte_limit = || HwpError::ResourceLimit("raster byte count overflow".into());
let stride = (width as usize).checked_mul(4).ok_or_else(byte_limit)?;
let output_bytes = stride.checked_mul(height as usize).ok_or_else(byte_limit)?;
Ok(RasterRegionWindow {
origin_x,
origin_y,
width,
height,
stride,
output_bytes,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RasterRegionPixels {
pub pixels: RasterPixels,
pub origin_x: i32,
pub origin_y: i32,
}
impl RasterPixels {
pub fn validate(&self) -> LayerRenderResult<()> {
let invalid =
|| HwpError::RenderError("invalid RGBA raster dimensions/stride/buffer".into());
if self.width <= 0 || self.height <= 0 {
return Err(invalid());
}
let row_bytes = (self.width as usize).checked_mul(4).ok_or_else(invalid)?;
let buffer_bytes = self
.stride
.checked_mul(self.height as usize)
.ok_or_else(invalid)?;
if self.stride < row_bytes || self.bytes.len() != buffer_bytes {
return Err(invalid());
}
Ok(())
}
pub fn opaque_rgba_on_white(&self) -> LayerRenderResult<Vec<u8>> {
self.validate()?;
let mut rgba = Vec::with_capacity(self.width as usize * self.height as usize * 4);
for row in self.bytes.chunks_exact(self.stride) {
for pixel in row[..self.width as usize * 4].chunks_exact(4) {
if pixel[..3].iter().any(|channel| *channel > pixel[3]) {
return Err(HwpError::RenderError(
"invalid premultiplied RGBA channel".into(),
));
}
let white = 255 - pixel[3];
rgba.extend_from_slice(&[
pixel[0] + white,
pixel[1] + white,
pixel[2] + white,
255,
]);
}
}
Ok(rgba)
}
}