Newer
Older
//! Color manipulation functions and constants.
use boytacean_common::util::copy_fast;
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
pub const RGB_SIZE: usize = 3;
pub const RGBA_SIZE: usize = 4;
pub const RGB888_SIZE: usize = 3;
pub const XRGB8888_SIZE: usize = 4;
pub const RGB1555_SIZE: usize = 2;
pub const RGB565_SIZE: usize = 2;
/// Defines the Game Boy pixel type as a buffer
/// with the size of RGB (3 bytes).
pub type Pixel = [u8; RGB_SIZE];
/// Defines a transparent Game Boy pixel type as a buffer
/// with the size of RGBA (4 bytes).
pub type PixelAlpha = [u8; RGBA_SIZE];
/// Defines a pixel with 5 bits per channel plus a padding
/// bit at the beginning.
pub type PixelRgb1555 = [u8; RGB1555_SIZE];
/// Defines a pixel with 5 bits per channel except for the
/// green channel which uses 6 bits.
pub type PixelRgb565 = [u8; RGB565_SIZE];
pub fn rgb555_to_rgb888(first: u8, second: u8) -> Pixel {
let r = (first & 0x1f) << 3;
let g = (((first & 0xe0) >> 5) | ((second & 0x03) << 3)) << 3;
let b = ((second & 0x7c) >> 2) << 3;
[r, g, b]
}
pub fn rgb888_to_rgb1555(first: u8, second: u8, third: u8) -> PixelRgb1555 {
let pixel = rgb888_to_rgb1555_u16(first, second, third);
[pixel as u8, (pixel >> 8) as u8]
}
pub fn rgb888_to_rgb1555_u16(first: u8, second: u8, third: u8) -> u16 {
let r = (first as u16 >> 3) & 0x1f;
let g = (second as u16 >> 3) & 0x1f;
let b = (third as u16 >> 3) & 0x1f;
let a = 1;
(a << 15) | (r << 10) | (g << 5) | b
}
pub fn rgb888_to_rgb565(first: u8, second: u8, third: u8) -> PixelRgb565 {
let pixel = rgb888_to_rgb565_u16(first, second, third);
[pixel as u8, (pixel >> 8) as u8]
}
pub fn rgb888_to_rgb565_u16(first: u8, second: u8, third: u8) -> u16 {
let r = (first as u16 >> 3) & 0x1f;
let g = (second as u16 >> 2) & 0x3f;
let b = (third as u16 >> 3) & 0x1f;
(r << 11) | (g << 5) | b
}
pub fn rgb888_to_rgb1555_array(rgb888_pixels: &[u8], rgb1555_pixels: &mut [u8]) {
#[cfg(feature = "simd")]
{
rgb888_to_rgb1555_simd(rgb888_pixels, rgb1555_pixels);
}
#[cfg(not(feature = "simd"))]
{
rgb888_to_rgb1555_scalar(rgb888_pixels, rgb1555_pixels);
}
}
/// Converts an array of RGB888 pixels to RGB565 format using a scalar implementation.
///
/// This method should provide the same results as the SIMD implementation.
pub fn rgb888_to_rgb1555_scalar(rgb888_pixels: &[u8], rgb1555_pixels: &mut [u8]) {
assert!(
rgb888_pixels.len() % 3 == 0,
"Length of rgb888_pixels must be a multiple of 3"
);
assert!(
rgb1555_pixels.len() % 2 == 0,
"Length of rgb1555_pixels must be a multiple of 2"
);
assert!(
rgb888_pixels.len() / 3 == rgb1555_pixels.len() / 2,
"Length of rgb1555_pixels must be two thirds the length of rgb888_pixels"
);
for index in 0..rgb888_pixels.len() / RGB_SIZE {
let (r, g, b) = (
rgb888_pixels[index * RGB_SIZE],
rgb888_pixels[index * RGB_SIZE + 1],
rgb888_pixels[index * RGB_SIZE + 2],
);
let rgb1555 = rgb888_to_rgb1555(r, g, b);
let output_offset = index * RGB1555_SIZE;
copy_fast(
&rgb1555,
&mut rgb1555_pixels[output_offset..output_offset + RGB1555_SIZE],
RGB1555_SIZE,
)
}
}
/// Converts an array of RGB888 pixels to RGB1555 format using SIMD.
///
/// This method is only available when the `simd` feature is enabled.
///
/// Note: The length of `rgb888_pixels` must be a multiple of 3, and
/// `rgb1555_pixels` must be a multiple of 2.
#[cfg(feature = "simd")]
pub fn rgb888_to_rgb1555_simd(rgb888_pixels: &[u8], rgb1555_pixels: &mut [u8]) {
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
use std::simd::u8x16;
const SIMD_WIDTH: usize = 16;
assert!(
rgb888_pixels.len() % 3 == 0,
"Length of rgb888_pixels must be a multiple of 3"
);
assert!(
rgb1555_pixels.len() % 2 == 0,
"Length of rgb1555_pixels must be a multiple of 2"
);
assert!(
rgb888_pixels.len() / 3 == rgb1555_pixels.len() / 2,
"Length of rgb1555_pixels must be two thirds the length of rgb888_pixels"
);
let num_pixels = rgb888_pixels.len() / 3;
let simd_chunks = num_pixels / SIMD_WIDTH;
for index in 0..simd_chunks {
let offset = index * SIMD_WIDTH * 3;
let r = u8x16::from_slice(&[
rgb888_pixels[offset],
rgb888_pixels[offset + 3],
rgb888_pixels[offset + 6],
rgb888_pixels[offset + 9],
rgb888_pixels[offset + 12],
rgb888_pixels[offset + 15],
rgb888_pixels[offset + 18],
rgb888_pixels[offset + 21],
rgb888_pixels[offset + 24],
rgb888_pixels[offset + 27],
rgb888_pixels[offset + 30],
rgb888_pixels[offset + 33],
rgb888_pixels[offset + 36],
rgb888_pixels[offset + 39],
rgb888_pixels[offset + 42],
rgb888_pixels[offset + 45],
]);
let g = u8x16::from_slice(&[
rgb888_pixels[offset + 1],
rgb888_pixels[offset + 4],
rgb888_pixels[offset + 7],
rgb888_pixels[offset + 10],
rgb888_pixels[offset + 13],
rgb888_pixels[offset + 16],
rgb888_pixels[offset + 19],
rgb888_pixels[offset + 22],
rgb888_pixels[offset + 25],
rgb888_pixels[offset + 28],
rgb888_pixels[offset + 31],
rgb888_pixels[offset + 34],
rgb888_pixels[offset + 37],
rgb888_pixels[offset + 40],
rgb888_pixels[offset + 43],
rgb888_pixels[offset + 46],
]);
let b = u8x16::from_slice(&[
rgb888_pixels[offset + 2],
rgb888_pixels[offset + 5],
rgb888_pixels[offset + 8],
rgb888_pixels[offset + 11],
rgb888_pixels[offset + 14],
rgb888_pixels[offset + 17],
rgb888_pixels[offset + 20],
rgb888_pixels[offset + 23],
rgb888_pixels[offset + 26],
rgb888_pixels[offset + 29],
rgb888_pixels[offset + 32],
rgb888_pixels[offset + 35],
rgb888_pixels[offset + 38],
rgb888_pixels[offset + 41],
rgb888_pixels[offset + 44],
rgb888_pixels[offset + 47],
]);
let r_shifted_high = (r >> 1) & u8x16::splat(0x7c);
let g_shifted_high = (g >> 6) & u8x16::splat(0x03);
let g_shifted_low = (g << 2) & u8x16::splat(0xe0);
let b_shifted = (b >> 3) & u8x16::splat(0x3f);
let high_byte = u8x16::splat(0x80) | r_shifted_high | g_shifted_high;
let low_byte = g_shifted_low | b_shifted;
let output_offset = index * SIMD_WIDTH * RGB1555_SIZE;
interleave_arrays(
low_byte.as_array(),
high_byte.as_array(),
&mut rgb1555_pixels[output_offset..output_offset + SIMD_WIDTH * RGB1555_SIZE],
);
}
let remainder = num_pixels % SIMD_WIDTH;
let offset = simd_chunks * SIMD_WIDTH * RGB_SIZE;
let offset_rgb1555 = simd_chunks * SIMD_WIDTH * RGB1555_SIZE;
for index in 0..remainder {
let current_offset = offset + index * RGB_SIZE;
let (r, g, b) = (
rgb888_pixels[current_offset],
rgb888_pixels[current_offset + 1],
rgb888_pixels[current_offset + 2],
);
let rgb1555 = rgb888_to_rgb1555(r, g, b);
let output_offset = offset_rgb1555 + index * RGB1555_SIZE;
copy_fast(
&rgb1555,
&mut rgb1555_pixels[output_offset..output_offset + RGB1555_SIZE],
RGB1555_SIZE,
);
}
}
#[cfg(test)]
mod tests {
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use super::{rgb888_to_rgb1555, rgb888_to_rgb1555_scalar};
#[test]
fn test_rgb888_to_rgb1555() {
let result = rgb888_to_rgb1555(255, 0, 0);
assert_eq!(result, [0b00000000, 0b11111100]);
let result = rgb888_to_rgb1555(0, 255, 0);
assert_eq!(result, [0b11100000, 0b10000011]);
let result = rgb888_to_rgb1555(0, 0, 255);
assert_eq!(result, [0b00011111, 0b10000000]);
let result = rgb888_to_rgb1555(255, 255, 0);
assert_eq!(result, [0b11100000, 0b11111111]);
}
#[test]
fn test_rgb888_to_rgb1555_scalar() {
let rgb888_pixels: Vec<u8> = vec![
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
020, 020, 200, // Blueish
];
let mut rgb1555_pixels: Vec<u8> = vec![0; 36];
rgb888_to_rgb1555_scalar(&rgb888_pixels, &mut rgb1555_pixels);
let expected_rgb1555: Vec<u8> = vec![
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b01011001, 0b10001000, // Blueish
];
assert_eq!(rgb1555_pixels, expected_rgb1555);
}
#[test]
#[cfg(feature = "simd")]
fn test_rgb888_to_rgb1555_simd() {
use super::rgb888_to_rgb1555_simd;
let rgb888_pixels: Vec<u8> = vec![
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
000, 255, 000, // Green
000, 000, 255, // Blue
255, 255, 000, // Yellow
255, 000, 000, // Red
020, 020, 200, // Blueish
];
let mut rgb1555_pixels: Vec<u8> = vec![0; 36];
rgb888_to_rgb1555_simd(&rgb888_pixels, &mut rgb1555_pixels);
let expected_rgb1555: Vec<u8> = vec![
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b11100000, 0b10000011, // Green
0b00011111, 0b10000000, // Blue
0b11100000, 0b11111111, // Yellow
0b00000000, 0b11111100, // Red
0b01011001, 0b10001000, // Blueish
];
assert_eq!(rgb1555_pixels, expected_rgb1555);
}
}