use crate::error::JpegError;
use crate::info::{ColorSpace, SofKind};
use j2k_core::CodecError;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[doc(hidden)]
pub enum FastPacketError {
#[error("{0}")]
Decode(#[from] JpegError),
#[error("JPEG fast packet does not support SOF kind {0:?}")]
UnsupportedSof(SofKind),
#[error("JPEG fast packet does not support color space {0:?}")]
UnsupportedColorSpace(ColorSpace),
#[error("JPEG component sampling does not match the selected fast-packet family")]
UnsupportedSampling,
#[error("JPEG scan component order does not match the fast-packet contract")]
UnsupportedComponentOrder,
#[error("JPEG fast packet input has no scan payload")]
MissingScan,
#[error("JPEG fast packet input is missing quantization table {slot}")]
MissingQuantTable {
slot: u8,
},
#[error("JPEG fast packet input is missing {kind:?} Huffman table {slot}")]
MissingHuffmanTable {
kind: TableKind,
slot: u8,
},
#[error("JPEG fast packet does not support entropy marker 0xff{marker:02x}")]
EntropyMarkerUnsupported {
marker: u8,
},
#[error("JPEG entropy payload ended before fast-packet construction completed")]
TruncatedEntropy,
}
impl FastPacketError {
#[must_use]
pub const fn is_capability_mismatch(&self) -> bool {
matches!(
self,
Self::UnsupportedSof(_)
| Self::UnsupportedColorSpace(_)
| Self::UnsupportedSampling
| Self::UnsupportedComponentOrder
| Self::EntropyMarkerUnsupported { .. }
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[doc(hidden)]
pub enum TableKind {
Dc,
Ac,
}
#[doc(hidden)]
impl CodecError for FastPacketError {
fn is_truncated(&self) -> bool {
matches!(self, Self::TruncatedEntropy)
|| matches!(self, Self::Decode(error) if error.is_truncated())
}
fn is_not_implemented(&self) -> bool {
self.is_capability_mismatch()
|| matches!(self, Self::Decode(error) if error.is_not_implemented())
}
fn is_unsupported(&self) -> bool {
self.is_capability_mismatch()
|| matches!(self, Self::Decode(error) if error.is_unsupported())
}
fn is_buffer_error(&self) -> bool {
matches!(self, Self::Decode(error) if error.is_buffer_error())
}
}
#[cfg(test)]
mod tests;