firefly_rust/graphics/
image.rs1use crate::*;
2
3pub struct Image<'a> {
8 pub(crate) raw: &'a [u8],
9}
10
11impl<'a> From<File<'a>> for Image<'a> {
12 fn from(value: File<'a>) -> Self {
13 Self { raw: value.raw }
14 }
15}
16
17#[cfg(feature = "alloc")]
18impl<'a> From<&'a FileBuf> for Image<'a> {
19 fn from(value: &'a FileBuf) -> Self {
20 Self { raw: &value.raw }
21 }
22}
23
24impl<'a> From<Canvas<'a>> for Image<'a> {
25 fn from(value: Canvas<'a>) -> Self {
26 Self { raw: value.raw }
27 }
28}
29
30#[cfg(feature = "alloc")]
31impl<'a> From<&'a CanvasBuf> for Image<'a> {
32 fn from(value: &'a CanvasBuf) -> Self {
33 Self { raw: &value.raw }
34 }
35}
36
37impl<'a> Image<'a> {
38 #[must_use]
46 pub const unsafe fn from_bytes(raw: &'a [u8]) -> Self {
47 Self { raw }
48 }
49
50 #[must_use]
52 pub const fn sub(&self, p: Point, s: Size) -> SubImage<'a> {
53 SubImage {
54 point: p,
55 size: s,
56 raw: self.raw,
57 }
58 }
59
60 #[must_use]
62 pub const fn bpp(&self) -> u8 {
63 self.raw[1]
64 }
65
66 #[must_use]
68 pub fn transparency(&self) -> Color {
69 Color::from(self.raw[4] + 1)
70 }
71
72 #[must_use]
83 pub const fn pixels(&self) -> usize {
84 self.raw.len() * 8 / self.bpp() as usize
85 }
86
87 #[must_use]
89 pub fn width(&self) -> u16 {
90 let big = u16::from(self.raw[2]);
91 let little = u16::from(self.raw[3]);
92 big | (little << 8)
93 }
94
95 #[must_use]
97 pub fn height(&self) -> u16 {
98 let p = self.pixels();
99 let w = self.width() as usize;
100 p.checked_div(w).unwrap_or(0) as u16
101 }
102
103 #[must_use]
105 pub fn size(&self) -> Size {
106 Size {
107 width: i32::from(self.width()),
108 height: i32::from(self.height()),
109 }
110 }
111
112 #[must_use]
114 pub fn get_color(&self, p: u8) -> Color {
115 if p > 15 {
116 return Color::None;
117 }
118 let byte_idx = usize::from(5 + p / 2);
119 let mut byte_val = self.raw[byte_idx];
120 if p.is_multiple_of(2) {
121 byte_val >>= 4;
122 }
123 byte_val &= 0b1111;
124 let transp = self.raw[4];
125 if byte_val == transp {
126 return Color::None;
127 }
128 Color::from(byte_val + 1)
129 }
130}
131
132pub struct SubImage<'a> {
134 pub(crate) point: Point,
135 pub(crate) size: Size,
136 pub(crate) raw: &'a [u8],
137}