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#[derive(Copy, Clone, Debug)]
33pub struct PicScaleBufferMismatch {
34    pub expected: usize,
35    pub width: usize,
36    pub height: usize,
37    pub channels: usize,
38    pub slice_len: usize,
39}
40
41#[derive(Debug)]
42pub enum PicScaleError {
43    ZeroImageDimensions,
44    SourceImageIsTooLarge,
45    DestinationImageIsTooLarge,
46    BufferMismatch(PicScaleBufferMismatch),
47    InvalidStride(usize, usize),
48    UnsupportedBitDepth(usize),
49    UnknownResizingFilter,
50}
51
52impl PicScaleError {
53    #[inline]
54    pub fn code(&self) -> usize {
55        match self {
56            PicScaleError::ZeroImageDimensions => 1,
57            PicScaleError::SourceImageIsTooLarge => 2,
58            PicScaleError::DestinationImageIsTooLarge => 3,
59            PicScaleError::BufferMismatch(_) => 4,
60            PicScaleError::InvalidStride(_, _) => 5,
61            PicScaleError::UnsupportedBitDepth(_) => 6,
62            PicScaleError::UnknownResizingFilter => 7,
63        }
64    }
65}
66
67impl Display for PicScaleError {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        match self {
70            PicScaleError::InvalidStride(min_stride, real_stride) => f.write_fmt(format_args!(
71                "Stride must be at least {}, but received {}",
72                min_stride, real_stride
73            )),
74            PicScaleError::ZeroImageDimensions => {
75                f.write_str("One of image dimensions is 0, this should not happen")
76            }
77            PicScaleError::SourceImageIsTooLarge => {
78                f.write_str("Input image larger than memory capabilities")
79            }
80            PicScaleError::DestinationImageIsTooLarge => {
81                f.write_str("Destination image larger than memory capabilities")
82            }
83            PicScaleError::BufferMismatch(buffer_mismatch) => f.write_fmt(format_args!(
84                "Image buffer len expected to be {} [w({})*h({})*channels({})] but received {}",
85                buffer_mismatch.expected,
86                buffer_mismatch.width,
87                buffer_mismatch.height,
88                buffer_mismatch.channels,
89                buffer_mismatch.slice_len,
90            )),
91            PicScaleError::UnsupportedBitDepth(depth) => f.write_fmt(format_args!(
92                "Bit depth must be in [1, 16] but got {}",
93                depth
94            )),
95            PicScaleError::UnknownResizingFilter => {
96                f.write_str("Unknown resizing filter was requested")
97            }
98        }
99    }
100}
101
102impl Error for PicScaleError {}