Skip to main content

oxigdal_jpeg2000/
error.rs

1//! Error types for JPEG2000 driver
2
3use thiserror::Error;
4
5/// JPEG2000 specific errors
6#[derive(Error, Debug)]
7pub enum Jpeg2000Error {
8    /// Invalid JP2 signature
9    #[error("Invalid JP2 signature: expected JP2 magic bytes")]
10    InvalidSignature,
11
12    /// Invalid box type
13    #[error("Invalid or unsupported box type: {0}")]
14    InvalidBoxType(String),
15
16    /// Box parsing error
17    #[error("Failed to parse box {box_type}: {reason}")]
18    BoxParseError {
19        /// Box type that failed to parse
20        box_type: String,
21        /// Reason for failure
22        reason: String,
23    },
24
25    /// Invalid codestream marker
26    #[error("Invalid JPEG2000 codestream marker: 0x{0:04X}")]
27    InvalidMarker(u16),
28
29    /// Codestream parsing error
30    #[error("Failed to parse codestream: {0}")]
31    CodestreamError(String),
32
33    /// Unsupported feature
34    #[error("Unsupported JPEG2000 feature: {0}")]
35    UnsupportedFeature(String),
36
37    /// Invalid image header
38    #[error("Invalid image header: {0}")]
39    InvalidImageHeader(String),
40
41    /// Invalid tile
42    #[error("Invalid tile parameters: {0}")]
43    InvalidTile(String),
44
45    /// Wavelet transform error
46    #[error("Wavelet transform error: {0}")]
47    WaveletError(String),
48
49    /// Tier-1 decoding error
50    #[error("Tier-1 (EBCOT) decoding error: {0}")]
51    Tier1Error(String),
52
53    /// Tier-2 decoding error
54    #[error("Tier-2 (packet) decoding error: {0}")]
55    Tier2Error(String),
56
57    /// Color space conversion error
58    #[error("Color space conversion error: {0}")]
59    ColorError(String),
60
61    /// Invalid metadata
62    #[error("Invalid metadata: {0}")]
63    InvalidMetadata(String),
64
65    /// IO error
66    #[error("IO error: {0}")]
67    IoError(#[from] std::io::Error),
68
69    /// Insufficient data
70    #[error("Insufficient data: expected {expected} bytes, got {actual}")]
71    InsufficientData {
72        /// Expected number of bytes
73        expected: usize,
74        /// Actual number of bytes available
75        actual: usize,
76    },
77
78    /// Invalid dimension
79    #[error("Invalid dimension: {0}")]
80    InvalidDimension(String),
81
82    /// Memory allocation error
83    #[error("Failed to allocate memory: {0}")]
84    AllocationError(String),
85
86    /// Generic error
87    #[error("{0}")]
88    Other(String),
89}
90
91/// Result type for JPEG2000 operations
92pub type Result<T> = std::result::Result<T, Jpeg2000Error>;
93
94/// Error resilience mode for JPEG2000 decoding
95///
96/// Determines how the decoder handles corrupted or invalid data.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
98pub enum ResilienceMode {
99    /// No error resilience - fail on any error
100    #[default]
101    None,
102    /// Basic error resilience - skip corrupted packets, use error concealment
103    Basic,
104    /// Full error resilience - aggressive error recovery and concealment
105    Full,
106}
107
108impl ResilienceMode {
109    /// Check if error resilience is enabled
110    pub fn is_enabled(&self) -> bool {
111        !matches!(self, Self::None)
112    }
113
114    /// Check if this is full resilience mode
115    pub fn is_full(&self) -> bool {
116        matches!(self, Self::Full)
117    }
118
119    /// Check if this is basic or full resilience mode
120    pub fn is_basic_or_higher(&self) -> bool {
121        matches!(self, Self::Basic | Self::Full)
122    }
123}