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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use serde_derive::{Deserialize, Serialize};
use byteorder::{LittleEndian, ReadBytesExt};
pub struct Color {
red: f32,
green: f32,
blue: f32,
alpha: f32,
}
impl Color {
pub fn new(red: f32, green: f32, blue: f32, alpha: f32) -> Color {
Self {
red,
green,
blue,
alpha,
}
}
pub fn as_rgba(&self) -> u32 {
let range = 255.0;
let red = (range * self.red) as u32;
let green = (range * self.green) as u32;
let blue = (range * self.blue) as u32;
let alpha = (range * self.alpha) as u32;
let mut rgba = alpha << 24;
rgba |= blue << 16;
rgba |= green << 8;
rgba |= red;
rgba
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Image {
width: i32,
height: i32,
data: Vec<u32>,
}
impl Image {
pub fn new(width: i32, height: i32, color: Color) -> Self {
let color = color.as_rgba();
let mut d = Vec::with_capacity((width * height) as usize);
for _ in 0..width * height {
d.push(color);
}
Self {
width,
height,
data: d,
}
}
pub fn from_data(width: i32, height: i32, data: &[u8]) -> Self {
let mut d = Vec::with_capacity((width * height) as usize);
for i in 0..width * height {
let pixel = (&data[i as usize * 4..i as usize * 4 + 4])
.read_u32::<LittleEndian>()
.unwrap();
d.push(pixel);
}
Self {
width,
height,
data: d,
}
}
pub fn set_pixel(&mut self, x: i32, y: i32, color: Color) {
let width = self.width();
self.pixels_mut()[(y * width + x) as usize] = color.as_rgba();
}
pub fn width(&self) -> i32 {
self.width
}
pub fn height(&self) -> i32 {
self.height
}
pub fn pixels(&self) -> &[u32] {
&self.data
}
pub fn pixels8(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(self.data.as_ptr() as *const u8, self.data.len() * 4)
}
}
pub fn pixels_mut(&mut self) -> &mut [u32] {
&mut self.data
}
pub fn pixels8_mut(&mut self) -> &mut [u8] {
unsafe {
std::slice::from_raw_parts_mut(self.data.as_mut_ptr() as *mut u8, self.data.len() * 4)
}
}
pub fn blit(&mut self, source: &Image, x: i32, y: i32, width: i32, height: i32) {
let input_width = source.width();
let input_height = source.height();
let output_width = self.width();
let draw_at_x = x;
let draw_at_y = y;
let x_ratio = input_width as f32 / width as f32;
let y_ratio = input_height as f32 / height as f32;
let pixels = source.pixels();
for y in 0..height {
for x in 0..width {
let sample_x = (x as f32 * x_ratio) as i32;
let sample_y = (y as f32 * y_ratio) as i32;
let output = self.pixels_mut();
let o = ((y + draw_at_y) * output_width + x + draw_at_x) as usize;
if o >= output.len() {
continue;
}
let i = (sample_y * input_width + sample_x) as usize;
if i >= pixels.len() {
continue;
}
if pixels[i] & 0xFF_00_00_00 != 0xFF_00_00_00 {
continue;
}
output[o] = pixels[i];
}
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Sound {
sample_rate: i32,
samples: Vec<f32>,
}
impl Sound {
pub fn from_data(sample_rate: i32, samples: &[f32]) -> Self {
Self {
sample_rate,
samples: samples.to_vec(),
}
}
pub fn with_buffer_size(sample_rate: i32, sample_count: i32) -> Self {
let mut samples = Vec::with_capacity(sample_count as usize);
samples.resize(sample_count as usize, 0.0);
Self::from_data(sample_rate, &samples)
}
pub fn with_buffer_sized_to_step(sample_rate: i32, steps_per_second: i32) -> Self {
let sample_count = sample_rate / steps_per_second;
Self::with_buffer_size(sample_rate, sample_count)
}
pub fn set_sample(&mut self, index: i32, sample: f32) {
self.samples_mut()[index as usize] = sample;
}
pub fn sample_rate(&self) -> i32 {
self.sample_rate
}
pub fn sample_count(&self) -> i32 {
self.samples.len() as i32
}
pub fn samples(&self) -> &[f32] {
&self.samples
}
pub fn samples_mut(&mut self) -> &mut [f32] {
&mut self.samples
}
pub fn sample(&self, start: i32, length: i32) -> Self {
Self {
sample_rate: self.sample_rate,
samples: self.samples[(start as usize)..((start + length) as usize)].to_vec(),
}
}
}