| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2008-2019 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_gain_map.h" |
| 10 |
|
| 11 |
#include "dng_exceptions.h" |
| 12 |
#include "dng_globals.h" |
| 13 |
#include "dng_host.h" |
| 14 |
#include "dng_negative.h" |
| 15 |
#include "dng_pixel_buffer.h" |
| 16 |
#include "dng_safe_arithmetic.h" |
| 17 |
#include "dng_sdk_limits.h" |
| 18 |
#include "dng_stream.h" |
| 19 |
#include "dng_tag_values.h" |
| 20 |
|
| 21 |
/*****************************************************************************/ |
| 22 |
|
| 23 |
class dng_gain_map_interpolator |
| 24 |
{ |
| 25 |
|
| 26 |
private: |
| 27 |
|
| 28 |
const dng_gain_map &fMap; |
| 29 |
|
| 30 |
dng_point_real64 fScale; |
| 31 |
dng_point_real64 fOffset; |
| 32 |
|
| 33 |
int32 fColumn; |
| 34 |
int32 fPlane; |
| 35 |
|
| 36 |
uint32 fRowIndex1; |
| 37 |
uint32 fRowIndex2; |
| 38 |
real32 fRowFract; |
| 39 |
|
| 40 |
int32 fResetColumn; |
| 41 |
|
| 42 |
real32 fValueBase; |
| 43 |
real32 fValueStep; |
| 44 |
real32 fValueIndex; |
| 45 |
|
| 46 |
public: |
| 47 |
|
| 48 |
dng_gain_map_interpolator (const dng_gain_map &map, |
| 49 |
const dng_rect &mapBounds, |
| 50 |
int32 row, |
| 51 |
int32 column, |
| 52 |
uint32 plane); |
| 53 |
|
| 54 |
real32 Interpolate () const |
| 55 |
{ |
| 56 |
|
| 57 |
return fValueBase + fValueStep * fValueIndex; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
void Increment () |
| 62 |
{ |
| 63 |
|
| 64 |
if (++fColumn >= fResetColumn) |
| 65 |
{ |
| 66 |
|
| 67 |
ResetColumn (); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
else |
| 72 |
{ |
| 73 |
|
| 74 |
fValueIndex += 1.0f; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
private: |
| 81 |
|
| 82 |
real32 InterpolateEntry (uint32 colIndex); |
| 83 |
|
| 84 |
void ResetColumn (); |
| 85 |
|
| 86 |
}; |
| 87 |
|
| 88 |
/*****************************************************************************/ |
| 89 |
|
| 90 |
dng_gain_map_interpolator::dng_gain_map_interpolator (const dng_gain_map &map, |
| 91 |
const dng_rect &mapBounds, |
| 92 |
int32 row, |
| 93 |
int32 column, |
| 94 |
uint32 plane) |
| 95 |
|
| 96 |
: fMap (map) |
| 97 |
|
| 98 |
, fScale (1.0 / mapBounds.H (), |
| 99 |
1.0 / mapBounds.W ()) |
| 100 |
|
| 101 |
, fOffset (0.5 - mapBounds.t, |
| 102 |
0.5 - mapBounds.l) |
| 103 |
|
| 104 |
, fColumn (column) |
| 105 |
, fPlane (plane) |
| 106 |
|
| 107 |
, fRowIndex1 (0) |
| 108 |
, fRowIndex2 (0) |
| 109 |
, fRowFract (0.0f) |
| 110 |
|
| 111 |
, fResetColumn (0) |
| 112 |
|
| 113 |
, fValueBase (0.0f) |
| 114 |
, fValueStep (0.0f) |
| 115 |
, fValueIndex (0.0f) |
| 116 |
|
| 117 |
{ |
| 118 |
|
| 119 |
real64 rowIndexF = (fScale.v * (row + fOffset.v) - |
| 120 |
fMap.Origin ().v) / fMap.Spacing ().v; |
| 121 |
|
| 122 |
if (rowIndexF <= 0.0) |
| 123 |
{ |
| 124 |
|
| 125 |
fRowIndex1 = 0; |
| 126 |
fRowIndex2 = 0; |
| 127 |
|
| 128 |
fRowFract = 0.0f; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
else |
| 133 |
{ |
| 134 |
|
| 135 |
if (fMap.Points ().v < 1) |
| 136 |
{ |
| 137 |
ThrowProgramError ("Empty gain map"); |
| 138 |
} |
| 139 |
|
| 140 |
uint32 lastRow = static_cast<uint32> (fMap.Points ().v - 1); |
| 141 |
|
| 142 |
if (rowIndexF >= static_cast<real64> (lastRow)) |
| 143 |
{ |
| 144 |
|
| 145 |
fRowIndex1 = lastRow; |
| 146 |
fRowIndex2 = fRowIndex1; |
| 147 |
|
| 148 |
fRowFract = 0.0f; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
else |
| 153 |
{ |
| 154 |
|
| 155 |
// If we got here, we know that rowIndexF can safely be converted to |
| 156 |
// a uint32 and that static_cast<uint32> (rowIndexF) < lastRow. This |
| 157 |
// implies fRowIndex2 <= lastRow below. |
| 158 |
|
| 159 |
fRowIndex1 = static_cast<uint32> (rowIndexF); |
| 160 |
fRowIndex2 = fRowIndex1 + 1; |
| 161 |
|
| 162 |
fRowFract = (real32) (rowIndexF - (real64) fRowIndex1); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
ResetColumn (); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
/*****************************************************************************/ |
| 173 |
|
| 174 |
real32 dng_gain_map_interpolator::InterpolateEntry (uint32 colIndex) |
| 175 |
{ |
| 176 |
|
| 177 |
return fMap.Entry (fRowIndex1, colIndex, fPlane) * (1.0f - fRowFract) + |
| 178 |
fMap.Entry (fRowIndex2, colIndex, fPlane) * ( fRowFract); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/*****************************************************************************/ |
| 183 |
|
| 184 |
void dng_gain_map_interpolator::ResetColumn () |
| 185 |
{ |
| 186 |
|
| 187 |
real64 colIndexF = ((fScale.h * (fColumn + fOffset.h)) - |
| 188 |
fMap.Origin ().h) / fMap.Spacing ().h; |
| 189 |
|
| 190 |
if (colIndexF <= 0.0) |
| 191 |
{ |
| 192 |
|
| 193 |
fValueBase = InterpolateEntry (0); |
| 194 |
|
| 195 |
fValueStep = 0.0f; |
| 196 |
|
| 197 |
fResetColumn = (int32) ceil (fMap.Origin ().h / fScale.h - fOffset.h); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
else |
| 202 |
{ |
| 203 |
|
| 204 |
if (fMap.Points ().h < 1) |
| 205 |
{ |
| 206 |
ThrowProgramError ("Empty gain map"); |
| 207 |
} |
| 208 |
|
| 209 |
uint32 lastCol = static_cast<uint32> (fMap.Points ().h - 1); |
| 210 |
|
| 211 |
if (colIndexF >= static_cast<real64> (lastCol)) |
| 212 |
{ |
| 213 |
|
| 214 |
fValueBase = InterpolateEntry (lastCol); |
| 215 |
|
| 216 |
fValueStep = 0.0f; |
| 217 |
|
| 218 |
fResetColumn = 0x7FFFFFFF; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
else |
| 223 |
{ |
| 224 |
|
| 225 |
// If we got here, we know that colIndexF can safely be converted |
| 226 |
// to a uint32 and that static_cast<uint32> (colIndexF) < lastCol. |
| 227 |
// This implies colIndex + 1 <= lastCol, i.e. the argument to |
| 228 |
// InterpolateEntry() below is valid. |
| 229 |
|
| 230 |
uint32 colIndex = static_cast<uint32> (colIndexF); |
| 231 |
|
| 232 |
real64 base = InterpolateEntry (colIndex); |
| 233 |
real64 delta = InterpolateEntry (colIndex + 1) - base; |
| 234 |
|
| 235 |
fValueBase = (real32) (base + delta * (colIndexF - (real64) colIndex)); |
| 236 |
|
| 237 |
fValueStep = (real32) ((delta * fScale.h) / fMap.Spacing ().h); |
| 238 |
|
| 239 |
fResetColumn = |
| 240 |
ConvertDoubleToInt32 (ceil (((colIndex + 1) * fMap.Spacing ().h + |
| 241 |
fMap.Origin ().h) / fScale.h - fOffset.h)); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
fValueIndex = 0.0f; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
/*****************************************************************************/ |
| 252 |
|
| 253 |
dng_gain_map::dng_gain_map (dng_memory_allocator &allocator, |
| 254 |
const dng_point &points, |
| 255 |
const dng_point_real64 &spacing, |
| 256 |
const dng_point_real64 &origin, |
| 257 |
uint32 planes) |
| 258 |
|
| 259 |
: fPoints (points) |
| 260 |
, fSpacing (spacing) |
| 261 |
, fOrigin (origin) |
| 262 |
, fPlanes (planes) |
| 263 |
|
| 264 |
, fRowStep (SafeUint32Mult (planes, points.h)) |
| 265 |
|
| 266 |
, fBuffer () |
| 267 |
|
| 268 |
{ |
| 269 |
|
| 270 |
fBuffer.Reset (allocator.Allocate (ComputeBufferSize (ttFloat, |
| 271 |
fPoints, |
| 272 |
fPlanes, |
| 273 |
padSIMDBytes))); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/*****************************************************************************/ |
| 278 |
|
| 279 |
real32 dng_gain_map::Interpolate (int32 row, |
| 280 |
int32 col, |
| 281 |
uint32 plane, |
| 282 |
const dng_rect &bounds) const |
| 283 |
{ |
| 284 |
|
| 285 |
dng_gain_map_interpolator interp (*this, |
| 286 |
bounds, |
| 287 |
row, |
| 288 |
col, |
| 289 |
plane); |
| 290 |
|
| 291 |
return interp.Interpolate (); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
/*****************************************************************************/ |
| 296 |
|
| 297 |
uint32 dng_gain_map::PutStreamSize () const |
| 298 |
{ |
| 299 |
|
| 300 |
return 44 + fPoints.v * fPoints.h * fPlanes * 4; |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
/*****************************************************************************/ |
| 305 |
|
| 306 |
void dng_gain_map::PutStream (dng_stream &stream) const |
| 307 |
{ |
| 308 |
|
| 309 |
stream.Put_uint32 (fPoints.v); |
| 310 |
stream.Put_uint32 (fPoints.h); |
| 311 |
|
| 312 |
stream.Put_real64 (fSpacing.v); |
| 313 |
stream.Put_real64 (fSpacing.h); |
| 314 |
|
| 315 |
stream.Put_real64 (fOrigin.v); |
| 316 |
stream.Put_real64 (fOrigin.h); |
| 317 |
|
| 318 |
stream.Put_uint32 (fPlanes); |
| 319 |
|
| 320 |
for (int32 rowIndex = 0; rowIndex < fPoints.v; rowIndex++) |
| 321 |
{ |
| 322 |
|
| 323 |
for (int32 colIndex = 0; colIndex < fPoints.h; colIndex++) |
| 324 |
{ |
| 325 |
|
| 326 |
for (uint32 plane = 0; plane < fPlanes; plane++) |
| 327 |
{ |
| 328 |
|
| 329 |
stream.Put_real32 (Entry (rowIndex, |
| 330 |
colIndex, |
| 331 |
plane)); |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
/*****************************************************************************/ |
| 342 |
|
| 343 |
dng_gain_map * dng_gain_map::GetStream (dng_host &host, |
| 344 |
dng_stream &stream) |
| 345 |
{ |
| 346 |
|
| 347 |
dng_point mapPoints; |
| 348 |
|
| 349 |
mapPoints.v = stream.Get_uint32 (); |
| 350 |
mapPoints.h = stream.Get_uint32 (); |
| 351 |
|
| 352 |
dng_point_real64 mapSpacing; |
| 353 |
|
| 354 |
mapSpacing.v = stream.Get_real64 (); |
| 355 |
mapSpacing.h = stream.Get_real64 (); |
| 356 |
|
| 357 |
dng_point_real64 mapOrigin; |
| 358 |
|
| 359 |
mapOrigin.v = stream.Get_real64 (); |
| 360 |
mapOrigin.h = stream.Get_real64 (); |
| 361 |
|
| 362 |
uint32 mapPlanes = stream.Get_uint32 (); |
| 363 |
|
| 364 |
#if qDNGValidate |
| 365 |
|
| 366 |
if (gVerbose) |
| 367 |
{ |
| 368 |
|
| 369 |
printf ("Points: v=%d, h=%d\n", |
| 370 |
(int) mapPoints.v, |
| 371 |
(int) mapPoints.h); |
| 372 |
|
| 373 |
printf ("Spacing: v=%.6f, h=%.6f\n", |
| 374 |
mapSpacing.v, |
| 375 |
mapSpacing.h); |
| 376 |
|
| 377 |
printf ("Origin: v=%.6f, h=%.6f\n", |
| 378 |
mapOrigin.v, |
| 379 |
mapOrigin.h); |
| 380 |
|
| 381 |
printf ("Planes: %u\n", |
| 382 |
(unsigned) mapPlanes); |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
#endif |
| 387 |
|
| 388 |
if (mapPoints.v == 1) |
| 389 |
{ |
| 390 |
mapSpacing.v = 1.0; |
| 391 |
mapOrigin.v = 0.0; |
| 392 |
} |
| 393 |
|
| 394 |
if (mapPoints.h == 1) |
| 395 |
{ |
| 396 |
mapSpacing.h = 1.0; |
| 397 |
mapOrigin.h = 0.0; |
| 398 |
} |
| 399 |
|
| 400 |
if (mapPoints.v < 1 || |
| 401 |
mapPoints.h < 1 || |
| 402 |
mapSpacing.v <= 0.0 || |
| 403 |
mapSpacing.h <= 0.0 || |
| 404 |
mapPlanes < 1) |
| 405 |
{ |
| 406 |
ThrowBadFormat (); |
| 407 |
} |
| 408 |
|
| 409 |
AutoPtr<dng_gain_map> map (new dng_gain_map (host.Allocator (), |
| 410 |
mapPoints, |
| 411 |
mapSpacing, |
| 412 |
mapOrigin, |
| 413 |
mapPlanes)); |
| 414 |
|
| 415 |
#if qDNGValidate |
| 416 |
|
| 417 |
uint32 linesPrinted = 0; |
| 418 |
uint32 linesSkipped = 0; |
| 419 |
|
| 420 |
#endif |
| 421 |
|
| 422 |
for (int32 rowIndex = 0; rowIndex < mapPoints.v; rowIndex++) |
| 423 |
{ |
| 424 |
|
| 425 |
for (int32 colIndex = 0; colIndex < mapPoints.h; colIndex++) |
| 426 |
{ |
| 427 |
|
| 428 |
for (uint32 plane = 0; plane < mapPlanes; plane++) |
| 429 |
{ |
| 430 |
|
| 431 |
real32 x = stream.Get_real32 (); |
| 432 |
|
| 433 |
map->Entry (rowIndex, colIndex, plane) = x; |
| 434 |
|
| 435 |
#if qDNGValidate |
| 436 |
|
| 437 |
if (gVerbose) |
| 438 |
{ |
| 439 |
|
| 440 |
if (linesPrinted < gDumpLineLimit) |
| 441 |
{ |
| 442 |
|
| 443 |
printf ("\tMap [%3u] [%3u] [%u] = %.4f\n", |
| 444 |
(unsigned) rowIndex, |
| 445 |
(unsigned) colIndex, |
| 446 |
(unsigned) plane, |
| 447 |
x); |
| 448 |
|
| 449 |
linesPrinted++; |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
else |
| 454 |
linesSkipped++; |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
#endif |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
} |
| 463 |
|
| 464 |
} |
| 465 |
|
| 466 |
#if qDNGValidate |
| 467 |
|
| 468 |
if (linesSkipped) |
| 469 |
{ |
| 470 |
|
| 471 |
printf ("\t ... %u map entries skipped\n", (unsigned) linesSkipped); |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
#endif |
| 476 |
|
| 477 |
return map.Release (); |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
/*****************************************************************************/ |
| 482 |
|
| 483 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 484 |
dng_gain_table_map::dng_gain_table_map (dng_memory_allocator &allocator, |
| 485 |
const dng_point &points, |
| 486 |
const dng_point_real64 &spacing, |
| 487 |
const dng_point_real64 &origin, |
| 488 |
uint32 numTablePoints, |
| 489 |
const real32 weights [5], |
| 490 |
uint32 dataType, |
| 491 |
real32 gamma, |
| 492 |
real32 gainMin, |
| 493 |
real32 gainMax) |
| 494 |
|
| 495 |
: fPoints (points) |
| 496 |
, fSpacing (spacing) |
| 497 |
, fOrigin (origin) |
| 498 |
, fNumTablePoints (numTablePoints) |
| 499 |
|
| 500 |
, fRowStep (fNumTablePoints * points.h) |
| 501 |
, fColStep (fNumTablePoints) |
| 502 |
|
| 503 |
, fNumSamples (SafeUint32Mult (points.h, |
| 504 |
points.v, |
| 505 |
numTablePoints)) |
| 506 |
|
| 507 |
, fDataType (dataType) |
| 508 |
|
| 509 |
, fGamma (gamma) |
| 510 |
|
| 511 |
, fGainMin (gainMin) |
| 512 |
, fGainMax (gainMax) |
| 513 |
|
| 514 |
{ |
| 515 |
|
| 516 |
DNG_REQUIRE (dataType <= 3, "Unsupported DataType"); |
| 517 |
|
| 518 |
DNG_REQUIRE (gamma >= kProfileGainTableMap_MinGamma && |
| 519 |
gamma <= kProfileGainTableMap_MaxGamma, |
| 520 |
"Gamma out of range"); |
| 521 |
|
| 522 |
DNG_REQUIRE (gainMin >= kProfileGainTableMap_MinGainValue, |
| 523 |
"GainMin out of range"); |
| 524 |
|
| 525 |
DNG_REQUIRE (gainMax <= kProfileGainTableMap_MaxGainValue, |
| 526 |
"GainMax out of range"); |
| 527 |
|
| 528 |
fSampleBytes = SafeUint32Mult (fNumSamples, |
| 529 |
(uint32) sizeof (real32)); |
| 530 |
|
| 531 |
if (fNumSamples >= kMaxProfileGainTableMapPoints) |
| 532 |
{ |
| 533 |
|
| 534 |
ThrowBadFormat ("too many points in gain table map"); |
| 535 |
|
| 536 |
} |
| 537 |
|
| 538 |
memcpy (fMapInputWeights, |
| 539 |
weights, |
| 540 |
sizeof (fMapInputWeights)); |
| 541 |
|
| 542 |
fBuffer.Reset (allocator.Allocate (ComputeBufferSize (ttFloat, |
| 543 |
fPoints, |
| 544 |
numTablePoints, |
| 545 |
padSIMDBytes))); |
| 546 |
|
| 547 |
} |
| 548 |
|
| 549 |
/*****************************************************************************/ |
| 550 |
|
| 551 |
void * dng_gain_table_map::RawTablePtr () const |
| 552 |
{ |
| 553 |
|
| 554 |
DNG_REQUIRE (fBuffer.Get (), "fBuffer"); |
| 555 |
|
| 556 |
// The table currently starts at the beginning of the block's Buffer. |
| 557 |
|
| 558 |
return fBuffer->Buffer (); |
| 559 |
|
| 560 |
} |
| 561 |
|
| 562 |
/*****************************************************************************/ |
| 563 |
|
| 564 |
uint32 dng_gain_table_map::RawTableNumBytes () const |
| 565 |
{ |
| 566 |
|
| 567 |
// This is a real32 table. |
| 568 |
|
| 569 |
return fNumSamples * sizeof (real32); |
| 570 |
|
| 571 |
} |
| 572 |
|
| 573 |
/*****************************************************************************/ |
| 574 |
|
| 575 |
uint32 dng_gain_table_map::PutStreamSize () const |
| 576 |
{ |
| 577 |
|
| 578 |
return ((2 * 4) + // MapPointsV, MapPointsH |
| 579 |
(2 * 8) + // MapSpacingV, MapSpacingH |
| 580 |
(2 * 8) + // MapOriginV, MapOriginH |
| 581 |
( 4) + // MapPoints |
| 582 |
(5 * 4) + // MapInputWeights |
| 583 |
(RequiresVersion2 () ? (4 * 4) |
| 584 |
: 0) + // DataType, Gamma, GainMin & Max |
| 585 |
DataStorageBytes ()); |
| 586 |
|
| 587 |
} |
| 588 |
|
| 589 |
/*****************************************************************************/ |
| 590 |
|
| 591 |
void dng_gain_table_map::AddDigest (dng_md5_printer &printer) const |
| 592 |
{ |
| 593 |
|
| 594 |
if (SupportsVersion1 ()) |
| 595 |
printer.Process ("ProfileGainTableMap", 19); |
| 596 |
|
| 597 |
else |
| 598 |
printer.Process ("ProfileGainTableMap2", 20); |
| 599 |
|
| 600 |
EnsureFingerprint (); |
| 601 |
|
| 602 |
printer.Process (fFingerprint.data, dng_fingerprint::kDNGFingerprintSize); |
| 603 |
|
| 604 |
} |
| 605 |
|
| 606 |
/*****************************************************************************/ |
| 607 |
|
| 608 |
void dng_gain_table_map::EnsureFingerprint () const |
| 609 |
{ |
| 610 |
|
| 611 |
if (fFingerprint.IsNull ()) |
| 612 |
{ |
| 613 |
|
| 614 |
dng_md5_printer_stream stream; |
| 615 |
|
| 616 |
PutStream (stream); |
| 617 |
|
| 618 |
fFingerprint = stream.Result (); |
| 619 |
|
| 620 |
} |
| 621 |
|
| 622 |
} |
| 623 |
|
| 624 |
/*****************************************************************************/ |
| 625 |
|
| 626 |
dng_fingerprint dng_gain_table_map::GetFingerprint () const |
| 627 |
{ |
| 628 |
|
| 629 |
EnsureFingerprint (); |
| 630 |
|
| 631 |
return fFingerprint; |
| 632 |
|
| 633 |
} |
| 634 |
|
| 635 |
/*****************************************************************************/ |
| 636 |
|
| 637 |
void dng_gain_table_map::PutStream (dng_stream &stream, |
| 638 |
bool forceVersion2) const |
| 639 |
{ |
| 640 |
|
| 641 |
stream.Put_uint32 (fPoints.v); |
| 642 |
stream.Put_uint32 (fPoints.h); |
| 643 |
|
| 644 |
stream.Put_real64 (fSpacing.v); |
| 645 |
stream.Put_real64 (fSpacing.h); |
| 646 |
|
| 647 |
stream.Put_real64 (fOrigin.v); |
| 648 |
stream.Put_real64 (fOrigin.h); |
| 649 |
|
| 650 |
stream.Put_uint32 (fNumTablePoints); |
| 651 |
|
| 652 |
for (uint32 i = 0; i < 5; i++) |
| 653 |
{ |
| 654 |
stream.Put_real32 (fMapInputWeights [i]); |
| 655 |
} |
| 656 |
|
| 657 |
if (RequiresVersion2 () || forceVersion2) |
| 658 |
{ |
| 659 |
stream.Put_uint32 (fDataType); |
| 660 |
stream.Put_real32 (fGamma); |
| 661 |
stream.Put_real32 (fGainMin); |
| 662 |
stream.Put_real32 (fGainMax); |
| 663 |
} |
| 664 |
|
| 665 |
const uint32 storageBytes = DataStorageBytes (); |
| 666 |
|
| 667 |
// If we have an original buffer, then just use that. |
| 668 |
|
| 669 |
if (fOriginalBuffer.Get () && |
| 670 |
fOriginalBuffer->LogicalSize () == storageBytes) |
| 671 |
{ |
| 672 |
|
| 673 |
stream.Put (fOriginalBuffer->Buffer (), |
| 674 |
storageBytes); |
| 675 |
|
| 676 |
} |
| 677 |
|
| 678 |
else |
| 679 |
{ |
| 680 |
|
| 681 |
const bool isFloat32 = IsFloat32 (); |
| 682 |
const bool isFloat16 = IsFloat16 (); |
| 683 |
const bool is8bit = IsUint8 (); |
| 684 |
|
| 685 |
for (int32 rowIndex = 0; rowIndex < fPoints.v; rowIndex++) |
| 686 |
{ |
| 687 |
|
| 688 |
for (int32 colIndex = 0; colIndex < fPoints.h; colIndex++) |
| 689 |
{ |
| 690 |
|
| 691 |
// Write 32-bit float values directly. |
| 692 |
|
| 693 |
if (isFloat32) |
| 694 |
{ |
| 695 |
|
| 696 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 697 |
{ |
| 698 |
|
| 699 |
stream.Put_real32 (Entry (rowIndex, |
| 700 |
colIndex, |
| 701 |
p)); |
| 702 |
|
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
|
| 707 |
// Convert fp32 values to fp16. |
| 708 |
|
| 709 |
else if (isFloat16) |
| 710 |
{ |
| 711 |
|
| 712 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 713 |
{ |
| 714 |
|
| 715 |
real32 x = Entry (rowIndex, colIndex, p); |
| 716 |
|
| 717 |
uint16 x16 = DNG_FloatToHalf (*(const uint32 *) &x); |
| 718 |
|
| 719 |
stream.Put_uint16 (x16); |
| 720 |
|
| 721 |
} |
| 722 |
|
| 723 |
} |
| 724 |
|
| 725 |
// Convert fp32 values to uint8. |
| 726 |
|
| 727 |
else if (is8bit) |
| 728 |
{ |
| 729 |
|
| 730 |
DNG_REQUIRE (fGainMax >= fGainMin, |
| 731 |
"Expected fGainMax >= fGainMin"); |
| 732 |
|
| 733 |
if (fGainMax == fGainMin) |
| 734 |
{ |
| 735 |
|
| 736 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 737 |
stream.Put_uint8 (0); |
| 738 |
|
| 739 |
} |
| 740 |
|
| 741 |
else |
| 742 |
{ |
| 743 |
|
| 744 |
const real32 scale = 1.0f / (fGainMax - fGainMin); |
| 745 |
|
| 746 |
const real32 offset = -fGainMin * scale; |
| 747 |
|
| 748 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 749 |
{ |
| 750 |
|
| 751 |
real32 x = Entry (rowIndex, colIndex, p); |
| 752 |
|
| 753 |
// Map via GainMin and GainMax. |
| 754 |
|
| 755 |
x = Pin_real32 (x * scale + offset); |
| 756 |
|
| 757 |
stream.Put_uint8 ((uint8) Round_int32 (x * 255.0f)); |
| 758 |
|
| 759 |
} |
| 760 |
|
| 761 |
} |
| 762 |
|
| 763 |
} |
| 764 |
|
| 765 |
// Convert fp32 values to uint16. |
| 766 |
|
| 767 |
else // 16-bit |
| 768 |
{ |
| 769 |
|
| 770 |
DNG_REQUIRE (fGainMax >= fGainMin, |
| 771 |
"Expected fGainMax >= fGainMin"); |
| 772 |
|
| 773 |
if (fGainMax == fGainMin) |
| 774 |
{ |
| 775 |
|
| 776 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 777 |
stream.Put_uint16 (0); |
| 778 |
|
| 779 |
} |
| 780 |
|
| 781 |
else |
| 782 |
{ |
| 783 |
|
| 784 |
const real32 scale = 1.0f / (fGainMax - fGainMin); |
| 785 |
|
| 786 |
const real32 offset = -fGainMin * scale; |
| 787 |
|
| 788 |
for (uint32 p = 0; p < fNumTablePoints; p++) |
| 789 |
{ |
| 790 |
|
| 791 |
real32 x = Entry (rowIndex, colIndex, p); |
| 792 |
|
| 793 |
// Map via GainMin and GainMax. |
| 794 |
|
| 795 |
x = Pin_real32 (x * scale + offset); |
| 796 |
|
| 797 |
stream.Put_uint16 ((uint16) Round_int32 (x * 65535.0f)); |
| 798 |
|
| 799 |
} |
| 800 |
|
| 801 |
} |
| 802 |
|
| 803 |
} // fp32 vs u8 vs u16 cases |
| 804 |
|
| 805 |
} // cols |
| 806 |
|
| 807 |
} // rows |
| 808 |
|
| 809 |
} // original vs no original buffer |
| 810 |
|
| 811 |
} |
| 812 |
|
| 813 |
/*****************************************************************************/ |
| 814 |
|
| 815 |
dng_gain_table_map * dng_gain_table_map::GetStream (dng_host &host, |
| 816 |
dng_stream &stream, |
| 817 |
const bool useVersion2) |
| 818 |
{ |
| 819 |
|
| 820 |
dng_point mapPoints; |
| 821 |
|
| 822 |
mapPoints.v = stream.Get_uint32 (); |
| 823 |
mapPoints.h = stream.Get_uint32 (); |
| 824 |
|
| 825 |
dng_point_real64 mapSpacing; |
| 826 |
|
| 827 |
mapSpacing.v = stream.Get_real64 (); |
| 828 |
mapSpacing.h = stream.Get_real64 (); |
| 829 |
|
| 830 |
dng_point_real64 mapOrigin; |
| 831 |
|
| 832 |
mapOrigin.v = stream.Get_real64 (); |
| 833 |
mapOrigin.h = stream.Get_real64 (); |
| 834 |
|
| 835 |
uint32 numTablePoints = stream.Get_uint32 (); |
| 836 |
|
| 837 |
real32 weights [5]; |
| 838 |
|
| 839 |
for (uint32 i = 0; i < 5; i++) |
| 840 |
{ |
| 841 |
weights [i] = stream.Get_real32 (); |
| 842 |
} |
| 843 |
|
| 844 |
uint32 dataType = 3; |
| 845 |
real32 gamma = 1.0f; |
| 846 |
real32 gainMin = 1.0f; |
| 847 |
real32 gainMax = 1.0f; |
| 848 |
|
| 849 |
if (useVersion2) |
| 850 |
{ |
| 851 |
|
| 852 |
dataType = stream.Get_uint32 (); |
| 853 |
gamma = stream.Get_real32 (); |
| 854 |
gainMin = stream.Get_real32 (); |
| 855 |
gainMax = stream.Get_real32 (); |
| 856 |
|
| 857 |
if (gamma < kProfileGainTableMap_MinGamma || |
| 858 |
gamma > kProfileGainTableMap_MaxGamma) |
| 859 |
{ |
| 860 |
ThrowBadFormat ("Gamma out of range in ProfileGainTableMap2"); |
| 861 |
} |
| 862 |
|
| 863 |
if (dataType > 3) |
| 864 |
{ |
| 865 |
ThrowBadFormat ("Unsupported DataType in ProfileGainTableMap2"); |
| 866 |
} |
| 867 |
|
| 868 |
if (gainMin < kProfileGainTableMap_MinGainValue) |
| 869 |
{ |
| 870 |
ThrowBadFormat ("GainMin out of range in ProfileGainTableMap2"); |
| 871 |
} |
| 872 |
|
| 873 |
if (gainMax > kProfileGainTableMap_MaxGainValue) |
| 874 |
{ |
| 875 |
ThrowBadFormat ("GainMax out of range in ProfileGainTableMap2"); |
| 876 |
} |
| 877 |
|
| 878 |
} |
| 879 |
|
| 880 |
#if qDNGValidate |
| 881 |
|
| 882 |
if (gVerbose) |
| 883 |
{ |
| 884 |
|
| 885 |
printf ("GainTableMap:\n"); |
| 886 |
|
| 887 |
printf (" Points: v=%d, h=%d\n", |
| 888 |
(int) mapPoints.v, |
| 889 |
(int) mapPoints.h); |
| 890 |
|
| 891 |
printf (" Spacing: v=%.6f, h=%.6f\n", |
| 892 |
mapSpacing.v, |
| 893 |
mapSpacing.h); |
| 894 |
|
| 895 |
printf (" Origin: v=%.6f, h=%.6f\n", |
| 896 |
mapOrigin.v, |
| 897 |
mapOrigin.h); |
| 898 |
|
| 899 |
printf (" NumTablePoints: %u\n", |
| 900 |
(unsigned) numTablePoints); |
| 901 |
|
| 902 |
printf (" Weights: %.4f, %.4f, %.4f, %.4f, %.4f\n", |
| 903 |
(float) weights [0], |
| 904 |
(float) weights [1], |
| 905 |
(float) weights [2], |
| 906 |
(float) weights [3], |
| 907 |
(float) weights [4]); |
| 908 |
|
| 909 |
printf (" DataType: %u\n", dataType); |
| 910 |
printf (" Gamma: %.3f\n", gamma); |
| 911 |
printf (" GainMin: %.4f\n", gainMin); |
| 912 |
printf (" GainMax: %.4f\n", gainMax); |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
#endif |
| 917 |
|
| 918 |
if (mapPoints.v == 1) |
| 919 |
{ |
| 920 |
mapSpacing.v = 1.0; |
| 921 |
mapOrigin.v = 0.0; |
| 922 |
} |
| 923 |
|
| 924 |
if (mapPoints.h == 1) |
| 925 |
{ |
| 926 |
mapSpacing.h = 1.0; |
| 927 |
mapOrigin.h = 0.0; |
| 928 |
} |
| 929 |
|
| 930 |
if (mapPoints.v < 1 || |
| 931 |
mapPoints.h < 1 || |
| 932 |
mapSpacing.v <= 0.0 || |
| 933 |
mapSpacing.h <= 0.0 || |
| 934 |
numTablePoints < 1) |
| 935 |
{ |
| 936 |
ThrowBadFormat (); |
| 937 |
} |
| 938 |
|
| 939 |
// Read the weights. |
| 940 |
|
| 941 |
AutoPtr<dng_gain_table_map> map |
| 942 |
(new dng_gain_table_map (host.Allocator (), |
| 943 |
mapPoints, |
| 944 |
mapSpacing, |
| 945 |
mapOrigin, |
| 946 |
numTablePoints, |
| 947 |
weights, |
| 948 |
dataType, |
| 949 |
gamma, |
| 950 |
gainMin, |
| 951 |
gainMax)); |
| 952 |
|
| 953 |
const bool is8bit = map->IsUint8 (); |
| 954 |
const bool isFloat16 = map->IsFloat16 (); |
| 955 |
const bool isFloat32 = map->IsFloat32 (); |
| 956 |
|
| 957 |
// If the data type is not 32-bit float, then keep a copy of the original |
| 958 |
// encoding around, for round-tripping purposes. |
| 959 |
|
| 960 |
void *origPtr = nullptr; |
| 961 |
|
| 962 |
if (!isFloat32) |
| 963 |
{ |
| 964 |
|
| 965 |
map->fOriginalBuffer.Reset (host.Allocate (map->DataStorageBytes ())); |
| 966 |
|
| 967 |
origPtr = map->fOriginalBuffer->Buffer (); |
| 968 |
|
| 969 |
} |
| 970 |
|
| 971 |
uint8 *orig8ptr = (uint8 *) origPtr; |
| 972 |
uint16 *orig16ptr = (uint16 *) origPtr; |
| 973 |
|
| 974 |
constexpr real32 scale8 = 1.0f / 255.0f; |
| 975 |
constexpr real32 scale16 = 1.0f / 65535.0f; |
| 976 |
|
| 977 |
#if qDNGValidate |
| 978 |
|
| 979 |
uint32 linesPrinted = 0; |
| 980 |
uint32 linesSkipped = 0; |
| 981 |
|
| 982 |
#endif |
| 983 |
|
| 984 |
for (int32 rowIndex = 0; rowIndex < mapPoints.v; rowIndex++) |
| 985 |
{ |
| 986 |
|
| 987 |
for (int32 colIndex = 0; colIndex < mapPoints.h; colIndex++) |
| 988 |
{ |
| 989 |
|
| 990 |
for (uint32 p = 0; p < numTablePoints; p++) |
| 991 |
{ |
| 992 |
|
| 993 |
real32 x; |
| 994 |
|
| 995 |
if (isFloat32) |
| 996 |
{ |
| 997 |
|
| 998 |
x = stream.Get_real32 (); |
| 999 |
|
| 1000 |
#if qDNGValidate |
| 1001 |
|
| 1002 |
if (gVerbose) |
| 1003 |
{ |
| 1004 |
|
| 1005 |
if (linesPrinted < gDumpLineLimit) |
| 1006 |
{ |
| 1007 |
|
| 1008 |
printf ("\tMap [%3u] [%3u] [%3u] = %.4f\n", |
| 1009 |
(unsigned) rowIndex, |
| 1010 |
(unsigned) colIndex, |
| 1011 |
(unsigned) p, |
| 1012 |
x); |
| 1013 |
|
| 1014 |
linesPrinted++; |
| 1015 |
|
| 1016 |
} |
| 1017 |
|
| 1018 |
else |
| 1019 |
linesSkipped++; |
| 1020 |
|
| 1021 |
} |
| 1022 |
|
| 1023 |
#endif |
| 1024 |
|
| 1025 |
} |
| 1026 |
|
| 1027 |
else if (isFloat16) |
| 1028 |
{ |
| 1029 |
|
| 1030 |
uint16 x16 = stream.Get_uint16 (); |
| 1031 |
|
| 1032 |
uint32 x32 = DNG_HalfToFloat (x16); |
| 1033 |
|
| 1034 |
x = *(const real32 *) &x32; |
| 1035 |
|
| 1036 |
#if qDNGValidate |
| 1037 |
|
| 1038 |
if (gVerbose) |
| 1039 |
{ |
| 1040 |
|
| 1041 |
if (linesPrinted < gDumpLineLimit) |
| 1042 |
{ |
| 1043 |
|
| 1044 |
printf ("\tMap [%3u] [%3u] [%3u] = %.4f\n", |
| 1045 |
(unsigned) rowIndex, |
| 1046 |
(unsigned) colIndex, |
| 1047 |
(unsigned) p, |
| 1048 |
x); |
| 1049 |
|
| 1050 |
linesPrinted++; |
| 1051 |
|
| 1052 |
} |
| 1053 |
|
| 1054 |
else |
| 1055 |
linesSkipped++; |
| 1056 |
|
| 1057 |
} |
| 1058 |
|
| 1059 |
#endif |
| 1060 |
|
| 1061 |
} |
| 1062 |
|
| 1063 |
else if (is8bit) |
| 1064 |
{ |
| 1065 |
|
| 1066 |
uint8 x8 = stream.Get_uint8 (); |
| 1067 |
|
| 1068 |
x = gainMin + (x8 * scale8) * (gainMax - gainMin); |
| 1069 |
|
| 1070 |
*orig8ptr++ = x8; |
| 1071 |
|
| 1072 |
#if qDNGValidate |
| 1073 |
|
| 1074 |
if (gVerbose) |
| 1075 |
{ |
| 1076 |
|
| 1077 |
if (linesPrinted < gDumpLineLimit) |
| 1078 |
{ |
| 1079 |
|
| 1080 |
printf ("\tMap [%3u] [%3u] [%3u] = %3d (%.4f)\n", |
| 1081 |
(unsigned) rowIndex, |
| 1082 |
(unsigned) colIndex, |
| 1083 |
(unsigned) p, |
| 1084 |
(int) x8, |
| 1085 |
x); |
| 1086 |
|
| 1087 |
linesPrinted++; |
| 1088 |
|
| 1089 |
} |
| 1090 |
|
| 1091 |
else |
| 1092 |
linesSkipped++; |
| 1093 |
|
| 1094 |
} |
| 1095 |
|
| 1096 |
#endif |
| 1097 |
|
| 1098 |
} |
| 1099 |
|
| 1100 |
else |
| 1101 |
{ |
| 1102 |
|
| 1103 |
// uint16 path |
| 1104 |
|
| 1105 |
uint16 x16 = stream.Get_uint16 (); |
| 1106 |
|
| 1107 |
x = gainMin + (x16 * scale16) * (gainMax - gainMin); |
| 1108 |
|
| 1109 |
*orig16ptr++ = x16; |
| 1110 |
|
| 1111 |
#if qDNGValidate |
| 1112 |
|
| 1113 |
if (gVerbose) |
| 1114 |
{ |
| 1115 |
|
| 1116 |
if (linesPrinted < gDumpLineLimit) |
| 1117 |
{ |
| 1118 |
|
| 1119 |
printf ("\tMap [%3u] [%3u] [%3u] = %5d (%.4f)\n", |
| 1120 |
(unsigned) rowIndex, |
| 1121 |
(unsigned) colIndex, |
| 1122 |
(unsigned) p, |
| 1123 |
(int) x16, |
| 1124 |
x); |
| 1125 |
|
| 1126 |
linesPrinted++; |
| 1127 |
|
| 1128 |
} |
| 1129 |
|
| 1130 |
else |
| 1131 |
linesSkipped++; |
| 1132 |
|
| 1133 |
} |
| 1134 |
|
| 1135 |
#endif |
| 1136 |
|
| 1137 |
} |
| 1138 |
|
| 1139 |
// Check range and bad values. |
| 1140 |
|
| 1141 |
if (x < kProfileGainTableMap_MinGainValue || |
| 1142 |
x > kProfileGainTableMap_MaxGainValue) |
| 1143 |
{ |
| 1144 |
ThrowBadFormat ("ProfileGainTableMap entry value out of range"); |
| 1145 |
} |
| 1146 |
|
| 1147 |
if (x != x) |
| 1148 |
{ |
| 1149 |
ThrowBadFormat ("Invalid ProfileGainTableMap entry value"); |
| 1150 |
} |
| 1151 |
|
| 1152 |
// Store it in the fp32 table. |
| 1153 |
|
| 1154 |
map->Entry (rowIndex, colIndex, p) = x; |
| 1155 |
|
| 1156 |
} // for each point in the table |
| 1157 |
|
| 1158 |
} // for each column |
| 1159 |
|
| 1160 |
} // for each row |
| 1161 |
|
| 1162 |
#if qDNGValidate |
| 1163 |
|
| 1164 |
if (linesSkipped) |
| 1165 |
{ |
| 1166 |
|
| 1167 |
printf ("\t ... %u map entries skipped\n", (unsigned) linesSkipped); |
| 1168 |
|
| 1169 |
} |
| 1170 |
|
| 1171 |
#endif |
| 1172 |
|
| 1173 |
return map.Release (); |
| 1174 |
|
| 1175 |
} |
| 1176 |
|
| 1177 |
/*****************************************************************************/ |
| 1178 |
|
| 1179 |
bool dng_gain_table_map::SupportsVersion1 () const |
| 1180 |
{ |
| 1181 |
|
| 1182 |
return (fGamma == 1.0f && // NOP gamma |
| 1183 |
fDataType == 3); // 32-bit float |
| 1184 |
|
| 1185 |
} |
| 1186 |
|
| 1187 |
/*****************************************************************************/ |
| 1188 |
|
| 1189 |
uint32 dng_gain_table_map::DataStorageBytes () const |
| 1190 |
{ |
| 1191 |
|
| 1192 |
return SafeUint32Mult ((uint32) fPoints.v, |
| 1193 |
(uint32) fPoints.h, |
| 1194 |
fNumTablePoints, |
| 1195 |
BytesPerEntry ()); |
| 1196 |
|
| 1197 |
} |
| 1198 |
|
| 1199 |
/*****************************************************************************/ |
| 1200 |
|
| 1201 |
void dng_gain_table_map::ClearOriginalBuffer () |
| 1202 |
{ |
| 1203 |
|
| 1204 |
fOriginalBuffer.Reset (); |
| 1205 |
|
| 1206 |
} |
| 1207 |
|
| 1208 |
/*****************************************************************************/ |
| 1209 |
|
| 1210 |
bool dng_gain_table_map::HasOriginalBuffer () const |
| 1211 |
{ |
| 1212 |
|
| 1213 |
return fOriginalBuffer.Get () != nullptr; |
| 1214 |
|
| 1215 |
} |
| 1216 |
|
| 1217 |
/*****************************************************************************/ |
| 1218 |
|
| 1219 |
const dng_memory_block * dng_gain_table_map::OriginalBuffer () const |
| 1220 |
{ |
| 1221 |
|
| 1222 |
return fOriginalBuffer.Get (); |
| 1223 |
|
| 1224 |
} |
| 1225 |
|
| 1226 |
/*****************************************************************************/ |
| 1227 |
|
| 1228 |
void dng_gain_table_map::SetOriginalBuffer (AutoPtr<dng_memory_block> &block) |
| 1229 |
{ |
| 1230 |
|
| 1231 |
fOriginalBuffer.Reset (block.Release ()); |
| 1232 |
|
| 1233 |
} |
| 1234 |
|
| 1235 |
/*****************************************************************************/ |
| 1236 |
/*****************************************************************************/ |
| 1237 |
/*****************************************************************************/ |
| 1238 |
|
| 1239 |
dng_opcode_GainMap::dng_opcode_GainMap (const dng_area_spec &areaSpec, |
| 1240 |
AutoPtr<dng_gain_map> &gainMap) |
| 1241 |
|
| 1242 |
: dng_inplace_opcode (dngOpcode_GainMap, |
| 1243 |
dngVersion_1_3_0_0, |
| 1244 |
kFlag_None) |
| 1245 |
|
| 1246 |
, fAreaSpec (areaSpec) |
| 1247 |
|
| 1248 |
, fGainMap () |
| 1249 |
|
| 1250 |
{ |
| 1251 |
|
| 1252 |
fGainMap.Reset (gainMap.Release ()); |
| 1253 |
|
| 1254 |
} |
| 1255 |
|
| 1256 |
/*****************************************************************************/ |
| 1257 |
|
| 1258 |
dng_opcode_GainMap::dng_opcode_GainMap (dng_host &host, |
| 1259 |
dng_stream &stream) |
| 1260 |
|
| 1261 |
: dng_inplace_opcode (dngOpcode_GainMap, |
| 1262 |
stream, |
| 1263 |
"GainMap") |
| 1264 |
|
| 1265 |
, fAreaSpec () |
| 1266 |
|
| 1267 |
, fGainMap () |
| 1268 |
|
| 1269 |
{ |
| 1270 |
|
| 1271 |
uint32 byteCount = stream.Get_uint32 (); |
| 1272 |
|
| 1273 |
uint64 startPosition = stream.Position (); |
| 1274 |
|
| 1275 |
fAreaSpec.GetData (stream); |
| 1276 |
|
| 1277 |
fGainMap.Reset (dng_gain_map::GetStream (host, stream)); |
| 1278 |
|
| 1279 |
if (stream.Position () != startPosition + byteCount) |
| 1280 |
{ |
| 1281 |
ThrowBadFormat (); |
| 1282 |
} |
| 1283 |
|
| 1284 |
} |
| 1285 |
|
| 1286 |
/*****************************************************************************/ |
| 1287 |
|
| 1288 |
void dng_opcode_GainMap::PutData (dng_stream &stream) const |
| 1289 |
{ |
| 1290 |
|
| 1291 |
stream.Put_uint32 (dng_area_spec::kDataSize + |
| 1292 |
fGainMap->PutStreamSize ()); |
| 1293 |
|
| 1294 |
fAreaSpec.PutData (stream); |
| 1295 |
|
| 1296 |
fGainMap->PutStream (stream); |
| 1297 |
|
| 1298 |
} |
| 1299 |
|
| 1300 |
/*****************************************************************************/ |
| 1301 |
|
| 1302 |
void dng_opcode_GainMap::ProcessArea (dng_negative &negative, |
| 1303 |
uint32 /* threadIndex */, |
| 1304 |
dng_pixel_buffer &buffer, |
| 1305 |
const dng_rect &dstArea, |
| 1306 |
const dng_rect &imageBounds) |
| 1307 |
{ |
| 1308 |
|
| 1309 |
dng_rect overlap = fAreaSpec.ScaledOverlap (dstArea); |
| 1310 |
|
| 1311 |
if (overlap.NotEmpty ()) |
| 1312 |
{ |
| 1313 |
|
| 1314 |
uint16 blackLevel = (Stage () >= 2) ? negative.Stage3BlackLevel () : 0; |
| 1315 |
|
| 1316 |
real32 blackScale1 = 1.0f; |
| 1317 |
real32 blackScale2 = 1.0f; |
| 1318 |
real32 blackOffset1 = 0.0f; |
| 1319 |
real32 blackOffset2 = 0.0f; |
| 1320 |
|
| 1321 |
if (blackLevel != 0) |
| 1322 |
{ |
| 1323 |
|
| 1324 |
blackOffset2 = ((real32) blackLevel) / 65535.0f; |
| 1325 |
blackScale2 = 1.0f - blackOffset2; |
| 1326 |
blackScale1 = 1.0f / blackScale2; |
| 1327 |
blackOffset1 = 1.0f - blackScale1; |
| 1328 |
|
| 1329 |
} |
| 1330 |
|
| 1331 |
uint32 cols = overlap.W (); |
| 1332 |
|
| 1333 |
uint32 colPitch = fAreaSpec.ColPitch (); |
| 1334 |
|
| 1335 |
colPitch = Min_uint32 (colPitch, cols); |
| 1336 |
|
| 1337 |
for (uint32 plane = fAreaSpec.Plane (); |
| 1338 |
plane < fAreaSpec.Plane () + fAreaSpec.Planes () && |
| 1339 |
plane < buffer.Planes (); |
| 1340 |
plane++) |
| 1341 |
{ |
| 1342 |
|
| 1343 |
uint32 mapPlane = Min_uint32 (plane, fGainMap->Planes () - 1); |
| 1344 |
|
| 1345 |
for (int32 row = overlap.t; row < overlap.b; row += fAreaSpec.RowPitch ()) |
| 1346 |
{ |
| 1347 |
|
| 1348 |
real32 *dPtr = buffer.DirtyPixel_real32 (row, overlap.l, plane); |
| 1349 |
|
| 1350 |
dng_gain_map_interpolator interp (*fGainMap, |
| 1351 |
imageBounds, |
| 1352 |
row, |
| 1353 |
overlap.l, |
| 1354 |
mapPlane); |
| 1355 |
|
| 1356 |
if (blackLevel != 0) |
| 1357 |
{ |
| 1358 |
|
| 1359 |
for (uint32 col = 0; col < cols; col += colPitch) |
| 1360 |
{ |
| 1361 |
|
| 1362 |
dPtr [col] = dPtr [col] * blackScale1 + blackOffset1; |
| 1363 |
|
| 1364 |
} |
| 1365 |
|
| 1366 |
} |
| 1367 |
|
| 1368 |
for (uint32 col = 0; col < cols; col += colPitch) |
| 1369 |
{ |
| 1370 |
|
| 1371 |
real32 gain = interp.Interpolate (); |
| 1372 |
|
| 1373 |
dPtr [col] = Min_real32 (dPtr [col] * gain, 1.0f); |
| 1374 |
|
| 1375 |
for (uint32 j = 0; j < colPitch; j++) |
| 1376 |
{ |
| 1377 |
interp.Increment (); |
| 1378 |
} |
| 1379 |
|
| 1380 |
} |
| 1381 |
|
| 1382 |
if (blackLevel != 0) |
| 1383 |
{ |
| 1384 |
|
| 1385 |
for (uint32 col = 0; col < cols; col += colPitch) |
| 1386 |
{ |
| 1387 |
|
| 1388 |
dPtr [col] = dPtr [col] * blackScale2 + blackOffset2; |
| 1389 |
|
| 1390 |
} |
| 1391 |
|
| 1392 |
} |
| 1393 |
|
| 1394 |
} |
| 1395 |
|
| 1396 |
} |
| 1397 |
|
| 1398 |
} |
| 1399 |
|
| 1400 |
} |
| 1401 |
|
| 1402 |
/*****************************************************************************/ |