Skip to main content

edgefirst_codec/
decoder.rs

1// SPDX-FileCopyrightText: Copyright 2026 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4//! [`ImageDecoder`] — reusable decoder state for zero-allocation hot loops.
5
6use crate::error::CodecError;
7#[cfg(not(feature = "png"))]
8use crate::error::UnsupportedFeature;
9use crate::options::ImageInfo;
10use crate::pixel::ImagePixel;
11use edgefirst_tensor::{DType, Tensor, TensorDyn};
12use std::io::Read;
13
14/// IDCT accuracy/speed selection for the software JPEG decoder.
15///
16/// [`Accurate`](Self::Accurate) is the default and the kernel every published
17/// benchmark quotes: the `islow`-class Loeffler IDCT, bit-comparable to
18/// libjpeg-turbo's default. [`Fast`](Self::Fast) opts into the AAN
19/// `ifast`-class kernel — roughly an eighth of the multiplies, at a small,
20/// bounded pixel accuracy cost (and the documented `ifast` degeneration at
21/// very high quality factors). Fast is advisory: paths without a fast kernel
22/// (non-NEON tiers, hardware V4L2/nvJPEG decoders, PNG) use their normal
23/// accurate path.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub enum DctMethod {
26    /// Accurate `islow`-class IDCT (default).
27    #[default]
28    Accurate,
29    /// Fast AAN `ifast`-class IDCT (opt-in).
30    Fast,
31}
32
33/// Chroma upsampling filter for a fused native-4:2:0 `Rgb` decode (see
34/// [`ImageDecoder::set_output_format`]).
35///
36/// [`Box`](Self::Box) — 2×2 nearest-neighbour replication — is the only
37/// filter implemented today; the type exists so that a future
38/// fancy/triangle-filtered mode (libjpeg-turbo's default, and a materially
39/// more expensive one — see BENCHMARKS.md) can be added as a new variant
40/// without a breaking API change to
41/// [`ImageDecoder::set_chroma_upsample`]. `#[non_exhaustive]` for the same
42/// reason: adding a variant later must not be a semver break for callers who
43/// match on this type.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
45#[non_exhaustive]
46pub enum ChromaUpsample {
47    /// 2×2 nearest-neighbour (box) chroma replication.
48    #[default]
49    Box,
50}
51
52/// Reusable image decoder with internal scratch buffers.
53///
54/// Create one `ImageDecoder` at program initialisation and pass it to
55/// [`ImageLoad::load_image`](crate::ImageLoad::load_image) in the hot loop.
56/// The scratch buffers grow to the high-water mark and are reused across calls
57/// — no per-frame allocations after the first few frames.
58///
59/// By default the decoder produces the source's native format and configures
60/// the destination tensor's dimensions + pixel format accordingly (JPEG →
61/// `Nv12`/`Nv16`/`Nv24`/`Grey`, PNG → `Rgb`/`Rgba`/`Grey`).
62/// [`set_output_format`](Self::set_output_format) opts a JPEG decode into a
63/// fused `Rgb` or `Nv12` output, where the colour conversion or chroma
64/// downsample happens at the MCU write stage instead of in a second pass. The
65/// decoder never rotates or resizes; use `ImageProcessor::convert()` for that.
66///
67/// # Example
68///
69/// ```rust,no_run
70/// use edgefirst_codec::{ImageDecoder, ImageLoad};
71/// use edgefirst_tensor::{CpuAccess, Tensor, TensorTrait, TensorMemory, PixelFormat};
72///
73/// let mut decoder = ImageDecoder::new();
74/// // Allocate a buffer large enough for the biggest expected image. The
75/// // decoder CPU-writes the pixels, so declare `CpuAccess::Write`.
76/// let mut tensor = Tensor::<u8>::image(1920, 1080, PixelFormat::Nv12, Some(TensorMemory::Mem),
77///                                       CpuAccess::Write)
78///     .expect("alloc");
79///
80/// for _ in 0..100 {
81///     let frame = std::fs::read("frame.jpg").unwrap();
82///     let _info = tensor.load_image(&mut decoder, &frame);
83/// }
84/// ```
85pub struct ImageDecoder {
86    /// Reusable JPEG decoder state (Huffman tables, MCU scratch buffers).
87    pub(crate) jpeg_state: crate::jpeg::JpegDecoderState,
88    /// Scratch buffer for PNG decode output.
89    pub(crate) scratch: Vec<u8>,
90    /// Buffer for `Read` → `&[u8]` conversion.
91    pub(crate) input_buffer: Vec<u8>,
92}
93
94impl ImageDecoder {
95    /// Create a new decoder with empty scratch buffers.
96    pub fn new() -> Self {
97        Self {
98            jpeg_state: crate::jpeg::JpegDecoderState::new(),
99            scratch: Vec::new(),
100            input_buffer: Vec::new(),
101        }
102    }
103
104    /// Request a fused decode output format instead of the source's native
105    /// format (JPEG only; PNG is unaffected).
106    ///
107    /// This is a **pure CPU, single-pass** path inside the software JPEG
108    /// decoder — colour conversion / chroma downsample happens at the MCU
109    /// write stage. It is **not** a GPU hybrid or nvJPEG path; V4L2/nvJPEG
110    /// are bypassed whenever the resolved output differs from native.
111    ///
112    /// - `Some(Rgb)`: 4:4:4 colour JPEGs decode straight to interleaved RGB
113    ///   (no chroma resampling); native 4:2:0 colour JPEGs also decode
114    ///   straight to RGB, with a 2×2 nearest-neighbour (box) chroma upsample
115    ///   fused into the write — not libjpeg's fancy/triangle upsampling, a
116    ///   deliberate speed tradeoff (see BENCHMARKS.md). Other sources (4:2:2,
117    ///   greyscale, non-standard subsamplings) cannot honour an RGB request:
118    ///   the decode returns [`CodecError::UnsupportedFormat`] rather than
119    ///   silently substituting the native semi-planar format. Check the
120    ///   source's subsampling first (e.g. via [`crate::peek_info`]) if it
121    ///   isn't known to be 4:4:4 or 4:2:0.
122    /// - `Some(Nv12)`: colour JPEGs decode to NV12, downsampling chroma at
123    ///   the write stage (2×2 average for 4:4:4, vertical for 4:2:2).
124    /// - `None` (default): native format (`Nv12`/`Nv16`/`Nv24`/`Grey`).
125    ///
126    /// Callers still run `ImageProcessor::convert()` on the result for
127    /// model-input preprocessing (letterbox, resize, EXIF orientation). With
128    /// fused RGB that convert step is typically a pure resize.
129    pub fn set_output_format(&mut self, format: Option<edgefirst_tensor::PixelFormat>) {
130        self.jpeg_state.preferred_format = format;
131    }
132
133    /// Select the software JPEG IDCT kernel class — see [`DctMethod`].
134    /// Off (Accurate) by default; `EDGEFIRST_CODEC_DCT=fast` in the
135    /// environment flips a **new** decoder's default for A/B runs.
136    pub fn set_dct_method(&mut self, method: DctMethod) {
137        self.jpeg_state.fast_dct = method == DctMethod::Fast;
138    }
139
140    /// Select the chroma upsampling filter for a fused native-4:2:0 `Rgb`
141    /// decode — see [`ChromaUpsample`]. [`ChromaUpsample::Box`] (the
142    /// default) is the only filter implemented today; this setter exists so
143    /// a future filter can be selected without changing
144    /// [`set_output_format`](Self::set_output_format)'s signature. Has no
145    /// effect on 4:4:4 sources (no chroma resampling occurs there) or when
146    /// the output format isn't `Rgb`.
147    pub fn set_chroma_upsample(&mut self, mode: ChromaUpsample) {
148        self.jpeg_state.chroma_upsample = mode;
149    }
150
151    /// Decode image data into a typed tensor, configuring its dimensions and
152    /// pixel format to the decoded output format — the source's native format
153    /// unless [`set_output_format`](Self::set_output_format) selected a fused
154    /// one.
155    ///
156    /// Detects the image format (JPEG or PNG) from magic bytes.
157    ///
158    /// # Errors
159    ///
160    /// - [`CodecError::InsufficientCapacity`] if the image is larger than the
161    ///   tensor's allocation
162    /// - [`CodecError::UnsupportedDtype`] if `T` is not valid for the output
163    ///   format (JPEG NV12/NV16/NV24/RGB/GREY require `u8`)
164    /// - [`CodecError::InvalidData`] if the data is not a valid JPEG or PNG,
165    ///   including a JPEG whose entropy-coded scan is truncated
166    pub fn decode_into<T: ImagePixel>(
167        &mut self,
168        data: &[u8],
169        dst: &mut Tensor<T>,
170    ) -> crate::Result<ImageInfo> {
171        if is_jpeg(data) {
172            crate::jpeg::decode_jpeg_into(data, dst, &mut self.jpeg_state)
173        } else if is_png(data) {
174            {
175                #[cfg(feature = "png")]
176                {
177                    crate::png::decode_png_into(data, dst, &mut self.scratch)
178                }
179                #[cfg(not(feature = "png"))]
180                {
181                    Err(CodecError::Unsupported(UnsupportedFeature::PngNotEnabled))
182                }
183            }
184        } else {
185            Err(CodecError::InvalidData(
186                "unrecognized image format (expected JPEG or PNG magic bytes)".into(),
187            ))
188        }
189    }
190
191    /// Decode image data into a type-erased tensor.
192    pub fn decode_into_dyn(
193        &mut self,
194        data: &[u8],
195        dst: &mut TensorDyn,
196    ) -> crate::Result<ImageInfo> {
197        match dst.dtype() {
198            DType::U8 => self.decode_into(data, dst.as_typed_mut::<u8>().expect("dtype checked")),
199            DType::I8 => self.decode_into(data, dst.as_typed_mut::<i8>().expect("dtype checked")),
200            DType::U16 => self.decode_into(data, dst.as_typed_mut::<u16>().expect("dtype checked")),
201            DType::I16 => self.decode_into(data, dst.as_typed_mut::<i16>().expect("dtype checked")),
202            DType::F32 => self.decode_into(data, dst.as_typed_mut::<f32>().expect("dtype checked")),
203            other => Err(CodecError::UnsupportedDtype(other)),
204        }
205    }
206
207    /// Read all bytes from a `Read` source into the internal input buffer, then
208    /// decode. The input buffer is reused across calls.
209    pub fn decode_from_reader<T: ImagePixel, R: Read>(
210        &mut self,
211        mut reader: R,
212        dst: &mut Tensor<T>,
213    ) -> crate::Result<ImageInfo> {
214        self.input_buffer.clear();
215        reader.read_to_end(&mut self.input_buffer)?;
216        decode_into_inner(
217            &mut self.jpeg_state,
218            &mut self.scratch,
219            &self.input_buffer,
220            dst,
221        )
222    }
223
224    /// Read all bytes from a `Read` source, then decode into a type-erased
225    /// tensor.
226    pub fn decode_from_reader_dyn<R: Read>(
227        &mut self,
228        mut reader: R,
229        dst: &mut TensorDyn,
230    ) -> crate::Result<ImageInfo> {
231        self.input_buffer.clear();
232        reader.read_to_end(&mut self.input_buffer)?;
233        decode_into_inner_dyn(
234            &mut self.jpeg_state,
235            &mut self.scratch,
236            &self.input_buffer,
237            dst,
238        )
239    }
240}
241
242/// Internal decode entry point parameterised over disjoint `&mut` borrows so
243/// callers can read from a `&[u8]` borrowed from one field of [`ImageDecoder`]
244/// while mutably borrowing the others.
245pub(crate) fn decode_into_inner<T: ImagePixel>(
246    jpeg_state: &mut crate::jpeg::JpegDecoderState,
247    scratch: &mut Vec<u8>,
248    data: &[u8],
249    dst: &mut Tensor<T>,
250) -> crate::Result<ImageInfo> {
251    if is_jpeg(data) {
252        crate::jpeg::decode_jpeg_into(data, dst, jpeg_state)
253    } else if is_png(data) {
254        {
255            #[cfg(feature = "png")]
256            {
257                crate::png::decode_png_into(data, dst, scratch)
258            }
259            #[cfg(not(feature = "png"))]
260            {
261                let _ = scratch;
262                Err(CodecError::Unsupported(UnsupportedFeature::PngNotEnabled))
263            }
264        }
265    } else {
266        Err(CodecError::InvalidData(
267            "unrecognized image format (expected JPEG or PNG magic bytes)".into(),
268        ))
269    }
270}
271
272pub(crate) fn decode_into_inner_dyn(
273    jpeg_state: &mut crate::jpeg::JpegDecoderState,
274    scratch: &mut Vec<u8>,
275    data: &[u8],
276    dst: &mut TensorDyn,
277) -> crate::Result<ImageInfo> {
278    match dst.dtype() {
279        DType::U8 => decode_into_inner(
280            jpeg_state,
281            scratch,
282            data,
283            dst.as_typed_mut::<u8>().expect("dtype checked"),
284        ),
285        DType::I8 => decode_into_inner(
286            jpeg_state,
287            scratch,
288            data,
289            dst.as_typed_mut::<i8>().expect("dtype checked"),
290        ),
291        DType::U16 => decode_into_inner(
292            jpeg_state,
293            scratch,
294            data,
295            dst.as_typed_mut::<u16>().expect("dtype checked"),
296        ),
297        DType::I16 => decode_into_inner(
298            jpeg_state,
299            scratch,
300            data,
301            dst.as_typed_mut::<i16>().expect("dtype checked"),
302        ),
303        DType::F32 => decode_into_inner(
304            jpeg_state,
305            scratch,
306            data,
307            dst.as_typed_mut::<f32>().expect("dtype checked"),
308        ),
309        other => Err(CodecError::UnsupportedDtype(other)),
310    }
311}
312
313impl Default for ImageDecoder {
314    fn default() -> Self {
315        Self::new()
316    }
317}
318
319/// Parse image headers and return the native dimensions, format, and EXIF
320/// orientation without decoding pixels.
321///
322/// Recommended for one-shot flows that allocate a tensor sized to the image:
323///
324/// ```rust,no_run
325/// use edgefirst_codec::{peek_info, ImageDecoder, ImageLoad};
326/// use edgefirst_tensor::{CpuAccess, Tensor, TensorMemory};
327///
328/// let data = std::fs::read("image.jpg").unwrap();
329/// let info = peek_info(&data).unwrap();
330/// let mut tensor = Tensor::<u8>::image(info.width, info.height, info.format,
331///                                       Some(TensorMemory::Mem),
332///                                       CpuAccess::Write).unwrap();
333/// let mut decoder = ImageDecoder::new();
334/// tensor.load_image(&mut decoder, &data).unwrap();
335/// ```
336pub fn peek_info(data: &[u8]) -> crate::Result<ImageInfo> {
337    if is_jpeg(data) {
338        crate::jpeg::peek_jpeg_info(data)
339    } else if is_png(data) {
340        {
341            #[cfg(feature = "png")]
342            {
343                crate::png::peek_png_info(data)
344            }
345            #[cfg(not(feature = "png"))]
346            {
347                Err(CodecError::Unsupported(UnsupportedFeature::PngNotEnabled))
348            }
349        }
350    } else {
351        Err(CodecError::InvalidData(
352            "unrecognized image format (expected JPEG or PNG magic bytes)".into(),
353        ))
354    }
355}
356
357/// Check for JPEG magic bytes (FFD8FF).
358fn is_jpeg(data: &[u8]) -> bool {
359    data.len() >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF
360}
361
362/// Check for PNG magic bytes (89504E47).
363fn is_png(data: &[u8]) -> bool {
364    data.len() >= 4 && data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47
365}
366
367#[cfg(test)]
368mod tests {
369    use super::*;
370
371    #[test]
372    fn magic_bytes_jpeg() {
373        assert!(is_jpeg(&[0xFF, 0xD8, 0xFF, 0xE0]));
374        assert!(!is_jpeg(&[0x89, 0x50, 0x4E, 0x47]));
375        assert!(!is_jpeg(&[]));
376        assert!(!is_jpeg(&[0xFF]));
377    }
378
379    #[test]
380    fn magic_bytes_png() {
381        assert!(is_png(&[0x89, 0x50, 0x4E, 0x47]));
382        assert!(!is_png(&[0xFF, 0xD8, 0xFF, 0xE0]));
383        assert!(!is_png(&[]));
384    }
385
386    #[test]
387    fn invalid_data() {
388        let mut decoder = ImageDecoder::new();
389        let mut tensor = Tensor::<u8>::image(
390            100,
391            100,
392            edgefirst_tensor::PixelFormat::Nv12,
393            Some(edgefirst_tensor::TensorMemory::Mem),
394            edgefirst_tensor::CpuAccess::ReadWrite,
395        )
396        .unwrap();
397        let result = decoder.decode_into(b"not an image", &mut tensor);
398        assert!(matches!(result, Err(CodecError::InvalidData(_))));
399    }
400}