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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use crate::prelude::*;
use crate::{
AlphaType, Color, Color4f, ColorSpace, ColorType, FilterQuality, IPoint, IRect, ISize,
ImageInfo,
};
use skia_bindings as sb;
use skia_bindings::SkPixmap;
use std::convert::TryInto;
use std::ffi::c_void;
use std::os::raw;
use std::{ptr, slice};
pub type Pixmap = Handle<SkPixmap>;
impl NativeDrop for SkPixmap {
fn drop(&mut self) {
unsafe { sb::C_SkPixmap_destruct(self) }
}
}
impl Default for Handle<SkPixmap> {
fn default() -> Self {
Pixmap::from_native(SkPixmap {
fPixels: ptr::null(),
fRowBytes: 0,
fInfo: construct(|ii| unsafe { sb::C_SkImageInfo_Construct(ii) }),
})
}
}
impl Handle<SkPixmap> {
pub fn new<'pixels>(
info: &ImageInfo,
pixels: &'pixels [u8],
row_bytes: usize,
) -> Borrows<'pixels, Self> {
let width: usize = info.width().try_into().unwrap();
let height: usize = info.height().try_into().unwrap();
assert!(row_bytes >= width * info.bytes_per_pixel());
assert!(pixels.len() >= height * row_bytes);
let pm = Pixmap::from_native(SkPixmap {
fPixels: ptr::null(),
fRowBytes: 0,
fInfo: ImageInfo::new_unknown(None).native().clone(),
});
pm.borrows(pixels)
}
pub fn reset(&mut self) -> &mut Self {
unsafe { self.native_mut().reset() }
self
}
pub fn set_color_space(&mut self, color_space: impl Into<Option<ColorSpace>>) -> &mut Self {
unsafe {
sb::C_SkPixmap_setColorSpace(self.native_mut(), color_space.into().into_ptr_or_null())
}
self
}
pub fn extract_subset(&self, area: impl AsRef<IRect>) -> Option<Pixmap> {
let mut pixmap = Pixmap::default();
unsafe {
self.native()
.extractSubset(pixmap.native_mut(), area.as_ref().native())
}
.if_true_some(pixmap)
}
pub fn info(&self) -> &ImageInfo {
ImageInfo::from_native_ref(&self.native().fInfo)
}
pub fn row_bytes(&self) -> usize {
self.native().fRowBytes
}
pub unsafe fn addr(&self) -> *const c_void {
self.native().fPixels
}
pub fn width(&self) -> i32 {
self.info().width()
}
pub fn height(&self) -> i32 {
self.info().height()
}
pub fn dimensions(&self) -> ISize {
self.info().dimensions()
}
pub fn color_type(&self) -> ColorType {
self.info().color_type()
}
pub fn alpha_type(&self) -> AlphaType {
self.info().alpha_type()
}
pub fn color_space(&self) -> Option<ColorSpace> {
self.info().color_space()
}
pub fn is_opaque(&self) -> bool {
self.alpha_type().is_opaque()
}
pub fn bounds(&self) -> IRect {
IRect::from_wh(self.width(), self.height())
}
pub fn row_bytes_as_pixels(&self) -> usize {
self.row_bytes() >> self.shift_per_pixel()
}
pub fn shift_per_pixel(&self) -> usize {
self.info().shift_per_pixel()
}
pub fn compute_byte_size(&self) -> usize {
self.info().compute_byte_size(self.row_bytes())
}
pub fn compute_is_opaque(&self) -> bool {
unsafe { self.native().computeIsOpaque() }
}
pub fn get_color(&self, p: impl Into<IPoint>) -> Color {
let p = p.into();
self.assert_pixel_exists(p);
Color::from_native(unsafe { self.native().getColor(p.x, p.y) })
}
pub fn get_alpha_f(&self, p: impl Into<IPoint>) -> f32 {
let p = p.into();
self.assert_pixel_exists(p);
unsafe { self.native().getAlphaf(p.x, p.y) }
}
fn assert_pixel_exists(&self, p: impl Into<IPoint>) {
let p = p.into();
assert!(!unsafe { self.addr() }.is_null());
assert!(p.x >= 0 && p.x < self.width());
assert!(p.y >= 0 && p.y < self.height());
}
pub unsafe fn addr_at(&self, p: impl Into<IPoint>) -> *const c_void {
let p = p.into();
(self.addr() as *const raw::c_char).add(self.info().compute_offset(p, self.row_bytes()))
as _
}
pub unsafe fn writable_addr(&self) -> *mut c_void {
self.addr() as _
}
pub unsafe fn writable_addr_at(&self, p: impl Into<IPoint>) -> *mut c_void {
self.addr_at(p) as _
}
pub fn read_pixels<P>(
&self,
dst_info: &ImageInfo,
pixels: &mut [P],
dst_row_bytes: usize,
src: impl Into<IPoint>,
) -> bool {
if pixels.elements_size_of()
!= (usize::try_from(dst_info.height()).unwrap() * dst_row_bytes)
{
return false;
}
let src = src.into();
unsafe {
self.native().readPixels(
dst_info.native(),
pixels.as_mut_ptr() as _,
dst_row_bytes,
src.x,
src.y,
)
}
}
pub fn read_pixels_to_pixmap(&self, dst: &Pixmap, src: impl Into<IPoint>) -> bool {
let row_bytes = dst.row_bytes();
let len = usize::try_from(dst.height()).unwrap() * row_bytes;
unsafe {
let addr = dst.writable_addr() as *mut raw::c_char;
self.read_pixels(
dst.info(),
slice::from_raw_parts_mut(addr, len),
row_bytes,
src,
)
}
}
pub fn scale_pixels(&self, dst: &Pixmap, filter_quality: FilterQuality) -> bool {
unsafe {
self.native()
.scalePixels(dst.native(), filter_quality.into_native())
}
}
pub fn erase(&self, color: impl Into<Color>, subset: Option<&IRect>) -> bool {
let color = color.into().into_native();
unsafe {
match subset {
Some(subset) => self.native().erase(color, subset.native()),
None => self.native().erase(color, self.bounds().native()),
}
}
}
pub fn erase_4f(&self, color: impl AsRef<Color4f>, subset: Option<&IRect>) -> bool {
let color = color.as_ref();
unsafe {
self.native()
.erase1(color.native(), subset.native_ptr_or_null())
}
}
}