1pub mod png;
7pub mod ppm;
8pub mod raw;
9
10use clap::ValueEnum;
11use webpkit::lossless::{Image, Metadata, PixelLayout};
12
13use crate::{error::CliError, format::raw::RawParams};
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
17pub enum InputFormat {
18 Png,
20 Ppm,
22 Pam,
24 Raw,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
30pub enum OutputFormat {
31 Png,
33 Ppm,
35 Pam,
37 Raw,
39}
40
41impl InputFormat {
42 #[must_use]
45 pub fn resolve(explicit: Option<Self>, extension: Option<&str>, bytes: &[u8]) -> Self {
46 explicit
47 .or_else(|| extension.and_then(Self::from_extension))
48 .or_else(|| Self::sniff(bytes))
49 .unwrap_or(Self::Raw)
50 }
51
52 fn from_extension(ext: &str) -> Option<Self> {
53 match ext {
54 "png" => Some(Self::Png),
55 "ppm" => Some(Self::Ppm),
56 "pam" => Some(Self::Pam),
57 "raw" | "rgba" | "argb" | "bgra" => Some(Self::Raw),
58 _ => None,
59 }
60 }
61
62 fn sniff(bytes: &[u8]) -> Option<Self> {
63 if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
64 Some(Self::Png)
65 } else if bytes.starts_with(b"P6") {
66 Some(Self::Ppm)
67 } else if bytes.starts_with(b"P7") {
68 Some(Self::Pam)
69 } else {
70 None
71 }
72 }
73}
74
75impl OutputFormat {
76 #[must_use]
79 pub fn resolve(explicit: Option<Self>, extension: Option<&str>) -> Self {
80 explicit
81 .or_else(|| extension.and_then(Self::from_extension))
82 .unwrap_or(Self::Png)
83 }
84
85 fn from_extension(ext: &str) -> Option<Self> {
86 match ext {
87 "png" => Some(Self::Png),
88 "ppm" => Some(Self::Ppm),
89 "pam" => Some(Self::Pam),
90 "raw" | "rgba" | "argb" | "bgra" => Some(Self::Raw),
91 _ => None,
92 }
93 }
94}
95
96pub fn read_image(
105 bytes: &[u8],
106 format: InputFormat,
107 raw: Option<RawParams>,
108) -> Result<Image, CliError> {
109 match format {
110 InputFormat::Png => png::read(bytes),
111 InputFormat::Ppm | InputFormat::Pam => ppm::read(bytes),
112 InputFormat::Raw => {
113 let params = raw.ok_or_else(|| {
114 CliError::RawConfig(
115 "raw input requires --width and --height (or use a PNG/PPM/PAM file)"
116 .to_owned(),
117 )
118 })?;
119 raw::read(bytes, params)
120 },
121 }
122}
123
124pub fn write_image(
133 image: &Image,
134 format: OutputFormat,
135 metadata: &Metadata,
136) -> Result<Vec<u8>, CliError> {
137 match format {
138 OutputFormat::Png => png::write(image, metadata),
139 OutputFormat::Ppm => Ok(ppm::write_ppm(image)),
140 OutputFormat::Pam => Ok(ppm::write_pam(image)),
141 OutputFormat::Raw => Ok(image.as_bytes().to_vec()),
142 }
143}
144
145#[must_use]
147pub fn to_rgba8(image: &Image) -> Vec<u8> {
148 let src = image.as_bytes();
149 match image.layout() {
150 PixelLayout::Rgba8 => src.to_vec(),
151 PixelLayout::Argb8 => reorder(src, [1, 2, 3, 0]),
152 PixelLayout::Bgra8 => reorder(src, [2, 1, 0, 3]),
153 }
154}
155
156fn reorder(src: &[u8], order: [usize; 4]) -> Vec<u8> {
158 let mut out = Vec::with_capacity(src.len());
159 for px in src.chunks_exact(4) {
160 out.extend_from_slice(&[px[order[0]], px[order[1]], px[order[2]], px[order[3]]]);
161 }
162 out
163}