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
#![feature(specialization)]
#![warn(missing_docs)]
extern crate bytes;
extern crate lodepng;
extern crate framing;
use bytes::Bytes;
use framing::video::{self, VideoFrame, Rgba, ChunkyFrame};
use lodepng::ffi::CVec;
use std::{mem, ptr, slice};
use std::path::Path;
pub struct Png<T> {
width: usize,
height: usize,
buffer: T
}
impl Png<Native> {
pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
match lodepng::decode32(bytes) {
Ok(bmp) => {
assert_eq!(bmp.buffer.len(), bmp.width * bmp.height);
Ok(Png {
width: bmp.width,
height: bmp.height,
buffer: Native::new(bmp.buffer)
})
},
Err(_) => Err(Error)
}
}
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
match lodepng::decode32_file(path) {
Ok(bmp) => {
assert_eq!(bmp.buffer.len(), bmp.width * bmp.height);
Ok(Png {
width: bmp.width,
height: bmp.height,
buffer: Native::new(bmp.buffer)
})
},
Err(_) => Err(Error)
}
}
}
impl<T> Png<T> {
pub fn buffer(&self) -> &T {
&self.buffer
}
pub fn into_buffer(self) -> T {
self.buffer
}
}
impl<T> Png<T> where T: AsRef<[u8]> {
pub fn from_bytes(
width: usize,
height: usize,
buffer: T
) -> Png<T> {
assert_eq!(width * height * 4, buffer.as_ref().len());
Png { width, height, buffer }
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let result = lodepng::encode32_file(
path,
self.buffer.as_ref(),
self.width,
self.height
);
match result {
Ok(_) => Ok(()),
Err(_) => Err(Error)
}
}
pub fn encode(&self) -> Result<CVec<u8>, Error> {
let result = lodepng::encode32(
self.buffer.as_ref(),
self.width,
self.height
);
match result {
Ok(vec) => Ok(vec),
Err(_) => Err(Error)
}
}
}
impl Png<Bytes> {
pub fn new<T>(frame: T) -> Self
where T: VideoFrame + Sync, T::Pixel: Into<Rgba> {
ChunkyFrame::new(video::map(|x| x.into(), frame)).into()
}
}
impl From<ChunkyFrame<Rgba>> for Png<Bytes> {
fn from(frame: ChunkyFrame<Rgba>) -> Self {
Png {
width: frame.width(),
height: frame.height(),
buffer: frame.bytes()
}
}
}
impl<T> AsRef<[u8]> for Png<T> where T: AsRef<[u8]> {
fn as_ref(&self) -> &[u8] {
self.buffer.as_ref()
}
}
impl<T> AsMut<[u8]> for Png<T> where T: AsMut<[u8]> {
fn as_mut(&mut self) -> &mut [u8] {
self.buffer.as_mut()
}
}
impl<T> VideoFrame for Png<T> where T: AsRef<[u8]> {
type Pixel = Rgba;
fn width(&self) -> usize { self.width }
fn height(&self) -> usize { self.height }
unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
let mut bytes: [u8; 4] = mem::uninitialized();
let offset = 4 * (y * self.width + x) as isize;
ptr::copy_nonoverlapping(
self.buffer.as_ref().as_ptr().offset(offset),
bytes.as_mut_ptr(),
4
);
bytes.into()
}
}
pub struct Native(CVec<lodepng::RGBA<u8>>, usize);
impl Native {
fn new(buffer: CVec<lodepng::RGBA<u8>>) -> Self {
let length = buffer.len() * mem::size_of::<lodepng::RGBA<u8>>();
Native(buffer, length)
}
}
impl AsRef<[u8]> for Native {
fn as_ref(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(
self.0.as_ref().as_ptr() as *const _ as *const _,
self.1
)
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct Error;
#[test]
fn lossless() {
use framing::video::Function;
let ugly_gradient = Function::new(1920, 1080, |x, y| {
Rgba((x % 256) as u8, (y % 256) as u8, 0, 255)
});
let png = Png::new(ugly_gradient);
let recoded = Png::decode(png.encode().unwrap().as_ref()).unwrap();
let before = png.buffer().as_ref();
let after = recoded.buffer().as_ref();
assert_eq!(before, after);
}