pic_scale/
pic_scale_error.rs1use std::error::Error;
30use std::fmt::Display;
31
32#[derive(Copy, Clone, Debug)]
34pub struct PicScaleBufferMismatch {
35 pub expected: usize,
36 pub width: usize,
37 pub height: usize,
38 pub channels: usize,
39 pub slice_len: usize,
40}
41
42#[derive(Debug)]
44pub enum PicScaleError {
45 ZeroImageDimensions,
46 SourceImageIsTooLarge,
47 DestinationImageIsTooLarge,
48 BufferMismatch(PicScaleBufferMismatch),
49 InvalidStride(usize, usize),
50 UnsupportedBitDepth(usize),
51 UnknownResizingFilter,
52 OutOfMemory(usize),
53}
54
55impl PicScaleError {
56 #[inline]
58 pub fn code(&self) -> usize {
59 match self {
60 PicScaleError::ZeroImageDimensions => 1,
61 PicScaleError::SourceImageIsTooLarge => 2,
62 PicScaleError::DestinationImageIsTooLarge => 3,
63 PicScaleError::BufferMismatch(_) => 4,
64 PicScaleError::InvalidStride(_, _) => 5,
65 PicScaleError::UnsupportedBitDepth(_) => 6,
66 PicScaleError::UnknownResizingFilter => 7,
67 PicScaleError::OutOfMemory(_) => 8,
68 }
69 }
70}
71
72impl Display for PicScaleError {
73 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74 match self {
75 PicScaleError::InvalidStride(min_stride, real_stride) => f.write_fmt(format_args!(
76 "Stride must be at least {min_stride}, but received {real_stride}",
77 )),
78 PicScaleError::ZeroImageDimensions => {
79 f.write_str("One of image dimensions is 0, this should not happen")
80 }
81 PicScaleError::SourceImageIsTooLarge => {
82 f.write_str("Input image larger than memory capabilities")
83 }
84 PicScaleError::DestinationImageIsTooLarge => {
85 f.write_str("Destination image larger than memory capabilities")
86 }
87 PicScaleError::BufferMismatch(buffer_mismatch) => f.write_fmt(format_args!(
88 "Image buffer len expected to be {} [w({})*h({})*channels({})] but received {}",
89 buffer_mismatch.expected,
90 buffer_mismatch.width,
91 buffer_mismatch.height,
92 buffer_mismatch.channels,
93 buffer_mismatch.slice_len,
94 )),
95 PicScaleError::UnsupportedBitDepth(depth) => {
96 f.write_fmt(format_args!("Bit-depth must be in [1, 16] but got {depth}",))
97 }
98 PicScaleError::UnknownResizingFilter => {
99 f.write_str("Unknown resizing filter was requested")
100 }
101 PicScaleError::OutOfMemory(capacity) => f.write_fmt(format_args!(
102 "There is no enough memory to allocate {capacity} bytes"
103 )),
104 }
105 }
106}
107
108impl Error for PicScaleError {}
109
110macro_rules! try_vec {
111 () => {
112 Vec::new()
113 };
114 ($elem:expr; $n:expr) => {{
115 let mut v = Vec::new();
116 v.try_reserve_exact($n)
117 .map_err(|_| crate::pic_scale_error::PicScaleError::OutOfMemory($n))?;
118 v.resize($n, $elem);
119 v
120 }};
121}
122
123pub(crate) use try_vec;