|
| 1 | +use crate::image::grammar::{ |
| 2 | + ColorType, |
| 3 | + ImageExt, |
| 4 | +}; |
1 | 5 | use anyhow::{ |
2 | 6 | bail, |
3 | 7 | Result, |
@@ -43,44 +47,6 @@ impl ImageHeader { |
43 | 47 | } |
44 | 48 | } |
45 | 49 |
|
46 | | -#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
47 | | -pub enum ColorType { |
48 | | - Grayscale = 0, |
49 | | - RGB = 2, |
50 | | - Palette = 3, |
51 | | - GrayscaleAlpha = 4, |
52 | | - RGBA = 6, |
53 | | -} |
54 | | - |
55 | | -impl ColorType { |
56 | | - pub(crate) const fn num_channels(&self) -> u8 { |
57 | | - match self { |
58 | | - Self::Grayscale => 1, |
59 | | - Self::RGB => 3, |
60 | | - Self::Palette => 1, |
61 | | - Self::GrayscaleAlpha => 2, |
62 | | - Self::RGBA => 4, |
63 | | - } |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -impl TryFrom<u8> for ColorType { |
68 | | - type Error = anyhow::Error; |
69 | | - |
70 | | - fn try_from(value: u8) -> Result<Self, Self::Error> { |
71 | | - let val = match value { |
72 | | - 0 => Self::Grayscale, |
73 | | - 2 => Self::RGB, |
74 | | - 3 => Self::Palette, |
75 | | - 4 => Self::GrayscaleAlpha, |
76 | | - 6 => Self::RGBA, |
77 | | - foreign => bail!("Unrecognized color type: {}", foreign), |
78 | | - }; |
79 | | - |
80 | | - Ok(val) |
81 | | - } |
82 | | -} |
83 | | - |
84 | 50 | #[derive(Debug)] |
85 | 51 | pub enum Filter { |
86 | 52 | None = 0, |
@@ -114,29 +80,24 @@ pub struct Png { |
114 | 80 | pub(crate) pixel_buffer: Vec<u8>, |
115 | 81 | } |
116 | 82 |
|
117 | | -impl Png { |
118 | | - pub const fn width(&self) -> u32 { |
| 83 | +impl ImageExt for Png { |
| 84 | + fn width(&self) -> u32 { |
119 | 85 | self.image_header.width |
120 | 86 | } |
121 | 87 |
|
122 | | - pub const fn height(&self) -> u32 { |
| 88 | + fn height(&self) -> u32 { |
123 | 89 | self.image_header.height |
124 | 90 | } |
125 | 91 |
|
126 | | - /// The dimensions of the image (width, height). |
127 | | - pub const fn dimensions(&self) -> (u32, u32) { |
128 | | - (self.width(), self.height()) |
129 | | - } |
130 | | - |
131 | | - pub const fn gamma(&self) -> u32 { |
| 92 | + fn gamma(&self) -> u32 { |
132 | 93 | self.gamma |
133 | 94 | } |
134 | 95 |
|
135 | | - pub const fn color_type(&self) -> ColorType { |
| 96 | + fn color_type(&self) -> ColorType { |
136 | 97 | self.image_header.color_type |
137 | 98 | } |
138 | 99 |
|
139 | | - pub fn to_rgb8(&self) -> Cow<'_, [u8]> { |
| 100 | + fn rgb8(&self) -> Cow<'_, [u8]> { |
140 | 101 | match self.color_type() { |
141 | 102 | ColorType::RGB => Cow::from(&self.pixel_buffer), |
142 | 103 | ColorType::RGBA => { |
@@ -170,7 +131,7 @@ impl Png { |
170 | 131 | } |
171 | 132 | } |
172 | 133 |
|
173 | | - pub fn to_rgba8(&self) -> Cow<'_, [u8]> { |
| 134 | + fn rgba8(&self) -> Cow<'_, [u8]> { |
174 | 135 | match self.color_type() { |
175 | 136 | ColorType::RGBA => Cow::from(&self.pixel_buffer), |
176 | 137 | ColorType::RGB => { |
@@ -204,7 +165,7 @@ impl Png { |
204 | 165 | } |
205 | 166 | } |
206 | 167 |
|
207 | | - pub fn to_bitmap(&self) -> Cow<'_, [u32]> { |
| 168 | + fn bitmap(&self) -> Cow<'_, [u32]> { |
208 | 169 | match self.color_type() { |
209 | 170 | ColorType::RGB => { |
210 | 171 | let b = self |
@@ -245,7 +206,9 @@ impl Png { |
245 | 206 | _ => todo!("What do other color type pixels look like?"), |
246 | 207 | } |
247 | 208 | } |
| 209 | +} |
248 | 210 |
|
| 211 | +impl Png { |
249 | 212 | #[cfg(test)] |
250 | 213 | #[allow(dead_code)] |
251 | 214 | pub(crate) fn write_to_binary_blob(&self, path: &str) -> Result<()> { |
|
0 commit comments