use nappgui_sys::{
pixbuf_cdata, pixbuf_convert, pixbuf_copy, pixbuf_create, pixbuf_data, pixbuf_destroy,
pixbuf_dsize, pixbuf_format, pixbuf_format_bpp, pixbuf_get, pixbuf_height, pixbuf_set,
pixbuf_size, pixbuf_trim, pixbuf_width,
};
use crate::types::PixFormat;
use super::Palette;
pub struct Pixbuf {
pub(crate) inner: *mut nappgui_sys::Pixbuf,
}
impl Pixbuf {
pub(crate) fn new(ptr: *mut nappgui_sys::Pixbuf) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
pub fn create(width: u32, height: u32, format: PixFormat) -> Self {
let pixbuf = unsafe { pixbuf_create(width, height, format as _) };
Self::new(pixbuf)
}
pub fn trim(&self, x: u32, y: u32, width: u32, height: u32) -> Self {
let pixbuf = unsafe { pixbuf_trim(self.inner, x, y, width, height) };
Self::new(pixbuf)
}
pub fn convert(&self, palette: &Palette, oformat: PixFormat) -> Self {
let pixbuf = unsafe { pixbuf_convert(self.inner, palette.inner, oformat as _) };
Self::new(pixbuf)
}
pub fn format(&self) -> PixFormat {
let result = unsafe { pixbuf_format(self.inner) };
PixFormat::try_from(result).unwrap()
}
pub fn width(&self) -> u32 {
unsafe { pixbuf_width(self.inner) }
}
pub fn height(&self) -> u32 {
unsafe { pixbuf_height(self.inner) }
}
pub fn size(&self) -> u32 {
unsafe { pixbuf_size(self.inner) }
}
pub fn dsize(&self) -> u32 {
unsafe { pixbuf_dsize(self.inner) }
}
pub fn cdata(&self) -> *const u8 {
unsafe { pixbuf_cdata(self.inner) }
}
pub fn data(&mut self) -> *mut u8 {
unsafe { pixbuf_data(self.inner) }
}
pub fn format_bpp(format: PixFormat) -> u32 {
unsafe { pixbuf_format_bpp(format as _) }
}
pub fn get(&self, x: u32, y: u32) -> u32 {
unsafe { pixbuf_get(self.inner, x, y) }
}
pub fn set(&mut self, x: u32, y: u32, value: u32) {
unsafe { pixbuf_set(self.inner, x, y, value) }
}
}
impl Clone for Pixbuf {
fn clone(&self) -> Self {
let pixbuf = unsafe { pixbuf_copy(self.inner) };
Self::new(pixbuf)
}
}
impl Drop for Pixbuf {
fn drop(&mut self) {
unsafe {
pixbuf_destroy(&mut self.inner);
}
}
}