show_image/features/
raqote.rs1use crate::error::ImageDataError;
4use crate::BoxImage;
5use crate::Image;
6use crate::ImageInfo;
7
8impl From<raqote::DrawTarget> for Image {
9 fn from(other: raqote::DrawTarget) -> Self {
10 let info = match draw_target_info(&other) {
11 Ok(x) => x,
12 Err(e) => return Image::Invalid(e),
13 };
14
15 let length = other.get_data_u8().len();
16 let buffer = Box::into_raw(other.into_vec().into_boxed_slice()) as *mut u8;
17 let buffer = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(buffer, length)) };
18
19 BoxImage::new(info, buffer).into()
20 }
21}
22
23impl From<&raqote::DrawTarget> for Image {
24 fn from(other: &raqote::DrawTarget) -> Self {
25 let info = match draw_target_info(other) {
26 Ok(x) => x,
27 Err(e) => return Image::Invalid(e),
28 };
29
30 let buffer = Box::from(other.get_data_u8());
31
32 BoxImage::new(info, buffer).into()
33 }
34}
35
36impl From<raqote::Image<'_>> for Image {
37 fn from(other: raqote::Image) -> Self {
38 let info = match image_info(&other) {
39 Ok(x) => x,
40 Err(e) => return Image::Invalid(e),
41 };
42
43 let buffer = other.data.as_ptr() as *const u8;
44 let buffer = unsafe { Box::from(std::slice::from_raw_parts(buffer, other.data.len() * 4)) };
45
46 BoxImage::new(info, buffer).into()
47 }
48}
49
50fn draw_target_info(draw_target: &raqote::DrawTarget) -> Result<ImageInfo, ImageDataError> {
51 if draw_target.width() < 0 || draw_target.height() < 0 {
52 Err(format!("DrawTarget has negative size: [{}, {}]", draw_target.width(), draw_target.height()).into())
53 } else {
54 Ok(ImageInfo::bgra8_premultiplied(
55 draw_target.width() as u32,
56 draw_target.height() as u32,
57 ))
58 }
59}
60
61fn image_info(&image: &raqote::Image) -> Result<ImageInfo, ImageDataError> {
62 if image.width < 0 || image.height < 0 {
63 Err(format!("DrawTarget has negative size: [{}, {}]", image.width, image.height).into())
64 } else {
65 Ok(ImageInfo::bgra8_premultiplied(image.width as u32, image.height as u32))
66 }
67}