1#![allow(clippy::assertions_on_constants)]
2
3use super::*;
4use crate::icon::{Pixel, RgbaIcon, PIXEL_SIZE};
5
6impl Pixel {
7 pub fn to_packed_argb(&self) -> Cardinal {
8 let mut cardinal: u32 = 0;
9 assert!(CARDINAL_SIZE >= PIXEL_SIZE);
10 let as_bytes: *mut u8 = &mut cardinal as *mut _ as *mut u8;
11 unsafe {
12 *as_bytes.offset(count:0) = self.b;
13 *as_bytes.offset(count:1) = self.g;
14 *as_bytes.offset(count:2) = self.r;
15 *as_bytes.offset(count:3) = self.a;
16 }
17 cardinal
18 }
19}
20
21impl RgbaIcon {
22 pub(crate) fn to_cardinals(&self) -> Vec<Cardinal> {
23 assert_eq!(self.rgba.len() % PIXEL_SIZE, 0);
24 let pixel_count: usize = self.rgba.len() / PIXEL_SIZE;
25 assert_eq!(pixel_count, (self.width * self.height) as usize);
26 let mut data: Vec = Vec::with_capacity(pixel_count);
27 data.push(self.width as Cardinal);
28 data.push(self.height as Cardinal);
29 let pixels: *const Pixel = self.rgba.as_ptr() as *const Pixel;
30 for pixel_index: usize in 0..pixel_count {
31 let pixel: &Pixel = unsafe { &*pixels.add(count:pixel_index) };
32 data.push(pixel.to_packed_argb());
33 }
34 data
35 }
36}
37