use crate::image::RgbImage;
pub use crate::prelude::*;
use fltk_sys::draw::*;
use std::os::raw;
pub fn draw_line(x1: i32, y1: i32, x2: i32, y2: i32) {
unsafe {
cfl_line(x1, y1, x2, y2);
}
}
pub fn draw_point(x: i32, y: i32) {
unsafe { cfl_point(x, y) }
}
pub fn draw_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_rect(x, y, w, h) }
}
pub fn draw_rect_with_color(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { cfl_rect_with_color(x, y, w, h, color as u32) }
}
pub fn draw_loop(x1: i32, y1: i32, x2: i32, y2: i32, x3: i32, y3: i32) {
unsafe {
cfl_loop(x1, y1, x2, y2, x3, y3);
}
}
pub fn draw_rect_fill(x: i32, y: i32, w: i32, h: i32, color: Color) {
unsafe { cfl_rectf_with_color(x, y, w, h, color as u32) }
}
pub fn draw_focus_rect(x: i32, y: i32, w: i32, h: i32) {
unsafe { cfl_focus_rect(x, y, w, h) }
}
pub fn set_draw_color(color: Color) {
unsafe { cfl_set_color_int(color.to_u32()) }
}
pub fn draw_circle(x: f64, y: f64, r: f64) {
unsafe {
cfl_circle(x, y, r);
}
}
pub fn draw_arc(x: i32, y: i32, w: i32, h: i32, a: f64, b: f64) {
unsafe {
cfl_arc(x, y, w, h, a, b);
}
}
pub fn draw_pie(x: i32, y: i32, w: i32, h: i32, a: f64, b: f64) {
unsafe {
cfl_pie(x, y, w, h, a, b);
}
}
pub fn capture_window<Window: WindowExt>(win: &mut Window) -> Option<RgbImage> {
let cp = win.width() as usize * win.height() as usize * 3;
win.show();
unsafe {
let x = cfl_read_image(std::ptr::null_mut(), 0, 0, win.width(), win.height(), 0);
if x.is_null() {
None
} else {
let x = std::slice::from_raw_parts(x, cp);
Some(RgbImage::new(x.to_vec(), win.width(), win.height()))
}
}
}
pub fn set_line_style(style: LineStyle, width: i32) {
unsafe {
cfl_line_style(
style as i32,
width,
std::ptr::null_mut() as *mut std::os::raw::c_char,
);
}
}
pub fn push_clip(x: i32, y: i32, w: i32, h: i32) {
unsafe {
cfl_push_clip(x, y, w, h);
}
}
pub fn pop_clip() {
unsafe {
cfl_pop_clip();
}
}
pub fn write_to_png_file(rgb_image: RgbImage, path: &std::path::Path) -> Result<(), FltkError> {
let (data, w, h) = rgb_image.into_parts();
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_png(
data.as_ptr() as *mut u8,
path.as_ptr() as *const raw::c_char,
w,
h,
) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}
pub fn write_to_jpg_file(rgb_image: RgbImage, path: &std::path::Path) -> Result<(), FltkError> {
let (data, w, h) = rgb_image.into_parts();
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_jpg(
data.as_ptr() as *mut u8,
path.as_ptr() as *const raw::c_char,
w,
h,
) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}
pub fn write_to_bmp_file(rgb_image: RgbImage, path: &std::path::Path) -> Result<(), FltkError> {
let (data, w, h) = rgb_image.into_parts();
let path = path.to_str().ok_or(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not convert path to string!",
)))?;
let path = std::ffi::CString::new(path)?;
unsafe {
match cfl_raw_image_to_bmp(
data.as_ptr() as *mut u8,
path.as_ptr() as *const raw::c_char,
w,
h,
) {
-1 => Err(FltkError::IoError(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not write image!",
))),
_ => Ok(()),
}
}
}