1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::image::RgbImage;
pub use crate::prelude::*;
use fltk_sys::draw::*;
use std::os::raw;

/// Draws a line
pub fn draw_line(x1: i32, y1: i32, x2: i32, y2: i32) {
    unsafe {
        cfl_line(x1, y1, x2, y2);
    }
}

/// Draws a point
pub fn draw_point(x: i32, y: i32) {
    unsafe { cfl_point(x, y) }
}

/// Draws a rectangle
pub fn draw_rect(x: i32, y: i32, w: i32, h: i32) {
    unsafe { cfl_rect(x, y, w, h) }
}

/// Draws a rectangle with border color
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) }
}

/// Draws a loop
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);
    }
}

/// Draws a filled rectangle
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) }
}

/// Draws a focus rectangle
pub fn draw_focus_rect(x: i32, y: i32, w: i32, h: i32) {
    unsafe { cfl_focus_rect(x, y, w, h) }
}

/// Sets the drawing color
pub fn set_draw_color(color: Color) {
    unsafe { cfl_set_color_int(color.to_u32()) }
}

/// Draws a circle
pub fn draw_circle(x: f64, y: f64, r: f64) {
    unsafe {
        cfl_circle(x, y, r);
    }
}

/// Draws an arc
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);
    }
}

/// Draws a filled pie
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);
    }
}

/// Captures part of the window and returns raw data
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()))
        }
    }
}

// /// Captures part of the window, returns a raw RGB data
// pub fn capture_window_part<Window: WindowExt>(
//     win: &Window,
//     x: i32,
//     y: i32,
//     w: i32,
//     h: i32,
// ) -> Vec<u8> {
//     assert!(
//         x + w <= win.width() && y + h <= win.height(),
//         "Captures must be less than the parent window's size!"
//     );
//     unsafe {
//         let x = cfl_capture_window_part(win.as_widget_ptr() as *mut raw::c_void, x, y, w, h);
//         assert!(!x.is_null());
//         let cp = w as usize * h as usize * 3;
//         let x = std::slice::from_raw_parts(x, cp);
//         x.to_vec()
//     }
// }

/// Sets the line style
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,
        );
    }
}

/// Limits drawing to a region
pub fn push_clip(x: i32, y: i32, w: i32, h: i32) {
    unsafe {
        cfl_push_clip(x, y, w, h);
    }
}

/// Puts the drawing back
pub fn pop_clip() {
    unsafe {
        cfl_pop_clip();
    }
}

/// Transforms raw data to png file
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(()),
        }
    }
}

/// Transforms raw data to jpg file
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(()),
        }
    }
}

/// Transforms raw data to bmp file
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(()),
        }
    }
}