webpkit_cli/format/
raw.rs1use webpkit::lossless::{Dimensions, Image, Metadata, PixelLayout};
4
5use crate::error::CliError;
6
7#[derive(Debug, Clone, Copy)]
9pub struct RawParams {
10 pub width: u32,
12 pub height: u32,
14 pub layout: PixelLayout,
16}
17
18pub fn read(bytes: &[u8], params: RawParams) -> Result<Image, CliError> {
25 let dims = Dimensions::new(params.width, params.height)?;
26 let expected = params.width as usize * params.height as usize * 4;
27 if bytes.len() != expected {
28 return Err(CliError::RawConfig(format!(
29 "raw input is {} bytes but {}x{} needs {expected}",
30 bytes.len(),
31 params.width,
32 params.height,
33 )));
34 }
35 let alpha_offset = match params.layout {
36 PixelLayout::Argb8 => 0,
37 PixelLayout::Rgba8 | PixelLayout::Bgra8 => 3,
38 };
39 let has_alpha = bytes.chunks_exact(4).any(|px| px[alpha_offset] != 0xff);
40 Ok(Image::from_parts(
41 dims,
42 params.layout,
43 bytes.to_vec(),
44 has_alpha,
45 Metadata::none(),
46 ))
47}