Debian Codegraph @

src/Libpfs/tm/TonemapOperator.cpp

raw
1 /*
2 * This file is a part of LuminanceHDR package.
3 * ----------------------------------------------------------------------
4 * Copyright (C) 2006,2007 Giuseppe Rota
5 * Copyright (C) 2011 Davide Anastasia
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * ----------------------------------------------------------------------
21 *
22 * Original Work
23 * @author Giuseppe Rota <[email protected]>
24 * Improvements, bugfixing
25 * @author Franco Comida <[email protected]>
26 * Refactory of TMThread.h class to TonemapOperator in order to remove
27 * dependency from QObject and QThread
28 * @author Davide Anastasia <[email protected]>
29 *
30 *
31 */
32
33 #include <boost/assign.hpp>
34 #include <boost/thread/mutex.hpp>
35 #include <map>
36
37 #include "TonemappingOperators/pfstmo.h"
38
39 #include "Libpfs/channel.h"
40 #include "Libpfs/colorspace/colorspace.h"
41 #include "Libpfs/frame.h"
42 #include "Libpfs/progress.h"
43 #include "Libpfs/tm/TonemapOperator.h"
44
45 using namespace boost::assign;
46
47 template <TMOperator Key, typename ConcreteClass>
48 struct TonemapOperatorRegister : public TonemapOperator {
49 static TonemapOperator *create() { return new ConcreteClass(); }
50
51 TMOperator getType() const { return Key; }
52 };
53
54 class TonemapOperatorMantiuk06
55 : public TonemapOperatorRegister<mantiuk06, TonemapOperatorMantiuk06> {
56 public:
57 void tonemapFrame(pfs::Frame &workingFrame, TonemappingOptions *opts,
58 pfs::Progress &ph) {
59 ph.setMaximum(100);
60
61 try {
62 pfstmo_mantiuk06(
63 workingFrame,
64 opts->operator_options.mantiuk06options.contrastfactor,
65 opts->operator_options.mantiuk06options.saturationfactor,
66 opts->operator_options.mantiuk06options.detailfactor,
67 opts->operator_options.mantiuk06options.contrastequalization,
68 ph);
69 } catch (...) {
70 throw std::runtime_error("Mantiuk06: Tonemap Failed");
71 }
72 }
73 };
74
75 struct TonemapOperatorMantiuk08
76 : public TonemapOperatorRegister<mantiuk08, TonemapOperatorMantiuk08> {
77 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
78 pfs::Progress &ph) {
79 ph.setMaximum(100);
80
81 // Convert to CS_XYZ: tm operator now use this colorspace
82 pfs::Channel *X, *Y, *Z;
83 workingframe.getXYZChannels(X, Y, Z);
84 pfs::transformColorSpace(pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z);
85
86 try {
87 pfstmo_mantiuk08(
88 workingframe,
89 opts->operator_options.mantiuk08options.colorsaturation,
90 opts->operator_options.mantiuk08options.contrastenhancement,
91 opts->operator_options.mantiuk08options.luminancelevel,
92 opts->operator_options.mantiuk08options.setluminance, ph);
93 } catch (...) {
94 throw std::runtime_error("Mantiuk08: Tonemap Failed");
95 }
96
97 pfs::transformColorSpace(pfs::CS_XYZ, X, Y, Z, pfs::CS_RGB, X, Y, Z);
98 }
99 };
100
101 struct TonemapOperatorFattal02
102 : public TonemapOperatorRegister<fattal, TonemapOperatorFattal02> {
103 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
104 pfs::Progress &ph) {
105 ph.setMaximum(100);
106
107 int detail_level = 0;
108 if (opts->xsize > 0) {
109 float ratio = (float)opts->origxsize / (float)opts->xsize;
110 if (ratio < 2)
111 detail_level = 3;
112 else if (ratio < 4)
113 detail_level = 2;
114 else if (ratio < 8)
115 detail_level = 1;
116 else
117 detail_level = 0;
118 } else
119 detail_level = 3;
120
121 try {
122 pfstmo_fattal02(workingframe,
123 opts->operator_options.fattaloptions.alpha,
124 opts->operator_options.fattaloptions.beta,
125 opts->operator_options.fattaloptions.color,
126 opts->operator_options.fattaloptions.noiseredux,
127 opts->operator_options.fattaloptions.newfattal,
128 opts->operator_options.fattaloptions.fftsolver,
129 detail_level, ph);
130 } catch (...) {
131 throw std::runtime_error("Fattal: Tonemap Failed");
132 }
133 }
134 };
135
136 struct TonemapOperatorFerradans11
137 : public TonemapOperatorRegister<ferradans, TonemapOperatorFerradans11> {
138 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
139 pfs::Progress &ph) {
140 ph.setMaximum(100);
141
142 try {
143 pfstmo_ferradans11(
144 workingframe, opts->operator_options.ferradansoptions.rho,
145 opts->operator_options.ferradansoptions.inv_alpha, ph);
146 } catch (...) {
147 throw std::runtime_error("Ferradans: Tonemap Failed");
148 }
149 }
150 };
151
152 struct TonemapOperatorMai11
153 : public TonemapOperatorRegister<mai, TonemapOperatorMai11> {
154 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
155 pfs::Progress &ph) {
156 ph.setMaximum(100);
157
158 try {
159 pfstmo_mai11(workingframe, ph);
160 } catch (...) {
161 throw std::runtime_error("Mai: Tonemap Failed");
162 }
163 }
164 };
165
166 struct TonemapOperatorDrago03
167 : public TonemapOperatorRegister<drago, TonemapOperatorDrago03> {
168 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
169 pfs::Progress &ph) {
170 ph.setMaximum(100); // this guy should not be here!
171
172 try {
173 pfstmo_drago03(workingframe,
174 opts->operator_options.dragooptions.bias, ph);
175 } catch (...) {
176 throw std::runtime_error("Drago: Tonemap Failed");
177 }
178 }
179 };
180
181 class TonemapOperatorDurand02
182 : public TonemapOperatorRegister<durand, TonemapOperatorDurand02> {
183 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
184 pfs::Progress &ph) {
185 ph.setMaximum(100);
186
187 try {
188 pfstmo_durand02(workingframe,
189 opts->operator_options.durandoptions.spatial,
190 opts->operator_options.durandoptions.range,
191 opts->operator_options.durandoptions.base, ph);
192 } catch (...) {
193 throw std::runtime_error("Durand: Tonemap Failed");
194 }
195 }
196 };
197
198 struct TonemapOperatorReinhard02
199 : public TonemapOperatorRegister<reinhard02, TonemapOperatorReinhard02> {
200 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
201 pfs::Progress &ph) {
202 ph.setMaximum(100);
203
204 // Convert to CS_XYZ: tm operator now use this colorspace
205 pfs::Channel *X, *Y, *Z;
206 workingframe.getXYZChannels(X, Y, Z);
207 pfs::transformColorSpace(pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z);
208
209 try {
210 pfstmo_reinhard02(
211 workingframe, opts->operator_options.reinhard02options.key,
212 opts->operator_options.reinhard02options.phi,
213 opts->operator_options.reinhard02options.range,
214 opts->operator_options.reinhard02options.lower,
215 opts->operator_options.reinhard02options.upper,
216 opts->operator_options.reinhard02options.scales, ph);
217 } catch (...) {
218 throw std::runtime_error("Reinhard02: Tonemap Failed");
219 }
220
221 pfs::transformColorSpace(pfs::CS_XYZ, X, Y, Z, pfs::CS_RGB, X, Y, Z);
222 }
223 };
224
225 struct TonemapOperatorReinhard05
226 : public TonemapOperatorRegister<reinhard05, TonemapOperatorReinhard05> {
227 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
228 pfs::Progress &ph) {
229 ph.setMaximum(100);
230
231 try {
232 pfstmo_reinhard05(
233 workingframe,
234 opts->operator_options.reinhard05options.brightness,
235 opts->operator_options.reinhard05options.chromaticAdaptation,
236 opts->operator_options.reinhard05options.lightAdaptation, ph);
237 } catch (...) {
238 throw std::runtime_error("Reinhard05: Tonemap Failed");
239 }
240 }
241 };
242
243 struct TonemapOperatorAshikhmin02
244 : public TonemapOperatorRegister<ashikhmin, TonemapOperatorAshikhmin02> {
245 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
246 pfs::Progress &ph) {
247 ph.setMaximum(100);
248
249 try {
250 pfstmo_ashikhmin02(
251 workingframe, opts->operator_options.ashikhminoptions.simple,
252 opts->operator_options.ashikhminoptions.lct,
253 (opts->operator_options.ashikhminoptions.eq2 ? 2 : 4), ph);
254 } catch (...) {
255 throw std::runtime_error("Ashikhmin: Tonemap Failed");
256 }
257 }
258 };
259
260 struct TonemapOperatorPattanaik00
261 : public TonemapOperatorRegister<pattanaik, TonemapOperatorPattanaik00> {
262 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
263 pfs::Progress &ph) {
264 ph.setMaximum(100);
265
266 // Convert to CS_XYZ: tm operator now use this colorspace
267 pfs::Channel *X, *Y, *Z;
268 workingframe.getXYZChannels(X, Y, Z);
269 pfs::transformColorSpace(pfs::CS_RGB, X, Y, Z, pfs::CS_XYZ, X, Y, Z);
270
271 try {
272 pfstmo_pattanaik00(
273 workingframe, opts->operator_options.pattanaikoptions.local,
274 opts->operator_options.pattanaikoptions.multiplier,
275 opts->operator_options.pattanaikoptions.cone, // * 1000,
276 opts->operator_options.pattanaikoptions.rod, // * 1000,
277 opts->operator_options.pattanaikoptions.autolum, ph);
278 } catch (...) {
279 throw std::runtime_error("Pattanaik: Tonemap Failed");
280 }
281
282 pfs::transformColorSpace(pfs::CS_XYZ, X, Y, Z, pfs::CS_RGB, X, Y, Z);
283 }
284 };
285
286 struct TonemapOperatorFerwerda96
287 : public TonemapOperatorRegister<ferwerda, TonemapOperatorFerwerda96> {
288 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
289 pfs::Progress &ph) {
290 ph.setMaximum(100);
291
292 try {
293 pfstmo_ferwerda96(
294 workingframe, opts->operator_options.ferwerdaoptions.multiplier,
295 opts->operator_options.ferwerdaoptions.adaptationluminance,
296 ph);
297 } catch (...) {
298 throw std::runtime_error("Ferwerda: Tonemap Failed");
299 }
300 }
301 };
302
303 struct TonemapOperatorKimKautz08
304 : public TonemapOperatorRegister<kimkautz, TonemapOperatorKimKautz08> {
305 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
306 pfs::Progress &ph) {
307 ph.setMaximum(100);
308
309 try {
310 pfstmo_kimkautz08(
311 workingframe,
312 opts->operator_options.kimkautzoptions.c1,
313 opts->operator_options.kimkautzoptions.c2,
314 ph);
315 } catch (...) {
316 throw std::runtime_error("KimKautz: Tonemap Failed");
317 }
318 }
319 };
320
321 struct TonemapOperatorVanHateren06
322 : public TonemapOperatorRegister<vanhateren, TonemapOperatorVanHateren06> {
323 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
324 pfs::Progress &ph) {
325 ph.setMaximum(100);
326
327 try {
328 pfstmo_vanhateren06(
329 workingframe,
330 opts->operator_options.vanhaterenoptions.pupil_area,
331 ph);
332 } catch (...) {
333 throw std::runtime_error("VanHateren: Tonemap Failed");
334 }
335 }
336 };
337
338 struct TonemapOperatorLischinski06
339 : public TonemapOperatorRegister<lischinski, TonemapOperatorLischinski06> {
340 void tonemapFrame(pfs::Frame &workingframe, TonemappingOptions *opts,
341 pfs::Progress &ph) {
342 ph.setMaximum(100);
343
344 try {
345 pfstmo_lischinski06(
346 workingframe, opts->operator_options.lischinskioptions.alpha,
347 ph);
348 } catch (...) {
349 throw std::runtime_error("Lischinski: Tonemap Failed");
350 }
351 }
352 };
353
354 typedef TonemapOperator *(*TonemapOperatorCreator)();
355 typedef std::map<TMOperator, TonemapOperatorCreator> TonemapOperatorCreatorMap;
356
357 inline const TonemapOperatorCreatorMap &registry() {
358 static TonemapOperatorCreatorMap reg = map_list_of
359 // XYZ -> *
360 (mantiuk06, TonemapOperatorRegister<mantiuk06, TonemapOperatorMantiuk06>::create)
361 (mantiuk08, TonemapOperatorRegister<mantiuk08, TonemapOperatorMantiuk08>::create)
362 (fattal, TonemapOperatorRegister<fattal, TonemapOperatorFattal02>::create)
363 (ferradans, TonemapOperatorRegister<ferradans, TonemapOperatorFerradans11>::create)
364 (mai, TonemapOperatorRegister<mai, TonemapOperatorMai11>::create)
365 (drago, TonemapOperatorRegister<drago, TonemapOperatorDrago03>::create)
366 (durand, TonemapOperatorRegister<durand, TonemapOperatorDurand02>::create)
367 (reinhard02, TonemapOperatorRegister<reinhard02, TonemapOperatorReinhard02>::create)
368 (reinhard05, TonemapOperatorRegister<reinhard05, TonemapOperatorReinhard05>::create)
369 (ashikhmin, TonemapOperatorRegister<ashikhmin, TonemapOperatorAshikhmin02>::create)
370 (pattanaik, TonemapOperatorRegister<pattanaik, TonemapOperatorPattanaik00>::create)
371 (ferwerda, TonemapOperatorRegister<ferwerda, TonemapOperatorFerwerda96>::create)
372 (kimkautz, TonemapOperatorRegister<kimkautz, TonemapOperatorKimKautz08>::create)
373 (vanhateren, TonemapOperatorRegister<vanhateren, TonemapOperatorVanHateren06>::create)
374 (lischinski, TonemapOperatorRegister<lischinski, TonemapOperatorLischinski06>::create);
375 return reg;
376 }
377
378 TonemapOperator::TonemapOperator() {}
379
380 TonemapOperator::~TonemapOperator() {}
381
382 TonemapOperator *TonemapOperator::getTonemapOperator(const TMOperator tmo) {
383 TonemapOperatorCreatorMap::const_iterator it = registry().find(tmo);
384 if (it != registry().end()) {
385 return (it->second)();
386 }
387 throw std::runtime_error("Invalid TMOperator");
388 }