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