zune_image/codecs/
farbfeld.rs1#![cfg_attr(feature = "docs", doc(cfg(feature = "farbfeld")))]
9#![cfg(feature = "farbfeld")]
10use zune_core::bit_depth::BitDepth;
15use zune_core::bytestream::{ZByteReaderTrait, ZByteWriterTrait};
16use zune_core::colorspace::ColorSpace;
17use zune_core::options::EncoderOptions;
18pub use zune_farbfeld::*;
19
20use crate::codecs::{create_options_for_encoder, ImageFormat};
21use crate::errors::{ImageErrors, ImgEncodeErrors};
22use crate::image::Image;
23use crate::metadata::ImageMetadata;
24use crate::traits::{DecodeInto, DecoderTrait, EncoderTrait};
25
26impl<T> DecoderTrait for FarbFeldDecoder<T>
27where
28 T: ZByteReaderTrait
29{
30 fn decode(&mut self) -> Result<Image, ImageErrors> {
31 let pixels = self.decode().map_err(ImageErrors::from)?;
32 let colorspace = self.colorspace();
33 let (width, height) = self.dimensions().unwrap();
34
35 let mut image = Image::from_u16(&pixels, width, height, colorspace);
36
37 image.metadata.format = Some(ImageFormat::Farbfeld);
38
39 Ok(image)
40 }
41
42 fn dimensions(&self) -> Option<(usize, usize)> {
43 self.dimensions()
44 }
45
46 fn out_colorspace(&self) -> ColorSpace {
47 self.colorspace()
48 }
49
50 fn name(&self) -> &'static str {
51 "Farbfeld Decoder"
52 }
53
54 fn is_experimental(&self) -> bool {
55 true
56 }
57
58 fn read_headers(&mut self) -> Result<Option<ImageMetadata>, crate::errors::ImageErrors> {
59 self.decode_headers()
60 .map_err(|e| ImageErrors::ImageDecodeErrors(format!("{:?}", e)))?;
61
62 let (width, height) = self.dimensions().unwrap();
63 let depth = self.bit_depth();
64
65 let metadata = ImageMetadata {
66 format: Some(ImageFormat::Farbfeld),
67 colorspace: self.colorspace(),
68 depth: depth,
69 width: width,
70 height: height,
71 ..Default::default()
72 };
73
74 Ok(Some(metadata))
75 }
76}
77
78impl From<FarbFeldErrors> for ImageErrors {
79 fn from(value: FarbFeldErrors) -> Self {
80 Self::ImageDecodeErrors(format!("ff: {value:?}"))
81 }
82}
83
84#[derive(Default)]
89pub struct FarbFeldEncoder {
90 options: Option<EncoderOptions>
91}
92
93impl FarbFeldEncoder {
94 pub fn new() -> FarbFeldEncoder {
96 FarbFeldEncoder::default()
97 }
98 pub fn new_with_options(options: EncoderOptions) -> FarbFeldEncoder {
100 FarbFeldEncoder {
101 options: Some(options)
102 }
103 }
104}
105
106impl EncoderTrait for FarbFeldEncoder {
107 fn name(&self) -> &'static str {
108 "farbfeld"
109 }
110
111 fn encode_inner<T: ZByteWriterTrait>(
112 &mut self, image: &Image, sink: T
113 ) -> Result<usize, ImageErrors> {
114 let options = create_options_for_encoder(self.options, image);
115
116 assert_eq!(image.depth(), BitDepth::Sixteen);
117
118 let data = &image.to_u8()[0];
119
120 let encoder = zune_farbfeld::FarbFeldEncoder::new(data, options);
121
122 let data = encoder
123 .encode(sink)
124 .map_err(<FarbFeldEncoderErrors as Into<ImgEncodeErrors>>::into)?;
125
126 Ok(data)
127 }
128
129 fn supported_colorspaces(&self) -> &'static [ColorSpace] {
130 &[ColorSpace::RGBA]
131 }
132
133 fn format(&self) -> ImageFormat {
134 ImageFormat::Farbfeld
135 }
136
137 fn supported_bit_depth(&self) -> &'static [BitDepth] {
138 &[BitDepth::Sixteen]
139 }
140
141 fn default_depth(&self, _: BitDepth) -> BitDepth {
142 BitDepth::Sixteen
143 }
144
145 fn default_colorspace(&self, _: ColorSpace) -> ColorSpace {
146 ColorSpace::RGBA
147 }
148 fn set_options(&mut self, opts: EncoderOptions) {
149 self.options = Some(opts)
150 }
151}
152
153impl From<FarbFeldEncoderErrors> for ImgEncodeErrors {
154 fn from(value: FarbFeldEncoderErrors) -> Self {
155 ImgEncodeErrors::ImageEncodeErrors(format!("ff: {:?}", value))
156 }
157}
158
159impl<T> DecodeInto for FarbFeldDecoder<T>
160where
161 T: ZByteReaderTrait
162{
163 type BufferType = u16;
164
165 fn decode_into(&mut self, buffer: &mut [Self::BufferType]) -> Result<(), ImageErrors> {
166 self.decode_into(buffer)
167 .map_err(<FarbFeldErrors as Into<ImageErrors>>::into)?;
168
169 Ok(())
170 }
171
172 fn decode_output_buffer_size(&mut self) -> Result<usize, ImageErrors> {
173 self.decode_headers()
174 .map_err(<FarbFeldErrors as Into<ImageErrors>>::into)?;
175
176 Ok(self.output_buffer_size().unwrap())
178 }
179}