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
use dbus::arg::messageitem::{MessageItem, MessageItemArray};
use image::GenericImageView as _;
use thiserror::Error;
use displaydoc::Display;
use std::cmp::Ordering;
use std::convert::TryFrom;
use std::path::Path;
use super::constants;
use crate::miniver::Version;
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
pub struct NotificationImage {
width: i32,
height: i32,
rowstride: i32,
alpha: bool,
bits_per_sample: i32,
channels: i32,
data: Vec<u8>,
}
impl NotificationImage {
pub fn from_rgb(width: i32, height: i32, data: Vec<u8>) -> Result<Self, Error> {
const MAX_SIZE: i32 = 0x0fff_ffff;
if width > MAX_SIZE || height > MAX_SIZE {
return Err(Error::TooBig);
}
let channels = 3i32;
let bits_per_sample = 8;
if data.len() != (width * height * channels) as usize {
Err(Error::WrongDataSize)
} else {
Ok(Self {
width,
height,
bits_per_sample,
channels,
data,
rowstride: width * channels,
alpha: false,
})
}
}
pub fn open<T: AsRef<Path> + Sized>(path: T) -> Result<Self, Error> {
let dyn_img = image::open(&path).map_err(Error::CantOpen)?;
NotificationImage::try_from(dyn_img)
}
}
impl TryFrom<image::DynamicImage> for NotificationImage {
type Error = Error;
fn try_from (dyn_img: image::DynamicImage) -> Result<Self, Self::Error> {
if let Some(image_data) = dyn_img.as_rgb8() {
let (width, height) = dyn_img.dimensions();
let image_data = image_data.clone().into_raw();
Ok( NotificationImage::from_rgb(
width as i32,
height as i32,
image_data)?)
} else {
Err(Error::CantConvert)
}
}
}
#[derive(Debug, Display, Error)]
pub enum Error {
TooBig,
WrongDataSize,
CantOpen(#[from] image::ImageError),
CantConvert,
}
pub fn image_spec(version: Version) -> String {
match version.cmp(&Version::new(1, 1)) {
Ordering::Less => constants::IMAGE_DATA_1_0.to_owned(),
Ordering::Equal => constants::IMAGE_DATA_1_1.to_owned(),
Ordering::Greater => constants::IMAGE_DATA.to_owned(),
}
}
pub struct NotificationImageMessage(NotificationImage);
impl From<NotificationImage> for NotificationImageMessage {
fn from(hint: NotificationImage) -> Self {
NotificationImageMessage(hint)
}
}
impl std::ops::Deref for NotificationImageMessage {
type Target = NotificationImage;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<NotificationImageMessage> for MessageItem {
fn from(img_msg: NotificationImageMessage) -> Self {
let img = img_msg.0;
let bytes = img.data.into_iter().map(MessageItem::Byte).collect();
MessageItem::Struct(vec![
MessageItem::Int32(img.width),
MessageItem::Int32(img.height),
MessageItem::Int32(img.rowstride),
MessageItem::Bool(img.alpha),
MessageItem::Int32(img.bits_per_sample),
MessageItem::Int32(img.channels),
MessageItem::Array(MessageItemArray::new(bytes, "ay".into()).unwrap()),
])
}
}