| 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 |
/** \file |
| 10 |
* Opcode to fix 2D uniformity defects, such as shading. |
| 11 |
*/ |
| 12 |
|
| 13 |
/*****************************************************************************/ |
| 14 |
|
| 15 |
#ifndef __dng_gain_map__ |
| 16 |
#define __dng_gain_map__ |
| 17 |
|
| 18 |
/*****************************************************************************/ |
| 19 |
|
| 20 |
#include "dng_classes.h" |
| 21 |
#include "dng_fingerprint.h" |
| 22 |
#include "dng_memory.h" |
| 23 |
#include "dng_misc_opcodes.h" |
| 24 |
#include "dng_tag_types.h" |
| 25 |
#include "dng_uncopyable.h" |
| 26 |
|
| 27 |
/*****************************************************************************/ |
| 28 |
|
| 29 |
/// @brief Holds a discrete (i.e., sampled) 2D representation of a gain map. This is |
| 30 |
/// effectively an image containing scale factors. |
| 31 |
|
| 32 |
class dng_gain_map: private dng_uncopyable |
| 33 |
{ |
| 34 |
|
| 35 |
private: |
| 36 |
|
| 37 |
dng_point fPoints; |
| 38 |
|
| 39 |
dng_point_real64 fSpacing; |
| 40 |
|
| 41 |
dng_point_real64 fOrigin; |
| 42 |
|
| 43 |
uint32 fPlanes; |
| 44 |
|
| 45 |
uint32 fRowStep; |
| 46 |
|
| 47 |
AutoPtr<dng_memory_block> fBuffer; |
| 48 |
|
| 49 |
public: |
| 50 |
|
| 51 |
/// Construct a gain map with the specified memory allocator, number of |
| 52 |
/// samples (points), sample spacing, origin, and number of color planes. |
| 53 |
|
| 54 |
dng_gain_map (dng_memory_allocator &allocator, |
| 55 |
const dng_point &points, |
| 56 |
const dng_point_real64 &spacing, |
| 57 |
const dng_point_real64 &origin, |
| 58 |
uint32 planes); |
| 59 |
|
| 60 |
/// The number of samples in the horizontal and vertical directions. |
| 61 |
|
| 62 |
const dng_point & Points () const |
| 63 |
{ |
| 64 |
return fPoints; |
| 65 |
} |
| 66 |
|
| 67 |
/// The space between adjacent samples in the horizontal and vertical |
| 68 |
/// directions. |
| 69 |
|
| 70 |
const dng_point_real64 & Spacing () const |
| 71 |
{ |
| 72 |
return fSpacing; |
| 73 |
} |
| 74 |
|
| 75 |
/// The 2D coordinate for the first (i.e., top-left-most) sample. |
| 76 |
|
| 77 |
const dng_point_real64 & Origin () const |
| 78 |
{ |
| 79 |
return fOrigin; |
| 80 |
} |
| 81 |
|
| 82 |
/// The number of color planes. |
| 83 |
|
| 84 |
uint32 Planes () const |
| 85 |
{ |
| 86 |
return fPlanes; |
| 87 |
} |
| 88 |
|
| 89 |
/// Getter for a gain map sample (specified by row, column, and plane). |
| 90 |
|
| 91 |
real32 & Entry (uint32 rowIndex, |
| 92 |
uint32 colIndex, |
| 93 |
uint32 plane) |
| 94 |
{ |
| 95 |
|
| 96 |
return *(fBuffer->Buffer_real32 () + |
| 97 |
rowIndex * fRowStep + |
| 98 |
colIndex * fPlanes + |
| 99 |
plane); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/// Getter for a gain map sample (specified by row index, column index, and |
| 104 |
/// plane index). |
| 105 |
|
| 106 |
const real32 & Entry (uint32 rowIndex, |
| 107 |
uint32 colIndex, |
| 108 |
uint32 plane) const |
| 109 |
{ |
| 110 |
|
| 111 |
return *(fBuffer->Buffer_real32 () + |
| 112 |
rowIndex * fRowStep + |
| 113 |
colIndex * fPlanes + |
| 114 |
plane); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/// Compute the interpolated gain (i.e., scale factor) at the specified pixel |
| 119 |
/// position and color plane, within the specified image bounds (in pixels). |
| 120 |
|
| 121 |
real32 Interpolate (int32 row, |
| 122 |
int32 col, |
| 123 |
uint32 plane, |
| 124 |
const dng_rect &bounds) const; |
| 125 |
|
| 126 |
/// The number of bytes needed to hold the gain map data. |
| 127 |
|
| 128 |
uint32 PutStreamSize () const; |
| 129 |
|
| 130 |
/// Write the gain map to the specified stream. |
| 131 |
|
| 132 |
void PutStream (dng_stream &stream) const; |
| 133 |
|
| 134 |
/// Read a gain map from the specified stream. |
| 135 |
|
| 136 |
static dng_gain_map * GetStream (dng_host &host, |
| 137 |
dng_stream &stream); |
| 138 |
|
| 139 |
}; |
| 140 |
|
| 141 |
/*****************************************************************************/ |
| 142 |
|
| 143 |
/// @brief Holds a discrete (i.e., sampled) 2D representation of a gain table |
| 144 |
/// map. This is effectively an image containing tables of scale factors. |
| 145 |
/// Corresponds to the ProfileGainTableMap tag introduced in DNG 1.6 and the |
| 146 |
/// ProfileGainTableMap2 tag introduced in DNG 1.7. |
| 147 |
|
| 148 |
class dng_gain_table_map: private dng_uncopyable |
| 149 |
{ |
| 150 |
|
| 151 |
private: |
| 152 |
|
| 153 |
dng_point fPoints; // MapPointsV, MapPointsH |
| 154 |
|
| 155 |
dng_point_real64 fSpacing; // MapSpacingV, MapSpacingH |
| 156 |
|
| 157 |
dng_point_real64 fOrigin; // MapOriginV, MapOriginH |
| 158 |
|
| 159 |
uint32 fNumTablePoints = 0; // MapPointsN |
| 160 |
|
| 161 |
uint32 fRowStep = 0; |
| 162 |
uint32 fColStep = 0; |
| 163 |
|
| 164 |
uint32 fNumSamples = 0; |
| 165 |
|
| 166 |
uint32 fSampleBytes = 0; |
| 167 |
|
| 168 |
real32 fMapInputWeights [5]; // MapInputWeights |
| 169 |
|
| 170 |
AutoPtr<dng_memory_block> fBuffer; |
| 171 |
|
| 172 |
mutable dng_fingerprint fFingerprint; |
| 173 |
|
| 174 |
// Fields to support ProfileGainTableMap2. |
| 175 |
|
| 176 |
// Data type of gain values. Supported values: |
| 177 |
// |
| 178 |
// 0 = 8-bit unsigned integer |
| 179 |
// 1 = 16-bit unsigned integer |
| 180 |
// 2 = 16-bit floating-point |
| 181 |
// 3 = 32-bit floating-point |
| 182 |
|
| 183 |
uint32 fDataType = 3; |
| 184 |
|
| 185 |
// Gamma value. |
| 186 |
|
| 187 |
real32 fGamma = 1.0f; |
| 188 |
|
| 189 |
// Minimum and maximum gain values when data type is integer. |
| 190 |
|
| 191 |
real32 fGainMin = 1.0f; |
| 192 |
real32 fGainMax = 1.0f; |
| 193 |
|
| 194 |
// Buffer used to hold a copy of the original table data; used only in |
| 195 |
// the case of integer data types. |
| 196 |
|
| 197 |
AutoPtr<dng_memory_block> fOriginalBuffer; |
| 198 |
|
| 199 |
public: |
| 200 |
|
| 201 |
/// Construct a gain map with the specified memory allocator, number |
| 202 |
/// of samples (points), sample spacing, origin, number of table |
| 203 |
/// points, and weights. |
| 204 |
|
| 205 |
dng_gain_table_map (dng_memory_allocator &allocator, |
| 206 |
const dng_point &points, |
| 207 |
const dng_point_real64 &spacing, |
| 208 |
const dng_point_real64 &origin, |
| 209 |
uint32 numTablePoints, |
| 210 |
const real32 weights [5], |
| 211 |
uint32 dataType = 2, |
| 212 |
real32 gamma = 1.0f, |
| 213 |
real32 gainMin = 1.0f, |
| 214 |
real32 gainMax = 1.0f); |
| 215 |
|
| 216 |
/// The number of samples in the horizontal and vertical directions. |
| 217 |
|
| 218 |
const dng_point & Points () const |
| 219 |
{ |
| 220 |
return fPoints; |
| 221 |
} |
| 222 |
|
| 223 |
/// The space between adjacent samples in the horizontal and vertical |
| 224 |
/// directions. |
| 225 |
|
| 226 |
const dng_point_real64 & Spacing () const |
| 227 |
{ |
| 228 |
return fSpacing; |
| 229 |
} |
| 230 |
|
| 231 |
/// The 2D coordinate for the first (i.e., top-left-most) sample. |
| 232 |
|
| 233 |
const dng_point_real64 & Origin () const |
| 234 |
{ |
| 235 |
return fOrigin; |
| 236 |
} |
| 237 |
|
| 238 |
/// Getter for the number of table points. |
| 239 |
|
| 240 |
uint32 NumTablePoints () const |
| 241 |
{ |
| 242 |
return fNumTablePoints; |
| 243 |
} |
| 244 |
|
| 245 |
/// Getter for total number of samples. Product of table points and |
| 246 |
/// NumTablePoints. |
| 247 |
|
| 248 |
uint32 NumSamples () const |
| 249 |
{ |
| 250 |
return fNumSamples; |
| 251 |
} |
| 252 |
|
| 253 |
/// Getter for number of bytes used to represent just the samples. |
| 254 |
|
| 255 |
uint32 SampleBytes () const |
| 256 |
{ |
| 257 |
return fSampleBytes; |
| 258 |
} |
| 259 |
|
| 260 |
/// Getter for MapInputWeights. |
| 261 |
|
| 262 |
const real32 * MapInputWeights () const |
| 263 |
{ |
| 264 |
return fMapInputWeights; |
| 265 |
} |
| 266 |
|
| 267 |
/// Getter for a gain table map sample (specified by row, column, and |
| 268 |
/// table index). |
| 269 |
|
| 270 |
real32 & Entry (uint32 rowIndex, |
| 271 |
uint32 colIndex, |
| 272 |
uint32 tableIndex) |
| 273 |
{ |
| 274 |
|
| 275 |
return *(fBuffer->Buffer_real32 () + |
| 276 |
rowIndex * fRowStep + |
| 277 |
colIndex * fColStep + |
| 278 |
tableIndex); |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
/// Getter for a gain map sample (specified by row index, column index, and |
| 283 |
/// plane index). |
| 284 |
|
| 285 |
const real32 & Entry (uint32 rowIndex, |
| 286 |
uint32 colIndex, |
| 287 |
uint32 tableIndex) const |
| 288 |
{ |
| 289 |
|
| 290 |
return *(fBuffer->Buffer_real32 () + |
| 291 |
rowIndex * fRowStep + |
| 292 |
colIndex * fColStep + |
| 293 |
tableIndex); |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
/// Getters for low level processing. |
| 298 |
|
| 299 |
uint32 RowStep () const |
| 300 |
{ |
| 301 |
return fRowStep; |
| 302 |
} |
| 303 |
|
| 304 |
uint32 ColStep () const |
| 305 |
{ |
| 306 |
return fColStep; |
| 307 |
} |
| 308 |
|
| 309 |
/// Memory block holding the fp32 version of the table. |
| 310 |
|
| 311 |
dng_memory_block * Block () |
| 312 |
{ |
| 313 |
return fBuffer.Get (); |
| 314 |
} |
| 315 |
|
| 316 |
const dng_memory_block * Block () const |
| 317 |
{ |
| 318 |
return fBuffer.Get (); |
| 319 |
} |
| 320 |
|
| 321 |
/// Direct access to the fp32 version of the table. |
| 322 |
|
| 323 |
void * RawTablePtr () const; |
| 324 |
|
| 325 |
uint32 RawTableNumBytes () const; |
| 326 |
|
| 327 |
/// The number of bytes needed to hold the gain table map data. |
| 328 |
|
| 329 |
uint32 PutStreamSize () const; |
| 330 |
|
| 331 |
/// Write the gain table map to the specified stream. |
| 332 |
|
| 333 |
void PutStream (dng_stream &stream, |
| 334 |
bool forceVersion2 = false) const; |
| 335 |
|
| 336 |
/// Add the gain table map to the given digest printer. |
| 337 |
|
| 338 |
void AddDigest (dng_md5_printer &printer) const; |
| 339 |
|
| 340 |
/// Fingerprint for the gain table map. Computed lazily. |
| 341 |
|
| 342 |
dng_fingerprint GetFingerprint () const; |
| 343 |
|
| 344 |
/// Read a gain table map from the specified stream. |
| 345 |
|
| 346 |
static dng_gain_table_map * GetStream (dng_host &host, |
| 347 |
dng_stream &stream, |
| 348 |
bool useVersion2 = false); |
| 349 |
|
| 350 |
/// APIs to support ProfileGainTableMap2. |
| 351 |
|
| 352 |
uint32 DataType () const |
| 353 |
{ |
| 354 |
return fDataType; |
| 355 |
} |
| 356 |
|
| 357 |
bool IsFloat16 () const |
| 358 |
{ |
| 359 |
return fDataType == 2; |
| 360 |
} |
| 361 |
|
| 362 |
bool IsFloat32 () const |
| 363 |
{ |
| 364 |
return fDataType >= 3; |
| 365 |
} |
| 366 |
|
| 367 |
bool IsUint8 () const |
| 368 |
{ |
| 369 |
return fDataType == 0; |
| 370 |
} |
| 371 |
|
| 372 |
bool IsUint16 () const |
| 373 |
{ |
| 374 |
return fDataType == 1; |
| 375 |
} |
| 376 |
|
| 377 |
real32 GainMin () const |
| 378 |
{ |
| 379 |
return fGainMin; |
| 380 |
} |
| 381 |
|
| 382 |
real32 GainMax () const |
| 383 |
{ |
| 384 |
return fGainMax; |
| 385 |
} |
| 386 |
|
| 387 |
real32 Gamma () const |
| 388 |
{ |
| 389 |
return fGamma; |
| 390 |
} |
| 391 |
|
| 392 |
bool SupportsVersion1 () const; |
| 393 |
|
| 394 |
bool RequiresVersion2 () const |
| 395 |
{ |
| 396 |
return !SupportsVersion1 (); |
| 397 |
} |
| 398 |
|
| 399 |
uint32 BytesPerEntry () const |
| 400 |
{ |
| 401 |
if (fDataType == 0) return 1; // u8 |
| 402 |
if (fDataType <= 2) return 2; // u16, fp16 |
| 403 |
return 4; // fp32 |
| 404 |
} |
| 405 |
|
| 406 |
// Bytes required to store the table data itself (not including |
| 407 |
// headers). |
| 408 |
|
| 409 |
uint32 DataStorageBytes () const; |
| 410 |
|
| 411 |
void ClearOriginalBuffer (); |
| 412 |
|
| 413 |
bool HasOriginalBuffer () const; |
| 414 |
|
| 415 |
const dng_memory_block * OriginalBuffer () const; |
| 416 |
|
| 417 |
void SetOriginalBuffer (AutoPtr<dng_memory_block> &blockToRelease); |
| 418 |
|
| 419 |
private: |
| 420 |
|
| 421 |
/// Ensure fingerprint has been calculated. |
| 422 |
|
| 423 |
void EnsureFingerprint () const; |
| 424 |
|
| 425 |
}; |
| 426 |
|
| 427 |
/*****************************************************************************/ |
| 428 |
|
| 429 |
/// @brief An opcode to fix 2D spatially-varying light falloff or color casts (i.e., |
| 430 |
/// uniformity issues). This is commonly due to shading. |
| 431 |
|
| 432 |
class dng_opcode_GainMap: public dng_inplace_opcode, |
| 433 |
private dng_uncopyable |
| 434 |
{ |
| 435 |
|
| 436 |
private: |
| 437 |
|
| 438 |
dng_area_spec fAreaSpec; |
| 439 |
|
| 440 |
AutoPtr<dng_gain_map> fGainMap; |
| 441 |
|
| 442 |
public: |
| 443 |
|
| 444 |
/// Construct a GainMap opcode for the specified image area and the specified |
| 445 |
/// gain map. |
| 446 |
|
| 447 |
dng_opcode_GainMap (const dng_area_spec &areaSpec, |
| 448 |
AutoPtr<dng_gain_map> &gainMap); |
| 449 |
|
| 450 |
/// Construct a GainMap opcode from the specified stream. |
| 451 |
|
| 452 |
dng_opcode_GainMap (dng_host &host, |
| 453 |
dng_stream &stream); |
| 454 |
|
| 455 |
/// Const accessors. |
| 456 |
|
| 457 |
const dng_area_spec& AreaSpec() const { return fAreaSpec; } |
| 458 |
const dng_gain_map& GainMap() const { return *fGainMap; } |
| 459 |
|
| 460 |
/// Write the opcode to the specified stream. |
| 461 |
|
| 462 |
virtual void PutData (dng_stream &stream) const override; |
| 463 |
|
| 464 |
/// The pixel data type of this opcode. |
| 465 |
|
| 466 |
virtual uint32 BufferPixelType (uint32 /* imagePixelType */) override |
| 467 |
{ |
| 468 |
return ttFloat; |
| 469 |
} |
| 470 |
|
| 471 |
/// The adjusted bounds (processing area) of this opcode. It is limited to |
| 472 |
/// the intersection of the specified image area and the GainMap area. |
| 473 |
|
| 474 |
virtual dng_rect ModifiedBounds (const dng_rect &imageBounds) override |
| 475 |
{ |
| 476 |
return fAreaSpec.ScaledOverlap (imageBounds); |
| 477 |
} |
| 478 |
|
| 479 |
/// Apply the gain map. |
| 480 |
|
| 481 |
virtual void ProcessArea (dng_negative &negative, |
| 482 |
uint32 threadIndex, |
| 483 |
dng_pixel_buffer &buffer, |
| 484 |
const dng_rect &dstArea, |
| 485 |
const dng_rect &imageBounds) override; |
| 486 |
|
| 487 |
void ApplyAreaScale (const dng_urational &scale) override |
| 488 |
{ |
| 489 |
fAreaSpec.ApplyAreaScale (scale); |
| 490 |
} |
| 491 |
|
| 492 |
}; |
| 493 |
|
| 494 |
/*****************************************************************************/ |
| 495 |
|
| 496 |
#endif |
| 497 |
|
| 498 |
/*****************************************************************************/ |