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("Invalid input: {0}")]
43    InvalidInput(String),
44
45    // Entropy coding errors
46    #[error("Invalid histogram: {0}")]
47    InvalidHistogram(String),
48
49    #[error("ANS encoding error: {0}")]
50    AnsEncodingError(String),
51
52    #[error("Bitstream error: {0}")]
53    Bitstream(String),
54
55    #[error("Too many unique symbols: found {found}, max {max} (minimal encoder limit)")]
56    TooManySymbols { found: usize, max: usize },
57
58    // Header errors
59    #[error("Invalid color encoding")]
60    InvalidColorEncoding,
61
62    #[error("Invalid extra channel configuration")]
63    InvalidExtraChannel,
64
65    #[error("Invalid frame header")]
66    InvalidFrameHeader,
67
68    // Transform errors
69    #[error("Invalid DCT size: {0}")]
70    InvalidDctSize(usize),
71
72    #[error("Transform coefficient overflow")]
73    TransformOverflow,
74
75    // General errors
76    #[error("Out of memory")]
77    OutOfMemory(#[from] TryReserveError),
78
79    #[cfg(feature = "std")]
80    #[error("I/O error: {0}")]
81    IoError(#[from] io::Error),
82
83    #[error("Encoding cancelled")]
84    Cancelled,
85
86    #[error("Feature not yet implemented: {0}")]
87    NotImplemented(String),
88}