| 1 | #[cfg(feature = "dbus")] |
| 2 | use dbus::arg::messageitem::{MessageItem, MessageItemArray}; |
| 3 | pub use image::DynamicImage; |
| 4 | |
| 5 | use std::cmp::Ordering; |
| 6 | use std::convert::TryFrom; |
| 7 | use std::error::Error; |
| 8 | use std::fmt; |
| 9 | use std::path::Path; |
| 10 | |
| 11 | use crate::miniver::Version; |
| 12 | |
| 13 | mod constants { |
| 14 | pub const IMAGE_DATA: &str = "image-data"; |
| 15 | pub const IMAGE_DATA_1_1: &str = "image_data"; |
| 16 | pub const IMAGE_DATA_1_0: &str = "icon_data"; |
| 17 | } |
| 18 | |
| 19 | /// Image data for inline notifications. Send via [`Notification::image_data()`](crate::Notification::image_data). |
| 20 | #[derive(PartialEq, Eq, Debug, Clone, Hash)] |
| 21 | pub struct Image { |
| 22 | width: i32, |
| 23 | height: i32, |
| 24 | rowstride: i32, |
| 25 | alpha: bool, |
| 26 | bits_per_sample: i32, |
| 27 | channels: i32, |
| 28 | data: Vec<u8>, |
| 29 | } |
| 30 | |
| 31 | impl Image { |
| 32 | fn from_raw_data( |
| 33 | width: i32, |
| 34 | height: i32, |
| 35 | data: Vec<u8>, |
| 36 | channels: i32, |
| 37 | bits_per_sample: i32, |
| 38 | alpha: bool, |
| 39 | ) -> Result<Self, ImageError> { |
| 40 | const MAX_SIZE: i32 = 0x0fff_ffff; |
| 41 | if width > MAX_SIZE || height > MAX_SIZE { |
| 42 | return Err(ImageError::TooBig); |
| 43 | } |
| 44 | |
| 45 | if data.len() != (width * height * channels) as usize { |
| 46 | Err(ImageError::WrongDataSize) |
| 47 | } else { |
| 48 | Ok(Self { |
| 49 | width, |
| 50 | height, |
| 51 | bits_per_sample, |
| 52 | channels, |
| 53 | data, |
| 54 | rowstride: width * channels, |
| 55 | alpha, |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Creates an image from a raw vector of bytes |
| 61 | pub fn from_rgb(width: i32, height: i32, data: Vec<u8>) -> Result<Self, ImageError> { |
| 62 | let channels = 3i32; |
| 63 | let bits_per_sample = 8; |
| 64 | Self::from_raw_data(width, height, data, channels, bits_per_sample, false) |
| 65 | } |
| 66 | |
| 67 | /// Creates an image from a raw vector of bytes with alpha |
| 68 | pub fn from_rgba(width: i32, height: i32, data: Vec<u8>) -> Result<Self, ImageError> { |
| 69 | let channels = 4i32; |
| 70 | let bits_per_sample = 8; |
| 71 | Self::from_raw_data(width, height, data, channels, bits_per_sample, true) |
| 72 | } |
| 73 | |
| 74 | /// Attempts to open the given path as an image. |
| 75 | pub fn open<T: AsRef<Path> + Sized>(path: T) -> Result<Self, ImageError> { |
| 76 | let dyn_img = image::open(&path).map_err(ImageError::CantOpen)?; |
| 77 | Image::try_from(dyn_img) |
| 78 | } |
| 79 | |
| 80 | #[cfg(all(feature = "images_no_default_features", feature = "zbus"))] |
| 81 | pub(crate) fn to_tuple(&self) -> (i32, i32, i32, bool, i32, i32, Vec<u8>) { |
| 82 | ( |
| 83 | self.width, |
| 84 | self.height, |
| 85 | self.rowstride, |
| 86 | self.alpha, |
| 87 | self.bits_per_sample, |
| 88 | self.channels, |
| 89 | self.data.clone(), |
| 90 | ) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl TryFrom<DynamicImage> for Image { |
| 95 | type Error = ImageError; |
| 96 | |
| 97 | fn try_from(dyn_img: DynamicImage) -> Result<Self, Self::Error> { |
| 98 | match dyn_img { |
| 99 | DynamicImage::ImageRgb8(img) => Self::try_from(img), |
| 100 | DynamicImage::ImageRgba8(img) => Self::try_from(img), |
| 101 | _ => Err(ImageError::CantConvert), |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl TryFrom<image::RgbImage> for Image { |
| 107 | type Error = ImageError; |
| 108 | |
| 109 | fn try_from(img: image::RgbImage) -> Result<Self, Self::Error> { |
| 110 | let (width, height) = img.dimensions(); |
| 111 | let image_data = img.into_raw(); |
| 112 | Image::from_rgb(width as i32, height as i32, image_data) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl TryFrom<image::RgbaImage> for Image { |
| 117 | type Error = ImageError; |
| 118 | |
| 119 | fn try_from(img: image::RgbaImage) -> Result<Self, Self::Error> { |
| 120 | let (width, height) = img.dimensions(); |
| 121 | let image_data = img.into_raw(); |
| 122 | Image::from_rgba(width as i32, height as i32, image_data) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /// Errors that can occur when creating an Image |
| 127 | #[derive(Debug)] |
| 128 | pub enum ImageError { |
| 129 | /// The given image is too big. DBus only has 32 bits for width / height |
| 130 | TooBig, |
| 131 | /// The given bytes don't match the width, height and channel count |
| 132 | WrongDataSize, |
| 133 | /// Can't open given path |
| 134 | CantOpen(image::ImageError), |
| 135 | /// Can't convert from given input |
| 136 | CantConvert, |
| 137 | } |
| 138 | |
| 139 | impl Error for ImageError { |
| 140 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 141 | use ImageError::*; |
| 142 | match self { |
| 143 | TooBig | WrongDataSize | CantConvert => None, |
| 144 | CantOpen(e) => Some(e), |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | impl fmt::Display for ImageError { |
| 150 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 151 | use ImageError::*; |
| 152 | match self { |
| 153 | TooBig => writeln!( |
| 154 | f, |
| 155 | "The given image is too big. DBus only has 32 bits for width / height" |
| 156 | ), |
| 157 | WrongDataSize => writeln!( |
| 158 | f, |
| 159 | "The given bytes don't match the width, height and channel count" |
| 160 | ), |
| 161 | CantOpen(e) => writeln!(f, "Can't open given path {}", e), |
| 162 | CantConvert => writeln!(f, "Can't convert from given input"), |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /// matching image data key for each spec version |
| 168 | #[cfg(feature = "dbus")] |
| 169 | pub(crate) fn image_spec(version: Version) -> String { |
| 170 | match version.cmp(&Version::new(1, 1)) { |
| 171 | Ordering::Less => constants::IMAGE_DATA_1_0.to_owned(), |
| 172 | Ordering::Equal => constants::IMAGE_DATA_1_1.to_owned(), |
| 173 | Ordering::Greater => constants::IMAGE_DATA.to_owned(), |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// matching image data key for each spec version |
| 178 | #[cfg(feature = "zbus")] |
| 179 | pub(crate) fn image_spec_str(version: Version) -> &'static str { |
| 180 | match version.cmp(&Version::new(1, 1)) { |
| 181 | Ordering::Less => constants::IMAGE_DATA_1_0, |
| 182 | Ordering::Equal => constants::IMAGE_DATA_1_1, |
| 183 | Ordering::Greater => constants::IMAGE_DATA, |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #[cfg(feature = "dbus")] |
| 188 | pub struct ImageMessage(Image); |
| 189 | |
| 190 | #[cfg(feature = "dbus")] |
| 191 | impl From<Image> for ImageMessage { |
| 192 | fn from(hint: Image) -> Self { |
| 193 | ImageMessage(hint) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl From<image::ImageError> for ImageError { |
| 198 | fn from(image_error: image::ImageError) -> Self { |
| 199 | ImageError::CantOpen(image_error) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | #[cfg(feature = "dbus")] |
| 204 | impl std::ops::Deref for ImageMessage { |
| 205 | type Target = Image; |
| 206 | |
| 207 | fn deref(&self) -> &Self::Target { |
| 208 | &self.0 |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #[cfg(feature = "dbus")] |
| 213 | impl From<ImageMessage> for MessageItem { |
| 214 | fn from(img_msg: ImageMessage) -> Self { |
| 215 | let img = img_msg.0; |
| 216 | |
| 217 | let bytes = img.data.into_iter().map(MessageItem::Byte).collect(); |
| 218 | |
| 219 | MessageItem::Struct(vec![ |
| 220 | MessageItem::Int32(img.width), |
| 221 | MessageItem::Int32(img.height), |
| 222 | MessageItem::Int32(img.rowstride), |
| 223 | MessageItem::Bool(img.alpha), |
| 224 | MessageItem::Int32(img.bits_per_sample), |
| 225 | MessageItem::Int32(img.channels), |
| 226 | MessageItem::Array(MessageItemArray::new(bytes, "ay".into()).unwrap()), |
| 227 | ]) |
| 228 | } |
| 229 | } |