Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_render.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2023 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_render.h"
10
11 #include "dng_1d_table.h"
12 #include "dng_big_table.h"
13 #include "dng_bottlenecks.h"
14 #include "dng_camera_profile.h"
15 #include "dng_color_space.h"
16 #include "dng_color_spec.h"
17 #include "dng_filter_task.h"
18 #include "dng_host.h"
19 #include "dng_image.h"
20 #include "dng_negative.h"
21 #include "dng_reference.h"
22 #include "dng_render.h"
23 #include "dng_resample.h"
24 #include "dng_safe_arithmetic.h"
25 #include "dng_utils.h"
26
27 /*****************************************************************************/
28
29 dng_function_zero_offset::dng_function_zero_offset (real64 zeroOffset)
30
31 : fZeroOffset (zeroOffset)
32
33 , fScale (1.0 / (1.0 - zeroOffset))
34
35 {
36
37 }
38
39 /*****************************************************************************/
40
41 real64 dng_function_zero_offset::Evaluate (real64 x) const
42 {
43
44 return Pin_real64 (0.0, (x - fZeroOffset) * fScale, 1.0);
45
46 }
47
48 /*****************************************************************************/
49
50 dng_function_exposure_ramp::dng_function_exposure_ramp (real64 white,
51 real64 black,
52 real64 minBlack,
53 bool supportOverrange)
54
55 : fSlope (1.0 / (white - black))
56 , fBlack (black)
57
58 , fRadius (0.0)
59 , fQScale (0.0)
60
61 , fSupportOverrange (supportOverrange)
62
63 {
64
65 const real64 kMaxCurveX = 0.5; // Fraction of minBlack.
66
67 const real64 kMaxCurveY = 1.0 / 16.0; // Fraction of white.
68
69 fRadius = Min_real64 (kMaxCurveX * minBlack,
70 kMaxCurveY / fSlope);
71
72 if (fRadius > 0.0)
73 fQScale= fSlope / (4.0 * fRadius);
74 else
75 fQScale = 0.0;
76
77 }
78
79 /*****************************************************************************/
80
81 real64 dng_function_exposure_ramp::Evaluate (real64 x) const
82 {
83
84 if (x <= fBlack - fRadius)
85 return 0.0;
86
87 if (x >= fBlack + fRadius)
88 {
89
90 real64 y = (x - fBlack) * fSlope;
91
92 if (!fSupportOverrange)
93 y = Min_real64 (y, 1.0);
94
95 return y;
96
97 }
98
99 real64 y = x - (fBlack - fRadius);
100
101 return fQScale * y * y;
102
103 }
104
105 /*****************************************************************************/
106
107 dng_function_exposure_tone::dng_function_exposure_tone (real64 exposure)
108
109 : fIsNOP (exposure >= 0.0)
110
111 , fSlope (0.0)
112
113 , a (0.0)
114 , b (0.0)
115 , c (0.0)
116
117 {
118
119 if (!fIsNOP)
120 {
121
122 // Find slope to use for the all except the highest two f-stops.
123
124 fSlope = pow (2.0, exposure);
125
126 // Find quadradic parameters that match this darking at the crossover
127 // point, yet still map pure white to pure white.
128
129 a = 16.0 / 9.0 * (1.0 - fSlope);
130
131 b = fSlope - 0.5 * a;
132
133 c = 1.0 - a - b;
134
135 }
136
137 }
138
139 /*****************************************************************************/
140
141 real64 dng_function_exposure_tone::Evaluate (real64 x) const
142 {
143
144 if (!fIsNOP)
145 {
146
147 if (x <= 0.25)
148 x = x * fSlope;
149
150 else
151 x = (a * x + b) * x + c;
152
153 }
154
155 return x;
156
157 }
158
159 /*****************************************************************************/
160
161 real64 dng_tone_curve_acr3_default::Evaluate (real64 x) const
162 {
163
164 static const real32 kTable [] =
165 {
166 0.00000f, 0.00078f, 0.00160f, 0.00242f,
167 0.00314f, 0.00385f, 0.00460f, 0.00539f,
168 0.00623f, 0.00712f, 0.00806f, 0.00906f,
169 0.01012f, 0.01122f, 0.01238f, 0.01359f,
170 0.01485f, 0.01616f, 0.01751f, 0.01890f,
171 0.02033f, 0.02180f, 0.02331f, 0.02485f,
172 0.02643f, 0.02804f, 0.02967f, 0.03134f,
173 0.03303f, 0.03475f, 0.03648f, 0.03824f,
174 0.04002f, 0.04181f, 0.04362f, 0.04545f,
175 0.04730f, 0.04916f, 0.05103f, 0.05292f,
176 0.05483f, 0.05675f, 0.05868f, 0.06063f,
177 0.06259f, 0.06457f, 0.06655f, 0.06856f,
178 0.07057f, 0.07259f, 0.07463f, 0.07668f,
179 0.07874f, 0.08081f, 0.08290f, 0.08499f,
180 0.08710f, 0.08921f, 0.09134f, 0.09348f,
181 0.09563f, 0.09779f, 0.09996f, 0.10214f,
182 0.10433f, 0.10652f, 0.10873f, 0.11095f,
183 0.11318f, 0.11541f, 0.11766f, 0.11991f,
184 0.12218f, 0.12445f, 0.12673f, 0.12902f,
185 0.13132f, 0.13363f, 0.13595f, 0.13827f,
186 0.14061f, 0.14295f, 0.14530f, 0.14765f,
187 0.15002f, 0.15239f, 0.15477f, 0.15716f,
188 0.15956f, 0.16197f, 0.16438f, 0.16680f,
189 0.16923f, 0.17166f, 0.17410f, 0.17655f,
190 0.17901f, 0.18148f, 0.18395f, 0.18643f,
191 0.18891f, 0.19141f, 0.19391f, 0.19641f,
192 0.19893f, 0.20145f, 0.20398f, 0.20651f,
193 0.20905f, 0.21160f, 0.21416f, 0.21672f,
194 0.21929f, 0.22185f, 0.22440f, 0.22696f,
195 0.22950f, 0.23204f, 0.23458f, 0.23711f,
196 0.23963f, 0.24215f, 0.24466f, 0.24717f,
197 0.24967f, 0.25216f, 0.25465f, 0.25713f,
198 0.25961f, 0.26208f, 0.26454f, 0.26700f,
199 0.26945f, 0.27189f, 0.27433f, 0.27676f,
200 0.27918f, 0.28160f, 0.28401f, 0.28641f,
201 0.28881f, 0.29120f, 0.29358f, 0.29596f,
202 0.29833f, 0.30069f, 0.30305f, 0.30540f,
203 0.30774f, 0.31008f, 0.31241f, 0.31473f,
204 0.31704f, 0.31935f, 0.32165f, 0.32395f,
205 0.32623f, 0.32851f, 0.33079f, 0.33305f,
206 0.33531f, 0.33756f, 0.33981f, 0.34205f,
207 0.34428f, 0.34650f, 0.34872f, 0.35093f,
208 0.35313f, 0.35532f, 0.35751f, 0.35969f,
209 0.36187f, 0.36404f, 0.36620f, 0.36835f,
210 0.37050f, 0.37264f, 0.37477f, 0.37689f,
211 0.37901f, 0.38112f, 0.38323f, 0.38533f,
212 0.38742f, 0.38950f, 0.39158f, 0.39365f,
213 0.39571f, 0.39777f, 0.39982f, 0.40186f,
214 0.40389f, 0.40592f, 0.40794f, 0.40996f,
215 0.41197f, 0.41397f, 0.41596f, 0.41795f,
216 0.41993f, 0.42191f, 0.42388f, 0.42584f,
217 0.42779f, 0.42974f, 0.43168f, 0.43362f,
218 0.43554f, 0.43747f, 0.43938f, 0.44129f,
219 0.44319f, 0.44509f, 0.44698f, 0.44886f,
220 0.45073f, 0.45260f, 0.45447f, 0.45632f,
221 0.45817f, 0.46002f, 0.46186f, 0.46369f,
222 0.46551f, 0.46733f, 0.46914f, 0.47095f,
223 0.47275f, 0.47454f, 0.47633f, 0.47811f,
224 0.47989f, 0.48166f, 0.48342f, 0.48518f,
225 0.48693f, 0.48867f, 0.49041f, 0.49214f,
226 0.49387f, 0.49559f, 0.49730f, 0.49901f,
227 0.50072f, 0.50241f, 0.50410f, 0.50579f,
228 0.50747f, 0.50914f, 0.51081f, 0.51247f,
229 0.51413f, 0.51578f, 0.51742f, 0.51906f,
230 0.52069f, 0.52232f, 0.52394f, 0.52556f,
231 0.52717f, 0.52878f, 0.53038f, 0.53197f,
232 0.53356f, 0.53514f, 0.53672f, 0.53829f,
233 0.53986f, 0.54142f, 0.54297f, 0.54452f,
234 0.54607f, 0.54761f, 0.54914f, 0.55067f,
235 0.55220f, 0.55371f, 0.55523f, 0.55673f,
236 0.55824f, 0.55973f, 0.56123f, 0.56271f,
237 0.56420f, 0.56567f, 0.56715f, 0.56861f,
238 0.57007f, 0.57153f, 0.57298f, 0.57443f,
239 0.57587f, 0.57731f, 0.57874f, 0.58017f,
240 0.58159f, 0.58301f, 0.58443f, 0.58583f,
241 0.58724f, 0.58864f, 0.59003f, 0.59142f,
242 0.59281f, 0.59419f, 0.59556f, 0.59694f,
243 0.59830f, 0.59966f, 0.60102f, 0.60238f,
244 0.60373f, 0.60507f, 0.60641f, 0.60775f,
245 0.60908f, 0.61040f, 0.61173f, 0.61305f,
246 0.61436f, 0.61567f, 0.61698f, 0.61828f,
247 0.61957f, 0.62087f, 0.62216f, 0.62344f,
248 0.62472f, 0.62600f, 0.62727f, 0.62854f,
249 0.62980f, 0.63106f, 0.63232f, 0.63357f,
250 0.63482f, 0.63606f, 0.63730f, 0.63854f,
251 0.63977f, 0.64100f, 0.64222f, 0.64344f,
252 0.64466f, 0.64587f, 0.64708f, 0.64829f,
253 0.64949f, 0.65069f, 0.65188f, 0.65307f,
254 0.65426f, 0.65544f, 0.65662f, 0.65779f,
255 0.65897f, 0.66013f, 0.66130f, 0.66246f,
256 0.66362f, 0.66477f, 0.66592f, 0.66707f,
257 0.66821f, 0.66935f, 0.67048f, 0.67162f,
258 0.67275f, 0.67387f, 0.67499f, 0.67611f,
259 0.67723f, 0.67834f, 0.67945f, 0.68055f,
260 0.68165f, 0.68275f, 0.68385f, 0.68494f,
261 0.68603f, 0.68711f, 0.68819f, 0.68927f,
262 0.69035f, 0.69142f, 0.69249f, 0.69355f,
263 0.69461f, 0.69567f, 0.69673f, 0.69778f,
264 0.69883f, 0.69988f, 0.70092f, 0.70196f,
265 0.70300f, 0.70403f, 0.70506f, 0.70609f,
266 0.70711f, 0.70813f, 0.70915f, 0.71017f,
267 0.71118f, 0.71219f, 0.71319f, 0.71420f,
268 0.71520f, 0.71620f, 0.71719f, 0.71818f,
269 0.71917f, 0.72016f, 0.72114f, 0.72212f,
270 0.72309f, 0.72407f, 0.72504f, 0.72601f,
271 0.72697f, 0.72794f, 0.72890f, 0.72985f,
272 0.73081f, 0.73176f, 0.73271f, 0.73365f,
273 0.73460f, 0.73554f, 0.73647f, 0.73741f,
274 0.73834f, 0.73927f, 0.74020f, 0.74112f,
275 0.74204f, 0.74296f, 0.74388f, 0.74479f,
276 0.74570f, 0.74661f, 0.74751f, 0.74842f,
277 0.74932f, 0.75021f, 0.75111f, 0.75200f,
278 0.75289f, 0.75378f, 0.75466f, 0.75555f,
279 0.75643f, 0.75730f, 0.75818f, 0.75905f,
280 0.75992f, 0.76079f, 0.76165f, 0.76251f,
281 0.76337f, 0.76423f, 0.76508f, 0.76594f,
282 0.76679f, 0.76763f, 0.76848f, 0.76932f,
283 0.77016f, 0.77100f, 0.77183f, 0.77267f,
284 0.77350f, 0.77432f, 0.77515f, 0.77597f,
285 0.77680f, 0.77761f, 0.77843f, 0.77924f,
286 0.78006f, 0.78087f, 0.78167f, 0.78248f,
287 0.78328f, 0.78408f, 0.78488f, 0.78568f,
288 0.78647f, 0.78726f, 0.78805f, 0.78884f,
289 0.78962f, 0.79040f, 0.79118f, 0.79196f,
290 0.79274f, 0.79351f, 0.79428f, 0.79505f,
291 0.79582f, 0.79658f, 0.79735f, 0.79811f,
292 0.79887f, 0.79962f, 0.80038f, 0.80113f,
293 0.80188f, 0.80263f, 0.80337f, 0.80412f,
294 0.80486f, 0.80560f, 0.80634f, 0.80707f,
295 0.80780f, 0.80854f, 0.80926f, 0.80999f,
296 0.81072f, 0.81144f, 0.81216f, 0.81288f,
297 0.81360f, 0.81431f, 0.81503f, 0.81574f,
298 0.81645f, 0.81715f, 0.81786f, 0.81856f,
299 0.81926f, 0.81996f, 0.82066f, 0.82135f,
300 0.82205f, 0.82274f, 0.82343f, 0.82412f,
301 0.82480f, 0.82549f, 0.82617f, 0.82685f,
302 0.82753f, 0.82820f, 0.82888f, 0.82955f,
303 0.83022f, 0.83089f, 0.83155f, 0.83222f,
304 0.83288f, 0.83354f, 0.83420f, 0.83486f,
305 0.83552f, 0.83617f, 0.83682f, 0.83747f,
306 0.83812f, 0.83877f, 0.83941f, 0.84005f,
307 0.84069f, 0.84133f, 0.84197f, 0.84261f,
308 0.84324f, 0.84387f, 0.84450f, 0.84513f,
309 0.84576f, 0.84639f, 0.84701f, 0.84763f,
310 0.84825f, 0.84887f, 0.84949f, 0.85010f,
311 0.85071f, 0.85132f, 0.85193f, 0.85254f,
312 0.85315f, 0.85375f, 0.85436f, 0.85496f,
313 0.85556f, 0.85615f, 0.85675f, 0.85735f,
314 0.85794f, 0.85853f, 0.85912f, 0.85971f,
315 0.86029f, 0.86088f, 0.86146f, 0.86204f,
316 0.86262f, 0.86320f, 0.86378f, 0.86435f,
317 0.86493f, 0.86550f, 0.86607f, 0.86664f,
318 0.86720f, 0.86777f, 0.86833f, 0.86889f,
319 0.86945f, 0.87001f, 0.87057f, 0.87113f,
320 0.87168f, 0.87223f, 0.87278f, 0.87333f,
321 0.87388f, 0.87443f, 0.87497f, 0.87552f,
322 0.87606f, 0.87660f, 0.87714f, 0.87768f,
323 0.87821f, 0.87875f, 0.87928f, 0.87981f,
324 0.88034f, 0.88087f, 0.88140f, 0.88192f,
325 0.88244f, 0.88297f, 0.88349f, 0.88401f,
326 0.88453f, 0.88504f, 0.88556f, 0.88607f,
327 0.88658f, 0.88709f, 0.88760f, 0.88811f,
328 0.88862f, 0.88912f, 0.88963f, 0.89013f,
329 0.89063f, 0.89113f, 0.89163f, 0.89212f,
330 0.89262f, 0.89311f, 0.89360f, 0.89409f,
331 0.89458f, 0.89507f, 0.89556f, 0.89604f,
332 0.89653f, 0.89701f, 0.89749f, 0.89797f,
333 0.89845f, 0.89892f, 0.89940f, 0.89987f,
334 0.90035f, 0.90082f, 0.90129f, 0.90176f,
335 0.90222f, 0.90269f, 0.90316f, 0.90362f,
336 0.90408f, 0.90454f, 0.90500f, 0.90546f,
337 0.90592f, 0.90637f, 0.90683f, 0.90728f,
338 0.90773f, 0.90818f, 0.90863f, 0.90908f,
339 0.90952f, 0.90997f, 0.91041f, 0.91085f,
340 0.91130f, 0.91173f, 0.91217f, 0.91261f,
341 0.91305f, 0.91348f, 0.91392f, 0.91435f,
342 0.91478f, 0.91521f, 0.91564f, 0.91606f,
343 0.91649f, 0.91691f, 0.91734f, 0.91776f,
344 0.91818f, 0.91860f, 0.91902f, 0.91944f,
345 0.91985f, 0.92027f, 0.92068f, 0.92109f,
346 0.92150f, 0.92191f, 0.92232f, 0.92273f,
347 0.92314f, 0.92354f, 0.92395f, 0.92435f,
348 0.92475f, 0.92515f, 0.92555f, 0.92595f,
349 0.92634f, 0.92674f, 0.92713f, 0.92753f,
350 0.92792f, 0.92831f, 0.92870f, 0.92909f,
351 0.92947f, 0.92986f, 0.93025f, 0.93063f,
352 0.93101f, 0.93139f, 0.93177f, 0.93215f,
353 0.93253f, 0.93291f, 0.93328f, 0.93366f,
354 0.93403f, 0.93440f, 0.93478f, 0.93515f,
355 0.93551f, 0.93588f, 0.93625f, 0.93661f,
356 0.93698f, 0.93734f, 0.93770f, 0.93807f,
357 0.93843f, 0.93878f, 0.93914f, 0.93950f,
358 0.93986f, 0.94021f, 0.94056f, 0.94092f,
359 0.94127f, 0.94162f, 0.94197f, 0.94231f,
360 0.94266f, 0.94301f, 0.94335f, 0.94369f,
361 0.94404f, 0.94438f, 0.94472f, 0.94506f,
362 0.94540f, 0.94573f, 0.94607f, 0.94641f,
363 0.94674f, 0.94707f, 0.94740f, 0.94774f,
364 0.94807f, 0.94839f, 0.94872f, 0.94905f,
365 0.94937f, 0.94970f, 0.95002f, 0.95035f,
366 0.95067f, 0.95099f, 0.95131f, 0.95163f,
367 0.95194f, 0.95226f, 0.95257f, 0.95289f,
368 0.95320f, 0.95351f, 0.95383f, 0.95414f,
369 0.95445f, 0.95475f, 0.95506f, 0.95537f,
370 0.95567f, 0.95598f, 0.95628f, 0.95658f,
371 0.95688f, 0.95718f, 0.95748f, 0.95778f,
372 0.95808f, 0.95838f, 0.95867f, 0.95897f,
373 0.95926f, 0.95955f, 0.95984f, 0.96013f,
374 0.96042f, 0.96071f, 0.96100f, 0.96129f,
375 0.96157f, 0.96186f, 0.96214f, 0.96242f,
376 0.96271f, 0.96299f, 0.96327f, 0.96355f,
377 0.96382f, 0.96410f, 0.96438f, 0.96465f,
378 0.96493f, 0.96520f, 0.96547f, 0.96574f,
379 0.96602f, 0.96629f, 0.96655f, 0.96682f,
380 0.96709f, 0.96735f, 0.96762f, 0.96788f,
381 0.96815f, 0.96841f, 0.96867f, 0.96893f,
382 0.96919f, 0.96945f, 0.96971f, 0.96996f,
383 0.97022f, 0.97047f, 0.97073f, 0.97098f,
384 0.97123f, 0.97149f, 0.97174f, 0.97199f,
385 0.97223f, 0.97248f, 0.97273f, 0.97297f,
386 0.97322f, 0.97346f, 0.97371f, 0.97395f,
387 0.97419f, 0.97443f, 0.97467f, 0.97491f,
388 0.97515f, 0.97539f, 0.97562f, 0.97586f,
389 0.97609f, 0.97633f, 0.97656f, 0.97679f,
390 0.97702f, 0.97725f, 0.97748f, 0.97771f,
391 0.97794f, 0.97817f, 0.97839f, 0.97862f,
392 0.97884f, 0.97907f, 0.97929f, 0.97951f,
393 0.97973f, 0.97995f, 0.98017f, 0.98039f,
394 0.98061f, 0.98082f, 0.98104f, 0.98125f,
395 0.98147f, 0.98168f, 0.98189f, 0.98211f,
396 0.98232f, 0.98253f, 0.98274f, 0.98295f,
397 0.98315f, 0.98336f, 0.98357f, 0.98377f,
398 0.98398f, 0.98418f, 0.98438f, 0.98458f,
399 0.98478f, 0.98498f, 0.98518f, 0.98538f,
400 0.98558f, 0.98578f, 0.98597f, 0.98617f,
401 0.98636f, 0.98656f, 0.98675f, 0.98694f,
402 0.98714f, 0.98733f, 0.98752f, 0.98771f,
403 0.98789f, 0.98808f, 0.98827f, 0.98845f,
404 0.98864f, 0.98882f, 0.98901f, 0.98919f,
405 0.98937f, 0.98955f, 0.98973f, 0.98991f,
406 0.99009f, 0.99027f, 0.99045f, 0.99063f,
407 0.99080f, 0.99098f, 0.99115f, 0.99133f,
408 0.99150f, 0.99167f, 0.99184f, 0.99201f,
409 0.99218f, 0.99235f, 0.99252f, 0.99269f,
410 0.99285f, 0.99302f, 0.99319f, 0.99335f,
411 0.99351f, 0.99368f, 0.99384f, 0.99400f,
412 0.99416f, 0.99432f, 0.99448f, 0.99464f,
413 0.99480f, 0.99495f, 0.99511f, 0.99527f,
414 0.99542f, 0.99558f, 0.99573f, 0.99588f,
415 0.99603f, 0.99619f, 0.99634f, 0.99649f,
416 0.99664f, 0.99678f, 0.99693f, 0.99708f,
417 0.99722f, 0.99737f, 0.99751f, 0.99766f,
418 0.99780f, 0.99794f, 0.99809f, 0.99823f,
419 0.99837f, 0.99851f, 0.99865f, 0.99879f,
420 0.99892f, 0.99906f, 0.99920f, 0.99933f,
421 0.99947f, 0.99960f, 0.99974f, 0.99987f,
422 1.00000f
423 };
424
425 const uint32 kTableSize = sizeof (kTable ) /
426 sizeof (kTable [0]);
427
428 real32 y = (real32) x * (real32) (kTableSize - 1);
429
430 int32 index = Pin_int32 (0, (int32) y, kTableSize - 2);
431
432 real32 fract = y - (real32) index;
433
434 return kTable [index ] * (1.0f - fract) +
435 kTable [index + 1] * ( fract);
436
437 }
438
439 /*****************************************************************************/
440
441 real64 dng_tone_curve_acr3_default::EvaluateInverse (real64 x) const
442 {
443
444 static const real32 kTable [] =
445 {
446 0.00000f, 0.00121f, 0.00237f, 0.00362f,
447 0.00496f, 0.00621f, 0.00738f, 0.00848f,
448 0.00951f, 0.01048f, 0.01139f, 0.01227f,
449 0.01312f, 0.01393f, 0.01471f, 0.01547f,
450 0.01620f, 0.01692f, 0.01763f, 0.01831f,
451 0.01899f, 0.01965f, 0.02030f, 0.02094f,
452 0.02157f, 0.02218f, 0.02280f, 0.02340f,
453 0.02399f, 0.02458f, 0.02517f, 0.02574f,
454 0.02631f, 0.02688f, 0.02744f, 0.02800f,
455 0.02855f, 0.02910f, 0.02965f, 0.03019f,
456 0.03072f, 0.03126f, 0.03179f, 0.03232f,
457 0.03285f, 0.03338f, 0.03390f, 0.03442f,
458 0.03493f, 0.03545f, 0.03596f, 0.03647f,
459 0.03698f, 0.03749f, 0.03799f, 0.03849f,
460 0.03899f, 0.03949f, 0.03998f, 0.04048f,
461 0.04097f, 0.04146f, 0.04195f, 0.04244f,
462 0.04292f, 0.04341f, 0.04389f, 0.04437f,
463 0.04485f, 0.04533f, 0.04580f, 0.04628f,
464 0.04675f, 0.04722f, 0.04769f, 0.04816f,
465 0.04863f, 0.04910f, 0.04956f, 0.05003f,
466 0.05049f, 0.05095f, 0.05141f, 0.05187f,
467 0.05233f, 0.05278f, 0.05324f, 0.05370f,
468 0.05415f, 0.05460f, 0.05505f, 0.05551f,
469 0.05595f, 0.05640f, 0.05685f, 0.05729f,
470 0.05774f, 0.05818f, 0.05863f, 0.05907f,
471 0.05951f, 0.05995f, 0.06039f, 0.06083f,
472 0.06126f, 0.06170f, 0.06214f, 0.06257f,
473 0.06301f, 0.06344f, 0.06388f, 0.06431f,
474 0.06474f, 0.06517f, 0.06560f, 0.06602f,
475 0.06645f, 0.06688f, 0.06731f, 0.06773f,
476 0.06815f, 0.06858f, 0.06900f, 0.06943f,
477 0.06985f, 0.07027f, 0.07069f, 0.07111f,
478 0.07152f, 0.07194f, 0.07236f, 0.07278f,
479 0.07319f, 0.07361f, 0.07402f, 0.07444f,
480 0.07485f, 0.07526f, 0.07567f, 0.07608f,
481 0.07650f, 0.07691f, 0.07732f, 0.07772f,
482 0.07813f, 0.07854f, 0.07895f, 0.07935f,
483 0.07976f, 0.08016f, 0.08057f, 0.08098f,
484 0.08138f, 0.08178f, 0.08218f, 0.08259f,
485 0.08299f, 0.08339f, 0.08379f, 0.08419f,
486 0.08459f, 0.08499f, 0.08539f, 0.08578f,
487 0.08618f, 0.08657f, 0.08697f, 0.08737f,
488 0.08776f, 0.08816f, 0.08855f, 0.08894f,
489 0.08934f, 0.08973f, 0.09012f, 0.09051f,
490 0.09091f, 0.09130f, 0.09169f, 0.09208f,
491 0.09247f, 0.09286f, 0.09324f, 0.09363f,
492 0.09402f, 0.09440f, 0.09479f, 0.09518f,
493 0.09556f, 0.09595f, 0.09633f, 0.09672f,
494 0.09710f, 0.09749f, 0.09787f, 0.09825f,
495 0.09863f, 0.09901f, 0.09939f, 0.09978f,
496 0.10016f, 0.10054f, 0.10092f, 0.10130f,
497 0.10167f, 0.10205f, 0.10243f, 0.10281f,
498 0.10319f, 0.10356f, 0.10394f, 0.10432f,
499 0.10469f, 0.10507f, 0.10544f, 0.10582f,
500 0.10619f, 0.10657f, 0.10694f, 0.10731f,
501 0.10768f, 0.10806f, 0.10843f, 0.10880f,
502 0.10917f, 0.10954f, 0.10991f, 0.11029f,
503 0.11066f, 0.11103f, 0.11141f, 0.11178f,
504 0.11215f, 0.11253f, 0.11290f, 0.11328f,
505 0.11365f, 0.11403f, 0.11440f, 0.11478f,
506 0.11516f, 0.11553f, 0.11591f, 0.11629f,
507 0.11666f, 0.11704f, 0.11742f, 0.11780f,
508 0.11818f, 0.11856f, 0.11894f, 0.11932f,
509 0.11970f, 0.12008f, 0.12046f, 0.12084f,
510 0.12122f, 0.12161f, 0.12199f, 0.12237f,
511 0.12276f, 0.12314f, 0.12352f, 0.12391f,
512 0.12429f, 0.12468f, 0.12506f, 0.12545f,
513 0.12583f, 0.12622f, 0.12661f, 0.12700f,
514 0.12738f, 0.12777f, 0.12816f, 0.12855f,
515 0.12894f, 0.12933f, 0.12972f, 0.13011f,
516 0.13050f, 0.13089f, 0.13129f, 0.13168f,
517 0.13207f, 0.13247f, 0.13286f, 0.13325f,
518 0.13365f, 0.13404f, 0.13444f, 0.13483f,
519 0.13523f, 0.13563f, 0.13603f, 0.13642f,
520 0.13682f, 0.13722f, 0.13762f, 0.13802f,
521 0.13842f, 0.13882f, 0.13922f, 0.13962f,
522 0.14003f, 0.14043f, 0.14083f, 0.14124f,
523 0.14164f, 0.14204f, 0.14245f, 0.14285f,
524 0.14326f, 0.14366f, 0.14407f, 0.14448f,
525 0.14489f, 0.14530f, 0.14570f, 0.14611f,
526 0.14652f, 0.14693f, 0.14734f, 0.14776f,
527 0.14817f, 0.14858f, 0.14900f, 0.14941f,
528 0.14982f, 0.15024f, 0.15065f, 0.15107f,
529 0.15148f, 0.15190f, 0.15232f, 0.15274f,
530 0.15316f, 0.15357f, 0.15399f, 0.15441f,
531 0.15483f, 0.15526f, 0.15568f, 0.15610f,
532 0.15652f, 0.15695f, 0.15737f, 0.15779f,
533 0.15822f, 0.15864f, 0.15907f, 0.15950f,
534 0.15992f, 0.16035f, 0.16078f, 0.16121f,
535 0.16164f, 0.16207f, 0.16250f, 0.16293f,
536 0.16337f, 0.16380f, 0.16423f, 0.16467f,
537 0.16511f, 0.16554f, 0.16598f, 0.16641f,
538 0.16685f, 0.16729f, 0.16773f, 0.16816f,
539 0.16860f, 0.16904f, 0.16949f, 0.16993f,
540 0.17037f, 0.17081f, 0.17126f, 0.17170f,
541 0.17215f, 0.17259f, 0.17304f, 0.17349f,
542 0.17393f, 0.17438f, 0.17483f, 0.17528f,
543 0.17573f, 0.17619f, 0.17664f, 0.17709f,
544 0.17754f, 0.17799f, 0.17845f, 0.17890f,
545 0.17936f, 0.17982f, 0.18028f, 0.18073f,
546 0.18119f, 0.18165f, 0.18211f, 0.18257f,
547 0.18303f, 0.18350f, 0.18396f, 0.18442f,
548 0.18489f, 0.18535f, 0.18582f, 0.18629f,
549 0.18676f, 0.18723f, 0.18770f, 0.18817f,
550 0.18864f, 0.18911f, 0.18958f, 0.19005f,
551 0.19053f, 0.19100f, 0.19147f, 0.19195f,
552 0.19243f, 0.19291f, 0.19339f, 0.19387f,
553 0.19435f, 0.19483f, 0.19531f, 0.19579f,
554 0.19627f, 0.19676f, 0.19724f, 0.19773f,
555 0.19821f, 0.19870f, 0.19919f, 0.19968f,
556 0.20017f, 0.20066f, 0.20115f, 0.20164f,
557 0.20214f, 0.20263f, 0.20313f, 0.20362f,
558 0.20412f, 0.20462f, 0.20512f, 0.20561f,
559 0.20611f, 0.20662f, 0.20712f, 0.20762f,
560 0.20812f, 0.20863f, 0.20913f, 0.20964f,
561 0.21015f, 0.21066f, 0.21117f, 0.21168f,
562 0.21219f, 0.21270f, 0.21321f, 0.21373f,
563 0.21424f, 0.21476f, 0.21527f, 0.21579f,
564 0.21631f, 0.21683f, 0.21735f, 0.21787f,
565 0.21839f, 0.21892f, 0.21944f, 0.21997f,
566 0.22049f, 0.22102f, 0.22155f, 0.22208f,
567 0.22261f, 0.22314f, 0.22367f, 0.22420f,
568 0.22474f, 0.22527f, 0.22581f, 0.22634f,
569 0.22688f, 0.22742f, 0.22796f, 0.22850f,
570 0.22905f, 0.22959f, 0.23013f, 0.23068f,
571 0.23123f, 0.23178f, 0.23232f, 0.23287f,
572 0.23343f, 0.23398f, 0.23453f, 0.23508f,
573 0.23564f, 0.23620f, 0.23675f, 0.23731f,
574 0.23787f, 0.23843f, 0.23899f, 0.23956f,
575 0.24012f, 0.24069f, 0.24125f, 0.24182f,
576 0.24239f, 0.24296f, 0.24353f, 0.24410f,
577 0.24468f, 0.24525f, 0.24582f, 0.24640f,
578 0.24698f, 0.24756f, 0.24814f, 0.24872f,
579 0.24931f, 0.24989f, 0.25048f, 0.25106f,
580 0.25165f, 0.25224f, 0.25283f, 0.25342f,
581 0.25401f, 0.25460f, 0.25520f, 0.25579f,
582 0.25639f, 0.25699f, 0.25759f, 0.25820f,
583 0.25880f, 0.25940f, 0.26001f, 0.26062f,
584 0.26122f, 0.26183f, 0.26244f, 0.26306f,
585 0.26367f, 0.26429f, 0.26490f, 0.26552f,
586 0.26614f, 0.26676f, 0.26738f, 0.26800f,
587 0.26863f, 0.26925f, 0.26988f, 0.27051f,
588 0.27114f, 0.27177f, 0.27240f, 0.27303f,
589 0.27367f, 0.27431f, 0.27495f, 0.27558f,
590 0.27623f, 0.27687f, 0.27751f, 0.27816f,
591 0.27881f, 0.27945f, 0.28011f, 0.28076f,
592 0.28141f, 0.28207f, 0.28272f, 0.28338f,
593 0.28404f, 0.28470f, 0.28536f, 0.28602f,
594 0.28669f, 0.28736f, 0.28802f, 0.28869f,
595 0.28937f, 0.29004f, 0.29071f, 0.29139f,
596 0.29207f, 0.29274f, 0.29342f, 0.29410f,
597 0.29479f, 0.29548f, 0.29616f, 0.29685f,
598 0.29754f, 0.29823f, 0.29893f, 0.29962f,
599 0.30032f, 0.30102f, 0.30172f, 0.30242f,
600 0.30312f, 0.30383f, 0.30453f, 0.30524f,
601 0.30595f, 0.30667f, 0.30738f, 0.30809f,
602 0.30881f, 0.30953f, 0.31025f, 0.31097f,
603 0.31170f, 0.31242f, 0.31315f, 0.31388f,
604 0.31461f, 0.31534f, 0.31608f, 0.31682f,
605 0.31755f, 0.31829f, 0.31904f, 0.31978f,
606 0.32053f, 0.32127f, 0.32202f, 0.32277f,
607 0.32353f, 0.32428f, 0.32504f, 0.32580f,
608 0.32656f, 0.32732f, 0.32808f, 0.32885f,
609 0.32962f, 0.33039f, 0.33116f, 0.33193f,
610 0.33271f, 0.33349f, 0.33427f, 0.33505f,
611 0.33583f, 0.33662f, 0.33741f, 0.33820f,
612 0.33899f, 0.33978f, 0.34058f, 0.34138f,
613 0.34218f, 0.34298f, 0.34378f, 0.34459f,
614 0.34540f, 0.34621f, 0.34702f, 0.34783f,
615 0.34865f, 0.34947f, 0.35029f, 0.35111f,
616 0.35194f, 0.35277f, 0.35360f, 0.35443f,
617 0.35526f, 0.35610f, 0.35694f, 0.35778f,
618 0.35862f, 0.35946f, 0.36032f, 0.36117f,
619 0.36202f, 0.36287f, 0.36372f, 0.36458f,
620 0.36545f, 0.36631f, 0.36718f, 0.36805f,
621 0.36891f, 0.36979f, 0.37066f, 0.37154f,
622 0.37242f, 0.37331f, 0.37419f, 0.37507f,
623 0.37596f, 0.37686f, 0.37775f, 0.37865f,
624 0.37955f, 0.38045f, 0.38136f, 0.38227f,
625 0.38317f, 0.38409f, 0.38500f, 0.38592f,
626 0.38684f, 0.38776f, 0.38869f, 0.38961f,
627 0.39055f, 0.39148f, 0.39242f, 0.39335f,
628 0.39430f, 0.39524f, 0.39619f, 0.39714f,
629 0.39809f, 0.39904f, 0.40000f, 0.40097f,
630 0.40193f, 0.40289f, 0.40386f, 0.40483f,
631 0.40581f, 0.40679f, 0.40777f, 0.40875f,
632 0.40974f, 0.41073f, 0.41172f, 0.41272f,
633 0.41372f, 0.41472f, 0.41572f, 0.41673f,
634 0.41774f, 0.41875f, 0.41977f, 0.42079f,
635 0.42181f, 0.42284f, 0.42386f, 0.42490f,
636 0.42594f, 0.42697f, 0.42801f, 0.42906f,
637 0.43011f, 0.43116f, 0.43222f, 0.43327f,
638 0.43434f, 0.43540f, 0.43647f, 0.43754f,
639 0.43862f, 0.43970f, 0.44077f, 0.44186f,
640 0.44295f, 0.44404f, 0.44514f, 0.44624f,
641 0.44734f, 0.44845f, 0.44956f, 0.45068f,
642 0.45179f, 0.45291f, 0.45404f, 0.45516f,
643 0.45630f, 0.45744f, 0.45858f, 0.45972f,
644 0.46086f, 0.46202f, 0.46318f, 0.46433f,
645 0.46550f, 0.46667f, 0.46784f, 0.46901f,
646 0.47019f, 0.47137f, 0.47256f, 0.47375f,
647 0.47495f, 0.47615f, 0.47735f, 0.47856f,
648 0.47977f, 0.48099f, 0.48222f, 0.48344f,
649 0.48467f, 0.48590f, 0.48714f, 0.48838f,
650 0.48963f, 0.49088f, 0.49213f, 0.49340f,
651 0.49466f, 0.49593f, 0.49721f, 0.49849f,
652 0.49977f, 0.50106f, 0.50236f, 0.50366f,
653 0.50496f, 0.50627f, 0.50758f, 0.50890f,
654 0.51023f, 0.51155f, 0.51289f, 0.51422f,
655 0.51556f, 0.51692f, 0.51827f, 0.51964f,
656 0.52100f, 0.52237f, 0.52374f, 0.52512f,
657 0.52651f, 0.52790f, 0.52930f, 0.53070f,
658 0.53212f, 0.53353f, 0.53495f, 0.53638f,
659 0.53781f, 0.53925f, 0.54070f, 0.54214f,
660 0.54360f, 0.54506f, 0.54653f, 0.54800f,
661 0.54949f, 0.55098f, 0.55247f, 0.55396f,
662 0.55548f, 0.55699f, 0.55851f, 0.56003f,
663 0.56156f, 0.56310f, 0.56464f, 0.56621f,
664 0.56777f, 0.56933f, 0.57091f, 0.57248f,
665 0.57407f, 0.57568f, 0.57727f, 0.57888f,
666 0.58050f, 0.58213f, 0.58376f, 0.58541f,
667 0.58705f, 0.58871f, 0.59037f, 0.59204f,
668 0.59373f, 0.59541f, 0.59712f, 0.59882f,
669 0.60052f, 0.60226f, 0.60399f, 0.60572f,
670 0.60748f, 0.60922f, 0.61099f, 0.61276f,
671 0.61455f, 0.61635f, 0.61814f, 0.61996f,
672 0.62178f, 0.62361f, 0.62545f, 0.62730f,
673 0.62917f, 0.63104f, 0.63291f, 0.63480f,
674 0.63671f, 0.63862f, 0.64054f, 0.64249f,
675 0.64443f, 0.64638f, 0.64835f, 0.65033f,
676 0.65232f, 0.65433f, 0.65633f, 0.65836f,
677 0.66041f, 0.66245f, 0.66452f, 0.66660f,
678 0.66868f, 0.67078f, 0.67290f, 0.67503f,
679 0.67717f, 0.67932f, 0.68151f, 0.68368f,
680 0.68587f, 0.68809f, 0.69033f, 0.69257f,
681 0.69482f, 0.69709f, 0.69939f, 0.70169f,
682 0.70402f, 0.70634f, 0.70869f, 0.71107f,
683 0.71346f, 0.71587f, 0.71829f, 0.72073f,
684 0.72320f, 0.72567f, 0.72818f, 0.73069f,
685 0.73323f, 0.73579f, 0.73838f, 0.74098f,
686 0.74360f, 0.74622f, 0.74890f, 0.75159f,
687 0.75429f, 0.75704f, 0.75979f, 0.76257f,
688 0.76537f, 0.76821f, 0.77109f, 0.77396f,
689 0.77688f, 0.77982f, 0.78278f, 0.78579f,
690 0.78883f, 0.79187f, 0.79498f, 0.79809f,
691 0.80127f, 0.80445f, 0.80767f, 0.81095f,
692 0.81424f, 0.81757f, 0.82094f, 0.82438f,
693 0.82782f, 0.83133f, 0.83488f, 0.83847f,
694 0.84210f, 0.84577f, 0.84951f, 0.85328f,
695 0.85713f, 0.86103f, 0.86499f, 0.86900f,
696 0.87306f, 0.87720f, 0.88139f, 0.88566f,
697 0.89000f, 0.89442f, 0.89891f, 0.90350f,
698 0.90818f, 0.91295f, 0.91780f, 0.92272f,
699 0.92780f, 0.93299f, 0.93828f, 0.94369f,
700 0.94926f, 0.95493f, 0.96082f, 0.96684f,
701 0.97305f, 0.97943f, 0.98605f, 0.99291f,
702 1.00000f
703 };
704
705 const uint32 kTableSize = sizeof (kTable ) /
706 sizeof (kTable [0]);
707
708 real32 y = (real32) x * (real32) (kTableSize - 1);
709
710 int32 index = Pin_int32 (0, (int32) y, kTableSize - 2);
711
712 real32 fract = y - (real32) index;
713
714 return kTable [index ] * (1.0f - fract) +
715 kTable [index + 1] * ( fract);
716
717 }
718
719 /*****************************************************************************/
720
721 const dng_1d_function & dng_tone_curve_acr3_default::Get ()
722 {
723
724 static dng_tone_curve_acr3_default static_dng_tone_curve_acr3_default;
725
726 return static_dng_tone_curve_acr3_default;
727
728 }
729
730 /*****************************************************************************/
731
732 class dng_render_task: public dng_filter_task
733 {
734
735 protected:
736
737 const dng_image *fSrcMask;
738
739 const dng_negative &fNegative;
740
741 const dng_render &fParams;
742
743 dng_point fSrcOffset;
744
745 dng_1d_table fZeroOffsetRamp;
746
747 dng_vector fCameraWhite;
748 dng_matrix fCameraToRGB;
749
750 AutoPtr<dng_hue_sat_map> fHueSatMap;
751
752 real64 fBaselineExposure = 0.0;
753
754 AutoPtr<dng_function_exposure_ramp> fExposureRamp;
755
756 AutoPtr<dng_hue_sat_map> fLookTable;
757
758 dng_1d_table fToneCurve;
759
760 AutoPtr<dng_line_real32> fToneCurveSlopeExtension;
761
762 dng_matrix fRGBtoFinal;
763
764 AutoPtr<dng_1d_table> fEncodeGamma;
765
766 AutoPtr<dng_1d_table> fHueSatMapEncode;
767 AutoPtr<dng_1d_table> fHueSatMapDecode;
768
769 AutoPtr<dng_1d_table> fLookTableEncode;
770 AutoPtr<dng_1d_table> fLookTableDecode;
771
772 AutoPtr<dng_memory_block> fTempBuffer [kMaxMPThreads];
773
774 AutoPtr<dng_memory_block> fMaskBuffer [kMaxMPThreads];
775
776 // RGBTables data.
777
778 dng_masked_rgb_table_render_data_sptr fRGBTablesData;
779
780 AutoPtr<dng_memory_block> fRGBTablesTempBuffer [kMaxMPThreads];
781
782 uint32 fNumTableTransformPlanes = 0;
783
784 bool fSupportOverrange = false;
785
786 public:
787
788 dng_render_task (const dng_image &srcImage,
789 const dng_image *srcMask,
790 dng_image &dstImage,
791 const dng_negative &negative,
792 const dng_render &params,
793 const dng_point &srcOffset);
794
795 virtual dng_rect SrcArea (const dng_rect &dstArea);
796
797 virtual void Start (uint32 threadCount,
798 const dng_rect &dstArea,
799 const dng_point &tileSize,
800 dng_memory_allocator *allocator,
801 dng_abort_sniffer *sniffer);
802
803 virtual void ProcessArea (uint32 threadIndex,
804 dng_pixel_buffer &srcBuffer,
805 dng_pixel_buffer &dstBuffer);
806
807 };
808
809 /*****************************************************************************/
810
811 dng_render_task::dng_render_task (const dng_image &srcImage,
812 const dng_image *srcMask,
813 dng_image &dstImage,
814 const dng_negative &negative,
815 const dng_render &params,
816 const dng_point &srcOffset)
817
818 : dng_filter_task ("dng_render_task",
819 srcImage,
820 dstImage)
821
822 , fSrcMask (srcMask )
823 , fNegative (negative )
824 , fParams (params )
825 , fSrcOffset (srcOffset)
826
827 {
828
829 fSrcPixelType = ttFloat;
830 fDstPixelType = ttFloat;
831
832 }
833
834 /*****************************************************************************/
835
836 dng_rect dng_render_task::SrcArea (const dng_rect &dstArea)
837 {
838
839 return dstArea + fSrcOffset;
840
841 }
842
843 /*****************************************************************************/
844
845 void dng_render_task::Start (uint32 threadCount,
846 const dng_rect &dstArea,
847 const dng_point &tileSize,
848 dng_memory_allocator *allocator,
849 dng_abort_sniffer *sniffer)
850 {
851
852 dng_filter_task::Start (threadCount,
853 dstArea,
854 tileSize,
855 allocator,
856 sniffer);
857
858 // Compute zero offset ramp, if any.
859
860 if (fNegative.Stage3BlackLevel ())
861 {
862
863 dng_function_zero_offset offsetFunction (fNegative.Stage3BlackLevelNormalized ());
864
865 fZeroOffsetRamp.Initialize (*allocator, offsetFunction);
866
867 }
868
869 // Compute camera space to linear ProPhoto RGB parameters.
870
871 const dng_camera_profile_id &profileID = fParams.CameraProfileID ();
872
873 dng_camera_profile profile;
874
875 bool hasProfile = false;
876
877 if (!fNegative.IsMonochrome ())
878 {
879
880 AutoPtr<dng_color_spec> spec (fNegative.MakeColorSpec (profileID));
881
882 if (fParams.WhiteXY ().IsValid ())
883 {
884
885 spec->SetWhiteXY (fParams.WhiteXY ());
886
887 }
888
889 else if (fNegative.HasCameraNeutral ())
890 {
891
892 spec->SetWhiteXY (spec->NeutralToXY (fNegative.CameraNeutral ()));
893
894 }
895
896 else if (fNegative.HasCameraWhiteXY ())
897 {
898
899 spec->SetWhiteXY (fNegative.CameraWhiteXY ());
900
901 }
902
903 else
904 {
905
906 spec->SetWhiteXY (D55_xy_coord ());
907
908 }
909
910 fCameraWhite = spec->CameraWhite ();
911
912 fCameraToRGB = dng_space_ProPhoto::Get ().MatrixFromPCS () *
913 spec->CameraToPCS ();
914
915 // Find Hue/Sat table, if any.
916
917 hasProfile = fNegative.GetProfileByID (profileID, profile);
918
919 if (hasProfile)
920 {
921
922 fSupportOverrange = profile.IsHDR ();
923
924 fHueSatMap.Reset (profile.HueSatMapForWhite (spec->WhiteXY ()));
925
926 if (profile.HasLookTable ())
927 {
928
929 fLookTable.Reset (new dng_hue_sat_map (profile.LookTable ()));
930
931 }
932
933 if (profile.HueSatMapEncoding () != encoding_Linear)
934 {
935
936 BuildHueSatMapEncodingTable (*allocator,
937 profile.HueSatMapEncoding (),
938 fHueSatMapEncode,
939 fHueSatMapDecode,
940 false);
941
942 }
943
944 if (profile.LookTableEncoding () != encoding_Linear)
945 {
946
947 BuildHueSatMapEncodingTable (*allocator,
948 profile.LookTableEncoding (),
949 fLookTableEncode,
950 fLookTableDecode,
951 false);
952
953 }
954
955 }
956
957 }
958
959 else
960 {
961
962 // Monochrome negatives.
963
964 hasProfile = fNegative.GetProfileByID (profileID, profile);
965
966 if (hasProfile)
967 {
968
969 fSupportOverrange = profile.IsHDR ();
970
971 }
972
973 }
974
975 // Compute exposure/shadows ramp.
976
977 fBaselineExposure = (fNegative.TotalBaselineExposure (profileID) -
978 (log (fNegative.Stage3Gain ()) / log (2.0)));
979
980 real64 exposure = fParams.Exposure () + fBaselineExposure;
981
982 {
983
984 real64 white = 1.0 / pow (2.0, Max_real64 (0.0, exposure));
985
986 real64 black = fParams.Shadows () *
987 fNegative.ShadowScale () *
988 fNegative.Stage3Gain () *
989 0.001;
990
991 black = Min_real64 (black, 0.99 * white);
992
993 fExposureRamp.Reset
994 (new dng_function_exposure_ramp (white,
995 black,
996 black,
997 fSupportOverrange));
998
999 }
1000
1001 // Compute tone curve.
1002
1003 {
1004
1005 // If there is any negative exposure compenation to perform
1006 // (beyond what the camera provides for with its baseline exposure),
1007 // we fake this by darkening the tone curve.
1008
1009 dng_function_exposure_tone exposureTone (exposure);
1010
1011 dng_1d_concatenate totalTone (exposureTone,
1012 fParams.ToneCurve ());
1013
1014 fToneCurve.Initialize (*allocator, totalTone);
1015
1016 if (fSupportOverrange)
1017 {
1018
1019 fToneCurveSlopeExtension.Reset (new dng_line_real32 (0.0f, 0.0f,
1020 1.0f, 1.0f));
1021
1022 fToneCurveSlopeExtension->fX = 1.0f;
1023
1024 fToneCurveSlopeExtension->fY = real32 (totalTone.Evaluate (1.0));
1025
1026 const real64 dx = 0.001;
1027
1028 real64 dy = totalTone.Evaluate (1.0) - totalTone.Evaluate (1.0 - dx);
1029
1030 fToneCurveSlopeExtension->fSlope = real32 (dy / dx);
1031
1032 }
1033
1034 }
1035
1036 // Compute linear ProPhoto RGB to final space parameters.
1037
1038 {
1039
1040 const dng_color_space &finalSpace = fParams.FinalSpace (&profile);
1041
1042 fRGBtoFinal = finalSpace.MatrixFromPCS () *
1043 dng_space_ProPhoto::Get ().MatrixToPCS ();
1044
1045 if (!finalSpace.GammaFunction ().IsIdentity ())
1046 {
1047
1048 fEncodeGamma.Reset (new dng_1d_table);
1049
1050 fEncodeGamma->Initialize (*allocator,
1051 finalSpace.GammaFunction ());
1052
1053 }
1054
1055 }
1056
1057 // Allocate temp buffer to hold one row of RGB data.
1058
1059 uint32 tempBufferSize = 0;
1060
1061 if (!SafeUint32Mult (tileSize.h, (uint32) sizeof (real32), &tempBufferSize) ||
1062 !SafeUint32Mult (tempBufferSize, 3, &tempBufferSize))
1063 {
1064
1065 ThrowOverflow ("Arithmetic overflow computing buffer size.");
1066
1067 }
1068
1069 for (uint32 threadIndex = 0; threadIndex < threadCount; threadIndex++)
1070 {
1071
1072 fTempBuffer [threadIndex] . Reset (allocator->Allocate (tempBufferSize));
1073
1074 }
1075
1076 // Allocate mask buffer to hold one tile of mask data, if needed.
1077
1078 if (fSrcMask)
1079 {
1080
1081 uint32 maskBufferSize = 0;
1082
1083 if (!SafeUint32Mult (tileSize.h, tileSize.v, &maskBufferSize) ||
1084 !SafeUint32Mult (maskBufferSize, (uint32) sizeof (real32), &maskBufferSize))
1085 {
1086
1087 ThrowOverflow ("Arithmetic overflow computing buffer size.");
1088
1089 }
1090
1091 for (uint32 threadIndex = 0; threadIndex < threadCount; threadIndex++)
1092 {
1093
1094 fMaskBuffer [threadIndex] . Reset (allocator->Allocate (maskBufferSize));
1095
1096 }
1097
1098 }
1099
1100 // Prepare RGBTables data, if needed.
1101
1102 if (hasProfile &&
1103 profile.HasMaskedRGBTables () &&
1104 !profile.MaskedRGBTables ().IsNOP ())
1105 {
1106
1107 auto &data = fRGBTablesData;
1108
1109 data.reset (new dng_masked_rgb_table_render_data);
1110
1111 data->Initialize (fNegative, profile);
1112
1113 if (data->IsNOP ())
1114 data.reset ();
1115
1116 else
1117 {
1118
1119 dng_host host (allocator,
1120 sniffer);
1121
1122 data->PrepareRGBtoRGBTableData (host);
1123
1124 // Add space for masked (non-background) table transform results.
1125
1126 uint32 numTableTransforms = uint32 (data->fMaskedTables.size ());
1127
1128 if (numTableTransforms > 0)
1129 {
1130
1131 // With the Weighted Sum method, we need 4 x the # of
1132 // transforms: 3 planes for the RGB table transform result, 1
1133 // plane for the semantic mask.
1134
1135 // With the Sequential method, we need just 1 plane per
1136 // transform to hold the semantic mask or the
1137 // dynamically-computed background mask.
1138
1139 fNumTableTransformPlanes = data->fUseSequentialMethod
1140 ? SafeUint32Mult (1, numTableTransforms)
1141 : SafeUint32Mult (4, numTableTransforms);
1142
1143 uint32 tableTransformBufferSize =
1144 SafeUint32Mult (tileSize.h,
1145 tileSize.v,
1146 fNumTableTransformPlanes,
1147 uint32 (sizeof (real32)));
1148
1149 for (uint32 threadIndex = 0;
1150 threadIndex < threadCount;
1151 threadIndex++)
1152 {
1153
1154 fRGBTablesTempBuffer [threadIndex] . Reset
1155 (allocator->Allocate (tableTransformBufferSize));
1156
1157 }
1158
1159 }
1160
1161 }
1162
1163 }
1164
1165 }
1166
1167 /*****************************************************************************/
1168
1169 static void ProcessRGBTables (const dng_masked_rgb_table_render_data &data,
1170 dng_pixel_buffer &buffer,
1171 dng_pixel_buffer &tempBuffer,
1172 const bool needsOverrange)
1173 {
1174
1175 const dng_rect dstArea = buffer.fArea;
1176
1177 // Special case: If there is only a background table, then just process
1178 // that and be done.
1179
1180 if (data.fMaskedTables.empty ())
1181 {
1182
1183 DNG_REQUIRE (data.fBackgroundTableData.Get (),
1184 "missing fBackgroundTableData");
1185
1186 data.fBackgroundTableData->Process_32 (buffer,
1187 nullptr, // no mask
1188 0, // no mask
1189 dstArea,
1190 0,
1191 needsOverrange);
1192
1193 return;
1194
1195 }
1196
1197 // There is at least 1 masked table. There may or may not be a background
1198 // table.
1199
1200 // Deal with sequential method.
1201
1202 if (data.fUseSequentialMethod)
1203 {
1204
1205 // Apply transforms sequentially using the incoming buffer in-place.
1206
1207 for (size_t i = 0; i < data.fMaskedTableData.size (); i++)
1208 {
1209
1210 auto &innerData = *data.fMaskedTableData [i];
1211
1212 innerData.Process_32 (buffer,
1213 &tempBuffer, // mask buffer
1214 (int32) i, // mask plane
1215 dstArea,
1216 0, // rgb start plane
1217 needsOverrange);
1218
1219 } // all tables
1220
1221 } // sequential method
1222
1223 else
1224 {
1225
1226 // Else we are using the Weighted Sum Method.
1227
1228 // Walk thru all masked tables.
1229
1230 for (size_t i = 0; i < data.fMaskedTableData.size (); i++)
1231 {
1232
1233 auto &innerData = *data.fMaskedTableData [i];
1234
1235 // The plane layout is:
1236
1237 // plane 0: transform 0 red
1238 // plane 1: transform 0 green
1239 // plane 2: transform 0 blue
1240 // plane 3: transform 0 semantic mask (alpha)
1241 //
1242 // plane 4: transform 1 red
1243 // plane 5: transform 1 green
1244 // plane 6: transform 1 blue
1245 // plane 7: transform 1 semantic mask (alpha)
1246 //
1247 // etc.
1248
1249 const uint32 dstPlane = 4 * (uint32) i;
1250
1251 // Copy source RGB to temp buffer, then apply table.
1252
1253 {
1254
1255 tempBuffer.CopyArea (buffer,
1256 dstArea,
1257 0, // srcPlane
1258 dstPlane,
1259 3); // planes
1260
1261 innerData.Process_32 (tempBuffer,
1262 nullptr, // no mask
1263 0, // no mask
1264 dstArea,
1265 dstPlane,
1266 needsOverrange);
1267
1268 }
1269
1270 }
1271
1272 // If there is a background table, then do it in-place on the incoming
1273 // buffer. This will overwrite the source values, which is ok because we
1274 // no longer need them.
1275
1276 if (data.fBackgroundTableData.Get ())
1277 {
1278
1279 data.fBackgroundTableData->Process_32 (buffer,
1280 nullptr, // no mask
1281 0, // no mask
1282 dstArea,
1283 0,
1284 needsOverrange);
1285
1286 }
1287
1288 // Composite.
1289
1290 const uint32 numTableTransforms = uint32 (data.fMaskedTables.size ());
1291
1292 RefMaskedRGBTables32 (buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, 0),
1293 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, 1),
1294 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, 2),
1295 tempBuffer.ConstPixel_real32 (dstArea.t, dstArea.l, 0),
1296 tempBuffer.ConstPixel_real32 (dstArea.t, dstArea.l, 1),
1297 tempBuffer.ConstPixel_real32 (dstArea.t, dstArea.l, 2),
1298 tempBuffer.ConstPixel_real32 (dstArea.t, dstArea.l, 3),
1299 numTableTransforms,
1300 buffer.RowStep (),
1301 tempBuffer.RowStep (),
1302 tempBuffer.PlaneStep (),
1303 dstArea.H (),
1304 dstArea.W ());
1305
1306 } // Weighted Sum Method
1307
1308 }
1309
1310 /*****************************************************************************/
1311
1312 static void DoBaseline1DFunction (const real32 *sPtr,
1313 real32 *dPtr,
1314 uint32 count,
1315 const dng_1d_function &function,
1316 const bool supportOverrange)
1317 {
1318
1319 if (supportOverrange)
1320 {
1321
1322 for (uint32 col = 0; col < count; col++)
1323 {
1324
1325 real32 x = sPtr [col];
1326
1327 real32 y = real32 (function.Evaluate (real64 (x)));
1328
1329 dPtr [col] = y;
1330
1331 }
1332
1333 }
1334
1335 else
1336 {
1337
1338 for (uint32 col = 0; col < count; col++)
1339 {
1340
1341 real32 x = Pin_real32 (0.0f, sPtr [col], 1.0f);
1342
1343 real32 y = real32 (function.Evaluate (real64 (x)));
1344
1345 dPtr [col] = Pin_real32 (0.0f, y, 1.0f);
1346
1347 }
1348
1349 }
1350
1351 }
1352
1353 /*****************************************************************************/
1354
1355 DNG_ALWAYS_INLINE real32 ApplyCurveOverrange (const dng_1d_table &table,
1356 const dng_line_real32 &slopeExtensionFunc,
1357 real32 x)
1358 {
1359
1360 if (x <= 1.0f)
1361 return table.Interpolate (Max_real32 (x, 0.0f));
1362
1363 return slopeExtensionFunc.Evaluate (x);
1364
1365 }
1366
1367 /*****************************************************************************/
1368
1369 static void DoBaselineRGBToneOverrange (const real32 *sPtrR,
1370 const real32 *sPtrG,
1371 const real32 *sPtrB,
1372 real32 *dPtrR,
1373 real32 *dPtrG,
1374 real32 *dPtrB,
1375 uint32 count,
1376 const dng_1d_table &table,
1377 const dng_line_real32 &slopeExtensionFunc)
1378 {
1379
1380 for (uint32 col = 0; col < count; col++)
1381 {
1382
1383 real32 r = sPtrR [col];
1384 real32 g = sPtrG [col];
1385 real32 b = sPtrB [col];
1386
1387 r = Max_real32 (r, 0.0f);
1388 g = Max_real32 (g, 0.0f);
1389 b = Max_real32 (b, 0.0f);
1390
1391 r = EncodeOverrange (r);
1392 g = EncodeOverrange (g);
1393 b = EncodeOverrange (b);
1394
1395 real32 rr;
1396 real32 gg;
1397 real32 bb;
1398
1399 #define RGBTone(r, g, b, rr, gg, bb) \
1400 { \
1401 \
1402 DNG_ASSERT (r >= g && g >= b && r > b, "Logic Error RGBTone"); \
1403 \
1404 rr = ApplyCurveOverrange (table, slopeExtensionFunc, r); \
1405 bb = ApplyCurveOverrange (table, slopeExtensionFunc, b); \
1406 \
1407 gg = bb + ((rr - bb) * (g - b) / (r - b)); \
1408 \
1409 }
1410
1411 if (r >= g)
1412 {
1413
1414 if (g > b)
1415 {
1416
1417 // Case 1: r >= g > b
1418
1419 RGBTone (r, g, b, rr, gg, bb);
1420
1421 }
1422
1423 else if (b > r)
1424 {
1425
1426 // Case 2: b > r >= g
1427
1428 RGBTone (b, r, g, bb, rr, gg);
1429
1430 }
1431
1432 else if (b > g)
1433 {
1434
1435 // Case 3: r >= b > g
1436
1437 RGBTone (r, b, g, rr, bb, gg);
1438
1439 }
1440
1441 else
1442 {
1443
1444 // Case 4: r >= g == b
1445
1446 DNG_ASSERT (r >= g && g == b, "Logic Error 2");
1447
1448 rr = table.Interpolate (r);
1449 gg = table.Interpolate (g);
1450 bb = gg;
1451
1452 }
1453
1454 }
1455
1456 else
1457 {
1458
1459 if (r >= b)
1460 {
1461
1462 // Case 5: g > r >= b
1463
1464 RGBTone (g, r, b, gg, rr, bb);
1465
1466 }
1467
1468 else if (b > g)
1469 {
1470
1471 // Case 6: b > g > r
1472
1473 RGBTone (b, g, r, bb, gg, rr);
1474
1475 }
1476
1477 else
1478 {
1479
1480 // Case 7: g >= b > r
1481
1482 RGBTone (g, b, r, gg, bb, rr);
1483
1484 }
1485
1486 }
1487
1488 #undef RGBTone
1489
1490 rr = DecodeOverrange (rr);
1491 gg = DecodeOverrange (gg);
1492 bb = DecodeOverrange (bb);
1493
1494 dPtrR [col] = rr;
1495 dPtrG [col] = gg;
1496 dPtrB [col] = bb;
1497
1498 }
1499
1500 }
1501
1502 /*****************************************************************************/
1503
1504 void dng_render_task::ProcessArea (uint32 threadIndex,
1505 dng_pixel_buffer &srcBuffer,
1506 dng_pixel_buffer &dstBuffer)
1507 {
1508
1509 dng_rect srcArea = srcBuffer.fArea;
1510 dng_rect dstArea = dstBuffer.fArea;
1511
1512 uint32 srcCols = srcArea.W ();
1513
1514 real32 *tPtrR = fTempBuffer [threadIndex]->Buffer_real32 ();
1515
1516 real32 *tPtrG = tPtrR + srcCols;
1517 real32 *tPtrB = tPtrG + srcCols;
1518
1519 dng_pixel_buffer maskBuffer;
1520
1521 if (fSrcMask)
1522 {
1523
1524 maskBuffer.fArea = srcArea;
1525 maskBuffer.fPlane = 0;
1526 maskBuffer.fPlanes = 1;
1527 maskBuffer.fRowStep = srcArea.W ();
1528 maskBuffer.fColStep = 1;
1529 maskBuffer.fPlaneStep = 0;
1530 maskBuffer.fPixelType = ttFloat;
1531 maskBuffer.fPixelSize = sizeof (real32);
1532 maskBuffer.fData = fMaskBuffer [threadIndex]->Buffer_real32 ();
1533 maskBuffer.fDirty = true;
1534
1535 fSrcMask->Get (maskBuffer);
1536
1537 }
1538
1539 // Prepare RGBTables data.
1540
1541 dng_pixel_buffer tempRGBTablesBuffer;
1542
1543 if (fRGBTablesData && !fRGBTablesData->fMaskedTableData.empty ())
1544 {
1545
1546 // Set up temporary pixel buffer to hold mask and transformed data.
1547
1548 auto &buffer = tempRGBTablesBuffer;
1549
1550 buffer.fArea = srcArea;
1551
1552 buffer.fPlanes = fNumTableTransformPlanes;
1553
1554 buffer.fColStep = 1;
1555 buffer.fPlaneStep = int32 (buffer.fArea.W ());
1556 buffer.fRowStep = SafeInt32Mult (buffer.fPlaneStep,
1557 int32 (buffer.fPlanes));
1558
1559 buffer.fPixelType = ttFloat;
1560 buffer.fPixelSize = TagTypeSize (buffer.fPixelType);
1561
1562 buffer.fData = (void *) fRGBTablesTempBuffer [threadIndex]->Buffer ();
1563
1564 // Load all the masks.
1565
1566 auto &data = *fRGBTablesData;
1567
1568 if (data.fUseSequentialMethod)
1569 {
1570
1571 // Load all the masks into the temp buffer.
1572 //
1573 // 1st mask goes into plane 0, 2nd mask goes into plane 1, etc.
1574 //
1575 // If there is a background mask, the corresponding fMaskTables
1576 // entry's mask image will be nullptr, and we will leave its
1577 // corresponding plane in the temp buffer uninitialized for now
1578 // (and just remember which index was the background index). Note
1579 // that there can be at most one background mask.
1580 //
1581 // Efficiency note: in the special case of no background table, it
1582 // is not necessary to load all masks at once. It suffices to load
1583 // just one at a time when processing the corresponding table.
1584 // However, for ease of implementation, we preload all masks to
1585 // faciliate computing the background mask, if any.
1586
1587 int32 backgroundIndex = -1; // start by assuming no bg mask
1588
1589 const int32 numTransforms = (int32) data.fMaskedTableData.size ();
1590
1591 for (int32 i = 0; i < numTransforms; i++)
1592 {
1593
1594 const int32 dstMaskPlane = i;
1595
1596 dng_pixel_buffer tempMaskBuffer = buffer;
1597
1598 tempMaskBuffer.fData = tempMaskBuffer.DirtyPixel (dstArea.t,
1599 dstArea.l,
1600 dstMaskPlane);
1601
1602 tempMaskBuffer.fPlanes = 1;
1603
1604 // If there is a semantic mask for this table, load it into the
1605 // 'ith' plane of the temp buffer.
1606
1607 auto &maskImagePtr = data.fMaskedTables [i].second.fMask;
1608
1609 if (maskImagePtr)
1610 maskImagePtr->Get (tempMaskBuffer,
1611 dng_image::edge_repeat);
1612
1613 // Otherwise, it is a background table, so just remember which
1614 // index it was. Note there can be at most 1 of these.
1615
1616 else
1617 backgroundIndex = i;
1618
1619 }
1620
1621 // If we have a background mask, then we need to compute the
1622 // background weight from the others masks that we already loaded.
1623
1624 if (backgroundIndex >= 0)
1625 {
1626
1627 const int32 cols = (int32) dstArea.W ();
1628
1629 const int32 mPlaneStep = buffer.PlaneStep ();
1630
1631 for (int32 row = dstArea.t; row < dstArea.b; row++)
1632 {
1633
1634 // Initialize mask pointer to first plane for this row.
1635
1636 const real32 *mPtr = buffer.ConstPixel_real32 (row,
1637 dstArea.l,
1638 0);
1639
1640 // We will write the result into the background mask plane.
1641
1642 real32 *dPtr = buffer.DirtyPixel_real32 (row,
1643 dstArea.l,
1644 backgroundIndex);
1645
1646 for (int32 j = 0; j < cols; j++)
1647 {
1648
1649 real32 mSum = 0.0f;
1650
1651 // Accumulate mask weights for all masks except the
1652 // background.
1653
1654 for (int32 t = 0; t < numTransforms; t++)
1655 {
1656
1657 if (t != backgroundIndex)
1658 {
1659
1660 real32 m = mPtr [j + (t * mPlaneStep)];
1661
1662 mSum += m;
1663
1664 }
1665
1666 } // # of transforms
1667
1668 // Compute the background weight.
1669
1670 real32 bgWeight = 1.0f - Min_real32 (1.0f, mSum);
1671
1672 dPtr [j] = bgWeight;
1673
1674 } // cols
1675
1676 } // rows
1677
1678 } // if there is a background mask
1679
1680 } // sequential method
1681
1682 else
1683 {
1684
1685 // Weighted Sum Method.
1686
1687 // Walk thru all masked tables.
1688
1689 for (size_t i = 0; i < data.fMaskedTableData.size (); i++)
1690 {
1691
1692 // Get semantic mask.
1693
1694 // The plane layout is:
1695
1696 // plane 0: transform 0 red
1697 // plane 1: transform 0 green
1698 // plane 2: transform 0 blue
1699 // plane 3: transform 0 semantic mask (alpha)
1700 //
1701 // plane 4: transform 1 red
1702 // plane 5: transform 1 green
1703 // plane 6: transform 1 blue
1704 // plane 7: transform 1 semantic mask (alpha)
1705 //
1706 // etc.
1707
1708 const uint32 dstPlane = 4 * (uint32) i;
1709
1710 const uint32 dstMaskPlane = dstPlane + 3;
1711
1712 dng_pixel_buffer tempMaskBuffer = tempRGBTablesBuffer;
1713
1714 tempMaskBuffer.fData = tempMaskBuffer.DirtyPixel (dstArea.t,
1715 dstArea.l,
1716 dstMaskPlane);
1717
1718 tempMaskBuffer.fPlanes = 1;
1719
1720 data.fMaskedTables [i].second.fMask->Get (tempMaskBuffer,
1721 dng_image::edge_repeat);
1722
1723 } // all masked tables
1724
1725 } // weighted sum method
1726
1727 } // RGBTables
1728
1729 // Process each row of the image.
1730
1731 for (int32 srcRow = srcArea.t; srcRow < srcArea.b; srcRow++)
1732 {
1733
1734 if (fNegative.Stage3BlackLevel ())
1735 {
1736
1737 for (uint32 plane = 0; plane < fSrcPlanes; plane++)
1738 {
1739
1740 real32 *sPtr = (real32 *)
1741 srcBuffer.DirtyPixel (srcRow,
1742 srcArea.l,
1743 plane);
1744
1745 DoBaseline1DTable (sPtr,
1746 sPtr,
1747 srcCols,
1748 fZeroOffsetRamp);
1749
1750 }
1751
1752 }
1753
1754 // First convert from camera native space to linear PhotoRGB,
1755 // applying the white balance and camera profile.
1756
1757 {
1758
1759 const real32 *sPtrA = (const real32 *)
1760 srcBuffer.ConstPixel (srcRow,
1761 srcArea.l,
1762 0);
1763
1764 if (fSrcPlanes == 1)
1765 {
1766
1767 // For monochrome cameras, this just requires copying
1768 // the data into all three color channels.
1769
1770 DoCopyBytes (sPtrA, tPtrR, srcCols * (uint32) sizeof (real32));
1771 DoCopyBytes (sPtrA, tPtrG, srcCols * (uint32) sizeof (real32));
1772 DoCopyBytes (sPtrA, tPtrB, srcCols * (uint32) sizeof (real32));
1773
1774 }
1775
1776 else
1777 {
1778
1779 const real32 *sPtrB = sPtrA + srcBuffer.fPlaneStep;
1780 const real32 *sPtrC = sPtrB + srcBuffer.fPlaneStep;
1781
1782 if (fSrcPlanes == 3)
1783 {
1784
1785 DoBaselineABCtoRGB (sPtrA,
1786 sPtrB,
1787 sPtrC,
1788 tPtrR,
1789 tPtrG,
1790 tPtrB,
1791 srcCols,
1792 fCameraWhite,
1793 fCameraToRGB);
1794
1795 }
1796
1797 else
1798 {
1799
1800 const real32 *sPtrD = sPtrC + srcBuffer.fPlaneStep;
1801
1802 DoBaselineABCDtoRGB (sPtrA,
1803 sPtrB,
1804 sPtrC,
1805 sPtrD,
1806 tPtrR,
1807 tPtrG,
1808 tPtrB,
1809 srcCols,
1810 fCameraWhite,
1811 fCameraToRGB);
1812
1813 }
1814
1815 // Apply Hue/Sat map, if any.
1816
1817 if (fHueSatMap.Get ())
1818 {
1819
1820 DoBaselineHueSatMap (tPtrR,
1821 tPtrG,
1822 tPtrB,
1823 tPtrR,
1824 tPtrG,
1825 tPtrB,
1826 srcCols,
1827 *fHueSatMap.Get (),
1828 fHueSatMapEncode.Get (),
1829 fHueSatMapDecode.Get (),
1830 fSupportOverrange);
1831
1832 }
1833
1834 }
1835
1836 }
1837
1838 // Apply ProfileGainTableMap / ProfileGainTableMap2.
1839 //
1840 // This step should be normally be done after applying
1841 // BaselineExposure. In this sample render pipeline, the
1842 // BaselineExposure step is combined with other steps (such as black
1843 // subtraction) into the exposure curve. Therefore, we choose to apply
1844 // the ProfileGainTableMap before the exposure adjustment and obtain
1845 // the correct result by effectively scaling the MapInputWeights
1846 // parameter by the baseline exposure.
1847
1848 // Select which ProfileGainTableMap to use.
1849
1850 std::shared_ptr<const dng_gain_table_map> pgtm;
1851
1852 // If the profile has a ProfileGainTableMap, then just use that.
1853
1854 const dng_camera_profile_id &profileID = fParams.CameraProfileID ();
1855
1856 dng_camera_profile profile;
1857
1858 if (fNegative.GetProfileByID (profileID, profile) &&
1859 profile.HasProfileGainTableMap ())
1860 {
1861
1862 pgtm = profile.ShareProfileGainTableMap ();
1863
1864 }
1865
1866 // Otherwise fall back to the PGTM attached to the negative itself,
1867 // which corresponds to either ProfileGainTableMap2 in IFD 0 or
1868 // ProfileGainTableMap in Raw IFD.
1869
1870 else if (fNegative.HasProfileGainTableMap ())
1871 {
1872
1873 pgtm = fNegative.ShareProfileGainTableMap ();
1874
1875 }
1876
1877 if (pgtm)
1878 {
1879
1880 const dng_rect activeArea = fNegative.Stage3Image ()->Bounds ();
1881
1882 const real32 exposureWeightGain = (real32) pow (2.0, fBaselineExposure);
1883
1884 DoBaselineProfileGainTableMap (tPtrR, // src
1885 tPtrG,
1886 tPtrB,
1887 tPtrR, // dst
1888 tPtrG,
1889 tPtrB,
1890 srcCols, // columns
1891 srcRow, // top of tile
1892 srcArea.l, // left of tile
1893 activeArea,
1894 exposureWeightGain,
1895 *pgtm,
1896 fSupportOverrange);
1897
1898 }
1899
1900 // Apply exposure curve.
1901
1902 if (fExposureRamp.Get ())
1903 {
1904
1905 DoBaseline1DFunction (tPtrR,
1906 tPtrR,
1907 srcCols,
1908 *fExposureRamp,
1909 fSupportOverrange);
1910
1911 DoBaseline1DFunction (tPtrG,
1912 tPtrG,
1913 srcCols,
1914 *fExposureRamp,
1915 fSupportOverrange);
1916
1917 DoBaseline1DFunction (tPtrB,
1918 tPtrB,
1919 srcCols,
1920 *fExposureRamp,
1921 fSupportOverrange);
1922
1923 }
1924
1925 // Apply look table, if any.
1926
1927 if (fLookTable.Get ())
1928 {
1929
1930 DoBaselineHueSatMap (tPtrR,
1931 tPtrG,
1932 tPtrB,
1933 tPtrR,
1934 tPtrG,
1935 tPtrB,
1936 srcCols,
1937 *fLookTable.Get (),
1938 fLookTableEncode.Get (),
1939 fLookTableDecode.Get (),
1940 fSupportOverrange);
1941
1942 }
1943
1944 // Apply baseline tone curve.
1945
1946 if (fToneCurveSlopeExtension.Get ())
1947 DoBaselineRGBToneOverrange (tPtrR,
1948 tPtrG,
1949 tPtrB,
1950 tPtrR,
1951 tPtrG,
1952 tPtrB,
1953 srcCols,
1954 fToneCurve,
1955 *fToneCurveSlopeExtension);
1956
1957 else
1958 DoBaselineRGBTone (tPtrR,
1959 tPtrG,
1960 tPtrB,
1961 tPtrR,
1962 tPtrG,
1963 tPtrB,
1964 srcCols,
1965 fToneCurve);
1966
1967 // Apply RGBTables, if any.
1968
1969 if (fRGBTablesData)
1970 {
1971
1972 dng_rect dstRowArea (srcRow,
1973 srcArea.l,
1974 srcRow + 1,
1975 srcArea.r);
1976
1977 dng_pixel_buffer buffer;
1978
1979 buffer.fArea = dstRowArea;
1980
1981 buffer.fPlanes = 3;
1982
1983 buffer.fColStep = 1;
1984 buffer.fPlaneStep = int32 (ptrdiff_t (tPtrG - tPtrR));
1985
1986 buffer.fPixelType = ttFloat;
1987 buffer.fPixelSize = TagTypeSize (buffer.fPixelType);
1988
1989 buffer.fData = (void *) tPtrR;
1990
1991 ProcessRGBTables (*fRGBTablesData,
1992 buffer,
1993 tempRGBTablesBuffer,
1994 fSupportOverrange);
1995
1996 }
1997
1998 // Convert to final color space.
1999
2000 int32 dstRow = srcRow + (dstArea.t - srcArea.t);
2001
2002 if (fDstPlanes == 1)
2003 {
2004
2005 real32 *dPtrG = dstBuffer.DirtyPixel_real32 (dstRow,
2006 dstArea.l,
2007 0);
2008
2009 DoBaselineRGBtoGray (tPtrR,
2010 tPtrG,
2011 tPtrB,
2012 dPtrG,
2013 srcCols,
2014 fRGBtoFinal,
2015 fSupportOverrange);
2016
2017 if (fEncodeGamma.Get ())
2018 DoBaseline1DTable (dPtrG,
2019 dPtrG,
2020 srcCols,
2021 *fEncodeGamma);
2022
2023 }
2024
2025 else
2026 {
2027
2028 real32 *dPtrR = dstBuffer.DirtyPixel_real32 (dstRow,
2029 dstArea.l,
2030 0);
2031
2032 real32 *dPtrG = dPtrR + dstBuffer.fPlaneStep;
2033 real32 *dPtrB = dPtrG + dstBuffer.fPlaneStep;
2034
2035 DoBaselineRGBtoRGB (tPtrR,
2036 tPtrG,
2037 tPtrB,
2038 dPtrR,
2039 dPtrG,
2040 dPtrB,
2041 srcCols,
2042 fRGBtoFinal,
2043 fSupportOverrange);
2044
2045 if (fEncodeGamma.Get ())
2046 {
2047
2048 DoBaseline1DTable (dPtrR,
2049 dPtrR,
2050 srcCols,
2051 *fEncodeGamma);
2052
2053 DoBaseline1DTable (dPtrG,
2054 dPtrG,
2055 srcCols,
2056 *fEncodeGamma);
2057
2058 DoBaseline1DTable (dPtrB,
2059 dPtrB,
2060 srcCols,
2061 *fEncodeGamma);
2062
2063 }
2064
2065 }
2066
2067 if (fSrcMask)
2068 {
2069
2070 const real32 *mPtr = maskBuffer.ConstPixel_real32 (srcRow,
2071 srcArea.l,
2072 0);
2073
2074 for (uint32 dstPlane = 0; dstPlane < fDstPlanes; dstPlane++)
2075 {
2076
2077 real32 *dPtr = dstBuffer.DirtyPixel_real32 (dstRow,
2078 dstArea.l,
2079 dstPlane);
2080
2081 for (uint32 col = 0; col < srcCols; col++)
2082 {
2083
2084 // White Matte
2085
2086 dPtr [col] = 1.0f - (1.0f - dPtr [col]) * mPtr [col];
2087
2088 }
2089
2090 }
2091
2092 }
2093
2094 } // each row
2095
2096 }
2097
2098 /*****************************************************************************/
2099
2100 dng_render::dng_render (dng_host &host,
2101 const dng_negative &negative)
2102
2103 : fHost (host)
2104 , fNegative (negative)
2105
2106 , fWhiteXY ()
2107
2108 , fExposure (0.0)
2109 , fShadows (5.0)
2110
2111 , fToneCurve (&dng_tone_curve_acr3_default::Get ())
2112
2113 , fFinalSpace (&dng_space_sRGB::Get ())
2114 , fFinalPixelType (ttByte)
2115
2116 , fMaximumSize (0)
2117
2118 , fProfileToneCurve ()
2119
2120 {
2121
2122 }
2123
2124 /*****************************************************************************/
2125
2126 dng_image * dng_render::Render ()
2127 {
2128
2129 // Switch to NOP default parameters for non-scene referred data.
2130
2131 if (fNegative.IsOutputReferred ())
2132 {
2133
2134 fShadows = 0.0;
2135
2136 fToneCurve = &dng_1d_identity::Get ();
2137
2138 }
2139
2140 // Use default tone curve from profile if any.
2141
2142 dng_camera_profile profile;
2143
2144 if (fNegative.GetProfileByID (CameraProfileID (),
2145 profile))
2146 {
2147
2148 if (profile.ToneCurve ().IsValid ())
2149 {
2150
2151 fProfileToneCurve.Reset (new dng_spline_solver);
2152
2153 profile.ToneCurve ().Solve (*fProfileToneCurve.Get ());
2154
2155 fToneCurve = fProfileToneCurve.Get ();
2156
2157 }
2158
2159 // Turn off default shadow mapping if requested by profile.
2160
2161 if (profile.DefaultBlackRender () == defaultBlackRender_None)
2162 {
2163
2164 fShadows = 0.0;
2165
2166 }
2167
2168 }
2169
2170 const dng_image *srcImage = fNegative.Stage3Image ();
2171
2172 const dng_image *srcMask = fNegative.TransparencyMask ();
2173
2174 dng_rect srcBounds = fNegative.DefaultCropArea ();
2175
2176 dng_point dstSize;
2177
2178 dstSize.h = fNegative.DefaultFinalWidth ();
2179 dstSize.v = fNegative.DefaultFinalHeight ();
2180
2181 if (MaximumSize ())
2182 {
2183
2184 if (Max_uint32 (dstSize.h, dstSize.v) > MaximumSize ())
2185 {
2186
2187 real64 ratio = fNegative.AspectRatio ();
2188
2189 if (ratio >= 1.0)
2190 {
2191 dstSize.h = MaximumSize ();
2192 dstSize.v = Max_uint32 (1, Round_uint32 (dstSize.h / ratio));
2193 }
2194
2195 else
2196 {
2197 dstSize.v = MaximumSize ();
2198 dstSize.h = Max_uint32 (1, Round_uint32 (dstSize.v * ratio));
2199 }
2200
2201 }
2202
2203 }
2204
2205 AutoPtr<dng_image> tempImage;
2206
2207 AutoPtr<dng_image> tempMask;
2208
2209 if (srcBounds.Size () != dstSize)
2210 {
2211
2212 tempImage.Reset (fHost.Make_dng_image (dstSize,
2213 srcImage->Planes (),
2214 srcImage->PixelType ()));
2215
2216 ResampleImage (fHost,
2217 *srcImage,
2218 *tempImage.Get (),
2219 srcBounds,
2220 tempImage->Bounds (),
2221 dng_resample_bicubic::Get ());
2222
2223 if (srcMask != NULL)
2224 {
2225
2226 tempMask.Reset (fHost.Make_dng_image (dstSize,
2227 srcMask->Planes (),
2228 srcMask->PixelType ()));
2229
2230 ResampleImage (fHost,
2231 *srcMask,
2232 *tempMask.Get (),
2233 srcBounds,
2234 tempMask->Bounds (),
2235 dng_resample_bicubic::Get ());
2236
2237 srcMask = tempMask.Get ();
2238
2239 }
2240
2241 srcImage = tempImage.Get ();
2242
2243 srcBounds = tempImage->Bounds ();
2244
2245 }
2246
2247 uint32 dstPlanes = FinalSpace (nullptr).IsMonochrome () ? 1 : 3;
2248
2249 AutoPtr<dng_image> dstImage (fHost.Make_dng_image (srcBounds.Size (),
2250 dstPlanes,
2251 FinalPixelType ()));
2252
2253 dng_render_task task (*srcImage,
2254 srcMask,
2255 *dstImage.Get (),
2256 fNegative,
2257 *this,
2258 srcBounds.TL ());
2259
2260 fHost.PerformAreaTask (task,
2261 dstImage->Bounds ());
2262
2263 return dstImage.Release ();
2264
2265 }
2266
2267 /*****************************************************************************/
2268
2269 const dng_color_space & dng_render::FinalSpace (const dng_camera_profile *profile) const
2270 {
2271
2272 const dng_color_space &finalSpace = (fFinalSpace != nullptr)
2273 ? *fFinalSpace
2274 : dng_space_sRGB::Get ();
2275
2276 // This SDK supports rendering overrange/HDR to linear floating-point
2277 // output where 1.0 means UI White. Substitute a linear gamma version of
2278 // the profile.
2279
2280 if (profile && profile->IsHDR ())
2281 {
2282
2283 const dng_color_space *ptr = &finalSpace;
2284
2285 if (ptr->IsMonochrome ())
2286 return dng_space_Gray_Linear::Get ();
2287
2288 if (ptr == &dng_space_sRGB::Get ())
2289 return dng_space_sRGB_Linear::Get ();
2290
2291 if (ptr == &dng_space_AdobeRGB::Get ())
2292 return dng_space_AdobeRGB_Linear::Get ();
2293
2294 if (ptr == &dng_space_ProPhoto::Get ())
2295 return dng_space_ProPhoto_Linear::Get ();
2296
2297 if (ptr == &dng_space_DisplayP3::Get ())
2298 return dng_space_LinearP3::Get ();
2299
2300 if (ptr == &dng_space_Rec2020::Get ())
2301 return dng_space_Rec2020_Linear::Get ();
2302
2303 }
2304
2305 return finalSpace;
2306
2307 }
2308
2309 /*****************************************************************************/