forked from Twinklebear/oidn-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenoise_exr.rs
164 lines (142 loc) · 4.37 KB
/
denoise_exr.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
extern crate docopt;
extern crate exr;
extern crate image;
extern crate oidn;
extern crate rayon;
extern crate serde;
use docopt::Docopt;
use exr::prelude::rgba_image as rgb_exr;
use rayon::prelude::*;
use serde::Deserialize;
use std::f32;
/// An example application that shows opening an HDR EXR image with optional
/// additional normal and albedo EXR images and denoising it with OIDN.
/// The denoised image is then tonemaped and saved out as a JPG
const USAGE: &str = "
denoise_exr
Usage:
denoise_exr -c <color.exr> -o <output.jpg> -e <exposure> [-a <albedo.exr>]
denoise_exr -c <color.exr> -o <output.jpg> -e <exposure> [(-a <albedo.exr> -n <normal.exr>)]
Options:
-c <color.exr>, --color <color.exr> Specify the input color image
-o <out.jpg> Specify the output file for the denoised and tonemapped JPG
-e <exposure>, --exposure <exposure> Specify the exposure to apply to the image
-a <albedo.exr>, --albedo <albedo.exr> Specify the albedo image
-n <normal.exr>, --normal <normal.exr> Specify the normal image (requires albedo)
";
#[derive(Debug, Deserialize)]
struct Args {
flag_c: String,
flag_o: String,
flag_e: f32,
flag_n: Option<String>,
flag_a: Option<String>,
}
fn linear_to_srgb(x: f32) -> f32 {
if x <= 0.0031308 {
12.92 * x
} else {
1.055 * f32::powf(x, 1.0 / 2.4) - 0.055
}
}
fn tonemap_kernel(x: f32) -> f32 {
let a = 0.22;
let b = 0.30;
let c = 0.10;
let d = 0.20;
let e = 0.01;
let f = 0.30;
((x * (a * x + c * b) + d * e) / (x * (a * x + b) + d * f)) - e / f
}
fn tonemap(x: f32) -> f32 {
let w = 11.2;
let scale = 1.758141;
tonemap_kernel(x * scale) / tonemap_kernel(w)
}
struct EXRData {
img: Vec<f32>,
width: usize,
height: usize,
}
impl EXRData {
fn new(width: usize, height: usize) -> EXRData {
EXRData {
img: vec![0f32; width * height * 3],
width,
height,
}
}
fn set_pixel(&mut self, x: usize, y: usize, pixel: &rgb_exr::Pixel) {
let i = (y * self.width + x) * 3;
self.img[i] = pixel.red.to_f32();
self.img[i + 1] = pixel.green.to_f32();
self.img[i + 2] = pixel.blue.to_f32();
}
}
/// Load an EXR file to an RGB f32 buffer
fn load_exr(file: &str) -> EXRData {
let (_info, image) = rgb_exr::ImageInfo::read_pixels_from_file(
file,
rgb_exr::read_options::high(),
|info: &rgb_exr::ImageInfo| -> EXRData {
EXRData::new(info.resolution.width(), info.resolution.height())
},
// set each pixel in the png buffer from the exr file
|image: &mut EXRData, pos: rgb_exr::Vec2<usize>, pixel: rgb_exr::Pixel| {
image.set_pixel(pos.x(), pos.y(), &pixel);
},
)
.unwrap();
image
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let mut color = load_exr(&args.flag_c);
let device = oidn::Device::new();
let albedo: EXRData;
let normal: EXRData;
let mut denoiser = oidn::RayTracing::new(&device);
denoiser
.srgb(false)
.hdr(true)
.image_dimensions(color.width, color.height);
if let Some(albedo_exr) = args.flag_a.clone() {
albedo = load_exr(&albedo_exr);
if let Some(normal_exr) = args.flag_n.clone() {
normal = load_exr(&normal_exr);
denoiser.albedo_normal(&albedo.img[..], &normal.img[..]);
} else {
denoiser.albedo(&albedo.img[..]);
}
}
denoiser
.filter_in_place(&mut color.img[..])
.expect("Invalid input image dimensions?");
if let Err(e) = device.get_error() {
println!("Error denosing image: {}", e.1);
}
let exposure = 2.0_f32.powf(args.flag_e);
let output_img = (0..color.img.len())
.into_par_iter()
.map(|i| {
let p = linear_to_srgb(tonemap(color.img[i] * exposure));
if p < 0.0 {
0u8
} else if p > 1.0 {
255u8
} else {
(p * 255.0) as u8
}
})
.collect::<Vec<_>>();
image::save_buffer(
&args.flag_o,
&output_img[..],
color.width as u32,
color.height as u32,
image::ColorType::Rgb8,
)
.expect("Failed to save output image");
}