use crate::Dssim;
use rgb::{RGB8, RGBA8};
use std::ptr;
pub type DssimImage = crate::DssimImage<f32>;
#[unsafe(no_mangle)]
pub extern "C" fn dssim_new() -> *mut Dssim {
let d = Box::new(crate::new());
Box::into_raw(d)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dssim_free(d: *mut Dssim) {
if d.is_null() {
return;
}
let _ = unsafe { Box::from_raw(d) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dssim_create_image_rgba(dssim: &mut Dssim, pixels: *const u8, width: u32, height: u32) -> *mut DssimImage {
if pixels.is_null() {
return ptr::null_mut();
}
let width = width as usize;
let height = height as usize;
let pixels = unsafe { std::slice::from_raw_parts(pixels.cast::<RGBA8>(), width * height) };
match dssim.create_image_rgba(pixels, width, height) {
Some(img) => Box::into_raw(Box::new(img)),
None => ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dssim_create_image_rgb(dssim: &mut Dssim, pixels: *const u8, width: u32, height: u32) -> *mut DssimImage {
if pixels.is_null() {
return ptr::null_mut();
}
let width = width as usize;
let height = height as usize;
let pixels = unsafe { std::slice::from_raw_parts(pixels.cast::<RGB8>(), width * height) };
match dssim.create_image_rgb(pixels, width, height) {
Some(img) => Box::into_raw(Box::new(img)),
None => ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dssim_free_image(img: *mut DssimImage) {
if img.is_null() {
return;
}
let _ = unsafe { Box::from_raw(img) };
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn dssim_compare(dssim: &mut Dssim, img1: *const DssimImage, img2: *const DssimImage) -> f64 {
let Some(img1) = (unsafe { img1.as_ref() }) else { return f64::NAN };
let Some(img2) = (unsafe { img2.as_ref() }) else { return f64::NAN };
let (val, _) = dssim.compare(img1, img2);
val.into()
}