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
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::slice;
use enums::{Format, SurfaceType};
use ffi;
#[cfg(feature = "use_glib")]
use glib::translate::*;
use std::fmt;
use surface::Surface;
use BorrowError;
use Status;
declare_surface!(ImageSurface, SurfaceType::Image);
impl ImageSurface {
pub fn create(format: Format, width: i32, height: i32) -> Result<ImageSurface, Status> {
unsafe {
Self::from_raw_full(ffi::cairo_image_surface_create(
format.into(),
width,
height,
))
}
}
pub fn create_for_data<D: AsMut<[u8]> + 'static>(
data: D,
format: Format,
width: i32,
height: i32,
stride: i32,
) -> Result<ImageSurface, Status> {
let mut data: Box<dyn AsMut<[u8]>> = Box::new(data);
let (ptr, len) = {
let data: &mut [u8] = (*data).as_mut();
(data.as_mut_ptr(), data.len())
};
assert!(len >= (height * stride) as usize);
let result = unsafe {
ImageSurface::from_raw_full(ffi::cairo_image_surface_create_for_data(
ptr,
format.into(),
width,
height,
stride,
))
};
if let Ok(surface) = &result {
static IMAGE_SURFACE_DATA: crate::UserDataKey<Box<dyn AsMut<[u8]>>> =
crate::UserDataKey::new();
surface.set_user_data(&IMAGE_SURFACE_DATA, Rc::new(data))
}
result
}
pub fn get_data(&mut self) -> Result<ImageSurfaceData, BorrowError> {
unsafe {
if ffi::cairo_surface_get_reference_count(self.to_raw_none()) > 1 {
return Err(BorrowError::NonExclusive);
}
self.flush();
match self.status() {
Status::Success => (),
status => return Err(BorrowError::from(status)),
}
if ffi::cairo_image_surface_get_data(self.to_raw_none()).is_null() {
return Err(BorrowError::from(Status::SurfaceFinished));
}
Ok(ImageSurfaceData::new(self))
}
}
pub fn get_format(&self) -> Format {
unsafe { Format::from(ffi::cairo_image_surface_get_format(self.to_raw_none())) }
}
pub fn get_height(&self) -> i32 {
unsafe { ffi::cairo_image_surface_get_height(self.to_raw_none()) }
}
pub fn get_stride(&self) -> i32 {
unsafe { ffi::cairo_image_surface_get_stride(self.to_raw_none()) }
}
pub fn get_width(&self) -> i32 {
unsafe { ffi::cairo_image_surface_get_width(self.to_raw_none()) }
}
}
#[derive(Debug)]
pub struct ImageSurfaceData<'a> {
surface: &'a mut ImageSurface,
slice: &'a mut [u8],
dirty: bool,
}
impl<'a> ImageSurfaceData<'a> {
fn new(surface: &'a mut ImageSurface) -> ImageSurfaceData<'a> {
unsafe {
let ptr = ffi::cairo_image_surface_get_data(surface.to_raw_none());
debug_assert!(!ptr.is_null());
let len = (surface.get_stride() as usize) * (surface.get_height() as usize);
ImageSurfaceData {
surface,
slice: slice::from_raw_parts_mut(ptr, len),
dirty: false,
}
}
}
}
impl<'a> Drop for ImageSurfaceData<'a> {
fn drop(&mut self) {
if self.dirty {
unsafe { ffi::cairo_surface_mark_dirty(self.surface.to_raw_none()) }
}
}
}
impl<'a> Deref for ImageSurfaceData<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.slice
}
}
impl<'a> DerefMut for ImageSurfaceData<'a> {
fn deref_mut(&mut self) -> &mut [u8] {
self.dirty = true;
self.slice
}
}
impl<'a> fmt::Display for ImageSurfaceData<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ImageSurfaceData")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_with_invalid_size_yields_error() {
let result = ImageSurface::create(Format::ARgb32, 50000, 50000);
assert!(result.is_err());
}
#[test]
fn create_for_data_with_invalid_stride_yields_error() {
let result = ImageSurface::create_for_data(vec![0u8; 10], Format::ARgb32, 1, 2, 5);
assert!(result.is_err());
}
#[test]
fn create_with_valid_size() {
let result = ImageSurface::create(Format::ARgb32, 10, 10);
assert!(result.is_ok());
let result = ImageSurface::create_for_data(vec![0u8; 40 * 10], Format::ARgb32, 10, 10, 40);
assert!(result.is_ok());
}
}