Skip to main content

j2k_transcode/
htj2k97_codeblock_error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Typed validation failures for shared HTJ2K 9/7 code-block options.
4
5use core::fmt;
6
7/// Code-block dimension whose exponent failed validation.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum Htj2k97CodeBlockAxis {
10    /// Horizontal code-block dimension.
11    Width,
12    /// Vertical code-block dimension.
13    Height,
14}
15
16impl fmt::Display for Htj2k97CodeBlockAxis {
17    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            Self::Width => formatter.write_str("width"),
20            Self::Height => formatter.write_str("height"),
21        }
22    }
23}
24
25/// Failure returned when shared HTJ2K 9/7 code-block options are unsupported.
26#[derive(Clone, Copy, Debug, Eq, PartialEq)]
27#[non_exhaustive]
28pub enum Htj2k97CodeBlockOptionsError {
29    /// Bit depth, guard bits, or the global quantization scale is unsupported.
30    NumericOptionsOutOfRange,
31    /// A subband scale, derived delta, or declared bitplane count is unsupported.
32    QuantizationOptionsOutOfRange,
33    /// A dimension exponent cannot be represented by the host index type.
34    DimensionExponentUnsupported {
35        /// Dimension whose exponent was rejected.
36        axis: Htj2k97CodeBlockAxis,
37        /// JPEG 2000 code-block exponent minus two.
38        exponent_minus_two: u8,
39    },
40    /// Decoded code-block dimensions exceed the HTJ2K side or area limit.
41    DimensionsExceedLimits {
42        /// Decoded code-block width.
43        width: usize,
44        /// Decoded code-block height.
45        height: usize,
46    },
47}
48
49impl Htj2k97CodeBlockOptionsError {
50    /// Returns allocation-free presentation text for legacy diagnostics.
51    #[must_use]
52    pub const fn reason(self) -> &'static str {
53        match self {
54            Self::NumericOptionsOutOfRange => {
55                "9/7 code-block options are outside supported numeric range"
56            }
57            Self::QuantizationOptionsOutOfRange => {
58                "9/7 code-block quantization options are outside supported range"
59            }
60            Self::DimensionExponentUnsupported { .. } => {
61                "9/7 code-block dimension exponent is unsupported"
62            }
63            Self::DimensionsExceedLimits { .. } => "9/7 code-block dimensions exceed HTJ2K limits",
64        }
65    }
66}
67
68impl fmt::Display for Htj2k97CodeBlockOptionsError {
69    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match *self {
71            Self::NumericOptionsOutOfRange | Self::QuantizationOptionsOutOfRange => {
72                formatter.write_str(self.reason())
73            }
74            Self::DimensionExponentUnsupported {
75                axis,
76                exponent_minus_two,
77            } => write!(
78                formatter,
79                "{}: {axis} exponent-minus-two {exponent_minus_two}",
80                self.reason()
81            ),
82            Self::DimensionsExceedLimits { width, height } => {
83                write!(formatter, "{}: {width}x{height}", self.reason())
84            }
85        }
86    }
87}
88
89impl std::error::Error for Htj2k97CodeBlockOptionsError {}