-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathmod.rs
245 lines (212 loc) · 6.17 KB
/
mod.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
extern crate rayon;
use self::rayon::prelude::*;
pub mod gofloat;
pub mod demosaic;
pub mod level;
pub mod colorspaces;
pub mod curves;
pub mod gamma;
pub mod transform;
use decoders::RawImage;
extern crate time;
extern crate toml;
extern crate serde;
extern crate serde_yaml;
use self::serde::Serialize;
use std::hash::{Hash, Hasher};
extern crate metrohash;
use self::metrohash::MetroHash;
use std::fmt::Debug;
extern crate multicache;
use self::multicache::MultiCache;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq)]
pub struct OpBuffer {
pub width: usize,
pub height: usize,
pub colors: usize,
pub data: Vec<f32>,
}
impl OpBuffer {
pub fn new(width: usize, height: usize, colors: usize) -> OpBuffer {
OpBuffer {
width: width,
height: height,
colors: colors,
data: vec![0.0; width*height*(colors as usize)],
}
}
pub fn mutate_lines<F>(&mut self, closure: &F)
where F : Fn(&mut [f32], usize)+Sync {
self.data.par_chunks_mut(self.width*self.colors).enumerate().for_each(|(row, line)| {
closure(line, row);
});
}
pub fn process_into_new<F>(&self, colors: usize, closure: &F) -> OpBuffer
where F : Fn(&mut [f32], &[f32])+Sync {
let mut out = OpBuffer::new(self.width, self.height, colors);
out.data.par_chunks_mut(out.width*out.colors).enumerate().for_each(|(row, line)| {
closure(line, &self.data[self.width*self.colors*row..]);
});
out
}
/// Helper function to allow human readable creation of `OpBuffer` instances
pub fn from_rgb_str_vec(data: Vec<&str>) -> OpBuffer {
let width = data.first().expect("Invalid data for rgb helper function").len();
let height = data.len();
let colors = 3;
let mut pixel_data: Vec<f32> = Vec::with_capacity(width * height * colors);
for row in data {
for col in row.chars() {
let (r, g, b) = match col {
'R' => (1.0, 0.0, 0.0),
'G' => (0.0, 1.0, 0.0),
'B' => (0.0, 0.0, 1.0),
'O' => (1.0, 1.0, 1.0),
' ' => (0.0, 0.0, 0.0),
c @ _ => panic!(format!(
"Invalid color '{}' sent to rgb expected any of 'RGBO '", c)),
};
pixel_data.push(r);
pixel_data.push(g);
pixel_data.push(b);
}
}
OpBuffer {
width: width,
height: height,
colors: colors,
data: pixel_data,
}
}
}
fn do_timing<O, F: FnMut() -> O>(name: &str, mut closure: F) -> O {
let from_time = time::precise_time_ns();
let ret = closure();
let to_time = time::precise_time_ns();
println!("{} ms for '{}'", (to_time - from_time)/1000000, name);
ret
}
pub trait ImageOp<'a>: Debug+Hash+Serialize {
fn name(&self) -> &str;
fn run(&self, pipeline: &mut PipelineGlobals, inid: u64, outid: u64);
fn to_settings(&self) -> String {
serde_yaml::to_string(self).unwrap()
}
}
#[derive(Debug)]
pub struct PipelineGlobals<'a> {
cache: MultiCache<u64, OpBuffer>,
maxwidth: usize,
maxheight: usize,
linear: bool,
image: &'a RawImage,
}
#[derive(Debug)]
pub struct PipelineOps {
gofloat: gofloat::OpGoFloat,
demosaic: demosaic::OpDemosaic,
level: level::OpLevel,
tolab: colorspaces::OpToLab,
basecurve: curves::OpBaseCurve,
fromlab: colorspaces::OpFromLab,
gamma: gamma::OpGamma,
transform: transform::OpTransform,
}
macro_rules! for_vals {
([$($val:expr),*] |$x:pat, $i:ident| $body:expr) => {
let mut pos = 0;
$({
let $x = $val;
pos += 1;
let $i = pos-1;
$body
})*
}
}
macro_rules! all_ops {
($ops:expr, |$x:pat, $i:ident| $body:expr) => {
for_vals!([
$ops.gofloat,
$ops.demosaic,
$ops.level,
$ops.tolab,
$ops.basecurve,
$ops.fromlab,
$ops.gamma,
$ops.transform
] |$x, $i| {
$body
});
}
}
#[derive(Debug)]
pub struct Pipeline<'a> {
globals: PipelineGlobals<'a>,
ops: PipelineOps,
}
impl<'a> Pipeline<'a> {
pub fn new(img: &RawImage, maxwidth: usize, maxheight: usize, linear: bool) -> Pipeline {
// Check if the image's orientation results in a rotation that
// swaps the maximum width with the maximum height
let (transpose, ..) = img.orientation.to_flips();
let (maxwidth, maxheight) = if transpose {
(maxheight, maxwidth)
} else {
(maxwidth, maxheight)
};
Pipeline {
globals: PipelineGlobals {
cache: MultiCache::new(1),
maxwidth,
maxheight,
linear,
image: img,
},
ops: PipelineOps {
gofloat: gofloat::OpGoFloat::new(img),
demosaic: demosaic::OpDemosaic::new(img),
level: level::OpLevel::new(img),
tolab: colorspaces::OpToLab::new(img),
basecurve: curves::OpBaseCurve::new(img),
fromlab: colorspaces::OpFromLab::new(img),
gamma: gamma::OpGamma::new(img),
transform: transform::OpTransform::new(img),
},
}
}
pub fn run(&mut self) -> Arc<OpBuffer> {
// Generate all the hashes for the operations
let mut hasher = MetroHash::new();
let mut ophashes = Vec::new();
all_ops!(self.ops, |ref op, _i| {
// Hash the name first as a zero sized struct doesn't actually do any hashing
op.name().hash(&mut hasher);
op.hash(&mut hasher);
ophashes.push(hasher.finish());
});
// Do the operations, starting with a dummy buffer id as gofloat doesn't use it
let mut bufin: u64 = 0;
all_ops!(self.ops, |ref op, i| {
let globals = &mut self.globals;
let hash = ophashes[i];
do_timing(op.name(), ||op.run(globals, bufin, hash));
bufin = hash;
});
self.globals.cache.get(bufin).unwrap()
}
}
fn simple_decode_full(img: &RawImage, maxwidth: usize, maxheight: usize, linear: bool) -> OpBuffer {
let buf = {
let mut pipeline = Pipeline::new(img, maxwidth, maxheight, linear);
pipeline.run()
};
// Since we've kept the pipeline to ourselves unwraping always works
Arc::try_unwrap(buf).unwrap()
}
pub fn simple_decode(img: &RawImage, maxwidth: usize, maxheight: usize) -> OpBuffer {
simple_decode_full(img, maxwidth, maxheight, false)
}
pub fn simple_decode_linear(img: &RawImage, maxwidth: usize, maxheight: usize) -> OpBuffer {
simple_decode_full(img, maxwidth, maxheight, true)
}