Skip to main content

slimg_core/codec/
jxl.rs

1use crate::error::{Error, Result};
2use crate::format::Format;
3
4use super::{Codec, EncodeOptions, ImageData};
5
6/// JXL codec — decode-only (encoding is not supported due to license restrictions).
7pub struct JxlCodec;
8
9impl Codec for JxlCodec {
10    fn format(&self) -> Format {
11        Format::Jxl
12    }
13
14    fn decode(&self, data: &[u8]) -> Result<ImageData> {
15        let img =
16            image::load_from_memory(data).map_err(|e| Error::Decode(format!("jxl decode: {e}")))?;
17
18        let rgba = img.to_rgba8();
19        let width = rgba.width();
20        let height = rgba.height();
21
22        Ok(ImageData::new(width, height, rgba.into_raw()))
23    }
24
25    fn encode(&self, _image: &ImageData, _options: &EncodeOptions) -> Result<Vec<u8>> {
26        Err(Error::EncodingNotSupported(Format::Jxl))
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn encode_returns_not_supported() {
36        let codec = JxlCodec;
37        let data = vec![0u8; 4 * 2 * 2];
38        let image = ImageData::new(2, 2, data);
39        let options = EncodeOptions { quality: 80 };
40
41        let result = codec.encode(&image, &options);
42        assert!(result.is_err(), "JXL encoding should not be supported");
43    }
44}