1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use {
glam::UVec2,
serde::{Deserialize, Serialize},
};
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct BitmapBuf {
color: BitmapColor,
fmt: BitmapFormat,
#[serde(with = "serde_bytes")]
pixels: Vec<u8>,
width: u32,
}
impl BitmapBuf {
pub fn new(color: BitmapColor, fmt: BitmapFormat, width: u32, pixels: Vec<u8>) -> Self {
Self {
color,
fmt,
pixels,
width,
}
}
pub fn color(&self) -> BitmapColor {
self.color
}
pub fn dims(&self) -> UVec2 {
UVec2::new(self.width, self.height())
}
pub fn format(&self) -> BitmapFormat {
self.fmt
}
pub fn height(&self) -> u32 {
let len = self.pixels.len() as u32;
let width = self.width();
let byte_height = len / width;
match self.fmt {
BitmapFormat::R => byte_height,
BitmapFormat::Rg => byte_height / 2,
BitmapFormat::Rgb => byte_height / 3,
BitmapFormat::Rgba => byte_height >> 2,
}
}
pub fn pixel(&self, x: u32, y: u32) -> &[u8] {
let offset = y as usize * self.stride() + x as usize * self.fmt.byte_len();
&self.pixels[offset..offset + self.fmt.byte_len()]
}
pub fn pixels(&self) -> &[u8] {
&self.pixels
}
pub fn pixels_as_format(&self, dst_fmt: BitmapFormat) -> impl Iterator<Item = u8> + '_ {
let stride = self.fmt.byte_len().min(dst_fmt.byte_len());
self.pixels
.chunks(self.fmt.byte_len())
.map(move |src| {
let mut dst = [0; 4];
dst[0..stride].copy_from_slice(&src[0..stride]);
dst.into_iter()
})
.flatten()
}
pub fn stride(&self) -> usize {
self.width() as usize * self.fmt.byte_len()
}
pub fn width(&self) -> u32 {
self.width
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum BitmapFormat {
#[serde(rename = "r")]
R,
#[serde(rename = "rg")]
Rg,
#[serde(rename = "rgb")]
Rgb,
#[serde(rename = "rgba")]
Rgba,
}
impl BitmapFormat {
#[inline]
pub const fn byte_len(self) -> usize {
match self {
Self::R => 1,
Self::Rg => 2,
Self::Rgb => 3,
Self::Rgba => 4,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum BitmapColor {
#[serde(rename = "linear")]
Linear,
#[serde(rename = "srgb")]
Srgb,
}