Skip to main content

jxl_encoder/
error.rs

1// Copyright (c) Imazen LLC and the JPEG XL Project Authors.
2// Algorithms and constants derived from libjxl (BSD-3-Clause).
3// Licensed under AGPL-3.0-or-later. Commercial licenses at https://www.imazen.io/pricing
4
5//! Error types for the JPEG XL encoder.
6
7use alloc::collections::TryReserveError;
8#[cfg(feature = "std")]
9use std::io;
10use thiserror::Error;
11
12/// Result type alias using the encoder's Error type.
13pub type Result<T, E = Error> = core::result::Result<T, E>;
14
15/// Encoder error types.
16#[derive(Error, Debug)]
17#[non_exhaustive]
18pub enum Error {
19    // BitWriter errors
20    #[error("BitWriter buffer overflow: tried to write {attempted} bits, capacity is {capacity}")]
21    BitWriterOverflow { attempted: usize, capacity: usize },
22
23    #[error("Too many bits per write call: {0}, max is 56")]
24    TooManyBitsPerCall(usize),
25
26    #[error("BitWriter not byte-aligned: {0} bits written")]
27    NotByteAligned(usize),
28
29    // Image errors
30    #[error("Invalid image dimensions: {0}x{1}")]
31    InvalidImageDimensions(usize, usize),
32
33    #[error("Image too large: {0}x{1}, max is {2}x{3}")]
34    ImageTooLarge(usize, usize, usize, usize),
35
36    #[error("Invalid bit depth: {0}")]
37    InvalidBitDepth(u32),
38
39    #[error("Invalid number of channels: {0}")]
40    InvalidChannelCount(usize),
41
42    #[error("Dimension overflow: {width}x{height}x{channels} exceeds usize")]
43    DimensionOverflow {
44        width: usize,
45        height: usize,
46        channels: usize,
47    },
48
49    #[error("Invalid input: {0}")]
50    InvalidInput(String),
51
52    // Entropy coding errors
53    #[error("Invalid histogram: {0}")]
54    InvalidHistogram(String),
55
56    #[error("ANS encoding error: {0}")]
57    AnsEncodingError(String),
58
59    #[error("Bitstream error: {0}")]
60    Bitstream(String),
61
62    #[error("Too many unique symbols: found {found}, max {max} (minimal encoder limit)")]
63    TooManySymbols { found: usize, max: usize },
64
65    // Header errors
66    #[error("Invalid color encoding")]
67    InvalidColorEncoding,
68
69    #[error("Invalid extra channel configuration")]
70    InvalidExtraChannel,
71
72    #[error("Invalid frame header")]
73    InvalidFrameHeader,
74
75    // Transform errors
76    #[error("Invalid DCT size: {0}")]
77    InvalidDctSize(usize),
78
79    #[error("Transform coefficient overflow")]
80    TransformOverflow,
81
82    // General errors
83    #[error("Out of memory")]
84    OutOfMemory(#[from] TryReserveError),
85
86    #[cfg(feature = "std")]
87    #[error("I/O error: {0}")]
88    IoError(#[from] io::Error),
89
90    #[error("Encoding cancelled")]
91    Cancelled,
92
93    #[error("Feature not yet implemented: {0}")]
94    NotImplemented(String),
95}