-
Notifications
You must be signed in to change notification settings - Fork 0
/
dib.rs
170 lines (161 loc) · 5.3 KB
/
dib.rs
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
use super::Clipboard;
use image::bmp::BmpDecoder;
use image::png::PNGEncoder;
use image::{DynamicImage, ImageDecoder, ImageFormat};
use std::io::Cursor;
use std::io::Read;
use winapi::um::wingdi::*;
use winapi::um::winuser::*;
#[derive(Debug, Copy, Clone)]
pub(crate) enum BitsPPx {
Binary,
HalfByte,
Byte,
Short,
Triplet,
Word,
Other(u16),
}
impl BitsPPx {
pub fn from(n: u16) -> Self {
match n {
1 => Self::Binary,
4 => Self::HalfByte,
8 => Self::Byte,
16 => Self::Short,
24 => Self::Triplet,
32 => Self::Word,
_ => Self::Other(n),
}
}
}
#[derive(Debug)]
pub(crate) enum DIBFmt {
PNG,
JPG,
RGB(Box<[RGBQUAD]>, BitsPPx),
Masked([RGBQUAD; 3], BitsPPx),
}
impl DIBFmt {
pub unsafe fn from(info: &BITMAPINFO) -> Option<Self> {
let bitc = info.bmiHeader.biBitCount;
match info.bmiHeader.biCompression {
BI_PNG => Some(Self::PNG),
BI_JPEG => Some(Self::JPG),
BI_RGB if bitc == 1 || bitc == 4 || bitc == 8 => Some(Self::RGB(
Box::<_>::from({
let ptr = &info.bmiColors as *const RGBQUAD;
let len = info.bmiHeader.biClrUsed;
std::slice::from_raw_parts(ptr, len as usize)
}),
BitsPPx::from(info.bmiHeader.biBitCount),
)),
BI_BITFIELDS if bitc == 16 || bitc == 32 => Some(Self::Masked(
*<*const _>::cast(&info.bmiColors),
BitsPPx::from(info.bmiHeader.biBitCount),
)),
_ => None,
}
}
}
pub(crate) struct DIB {
pub head: BITMAPFILEHEADER,
pub info: BITMAPINFOHEADER,
pub clrs: Option<Box<[RGBQUAD]>>,
pub data: Box<[u8]>,
}
impl DIB {
fn file_header(info: &BITMAPINFOHEADER, colorc: usize) -> BITMAPFILEHEADER {
let offset = {
std::mem::size_of::<BITMAPFILEHEADER>()
+ std::mem::size_of::<BITMAPINFOHEADER>()
+ colorc * std::mem::size_of::<RGBQUAD>()
} as u32;
BITMAPFILEHEADER {
bfType: 0x4D42, // 'BM'
bfReserved1: 0,
bfReserved2: 0,
bfSize: info.biSizeImage + offset,
bfOffBits: offset,
}
}
pub unsafe fn unclip(clipboard: &mut Clipboard) -> Option<std::io::Result<Self>> {
clipboard.get(CF_DIB).map(|gptr| {
let lock = gptr?;
let info = lock.as_ref::<BITMAPINFO>();
let fmt = DIBFmt::from(&info);
let clrs = fmt
.and_then(|fmt| match fmt {
DIBFmt::Masked(clrs, _) => Some(Box::<_>::from(clrs.as_ref())),
DIBFmt::RGB(clrs, _) => Some(clrs),
_ => None,
})
.and_then(|clrs| {
if clrs.len() == 0 {
return None;
}
return Some(clrs);
});
let colorc = if let Some(ref clrs) = clrs {
clrs.len()
} else {
0
};
let head = Self::file_header(&info.bmiHeader, colorc);
let data = Box::<_>::from({
let bmi_end = (&info.bmiHeader as *const BITMAPINFOHEADER).offset(1) as *const u8;
let ptr = bmi_end.offset((colorc * std::mem::size_of::<RGBQUAD>()) as isize);
let len = info.bmiHeader.biSizeImage as usize;
std::slice::from_raw_parts(ptr, len)
});
let info = info.bmiHeader;
Ok(Self {
head,
info,
clrs,
data,
})
})
}
pub unsafe fn encode_bmp<O: std::io::Write>(&self, ostrm: &mut O) -> std::io::Result<()> {
ostrm.write({
let ptr = <*const _>::cast(&self.head);
let len = std::mem::size_of::<BITMAPFILEHEADER>();
std::slice::from_raw_parts(ptr, len)
})?;
ostrm.write({
let ptr = <*const _>::cast(&self.info);
let len = std::mem::size_of::<BITMAPINFOHEADER>();
std::slice::from_raw_parts(ptr, len)
})?;
if let Some(ref clrs) = self.clrs {
ostrm.write({
let ptr = clrs.as_ptr() as *const _;
let len = std::mem::size_of::<RGBQUAD>() * clrs.len();
std::slice::from_raw_parts(ptr, len)
})?;
}
ostrm.write(&self.data)?;
Ok(())
}
pub unsafe fn encode_png<O: std::io::Write>(
&self,
mut ostrm: O,
) -> Result<(), image::error::ImageError> {
let img = {
let mut scratchpad = Vec::with_capacity({
std::mem::size_of::<BITMAPFILEHEADER>()
+ std::mem::size_of::<BITMAPINFOHEADER>()
+ self
.clrs
.as_ref()
.map_or(0, |clrs| std::mem::size_of::<RGBQUAD>() * clrs.len())
+ self.info.biSizeImage as usize
});
self.encode_bmp(&mut scratchpad)?;
image::load_from_memory_with_format(&scratchpad, ImageFormat::Bmp)?
};
img.write_to(&mut ostrm, ImageFormat::Png)?;
Ok(())
}
}