winit/platform_impl/linux/x11/util/
icon.rs

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 = 0;
9        assert!(CARDINAL_SIZE >= PIXEL_SIZE);
10        let as_bytes = &mut cardinal as *mut _ as *mut u8;
11        unsafe {
12            *as_bytes.offset(0) = self.b;
13            *as_bytes.offset(1) = self.g;
14            *as_bytes.offset(2) = self.r;
15            *as_bytes.offset(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 = self.rgba.len() / PIXEL_SIZE;
25        assert_eq!(pixel_count, (self.width * self.height) as usize);
26        let mut data = Vec::with_capacity(pixel_count);
27        data.push(self.width as Cardinal);
28        data.push(self.height as Cardinal);
29        let pixels = self.rgba.as_ptr() as *const Pixel;
30        for pixel_index in 0..pixel_count {
31            let pixel = unsafe { &*pixels.add(pixel_index) };
32            data.push(pixel.to_packed_argb());
33        }
34        data
35    }
36}