-
Notifications
You must be signed in to change notification settings - Fork 58
/
pixelformat.rs
344 lines (316 loc) · 11.8 KB
/
pixelformat.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2015 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Pixel format utility routines.
use num::iter::range;
use std::cmp;
use std::io::{Write, BufWriter};
/// 8-bit Y plane followed by 8-bit 2x2-subsampled U and V planes.
#[derive(Copy, Clone, Debug)]
pub struct I420;
/// 8-bit Y plane followed by an interleaved U/V plane containing 2x2 subsampled color difference
/// samples.
#[derive(Copy, Clone, Debug)]
pub struct NV12;
/// 8-bit indexes into a 24-bit color palette.
#[derive(Copy, Clone, Debug)]
pub struct Palette<'a> {
pub palette: &'a [RgbColor],
}
impl<'a> Palette<'a> {
pub fn empty() -> Palette<'static> {
Palette {
palette: &[],
}
}
}
/// 24-bit RGB.
#[derive(Copy, Clone, Debug)]
pub struct Rgb24;
#[derive(Copy, Clone)]
pub struct YuvColor {
pub y: f64,
pub u: f64,
pub v: f64,
}
#[derive(Copy, Clone, Debug)]
pub struct RgbColor {
pub r: u8,
pub g: u8,
pub b: u8,
}
/// Converts between pixel formats on the CPU.
pub trait ConvertPixelFormat<To> {
fn convert(&self,
to: &To,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()>;
}
impl ConvertPixelFormat<I420> for I420 {
fn convert(&self,
_: &I420,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
_: usize,
height: usize)
-> Result<(),()> {
for plane in range(0, 3) {
let (y_input_pixels, y_input_stride) = (input_pixels[plane], input_strides[plane]);
let y_output_pixels = &mut *output_pixels[plane];
let y_output_stride = output_strides[plane];
let minimum_stride = cmp::min(y_input_stride, y_output_stride);
let effective_height = if plane == 0 {
height
} else {
height / 2
};
let (mut input_index, mut output_index) = (0, 0);
for _ in range(0, effective_height) {
let input_row = &y_input_pixels[input_index..input_index + minimum_stride];
let mut output_row =
&mut y_output_pixels[output_index..output_index + minimum_stride];
output_row.copy_from_slice(input_row);
input_index += y_input_stride;
output_index += y_output_stride;
}
}
Ok(())
}
}
impl ConvertPixelFormat<I420> for NV12 {
fn convert(&self,
_: &I420,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()> {
// Copy over the Y plane.
let (y_input_pixels, y_input_stride) = (input_pixels[0], input_strides[0]);
let (mut input_index, mut output_index) = (0, 0);
for _ in range(0, height) {
let input_row = &y_input_pixels[input_index..input_index + width];
let mut output_row = &mut output_pixels[0][output_index..output_index + width];
output_row.copy_from_slice(input_row);
input_index += y_input_stride;
output_index += output_strides[0];
}
// Interleave the U and V planes.
let (y_input_pixels, y_input_stride) = (input_pixels[1], input_strides[1]);
let (y_output_u_pixels, y_output_v_pixels) = output_pixels.split_at_mut(2);
let y_output_u_pixels = &mut y_output_u_pixels[1];
let y_output_v_pixels = &mut y_output_v_pixels[0];
let y_output_u_stride = output_strides[1];
let y_output_v_stride = output_strides[2];
let effective_height = height / 2;
let (mut input_index, mut output_u_index, mut output_v_index) = (0, 0, 0);
for _ in range(0, effective_height) {
let input_row = &y_input_pixels[input_index..input_index + y_input_stride];
let output_u_row =
&mut y_output_u_pixels[output_u_index..output_u_index + width / 2];
let output_v_row =
&mut y_output_v_pixels[output_v_index..output_v_index + width / 2];
let mut u_writer = BufWriter::new(output_u_row);
let mut v_writer = BufWriter::new(output_v_row);
for x in range(0, width / 2) {
drop(u_writer.write_all(&[input_row[x * 2]]));
drop(v_writer.write_all(&[input_row[x * 2 + 1]]));
}
input_index += y_input_stride;
output_u_index += y_output_u_stride;
output_v_index += y_output_v_stride;
}
Ok(())
}
}
impl ConvertPixelFormat<Rgb24> for I420 {
fn convert(&self,
_: &Rgb24,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()> {
// FIXME(pcwalton): This does not convert the chroma yet. Dorothy has not yet left Kansas.
let (y_input_pixels, y_input_stride) = (input_pixels[0], input_strides[0]);
let (mut input_index, mut output_index) = (0, 0);
for _ in range(0, height) {
let input_row = &y_input_pixels[input_index..input_index + width * 3];
let output_row =
&mut output_pixels[0][output_index..output_index + output_strides[0]];
let mut writer = BufWriter::new(output_row);
for x in range(0, width) {
drop(writer.write_all(&[input_row[x], input_row[x], input_row[x]]));
}
input_index += y_input_stride;
output_index += output_strides[0];
}
Ok(())
}
}
impl<'a> ConvertPixelFormat<Rgb24> for Palette<'a> {
fn convert(&self,
_: &Rgb24,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()> {
let (y_input_pixels, y_input_stride) = (input_pixels[0], input_strides[0]);
let (mut input_index, mut output_index) = (0, 0);
for _ in range(0, height) {
let input_row = &y_input_pixels[input_index..input_index + width];
let output_row = &mut output_pixels[0][output_index..output_index + width * 3];
let mut writer = BufWriter::new(output_row);
for x in range(0, width) {
let color = self.palette[input_row[x] as usize];
drop(writer.write_all(&[color.r, color.g, color.b]));
}
input_index += y_input_stride;
output_index += output_strides[0];
}
Ok(())
}
}
impl ConvertPixelFormat<Rgb24> for Rgb24 {
fn convert(&self,
_: &Rgb24,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()> {
let (y_input_pixels, y_input_stride) = (input_pixels[0], input_strides[0]);
let (mut input_index, mut output_index) = (0, 0);
for _ in range(0, height) {
let input_row = &y_input_pixels[input_index..input_index + width * 3];
let mut output_row = &mut output_pixels[0][output_index..output_index + width * 3];
output_row.copy_from_slice(input_row);
input_index += y_input_stride;
output_index += output_strides[0];
}
Ok(())
}
}
/// Converts between color formats on the CPU.
pub trait ConvertColorFormat<To> {
fn convert(&self) -> To;
}
impl ConvertColorFormat<RgbColor> for YuvColor {
fn convert(&self) -> RgbColor {
const W_R: f64 = 0.299;
const W_G: f64 = 1.0 - W_R - W_B;
const W_B: f64 = 0.114;
const U_MAX: f64 = 1.0;
const V_MAX: f64 = 1.0;
let r = self.y + self.v * (1.0 - W_R) / V_MAX;
let g = self.y - self.u * (W_B * (1.0 - W_B)) / (U_MAX * W_G) -
self.v * (W_R * (1.0 - W_R)) / (V_MAX * W_G);
let b = self.y + self.u * (1.0 - W_B) / U_MAX;
RgbColor {
r: (r / 255.0) as u8,
g: (g / 255.0) as u8,
b: (b / 255.0) as u8,
}
}
}
/// Generic pixel format conversion with the pixel formats determined at runtime.
///
/// We follow the same nomenclature as the document here: http://www.fourcc.org/yuv.php
#[derive(Copy, Clone, Debug)]
pub enum PixelFormat<'a> {
I420,
NV12,
Indexed(Palette<'a>),
Rgb24,
}
impl<'a> ConvertPixelFormat<PixelFormat<'a>> for PixelFormat<'a> {
fn convert(&self,
to: &PixelFormat<'a>,
output_pixels: &mut [&mut [u8]],
output_strides: &[usize],
input_pixels: &[&[u8]],
input_strides: &[usize],
width: usize,
height: usize)
-> Result<(),()> {
match (*self, *to) {
(PixelFormat::I420, PixelFormat::I420) => {
I420.convert(&I420,
output_pixels,
output_strides,
input_pixels,
input_strides,
width,
height)
}
(PixelFormat::NV12, PixelFormat::I420) => {
NV12.convert(&I420,
output_pixels,
output_strides,
input_pixels,
input_strides,
width,
height)
}
(PixelFormat::I420, PixelFormat::Rgb24) => {
I420.convert(&Rgb24,
output_pixels,
output_strides,
input_pixels,
input_strides,
width,
height)
}
(PixelFormat::Indexed(palette), PixelFormat::Rgb24) => {
palette.convert(&Rgb24,
output_pixels,
output_strides,
input_pixels,
input_strides,
width,
height)
}
(PixelFormat::Rgb24, PixelFormat::Rgb24) => {
Rgb24.convert(&Rgb24,
output_pixels,
output_strides,
input_pixels,
input_strides,
width,
height)
}
(_, _) => Err(()),
}
}
}
impl<'a> PixelFormat<'a> {
/// Returns the number of planes in this pixel format.
pub fn planes(&self) -> usize {
match *self {
PixelFormat::I420 => 3,
PixelFormat::NV12 => 2,
PixelFormat::Indexed(_) | PixelFormat::Rgb24 => 1,
}
}
}