pic_scale/
pic_scale_error.rs

1/*
2 * Copyright (c) Radzivon Bartoshyk. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1.  Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * 3.  Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use std::error::Error;
30use std::fmt::Display;
31
32/// Buffer mismatch error description
33#[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/// Error enumeration type
43#[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    /// Returns error as int code
57    #[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;