Skip to content

Commit 006fbde

Browse files
committed
Add bindings for images (art4711/draw-pixmap branch)
1 parent a8a2dd9 commit 006fbde

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

ui-sys/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,35 @@ extern {
742742
pub fn uiNewColorButton() -> *mut uiColorButton;
743743
}
744744

745+
746+
pub enum uiImage {}
747+
pub type uiPixmap32Format = u32;
748+
749+
#[repr(C)]
750+
pub struct uiImageData {
751+
pub fmt: uiPixmap32Format,
752+
pub width: c_int,
753+
pub height: c_int,
754+
pub rowstride: c_int,
755+
pub data: *mut c_void,
756+
}
757+
758+
#[link(name = "ui")]
759+
extern {
760+
pub fn uiNewImage(width: c_int, height: c_int) -> *mut uiImage;
761+
pub fn uiFreeImage(img: *mut uiImage);
762+
pub fn uiImageGetFormat(img: *mut uiImage) -> uiPixmap32Format;
763+
pub fn uiImageGetData(img: *const uiImage, data: *mut uiImageData);
764+
pub fn uiImageLoadPixmap32Raw(img: *mut uiImage,
765+
x: c_int,
766+
y: c_int,
767+
width: c_int,
768+
height: c_int,
769+
rowstrideBytes: c_int,
770+
fmt: uiPixmap32Format,
771+
data: *const c_void);
772+
pub fn uiDrawImage(c: *mut uiDrawContext,
773+
x: c_double,
774+
y: c_double,
775+
img: *const uiImage);
776+
}

ui/src/draw.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub use ui_sys::uiDrawLineJoin as LineJoin;
1515
pub use ui_sys::uiDrawDefaultMiterLimit as DEFAULT_MITER_LIMIT;
1616
pub use ui_sys::uiDrawFillMode as FillMode;
1717

18+
use image;
19+
1820
pub struct Context {
1921
ui_draw_context: *mut uiDrawContext,
2022
}
@@ -85,6 +87,12 @@ impl Context {
8587
ui_sys::uiDrawText(self.ui_draw_context, x, y, layout.as_ui_draw_text_layout())
8688
}
8789
}
90+
91+
pub fn draw_image(&self, x: f64, y: f64, img: &image::Image) {
92+
unsafe {
93+
ui_sys::uiDrawImage(self.ui_draw_context, x, y, img.as_ui_draw_image())
94+
}
95+
}
8896
}
8997

9098
#[derive(Clone, Debug)]

ui/src/image.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use libc::c_void;
2+
use ui_sys::{self, uiImage, uiImageData, uiNewImage,
3+
uiFreeImage, uiImageGetFormat, uiImageGetData,
4+
uiImageLoadPixmap32Raw, uiDrawImage};
5+
use ui_sys::uiDrawContext;
6+
7+
pub struct Image {
8+
ui_image: *mut uiImage
9+
}
10+
11+
// #define uiPixmap32FormatOffsets(a,r,g,b) ((a) << 0 | (r) << 2 | (g) << 4 | (b) << 6)
12+
const uiPixmap32FormatOffsetMask: u32 = 0x0ff;
13+
const uiPixmap32FormatHasAlpha: u32 = 0x100;
14+
const uiPixmap32FormatAlphaPremultiplied: u32 = 0x200;
15+
const uiPixmap32FormatZeroRowBottom: u32 = 0x400;
16+
17+
impl Image {
18+
pub fn new(w: i32, h: i32) -> Image {
19+
unsafe {
20+
Image {
21+
ui_image: uiNewImage(w, h)
22+
}
23+
}
24+
}
25+
26+
#[inline]
27+
pub fn as_ui_draw_image(&self) -> *const uiImage {
28+
self.ui_image
29+
}
30+
31+
pub fn load_pixmap(&self, offset_x: i32, offset_y: i32, w: i32, h: i32, data: &[u32]) {
32+
unsafe {
33+
// uiImageLoadPixmap32Raw(uiImage *img, int x, int y, int width, int height,
34+
// int rowstrideBytes, uiPixmap32Format fmt, void *data);
35+
let img_data = get_image_data(self.ui_image);
36+
uiImageLoadPixmap32Raw(self.ui_image, offset_x, offset_y, w, h, w*4, img_data.fmt, data.as_ptr() as *const c_void);
37+
}
38+
}
39+
}
40+
41+
impl Drop for Image {
42+
fn drop(&mut self) {
43+
unsafe { uiFreeImage(self.ui_image) };
44+
}
45+
}
46+
47+
fn get_image_data(img: *const uiImage) -> uiImageData {
48+
use std::ptr;
49+
let mut d = uiImageData {
50+
fmt: 0,
51+
width: 0,
52+
height: 0,
53+
rowstride: 0,
54+
data: ptr::null_mut(),
55+
};
56+
57+
unsafe { uiImageGetData(img, &mut d) }
58+
59+
d
60+
}

ui/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use menus::{Menu, MenuItem};
1717
pub use ui::{InitError, InitOptions, init, main, msg_box, msg_box_error, on_should_quit};
1818
pub use ui::{open_file, queue_main, quit, save_file, uninit};
1919
pub use windows::Window;
20+
pub use image::Image;
2021

2122
#[macro_use]
2223
mod controls;
@@ -25,4 +26,5 @@ pub mod ffi_utils;
2526
mod menus;
2627
mod ui;
2728
mod windows;
29+
mod image;
2830

0 commit comments

Comments
 (0)