ici_files/
wrapper.rs

1use crate::errors::IndexedImageError;
2use crate::image::IndexedImage;
3use crate::prelude::*;
4
5/// Store static or animated images in a generic way
6///
7/// Supports most methods
8#[derive(Debug, Clone, PartialEq)]
9pub enum IndexedWrapper {
10    Static(IndexedImage),
11    Animated(AnimatedIndexedImage),
12}
13
14impl From<IndexedImage> for IndexedWrapper {
15    fn from(value: IndexedImage) -> Self {
16        IndexedWrapper::Static(value)
17    }
18}
19
20impl From<AnimatedIndexedImage> for IndexedWrapper {
21    fn from(value: AnimatedIndexedImage) -> Self {
22        IndexedWrapper::Animated(value)
23    }
24}
25
26impl IndexedWrapper {
27    /// Replace palette for image
28    /// Will only return an error if the new palette has fewer colors than the image needs
29    pub fn set_palette(&mut self, palette: &[Color]) -> Result<(), IndexedImageError> {
30        match self {
31            IndexedWrapper::Static(img) => img.set_palette(palette),
32            IndexedWrapper::Animated(img) => img.set_palette(palette),
33        }
34    }
35
36    /// Replace palette for image, any pixels outside the new palette will be replaced with `id`
37    /// Will only return an error if id is outside the new palette
38    pub fn set_palette_replace_id(
39        &mut self,
40        palette: &[Color],
41        id: u8,
42    ) -> Result<(), IndexedImageError> {
43        match self {
44            IndexedWrapper::Static(img) => img.set_palette_replace_id(palette, id),
45            IndexedWrapper::Animated(img) => img.set_palette_replace_id(palette, id),
46        }
47    }
48
49    /// Replace palette for image, any color indexes outside the palette will be expanded with `color`
50    pub fn set_palette_replace_color<C: Into<Color> + Copy>(
51        &mut self,
52        palette: &[Color],
53        color: C,
54    ) {
55        match self {
56            IndexedWrapper::Static(img) => img.set_palette_replace_color(palette, color),
57            IndexedWrapper::Animated(img) => img.set_palette_replace_color(palette, color),
58        }
59    }
60
61    pub fn size(&self) -> (u8, u8) {
62        match self {
63            IndexedWrapper::Static(img) => img.size(),
64            IndexedWrapper::Animated(img) => img.size(),
65        }
66    }
67
68    pub fn get_pixels(&self) -> &[u8] {
69        match self {
70            IndexedWrapper::Static(img) => img.get_pixels(),
71            IndexedWrapper::Animated(img) => img.get_pixels(),
72        }
73    }
74
75    pub fn get_pixel_index(&self, x: u8, y: u8) -> Result<usize, IndexedImageError> {
76        match self {
77            IndexedWrapper::Static(img) => img.get_pixel_index(x, y),
78            IndexedWrapper::Animated(img) => img.get_pixel_index(x, y),
79        }
80    }
81
82    pub fn get_color(&self, idx: u8) -> Result<Color, IndexedImageError> {
83        match self {
84            IndexedWrapper::Static(img) => img.get_color(idx),
85            IndexedWrapper::Animated(img) => img.get_color(idx),
86        }
87    }
88
89    pub fn set_color(&mut self, idx: u8, color: Color) -> Result<(), IndexedImageError> {
90        match self {
91            IndexedWrapper::Static(img) => img.set_color(idx, color),
92            IndexedWrapper::Animated(img) => img.set_color(idx, color),
93        }
94    }
95
96    pub fn get_palette(&self) -> &[Color] {
97        match self {
98            IndexedWrapper::Static(img) => img.get_palette(),
99            IndexedWrapper::Animated(img) => img.get_palette(),
100        }
101    }
102
103    pub fn min_palette_size_supported(&self) -> u8 {
104        match self {
105            IndexedWrapper::Static(img) => img.min_palette_size_supported(),
106            IndexedWrapper::Animated(img) => img.min_palette_size_supported(),
107        }
108    }
109
110    pub fn width(&self) -> u8 {
111        match self {
112            IndexedWrapper::Static(img) => img.width(),
113            IndexedWrapper::Animated(img) => img.width(),
114        }
115    }
116
117    pub fn height(&self) -> u8 {
118        match self {
119            IndexedWrapper::Static(img) => img.height(),
120            IndexedWrapper::Animated(img) => img.height(),
121        }
122    }
123
124    pub fn update(&mut self, delta: f64) {
125        match self {
126            IndexedWrapper::Static(_) => {}
127            IndexedWrapper::Animated(img) => img.update(delta),
128        }
129    }
130
131    pub fn reset(&mut self) {
132        match self {
133            IndexedWrapper::Static(_) => {}
134            IndexedWrapper::Animated(img) => img.reset(),
135        }
136    }
137
138    #[inline]
139    pub fn set_animate(&mut self, animate: bool) {
140        match self {
141            IndexedWrapper::Static(_) => {}
142            IndexedWrapper::Animated(img) => img.set_animate(animate),
143        }
144    }
145
146    #[inline]
147    pub fn animating(&self) -> bool {
148        match self {
149            IndexedWrapper::Static(_) => false,
150            IndexedWrapper::Animated(img) => img.animating(),
151        }
152    }
153
154    pub fn frame_count(&self) -> u8 {
155        match self {
156            IndexedWrapper::Static(_) => 1,
157            IndexedWrapper::Animated(img) => img.frame_count(),
158        }
159    }
160
161    pub fn is_animation(&self) -> bool {
162        matches!(self, IndexedWrapper::Animated(_))
163    }
164}