use std::{fs::File, io::BufWriter, path::Path};
use gdk_pixbuf::{Colorspace, Pixbuf};
use crate::window::BadIcon;
#[derive(Debug, Clone)]
pub struct PlatformIcon {
raw: Vec<u8>,
width: i32,
height: i32,
row_stride: i32,
}
impl From<PlatformIcon> for Pixbuf {
fn from(icon: PlatformIcon) -> Self {
Pixbuf::from_mut_slice(
icon.raw,
gdk_pixbuf::Colorspace::Rgb,
true,
8,
icon.width,
icon.height,
icon.row_stride,
)
}
}
impl PlatformIcon {
pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
let row_stride =
Pixbuf::calculate_rowstride(Colorspace::Rgb, true, 8, width as i32, height as i32);
Ok(Self {
raw: rgba,
width: width as i32,
height: height as i32,
row_stride,
})
}
pub fn write_to_png(&self, path: impl AsRef<Path>) {
let png = File::create(path).unwrap();
let ref mut w = BufWriter::new(png);
let mut encoder = png::Encoder::new(w, self.width as _, self.height as _);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(&self.raw).unwrap();
}
}