| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-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_mosaic_info.h" |
| 10 |
|
| 11 |
#include "dng_area_task.h" |
| 12 |
#include "dng_assertions.h" |
| 13 |
#include "dng_bottlenecks.h" |
| 14 |
#include "dng_exceptions.h" |
| 15 |
#include "dng_filter_task.h" |
| 16 |
#include "dng_host.h" |
| 17 |
#include "dng_ifd.h" |
| 18 |
#include "dng_image.h" |
| 19 |
#include "dng_info.h" |
| 20 |
#include "dng_negative.h" |
| 21 |
#include "dng_pixel_buffer.h" |
| 22 |
#include "dng_tag_types.h" |
| 23 |
#include "dng_tag_values.h" |
| 24 |
#include "dng_tile_iterator.h" |
| 25 |
#include "dng_utils.h" |
| 26 |
|
| 27 |
/*****************************************************************************/ |
| 28 |
|
| 29 |
// A interpolation kernel for a single pixel of a single plane. |
| 30 |
|
| 31 |
class dng_bilinear_kernel |
| 32 |
{ |
| 33 |
|
| 34 |
public: |
| 35 |
|
| 36 |
enum |
| 37 |
{ |
| 38 |
kMaxCount = 8 |
| 39 |
}; |
| 40 |
|
| 41 |
uint32 fCount; |
| 42 |
|
| 43 |
dng_point fDelta [kMaxCount]; |
| 44 |
|
| 45 |
real32 fWeight32 [kMaxCount]; |
| 46 |
uint16 fWeight16 [kMaxCount]; |
| 47 |
|
| 48 |
int32 fOffset [kMaxCount]; |
| 49 |
|
| 50 |
public: |
| 51 |
|
| 52 |
dng_bilinear_kernel () |
| 53 |
: fCount (0) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
void Add (const dng_point &delta, |
| 58 |
real32 weight); |
| 59 |
|
| 60 |
void Finalize (const dng_point &scale, |
| 61 |
uint32 patRow, |
| 62 |
uint32 patCol, |
| 63 |
int32 rowStep, |
| 64 |
int32 colStep); |
| 65 |
|
| 66 |
}; |
| 67 |
|
| 68 |
/*****************************************************************************/ |
| 69 |
|
| 70 |
void dng_bilinear_kernel::Add (const dng_point &delta, |
| 71 |
real32 weight) |
| 72 |
{ |
| 73 |
|
| 74 |
// Don't add zero weight elements. |
| 75 |
|
| 76 |
if (weight <= 0.0f) |
| 77 |
{ |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// If the delta already matches an existing element, just combine the |
| 82 |
// weights. |
| 83 |
|
| 84 |
for (uint32 j = 0; j < fCount; j++) |
| 85 |
{ |
| 86 |
|
| 87 |
if (fDelta [j] == delta) |
| 88 |
{ |
| 89 |
|
| 90 |
fWeight32 [j] += weight; |
| 91 |
|
| 92 |
return; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
// Add element to list. |
| 99 |
|
| 100 |
DNG_ASSERT (fCount < kMaxCount, "Too many kernel entries"); |
| 101 |
|
| 102 |
fDelta [fCount] = delta; |
| 103 |
fWeight32 [fCount] = weight; |
| 104 |
|
| 105 |
fCount++; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/*****************************************************************************/ |
| 110 |
|
| 111 |
void dng_bilinear_kernel::Finalize (const dng_point &scale, |
| 112 |
uint32 patRow, |
| 113 |
uint32 patCol, |
| 114 |
int32 rowStep, |
| 115 |
int32 colStep) |
| 116 |
{ |
| 117 |
|
| 118 |
uint32 j; |
| 119 |
|
| 120 |
// Adjust deltas to compensate for interpolation upscaling. |
| 121 |
|
| 122 |
for (j = 0; j < fCount; j++) |
| 123 |
{ |
| 124 |
|
| 125 |
dng_point &delta = fDelta [j]; |
| 126 |
|
| 127 |
if (scale.v == 2) |
| 128 |
{ |
| 129 |
|
| 130 |
delta.v = (delta.v + (int32) (patRow & 1)) >> 1; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
if (scale.h == 2) |
| 135 |
{ |
| 136 |
|
| 137 |
delta.h = (delta.h + (int32) (patCol & 1)) >> 1; |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
// Sort entries into row-column scan order. |
| 144 |
|
| 145 |
while (true) |
| 146 |
{ |
| 147 |
|
| 148 |
bool didSwap = false; |
| 149 |
|
| 150 |
for (j = 1; j < fCount; j++) |
| 151 |
{ |
| 152 |
|
| 153 |
dng_point &delta0 = fDelta [j - 1]; |
| 154 |
dng_point &delta1 = fDelta [j ]; |
| 155 |
|
| 156 |
if (delta0.v > delta1.v || |
| 157 |
(delta0.v == delta1.v && |
| 158 |
delta0.h > delta1.h)) |
| 159 |
{ |
| 160 |
|
| 161 |
didSwap = true; |
| 162 |
|
| 163 |
dng_point tempDelta = delta0; |
| 164 |
|
| 165 |
delta0 = delta1; |
| 166 |
delta1 = tempDelta; |
| 167 |
|
| 168 |
real32 tempWeight = fWeight32 [j - 1]; |
| 169 |
|
| 170 |
fWeight32 [j - 1] = fWeight32 [j]; |
| 171 |
fWeight32 [j ] = tempWeight; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
if (!didSwap) |
| 178 |
{ |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
// Calculate offsets. |
| 185 |
|
| 186 |
for (j = 0; j < fCount; j++) |
| 187 |
{ |
| 188 |
|
| 189 |
fOffset [j] = rowStep * fDelta [j].v + |
| 190 |
colStep * fDelta [j].h; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
// Calculate 16-bit weights. |
| 195 |
|
| 196 |
uint16 total = 0; |
| 197 |
uint32 biggest = 0; |
| 198 |
|
| 199 |
for (j = 0; j < fCount; j++) |
| 200 |
{ |
| 201 |
|
| 202 |
// Round weights to 8 fractional bits. |
| 203 |
|
| 204 |
fWeight16 [j] = (uint16) Round_uint32 (fWeight32 [j] * 256.0); |
| 205 |
|
| 206 |
// Keep track of total of weights. |
| 207 |
|
| 208 |
total += fWeight16 [j]; |
| 209 |
|
| 210 |
// Keep track of which weight is biggest. |
| 211 |
|
| 212 |
if (fWeight16 [biggest] < fWeight16 [j]) |
| 213 |
{ |
| 214 |
|
| 215 |
biggest = j; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
// Adjust largest entry so total of weights is exactly 256. |
| 222 |
|
| 223 |
fWeight16 [biggest] += (256 - total); |
| 224 |
|
| 225 |
// Recompute the floating point weights from the rounded integer weights |
| 226 |
// so results match more closely. |
| 227 |
|
| 228 |
for (j = 0; j < fCount; j++) |
| 229 |
{ |
| 230 |
|
| 231 |
fWeight32 [j] = fWeight16 [j] * (1.0f / 256.0f); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
/*****************************************************************************/ |
| 238 |
|
| 239 |
class dng_bilinear_pattern |
| 240 |
{ |
| 241 |
|
| 242 |
public: |
| 243 |
|
| 244 |
enum |
| 245 |
{ |
| 246 |
kMaxPattern = kMaxCFAPattern * 2 |
| 247 |
}; |
| 248 |
|
| 249 |
dng_point fScale; |
| 250 |
|
| 251 |
uint32 fPatRows; |
| 252 |
uint32 fPatCols; |
| 253 |
|
| 254 |
dng_bilinear_kernel fKernel [kMaxPattern] |
| 255 |
[kMaxPattern]; |
| 256 |
|
| 257 |
uint32 fCounts [kMaxPattern] |
| 258 |
[kMaxPattern]; |
| 259 |
|
| 260 |
int32 *fOffsets [kMaxPattern] |
| 261 |
[kMaxPattern]; |
| 262 |
|
| 263 |
uint16 *fWeights16 [kMaxPattern] |
| 264 |
[kMaxPattern]; |
| 265 |
|
| 266 |
real32 *fWeights32 [kMaxPattern] |
| 267 |
[kMaxPattern]; |
| 268 |
|
| 269 |
public: |
| 270 |
|
| 271 |
dng_bilinear_pattern () |
| 272 |
|
| 273 |
: fScale () |
| 274 |
, fPatRows (0) |
| 275 |
, fPatCols (0) |
| 276 |
|
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
private: |
| 281 |
|
| 282 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 283 |
uint32 DeltaRow (uint32 row, int32 delta) |
| 284 |
{ |
| 285 |
// Potential overflow in the conversion from delta to a uint32 as |
| 286 |
// well as in the subsequent addition is intentional. |
| 287 |
return (SafeUint32Add (row, fPatRows) + (uint32) delta) % fPatRows; |
| 288 |
} |
| 289 |
|
| 290 |
DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow") |
| 291 |
uint32 DeltaCol (uint32 col, int32 delta) |
| 292 |
{ |
| 293 |
// Potential overflow in the conversion from delta to a uint32 as |
| 294 |
// well as in the subsequent addition is intentional. |
| 295 |
return (SafeUint32Add (col, fPatCols) + (uint32) delta) % fPatCols; |
| 296 |
} |
| 297 |
|
| 298 |
real32 LinearWeight1 (int32 d1, int32 d2) |
| 299 |
{ |
| 300 |
if (d1 == d2) |
| 301 |
return 1.0f; |
| 302 |
else |
| 303 |
return d2 / (real32) (d2 - d1); |
| 304 |
} |
| 305 |
|
| 306 |
real32 LinearWeight2 (int32 d1, int32 d2) |
| 307 |
{ |
| 308 |
if (d1 == d2) |
| 309 |
return 0.0f; |
| 310 |
else |
| 311 |
return -d1 / (real32) (d2 - d1); |
| 312 |
} |
| 313 |
|
| 314 |
public: |
| 315 |
|
| 316 |
void Calculate (const dng_mosaic_info &info, |
| 317 |
uint32 dstPlane, |
| 318 |
int32 rowStep, |
| 319 |
int32 colStep); |
| 320 |
|
| 321 |
}; |
| 322 |
|
| 323 |
/*****************************************************************************/ |
| 324 |
|
| 325 |
void dng_bilinear_pattern::Calculate (const dng_mosaic_info &info, |
| 326 |
uint32 dstPlane, |
| 327 |
int32 rowStep, |
| 328 |
int32 colStep) |
| 329 |
{ |
| 330 |
|
| 331 |
uint32 j; |
| 332 |
uint32 k; |
| 333 |
uint32 patRow; |
| 334 |
uint32 patCol; |
| 335 |
|
| 336 |
// Find destination pattern size. |
| 337 |
|
| 338 |
fScale = info.FullScale (); |
| 339 |
|
| 340 |
fPatRows = info.fCFAPatternSize.v * fScale.v; |
| 341 |
fPatCols = info.fCFAPatternSize.h * fScale.h; |
| 342 |
|
| 343 |
// See if we need to scale up just while computing the kernels. |
| 344 |
|
| 345 |
dng_point tempScale (1, 1); |
| 346 |
|
| 347 |
if (info.fCFALayout >= 6) |
| 348 |
{ |
| 349 |
|
| 350 |
tempScale = dng_point (2, 2); |
| 351 |
|
| 352 |
fPatRows *= tempScale.v; |
| 353 |
fPatCols *= tempScale.h; |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
// Find a boolean map for this plane color and layout. |
| 358 |
|
| 359 |
bool map [kMaxPattern] |
| 360 |
[kMaxPattern]; |
| 361 |
|
| 362 |
uint8 planeColor = info.fCFAPlaneColor [dstPlane]; |
| 363 |
|
| 364 |
switch (info.fCFALayout) |
| 365 |
{ |
| 366 |
|
| 367 |
case 1: // Rectangular (or square) layout |
| 368 |
{ |
| 369 |
|
| 370 |
for (j = 0; j < fPatRows; j++) |
| 371 |
{ |
| 372 |
|
| 373 |
for (k = 0; k < fPatCols; k++) |
| 374 |
{ |
| 375 |
|
| 376 |
map [j] [k] = (info.fCFAPattern [j] [k] == planeColor); |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
break; |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
// Note that when the descriptions of the staggered patterns refer to even rows or |
| 387 |
// columns, this mean the second, fourth, etc. (i.e. using one-based numbering). |
| 388 |
// This needs to be clarified in the DNG specification. |
| 389 |
|
| 390 |
case 2: // Staggered layout A: even (1-based) columns are offset down by 1/2 row |
| 391 |
{ |
| 392 |
|
| 393 |
for (j = 0; j < fPatRows; j++) |
| 394 |
{ |
| 395 |
|
| 396 |
for (k = 0; k < fPatCols; k++) |
| 397 |
{ |
| 398 |
|
| 399 |
if ((j & 1) != (k & 1)) |
| 400 |
{ |
| 401 |
|
| 402 |
map [j] [k] = false; |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
else |
| 407 |
{ |
| 408 |
|
| 409 |
map [j] [k] = (info.fCFAPattern [j >> 1] [k] == planeColor); |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
break; |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
case 3: // Staggered layout B: even (1-based) columns are offset up by 1/2 row |
| 422 |
{ |
| 423 |
|
| 424 |
for (j = 0; j < fPatRows; j++) |
| 425 |
{ |
| 426 |
|
| 427 |
for (k = 0; k < fPatCols; k++) |
| 428 |
{ |
| 429 |
|
| 430 |
if ((j & 1) == (k & 1)) |
| 431 |
{ |
| 432 |
|
| 433 |
map [j] [k] = false; |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
else |
| 438 |
{ |
| 439 |
|
| 440 |
map [j] [k] = (info.fCFAPattern [j >> 1] [k] == planeColor); |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
break; |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
case 4: // Staggered layout C: even (1-based) rows are offset right by 1/2 column |
| 453 |
{ |
| 454 |
|
| 455 |
for (j = 0; j < fPatRows; j++) |
| 456 |
{ |
| 457 |
|
| 458 |
for (k = 0; k < fPatCols; k++) |
| 459 |
{ |
| 460 |
|
| 461 |
if ((j & 1) != (k & 1)) |
| 462 |
{ |
| 463 |
|
| 464 |
map [j] [k] = false; |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
else |
| 469 |
{ |
| 470 |
|
| 471 |
map [j] [k] = (info.fCFAPattern [j] [k >> 1] == planeColor); |
| 472 |
|
| 473 |
} |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
} |
| 478 |
|
| 479 |
break; |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
case 5: // Staggered layout D: even (1-based) rows are offset left by 1/2 column |
| 484 |
{ |
| 485 |
|
| 486 |
for (j = 0; j < fPatRows; j++) |
| 487 |
{ |
| 488 |
|
| 489 |
for (k = 0; k < fPatCols; k++) |
| 490 |
{ |
| 491 |
|
| 492 |
if ((j & 1) == (k & 1)) |
| 493 |
{ |
| 494 |
|
| 495 |
map [j] [k] = false; |
| 496 |
|
| 497 |
} |
| 498 |
|
| 499 |
else |
| 500 |
{ |
| 501 |
|
| 502 |
map [j] [k] = (info.fCFAPattern [j] [k >> 1] == planeColor); |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
break; |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
case 6: // Staggered layout E: even rows are offset up by 1/2 row, even columns are offset left by 1/2 column |
| 515 |
case 7: // Staggered layout F: even rows are offset up by 1/2 row, even columns are offset right by 1/2 column |
| 516 |
case 8: // Staggered layout G: even rows are offset down by 1/2 row, even columns are offset left by 1/2 column |
| 517 |
case 9: // Staggered layout H: even rows are offset down by 1/2 row, even columns are offset right by 1/2 column |
| 518 |
{ |
| 519 |
|
| 520 |
uint32 eRow = (info.fCFALayout == 6 || |
| 521 |
info.fCFALayout == 7) ? 1 : 3; |
| 522 |
|
| 523 |
uint32 eCol = (info.fCFALayout == 6 || |
| 524 |
info.fCFALayout == 8) ? 1 : 3; |
| 525 |
|
| 526 |
for (j = 0; j < fPatRows; j++) |
| 527 |
{ |
| 528 |
|
| 529 |
for (k = 0; k < fPatCols; k++) |
| 530 |
{ |
| 531 |
|
| 532 |
uint32 jj = j & 3; |
| 533 |
uint32 kk = k & 3; |
| 534 |
|
| 535 |
if ((jj != 0 && jj != eRow) || |
| 536 |
(kk != 0 && kk != eCol)) |
| 537 |
{ |
| 538 |
|
| 539 |
map [j] [k] = false; |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
else |
| 544 |
{ |
| 545 |
|
| 546 |
map [j] [k] = (info.fCFAPattern [((j >> 1) & ~1) + Min_uint32 (jj, 1)] |
| 547 |
[((k >> 1) & ~1) + Min_uint32 (kk, 1)] == planeColor); |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
} |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
break; |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
default: |
| 560 |
ThrowProgramError (); |
| 561 |
|
| 562 |
} |
| 563 |
|
| 564 |
// Find projections of maps. |
| 565 |
|
| 566 |
bool mapH [kMaxPattern]; |
| 567 |
bool mapV [kMaxPattern]; |
| 568 |
|
| 569 |
for (j = 0; j < kMaxPattern; j++) |
| 570 |
{ |
| 571 |
|
| 572 |
mapH [j] = false; |
| 573 |
mapV [j] = false; |
| 574 |
|
| 575 |
} |
| 576 |
|
| 577 |
for (j = 0; j < fPatRows; j++) |
| 578 |
{ |
| 579 |
|
| 580 |
for (k = 0; k < fPatCols; k++) |
| 581 |
{ |
| 582 |
|
| 583 |
if (map [j] [k]) |
| 584 |
{ |
| 585 |
|
| 586 |
mapV [j] = true; |
| 587 |
mapH [k] = true; |
| 588 |
|
| 589 |
} |
| 590 |
|
| 591 |
} |
| 592 |
|
| 593 |
} |
| 594 |
|
| 595 |
// Find kernel for each patten entry. |
| 596 |
|
| 597 |
for (patRow = 0; patRow < fPatRows; patRow += tempScale.v) |
| 598 |
{ |
| 599 |
|
| 600 |
for (patCol = 0; patCol < fPatCols; patCol += tempScale.h) |
| 601 |
{ |
| 602 |
|
| 603 |
dng_bilinear_kernel &kernel = fKernel [patRow] [patCol]; |
| 604 |
|
| 605 |
// Special case no interpolation case. |
| 606 |
|
| 607 |
if (map [patRow] [patCol]) |
| 608 |
{ |
| 609 |
|
| 610 |
kernel.Add (dng_point (0, 0), 1.0f); |
| 611 |
|
| 612 |
continue; |
| 613 |
|
| 614 |
} |
| 615 |
|
| 616 |
// Special case common patterns in 3 by 3 neighborhood. |
| 617 |
|
| 618 |
uint32 n = DeltaRow (patRow, -1); |
| 619 |
uint32 s = DeltaRow (patRow, 1); |
| 620 |
uint32 w = DeltaCol (patCol, -1); |
| 621 |
uint32 e = DeltaCol (patCol, 1); |
| 622 |
|
| 623 |
bool mapNW = map [n] [w]; |
| 624 |
bool mapN = map [n] [patCol]; |
| 625 |
bool mapNE = map [n] [e]; |
| 626 |
|
| 627 |
bool mapW = map [patRow] [w]; |
| 628 |
bool mapE = map [patRow] [e]; |
| 629 |
|
| 630 |
bool mapSW = map [s] [w]; |
| 631 |
bool mapS = map [s] [patCol]; |
| 632 |
bool mapSE = map [s] [e]; |
| 633 |
|
| 634 |
// All sides. |
| 635 |
|
| 636 |
if (mapN && mapS && mapW && mapW) |
| 637 |
{ |
| 638 |
|
| 639 |
kernel.Add (dng_point (-1, 0), 0.25f); |
| 640 |
kernel.Add (dng_point ( 0, -1), 0.25f); |
| 641 |
kernel.Add (dng_point ( 0, 1), 0.25f); |
| 642 |
kernel.Add (dng_point ( 1, 0), 0.25f); |
| 643 |
|
| 644 |
continue; |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
// N & S. |
| 649 |
|
| 650 |
if (mapN && mapS) |
| 651 |
{ |
| 652 |
|
| 653 |
kernel.Add (dng_point (-1, 0), 0.5f); |
| 654 |
kernel.Add (dng_point ( 1, 0), 0.5f); |
| 655 |
|
| 656 |
continue; |
| 657 |
|
| 658 |
} |
| 659 |
|
| 660 |
// E & W. |
| 661 |
|
| 662 |
if (mapW && mapE) |
| 663 |
{ |
| 664 |
|
| 665 |
kernel.Add (dng_point ( 0, -1), 0.5f); |
| 666 |
kernel.Add (dng_point ( 0, 1), 0.5f); |
| 667 |
|
| 668 |
continue; |
| 669 |
|
| 670 |
} |
| 671 |
|
| 672 |
// N & SW & SE. |
| 673 |
|
| 674 |
if (mapN && mapSW && mapSE) |
| 675 |
{ |
| 676 |
|
| 677 |
kernel.Add (dng_point (-1, 0), 0.50f); |
| 678 |
kernel.Add (dng_point ( 1, -1), 0.25f); |
| 679 |
kernel.Add (dng_point ( 1, 1), 0.25f); |
| 680 |
|
| 681 |
continue; |
| 682 |
|
| 683 |
} |
| 684 |
|
| 685 |
// S & NW & NE. |
| 686 |
|
| 687 |
if (mapS && mapNW && mapNE) |
| 688 |
{ |
| 689 |
|
| 690 |
kernel.Add (dng_point (-1, -1), 0.25f); |
| 691 |
kernel.Add (dng_point (-1, 1), 0.25f); |
| 692 |
kernel.Add (dng_point ( 1, 0), 0.50f); |
| 693 |
|
| 694 |
continue; |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
// W & NE & SE. |
| 699 |
|
| 700 |
if (mapW && mapNE && mapSE) |
| 701 |
{ |
| 702 |
|
| 703 |
kernel.Add (dng_point (-1, 1), 0.25f); |
| 704 |
kernel.Add (dng_point ( 0, -1), 0.50f); |
| 705 |
kernel.Add (dng_point ( 1, 1), 0.25f); |
| 706 |
|
| 707 |
continue; |
| 708 |
|
| 709 |
} |
| 710 |
|
| 711 |
// E & NW & SW. |
| 712 |
|
| 713 |
if (mapE && mapNW && mapSW) |
| 714 |
{ |
| 715 |
|
| 716 |
kernel.Add (dng_point (-1, -1), 0.25f); |
| 717 |
kernel.Add (dng_point ( 0, 1), 0.50f); |
| 718 |
kernel.Add (dng_point ( 1, -1), 0.25f); |
| 719 |
|
| 720 |
continue; |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
// Four corners. |
| 725 |
|
| 726 |
if (mapNW && mapNE && mapSE && mapSW) |
| 727 |
{ |
| 728 |
|
| 729 |
kernel.Add (dng_point (-1, -1), 0.25f); |
| 730 |
kernel.Add (dng_point (-1, 1), 0.25f); |
| 731 |
kernel.Add (dng_point ( 1, -1), 0.25f); |
| 732 |
kernel.Add (dng_point ( 1, 1), 0.25f); |
| 733 |
|
| 734 |
continue; |
| 735 |
|
| 736 |
} |
| 737 |
|
| 738 |
// NW & SE |
| 739 |
|
| 740 |
if (mapNW && mapSE) |
| 741 |
{ |
| 742 |
|
| 743 |
kernel.Add (dng_point (-1, -1), 0.50f); |
| 744 |
kernel.Add (dng_point ( 1, 1), 0.50f); |
| 745 |
|
| 746 |
continue; |
| 747 |
|
| 748 |
} |
| 749 |
|
| 750 |
// NE & SW |
| 751 |
|
| 752 |
if (mapNE && mapSW) |
| 753 |
{ |
| 754 |
|
| 755 |
kernel.Add (dng_point (-1, 1), 0.50f); |
| 756 |
kernel.Add (dng_point ( 1, -1), 0.50f); |
| 757 |
|
| 758 |
continue; |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
// Else use double-bilinear kernel. |
| 763 |
|
| 764 |
int32 dv1 = 0; |
| 765 |
int32 dv2 = 0; |
| 766 |
|
| 767 |
while (!mapV [DeltaRow (patRow, dv1)]) |
| 768 |
{ |
| 769 |
dv1--; |
| 770 |
} |
| 771 |
|
| 772 |
while (!mapV [DeltaRow (patRow, dv2)]) |
| 773 |
{ |
| 774 |
dv2++; |
| 775 |
} |
| 776 |
|
| 777 |
real32 w1 = LinearWeight1 (dv1, dv2) * 0.5f; |
| 778 |
real32 w2 = LinearWeight2 (dv1, dv2) * 0.5f; |
| 779 |
|
| 780 |
int32 v1 = DeltaRow (patRow, dv1); |
| 781 |
int32 v2 = DeltaRow (patRow, dv2); |
| 782 |
|
| 783 |
int32 dh1 = 0; |
| 784 |
int32 dh2 = 0; |
| 785 |
|
| 786 |
while (!map [v1] [DeltaCol (patCol, dh1)]) |
| 787 |
{ |
| 788 |
dh1--; |
| 789 |
} |
| 790 |
|
| 791 |
while (!map [v1] [DeltaCol (patCol, dh2)]) |
| 792 |
{ |
| 793 |
dh2++; |
| 794 |
} |
| 795 |
|
| 796 |
kernel.Add (dng_point (dv1, dh1), |
| 797 |
LinearWeight1 (dh1, dh2) * w1); |
| 798 |
|
| 799 |
kernel.Add (dng_point (dv1, dh2), |
| 800 |
LinearWeight2 (dh1, dh2) * w1); |
| 801 |
|
| 802 |
dh1 = 0; |
| 803 |
dh2 = 0; |
| 804 |
|
| 805 |
while (!map [v2] [DeltaCol (patCol, dh1)]) |
| 806 |
{ |
| 807 |
dh1--; |
| 808 |
} |
| 809 |
|
| 810 |
while (!map [v2] [DeltaCol (patCol, dh2)]) |
| 811 |
{ |
| 812 |
dh2++; |
| 813 |
} |
| 814 |
|
| 815 |
kernel.Add (dng_point (dv2, dh1), |
| 816 |
LinearWeight1 (dh1, dh2) * w2); |
| 817 |
|
| 818 |
kernel.Add (dng_point (dv2, dh2), |
| 819 |
LinearWeight2 (dh1, dh2) * w2); |
| 820 |
|
| 821 |
dh1 = 0; |
| 822 |
dh2 = 0; |
| 823 |
|
| 824 |
while (!mapH [DeltaCol (patCol, dh1)]) |
| 825 |
{ |
| 826 |
dh1--; |
| 827 |
} |
| 828 |
|
| 829 |
while (!mapH [DeltaCol (patCol, dh2)]) |
| 830 |
{ |
| 831 |
dh2++; |
| 832 |
} |
| 833 |
|
| 834 |
w1 = LinearWeight1 (dh1, dh2) * 0.5f; |
| 835 |
w2 = LinearWeight2 (dh1, dh2) * 0.5f; |
| 836 |
|
| 837 |
int32 h1 = DeltaCol (patCol, dh1); |
| 838 |
int32 h2 = DeltaCol (patCol, dh2); |
| 839 |
|
| 840 |
dv1 = 0; |
| 841 |
dv2 = 0; |
| 842 |
|
| 843 |
while (!map [DeltaRow (patRow, dv1)] [h1]) |
| 844 |
{ |
| 845 |
dv1--; |
| 846 |
} |
| 847 |
|
| 848 |
while (!map [DeltaRow (patRow, dv2)] [h1]) |
| 849 |
{ |
| 850 |
dv2++; |
| 851 |
} |
| 852 |
|
| 853 |
kernel.Add (dng_point (dv1, dh1), |
| 854 |
LinearWeight1 (dv1, dv2) * w1); |
| 855 |
|
| 856 |
kernel.Add (dng_point (dv2, dh1), |
| 857 |
LinearWeight2 (dv1, dv2) * w1); |
| 858 |
|
| 859 |
dv1 = 0; |
| 860 |
dv2 = 0; |
| 861 |
|
| 862 |
while (!map [DeltaRow (patRow, dv1)] [h2]) |
| 863 |
{ |
| 864 |
dv1--; |
| 865 |
} |
| 866 |
|
| 867 |
while (!map [DeltaRow (patRow, dv2)] [h2]) |
| 868 |
{ |
| 869 |
dv2++; |
| 870 |
} |
| 871 |
|
| 872 |
kernel.Add (dng_point (dv1, dh2), |
| 873 |
LinearWeight1 (dv1, dv2) * w2); |
| 874 |
|
| 875 |
kernel.Add (dng_point (dv2, dh2), |
| 876 |
LinearWeight2 (dv1, dv2) * w2); |
| 877 |
|
| 878 |
} |
| 879 |
|
| 880 |
} |
| 881 |
|
| 882 |
// Deal with temp scale case. |
| 883 |
|
| 884 |
if (tempScale == dng_point (2, 2)) |
| 885 |
{ |
| 886 |
|
| 887 |
fPatRows /= tempScale.v; |
| 888 |
fPatCols /= tempScale.h; |
| 889 |
|
| 890 |
for (patRow = 0; patRow < fPatRows; patRow++) |
| 891 |
{ |
| 892 |
|
| 893 |
for (patCol = 0; patCol < fPatCols; patCol++) |
| 894 |
{ |
| 895 |
|
| 896 |
int32 patRow2 = patRow << 1; |
| 897 |
int32 patCol2 = patCol << 1; |
| 898 |
|
| 899 |
dng_bilinear_kernel &kernel = fKernel [patRow2] [patCol2]; |
| 900 |
|
| 901 |
for (j = 0; j < kernel.fCount; j++) |
| 902 |
{ |
| 903 |
|
| 904 |
int32 x = patRow2 + kernel.fDelta [j].v; |
| 905 |
|
| 906 |
if ((x & 3) != 0) |
| 907 |
{ |
| 908 |
x = (x & ~3) + 2; |
| 909 |
} |
| 910 |
|
| 911 |
kernel.fDelta [j].v = ((x - patRow2) >> 1); |
| 912 |
|
| 913 |
x = patCol2 + kernel.fDelta [j].h; |
| 914 |
|
| 915 |
if ((x & 3) != 0) |
| 916 |
{ |
| 917 |
x = (x & ~3) + 2; |
| 918 |
} |
| 919 |
|
| 920 |
kernel.fDelta [j].h = ((x - patCol2) >> 1); |
| 921 |
|
| 922 |
} |
| 923 |
|
| 924 |
kernel.Finalize (fScale, |
| 925 |
patRow, |
| 926 |
patCol, |
| 927 |
rowStep, |
| 928 |
colStep); |
| 929 |
|
| 930 |
fCounts [patRow] [patCol] = kernel.fCount; |
| 931 |
fOffsets [patRow] [patCol] = kernel.fOffset; |
| 932 |
fWeights16 [patRow] [patCol] = kernel.fWeight16; |
| 933 |
fWeights32 [patRow] [patCol] = kernel.fWeight32; |
| 934 |
|
| 935 |
} |
| 936 |
|
| 937 |
} |
| 938 |
|
| 939 |
} |
| 940 |
|
| 941 |
// Non-temp scale case. |
| 942 |
|
| 943 |
else |
| 944 |
{ |
| 945 |
|
| 946 |
for (patRow = 0; patRow < fPatRows; patRow++) |
| 947 |
{ |
| 948 |
|
| 949 |
for (patCol = 0; patCol < fPatCols; patCol++) |
| 950 |
{ |
| 951 |
|
| 952 |
dng_bilinear_kernel &kernel = fKernel [patRow] [patCol]; |
| 953 |
|
| 954 |
kernel.Finalize (fScale, |
| 955 |
patRow, |
| 956 |
patCol, |
| 957 |
rowStep, |
| 958 |
colStep); |
| 959 |
|
| 960 |
fCounts [patRow] [patCol] = kernel.fCount; |
| 961 |
fOffsets [patRow] [patCol] = kernel.fOffset; |
| 962 |
fWeights16 [patRow] [patCol] = kernel.fWeight16; |
| 963 |
fWeights32 [patRow] [patCol] = kernel.fWeight32; |
| 964 |
|
| 965 |
} |
| 966 |
|
| 967 |
} |
| 968 |
|
| 969 |
} |
| 970 |
|
| 971 |
} |
| 972 |
|
| 973 |
/*****************************************************************************/ |
| 974 |
|
| 975 |
class dng_bilinear_interpolator |
| 976 |
{ |
| 977 |
|
| 978 |
private: |
| 979 |
|
| 980 |
dng_bilinear_pattern fPattern [kMaxColorPlanes]; |
| 981 |
|
| 982 |
public: |
| 983 |
|
| 984 |
dng_bilinear_interpolator (const dng_mosaic_info &info, |
| 985 |
int32 rowStep, |
| 986 |
int32 colStep); |
| 987 |
|
| 988 |
void Interpolate (dng_pixel_buffer &srcBuffer, |
| 989 |
dng_pixel_buffer &dstBuffer); |
| 990 |
|
| 991 |
}; |
| 992 |
|
| 993 |
/*****************************************************************************/ |
| 994 |
|
| 995 |
dng_bilinear_interpolator::dng_bilinear_interpolator (const dng_mosaic_info &info, |
| 996 |
int32 rowStep, |
| 997 |
int32 colStep) |
| 998 |
{ |
| 999 |
|
| 1000 |
for (uint32 dstPlane = 0; dstPlane < info.fColorPlanes; dstPlane++) |
| 1001 |
{ |
| 1002 |
|
| 1003 |
fPattern [dstPlane] . Calculate (info, |
| 1004 |
dstPlane, |
| 1005 |
rowStep, |
| 1006 |
colStep); |
| 1007 |
|
| 1008 |
} |
| 1009 |
|
| 1010 |
} |
| 1011 |
|
| 1012 |
/*****************************************************************************/ |
| 1013 |
|
| 1014 |
void dng_bilinear_interpolator::Interpolate (dng_pixel_buffer &srcBuffer, |
| 1015 |
dng_pixel_buffer &dstBuffer) |
| 1016 |
{ |
| 1017 |
|
| 1018 |
uint32 patCols = fPattern [0] . fPatCols; |
| 1019 |
uint32 patRows = fPattern [0] . fPatRows; |
| 1020 |
|
| 1021 |
dng_point scale = fPattern [0] . fScale; |
| 1022 |
|
| 1023 |
uint32 sRowShift = scale.v - 1; |
| 1024 |
uint32 sColShift = scale.h - 1; |
| 1025 |
|
| 1026 |
int32 dstCol = dstBuffer.fArea.l; |
| 1027 |
|
| 1028 |
int32 srcCol = dstCol >> sColShift; |
| 1029 |
|
| 1030 |
uint32 patPhase = dstCol % patCols; |
| 1031 |
|
| 1032 |
for (int32 dstRow = dstBuffer.fArea.t; |
| 1033 |
dstRow < dstBuffer.fArea.b; |
| 1034 |
dstRow++) |
| 1035 |
{ |
| 1036 |
|
| 1037 |
int32 srcRow = dstRow >> sRowShift; |
| 1038 |
|
| 1039 |
uint32 patRow = dstRow % patRows; |
| 1040 |
|
| 1041 |
for (uint32 dstPlane = 0; |
| 1042 |
dstPlane < dstBuffer.fPlanes; |
| 1043 |
dstPlane++) |
| 1044 |
{ |
| 1045 |
|
| 1046 |
const void *sPtr = srcBuffer.ConstPixel (srcRow, |
| 1047 |
srcCol, |
| 1048 |
srcBuffer.fPlane); |
| 1049 |
|
| 1050 |
void *dPtr = dstBuffer.DirtyPixel (dstRow, |
| 1051 |
dstCol, |
| 1052 |
dstPlane); |
| 1053 |
|
| 1054 |
if (dstBuffer.fPixelType == ttShort) |
| 1055 |
{ |
| 1056 |
|
| 1057 |
DoBilinearRow16 ((const uint16 *) sPtr, |
| 1058 |
(uint16 *) dPtr, |
| 1059 |
dstBuffer.fArea.W (), |
| 1060 |
patPhase, |
| 1061 |
patCols, |
| 1062 |
fPattern [dstPlane].fCounts [patRow], |
| 1063 |
fPattern [dstPlane].fOffsets [patRow], |
| 1064 |
fPattern [dstPlane].fWeights16 [patRow], |
| 1065 |
sColShift); |
| 1066 |
|
| 1067 |
} |
| 1068 |
|
| 1069 |
else |
| 1070 |
{ |
| 1071 |
|
| 1072 |
DoBilinearRow32 ((const real32 *) sPtr, |
| 1073 |
(real32 *) dPtr, |
| 1074 |
dstBuffer.fArea.W (), |
| 1075 |
patPhase, |
| 1076 |
patCols, |
| 1077 |
fPattern [dstPlane].fCounts [patRow], |
| 1078 |
fPattern [dstPlane].fOffsets [patRow], |
| 1079 |
fPattern [dstPlane].fWeights32 [patRow], |
| 1080 |
sColShift); |
| 1081 |
|
| 1082 |
} |
| 1083 |
|
| 1084 |
} |
| 1085 |
|
| 1086 |
} |
| 1087 |
|
| 1088 |
} |
| 1089 |
|
| 1090 |
/*****************************************************************************/ |
| 1091 |
|
| 1092 |
class dng_fast_interpolator: public dng_filter_task |
| 1093 |
{ |
| 1094 |
|
| 1095 |
protected: |
| 1096 |
|
| 1097 |
const dng_mosaic_info &fInfo; |
| 1098 |
|
| 1099 |
dng_point fDownScale; |
| 1100 |
|
| 1101 |
uint32 fFilterColor [kMaxCFAPattern] [kMaxCFAPattern]; |
| 1102 |
|
| 1103 |
public: |
| 1104 |
|
| 1105 |
dng_fast_interpolator (const dng_mosaic_info &info, |
| 1106 |
const dng_image &srcImage, |
| 1107 |
dng_image &dstImage, |
| 1108 |
const dng_point &downScale, |
| 1109 |
uint32 srcPlane); |
| 1110 |
|
| 1111 |
virtual dng_rect SrcArea (const dng_rect &dstArea); |
| 1112 |
|
| 1113 |
virtual void ProcessArea (uint32 threadIndex, |
| 1114 |
dng_pixel_buffer &srcBuffer, |
| 1115 |
dng_pixel_buffer &dstBuffer); |
| 1116 |
|
| 1117 |
}; |
| 1118 |
|
| 1119 |
/*****************************************************************************/ |
| 1120 |
|
| 1121 |
dng_fast_interpolator::dng_fast_interpolator (const dng_mosaic_info &info, |
| 1122 |
const dng_image &srcImage, |
| 1123 |
dng_image &dstImage, |
| 1124 |
const dng_point &downScale, |
| 1125 |
uint32 srcPlane) |
| 1126 |
|
| 1127 |
: dng_filter_task ("dng_fast_interpolator", |
| 1128 |
srcImage, |
| 1129 |
dstImage) |
| 1130 |
|
| 1131 |
, fInfo (info ) |
| 1132 |
, fDownScale (downScale) |
| 1133 |
|
| 1134 |
{ |
| 1135 |
|
| 1136 |
fSrcPlane = srcPlane; |
| 1137 |
fSrcPlanes = 1; |
| 1138 |
|
| 1139 |
fSrcPixelType = ttShort; |
| 1140 |
fDstPixelType = ttShort; |
| 1141 |
|
| 1142 |
fSrcRepeat = fInfo.fCFAPatternSize; |
| 1143 |
|
| 1144 |
fUnitCell = fInfo.fCFAPatternSize; |
| 1145 |
|
| 1146 |
fMaxTileSize = dng_point (256 / fDownScale.v, |
| 1147 |
256 / fDownScale.h); |
| 1148 |
|
| 1149 |
fMaxTileSize.h = Max_int32 (fMaxTileSize.h, fUnitCell.h); |
| 1150 |
fMaxTileSize.v = Max_int32 (fMaxTileSize.v, fUnitCell.v); |
| 1151 |
|
| 1152 |
// Find color map. |
| 1153 |
|
| 1154 |
{ |
| 1155 |
|
| 1156 |
for (int32 r = 0; r < fInfo.fCFAPatternSize.v; r++) |
| 1157 |
{ |
| 1158 |
|
| 1159 |
for (int32 c = 0; c < fInfo.fCFAPatternSize.h; c++) |
| 1160 |
{ |
| 1161 |
|
| 1162 |
uint8 key = fInfo.fCFAPattern [r] [c]; |
| 1163 |
|
| 1164 |
for (uint32 index = 0; index < fInfo.fColorPlanes; index++) |
| 1165 |
{ |
| 1166 |
|
| 1167 |
if (key == fInfo.fCFAPlaneColor [index]) |
| 1168 |
{ |
| 1169 |
|
| 1170 |
fFilterColor [r] [c] = index; |
| 1171 |
|
| 1172 |
break; |
| 1173 |
|
| 1174 |
} |
| 1175 |
|
| 1176 |
} |
| 1177 |
|
| 1178 |
} |
| 1179 |
|
| 1180 |
} |
| 1181 |
|
| 1182 |
} |
| 1183 |
|
| 1184 |
} |
| 1185 |
|
| 1186 |
/*****************************************************************************/ |
| 1187 |
|
| 1188 |
dng_rect dng_fast_interpolator::SrcArea (const dng_rect &dstArea) |
| 1189 |
{ |
| 1190 |
|
| 1191 |
return dng_rect (dstArea.t * fDownScale.v, |
| 1192 |
dstArea.l * fDownScale.h, |
| 1193 |
dstArea.b * fDownScale.v, |
| 1194 |
dstArea.r * fDownScale.h); |
| 1195 |
|
| 1196 |
} |
| 1197 |
|
| 1198 |
/*****************************************************************************/ |
| 1199 |
|
| 1200 |
void dng_fast_interpolator::ProcessArea (uint32 /* threadIndex */, |
| 1201 |
dng_pixel_buffer &srcBuffer, |
| 1202 |
dng_pixel_buffer &dstBuffer) |
| 1203 |
{ |
| 1204 |
|
| 1205 |
dng_rect srcArea = srcBuffer.fArea; |
| 1206 |
dng_rect dstArea = dstBuffer.fArea; |
| 1207 |
|
| 1208 |
// Downsample buffer. |
| 1209 |
|
| 1210 |
int32 srcRow = srcArea.t; |
| 1211 |
|
| 1212 |
uint32 srcRowPhase1 = 0; |
| 1213 |
uint32 srcRowPhase2 = 0; |
| 1214 |
|
| 1215 |
uint32 patRows = fInfo.fCFAPatternSize.v; |
| 1216 |
uint32 patCols = fInfo.fCFAPatternSize.h; |
| 1217 |
|
| 1218 |
uint32 cellRows = fDownScale.v; |
| 1219 |
uint32 cellCols = fDownScale.h; |
| 1220 |
|
| 1221 |
uint32 plane; |
| 1222 |
uint32 planes = fInfo.fColorPlanes; |
| 1223 |
|
| 1224 |
int32 dstPlaneStep = dstBuffer.fPlaneStep; |
| 1225 |
|
| 1226 |
uint32 total [kMaxColorPlanes]; |
| 1227 |
uint32 count [kMaxColorPlanes]; |
| 1228 |
|
| 1229 |
for (plane = 0; plane < planes; plane++) |
| 1230 |
{ |
| 1231 |
total [plane] = 0; |
| 1232 |
count [plane] = 0; |
| 1233 |
} |
| 1234 |
|
| 1235 |
for (int32 dstRow = dstArea.t; dstRow < dstArea.b; dstRow++) |
| 1236 |
{ |
| 1237 |
|
| 1238 |
const uint16 *sPtr = srcBuffer.ConstPixel_uint16 (srcRow, |
| 1239 |
srcArea.l, |
| 1240 |
fSrcPlane); |
| 1241 |
|
| 1242 |
uint16 *dPtr = dstBuffer.DirtyPixel_uint16 (dstRow, |
| 1243 |
dstArea.l, |
| 1244 |
0); |
| 1245 |
|
| 1246 |
uint32 srcColPhase1 = 0; |
| 1247 |
uint32 srcColPhase2 = 0; |
| 1248 |
|
| 1249 |
for (int32 dstCol = dstArea.l; dstCol < dstArea.r; dstCol++) |
| 1250 |
{ |
| 1251 |
|
| 1252 |
const uint16 *ssPtr = sPtr; |
| 1253 |
|
| 1254 |
srcRowPhase2 = srcRowPhase1; |
| 1255 |
|
| 1256 |
for (uint32 cellRow = 0; cellRow < cellRows; cellRow++) |
| 1257 |
{ |
| 1258 |
|
| 1259 |
const uint32 *filterRow = fFilterColor [srcRowPhase2]; |
| 1260 |
|
| 1261 |
if (++srcRowPhase2 == patRows) |
| 1262 |
{ |
| 1263 |
srcRowPhase2 = 0; |
| 1264 |
} |
| 1265 |
|
| 1266 |
srcColPhase2 = srcColPhase1; |
| 1267 |
|
| 1268 |
for (uint32 cellCol = 0; cellCol < cellCols; cellCol++) |
| 1269 |
{ |
| 1270 |
|
| 1271 |
uint32 color = filterRow [srcColPhase2]; |
| 1272 |
|
| 1273 |
if (++srcColPhase2 == patCols) |
| 1274 |
{ |
| 1275 |
srcColPhase2 = 0; |
| 1276 |
} |
| 1277 |
|
| 1278 |
total [color] += (uint32) ssPtr [cellCol]; |
| 1279 |
count [color] ++; |
| 1280 |
|
| 1281 |
} |
| 1282 |
|
| 1283 |
ssPtr += srcBuffer.fRowStep; |
| 1284 |
|
| 1285 |
} |
| 1286 |
|
| 1287 |
for (plane = 0; plane < planes; plane++) |
| 1288 |
{ |
| 1289 |
|
| 1290 |
uint32 t = total [plane]; |
| 1291 |
uint32 c = Max_uint32 (count [plane], 1); |
| 1292 |
|
| 1293 |
dPtr [plane * dstPlaneStep] = (uint16) ((t + (c >> 1)) / c); |
| 1294 |
|
| 1295 |
total [plane] = 0; |
| 1296 |
count [plane] = 0; |
| 1297 |
|
| 1298 |
} |
| 1299 |
|
| 1300 |
srcColPhase1 = srcColPhase2; |
| 1301 |
|
| 1302 |
sPtr += cellCols; |
| 1303 |
|
| 1304 |
dPtr ++; |
| 1305 |
|
| 1306 |
} |
| 1307 |
|
| 1308 |
srcRowPhase1 = srcRowPhase2; |
| 1309 |
|
| 1310 |
srcRow += cellRows; |
| 1311 |
|
| 1312 |
} |
| 1313 |
|
| 1314 |
} |
| 1315 |
|
| 1316 |
/*****************************************************************************/ |
| 1317 |
|
| 1318 |
dng_mosaic_info::dng_mosaic_info () |
| 1319 |
|
| 1320 |
: fCFAPatternSize () |
| 1321 |
, fColorPlanes (0) |
| 1322 |
, fCFALayout (1) |
| 1323 |
, fBayerGreenSplit (0) |
| 1324 |
, fSrcSize () |
| 1325 |
, fCroppedSize () |
| 1326 |
, fAspectRatio (1.0) |
| 1327 |
|
| 1328 |
{ |
| 1329 |
|
| 1330 |
} |
| 1331 |
|
| 1332 |
/*****************************************************************************/ |
| 1333 |
|
| 1334 |
dng_mosaic_info::~dng_mosaic_info () |
| 1335 |
{ |
| 1336 |
|
| 1337 |
} |
| 1338 |
|
| 1339 |
/*****************************************************************************/ |
| 1340 |
|
| 1341 |
void dng_mosaic_info::Parse (dng_host & /* host */, |
| 1342 |
dng_stream & /* stream */, |
| 1343 |
dng_info &info) |
| 1344 |
{ |
| 1345 |
|
| 1346 |
// Find main image IFD. |
| 1347 |
|
| 1348 |
dng_ifd &rawIFD = *info.fIFD [info.fMainIndex]; |
| 1349 |
|
| 1350 |
// This information only applies to CFA images. |
| 1351 |
|
| 1352 |
if (rawIFD.fPhotometricInterpretation != piCFA) |
| 1353 |
{ |
| 1354 |
return; |
| 1355 |
} |
| 1356 |
|
| 1357 |
// Copy CFA pattern. |
| 1358 |
|
| 1359 |
fCFAPatternSize.v = rawIFD.fCFARepeatPatternRows; |
| 1360 |
fCFAPatternSize.h = rawIFD.fCFARepeatPatternCols; |
| 1361 |
|
| 1362 |
DNG_REQUIRE (fCFAPatternSize.v >= 1 && fCFAPatternSize.v <= (int32) kMaxCFAPattern, |
| 1363 |
"Invalid fCFAPatternSize.v"); |
| 1364 |
|
| 1365 |
DNG_REQUIRE (fCFAPatternSize.h >= 1 && fCFAPatternSize.h <= (int32) kMaxCFAPattern, |
| 1366 |
"Invalid fCFAPatternSize.h"); |
| 1367 |
|
| 1368 |
for (int32 j = 0; j < fCFAPatternSize.v; j++) |
| 1369 |
{ |
| 1370 |
for (int32 k = 0; k < fCFAPatternSize.h; k++) |
| 1371 |
{ |
| 1372 |
fCFAPattern [j] [k] = rawIFD.fCFAPattern [j] [k]; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Copy CFA plane information. |
| 1377 |
|
| 1378 |
fColorPlanes = info.fShared->fCameraProfile.fColorPlanes; |
| 1379 |
|
| 1380 |
for (uint32 n = 0; n < fColorPlanes; n++) |
| 1381 |
{ |
| 1382 |
fCFAPlaneColor [n] = rawIFD.fCFAPlaneColor [n]; |
| 1383 |
} |
| 1384 |
|
| 1385 |
// Copy CFA layout information. |
| 1386 |
|
| 1387 |
fCFALayout = rawIFD.fCFALayout; |
| 1388 |
|
| 1389 |
// Green split value for Bayer patterns. |
| 1390 |
|
| 1391 |
fBayerGreenSplit = rawIFD.fBayerGreenSplit; |
| 1392 |
|
| 1393 |
} |
| 1394 |
|
| 1395 |
/*****************************************************************************/ |
| 1396 |
|
| 1397 |
void dng_mosaic_info::PostParse (dng_host & /* host */, |
| 1398 |
dng_negative &negative) |
| 1399 |
{ |
| 1400 |
|
| 1401 |
// Keep track of source image size. |
| 1402 |
|
| 1403 |
fSrcSize = negative.Stage2Image ()->Size (); |
| 1404 |
|
| 1405 |
// Default cropped size. |
| 1406 |
|
| 1407 |
fCroppedSize.v = Round_int32 (negative.DefaultCropSizeV ().As_real64 ()); |
| 1408 |
fCroppedSize.h = Round_int32 (negative.DefaultCropSizeH ().As_real64 ()); |
| 1409 |
|
| 1410 |
// Pixel aspect ratio. |
| 1411 |
|
| 1412 |
fAspectRatio = negative.DefaultScaleH ().As_real64 () / |
| 1413 |
negative.DefaultScaleV ().As_real64 (); |
| 1414 |
|
| 1415 |
} |
| 1416 |
|
| 1417 |
/*****************************************************************************/ |
| 1418 |
|
| 1419 |
bool dng_mosaic_info::SetFourColorBayer () |
| 1420 |
{ |
| 1421 |
|
| 1422 |
if (fCFAPatternSize != dng_point (2, 2)) |
| 1423 |
{ |
| 1424 |
return false; |
| 1425 |
} |
| 1426 |
|
| 1427 |
if (fColorPlanes != 3) |
| 1428 |
{ |
| 1429 |
return false; |
| 1430 |
} |
| 1431 |
|
| 1432 |
uint8 color0 = fCFAPlaneColor [0]; |
| 1433 |
uint8 color1 = fCFAPlaneColor [1]; |
| 1434 |
uint8 color2 = fCFAPlaneColor [2]; |
| 1435 |
|
| 1436 |
// Look for color 1 repeated twice in a diagonal. |
| 1437 |
|
| 1438 |
if ((fCFAPattern [0] [0] == color1 && fCFAPattern [1] [1] == color1) || |
| 1439 |
(fCFAPattern [0] [1] == color1 && fCFAPattern [1] [0] == color1)) |
| 1440 |
{ |
| 1441 |
|
| 1442 |
// OK, this looks like a Bayer pattern. |
| 1443 |
|
| 1444 |
// Find unused color code. |
| 1445 |
|
| 1446 |
uint8 color3 = 0; |
| 1447 |
|
| 1448 |
while (color3 == color0 || |
| 1449 |
color3 == color1 || |
| 1450 |
color3 == color2) |
| 1451 |
{ |
| 1452 |
color3++; |
| 1453 |
} |
| 1454 |
|
| 1455 |
// Switch the four color mosaic. |
| 1456 |
|
| 1457 |
fColorPlanes = 4; |
| 1458 |
|
| 1459 |
fCFAPlaneColor [3] = color3; |
| 1460 |
|
| 1461 |
// Replace the "green" in the "blue" rows with the new color. |
| 1462 |
|
| 1463 |
if (fCFAPattern [0] [0] == color0) |
| 1464 |
{ |
| 1465 |
fCFAPattern [1] [0] = color3; |
| 1466 |
} |
| 1467 |
|
| 1468 |
else if (fCFAPattern [0] [1] == color0) |
| 1469 |
{ |
| 1470 |
fCFAPattern [1] [1] = color3; |
| 1471 |
} |
| 1472 |
|
| 1473 |
else if (fCFAPattern [1] [0] == color0) |
| 1474 |
{ |
| 1475 |
fCFAPattern [0] [0] = color3; |
| 1476 |
} |
| 1477 |
|
| 1478 |
else |
| 1479 |
{ |
| 1480 |
fCFAPattern [0] [1] = color3; |
| 1481 |
} |
| 1482 |
|
| 1483 |
return true; |
| 1484 |
|
| 1485 |
} |
| 1486 |
|
| 1487 |
return false; |
| 1488 |
|
| 1489 |
} |
| 1490 |
|
| 1491 |
/*****************************************************************************/ |
| 1492 |
|
| 1493 |
dng_point dng_mosaic_info::FullScale () const |
| 1494 |
{ |
| 1495 |
|
| 1496 |
switch (fCFALayout) |
| 1497 |
{ |
| 1498 |
|
| 1499 |
// Staggered layouts with offset columns double the row count |
| 1500 |
// during interpolation. |
| 1501 |
|
| 1502 |
case 2: |
| 1503 |
case 3: |
| 1504 |
return dng_point (2, 1); |
| 1505 |
|
| 1506 |
// Staggered layouts with offset rows double the column count |
| 1507 |
// during interpolation. |
| 1508 |
|
| 1509 |
case 4: |
| 1510 |
case 5: |
| 1511 |
return dng_point (1, 2); |
| 1512 |
|
| 1513 |
// Otherwise there is no size change during interpolation. |
| 1514 |
|
| 1515 |
default: |
| 1516 |
break; |
| 1517 |
|
| 1518 |
} |
| 1519 |
|
| 1520 |
return dng_point (1, 1); |
| 1521 |
|
| 1522 |
} |
| 1523 |
|
| 1524 |
/*****************************************************************************/ |
| 1525 |
|
| 1526 |
bool dng_mosaic_info::IsSafeDownScale (const dng_point &downScale) const |
| 1527 |
{ |
| 1528 |
|
| 1529 |
if (downScale.v >= fCFAPatternSize.v && |
| 1530 |
downScale.h >= fCFAPatternSize.h) |
| 1531 |
{ |
| 1532 |
|
| 1533 |
return true; |
| 1534 |
|
| 1535 |
} |
| 1536 |
|
| 1537 |
dng_point test; |
| 1538 |
|
| 1539 |
test.v = Min_int32 (downScale.v, fCFAPatternSize.v); |
| 1540 |
test.h = Min_int32 (downScale.h, fCFAPatternSize.h); |
| 1541 |
|
| 1542 |
for (int32 phaseV = 0; phaseV < fCFAPatternSize.v; phaseV++) |
| 1543 |
{ |
| 1544 |
|
| 1545 |
for (int32 phaseH = 0; phaseH < fCFAPatternSize.h; phaseH++) |
| 1546 |
{ |
| 1547 |
|
| 1548 |
uint32 plane; |
| 1549 |
|
| 1550 |
bool contains [kMaxColorPlanes]; |
| 1551 |
|
| 1552 |
for (plane = 0; plane < fColorPlanes; plane++) |
| 1553 |
{ |
| 1554 |
|
| 1555 |
contains [plane] = false; |
| 1556 |
|
| 1557 |
} |
| 1558 |
|
| 1559 |
for (int32 srcRow = 0; srcRow < test.v; srcRow++) |
| 1560 |
{ |
| 1561 |
|
| 1562 |
for (int32 srcCol = 0; srcCol < test.h; srcCol++) |
| 1563 |
{ |
| 1564 |
|
| 1565 |
uint8 srcKey = fCFAPattern [(srcRow + phaseV) % fCFAPatternSize.v] |
| 1566 |
[(srcCol + phaseH) % fCFAPatternSize.h]; |
| 1567 |
|
| 1568 |
for (plane = 0; plane < fColorPlanes; plane++) |
| 1569 |
{ |
| 1570 |
|
| 1571 |
if (srcKey == fCFAPlaneColor [plane]) |
| 1572 |
{ |
| 1573 |
|
| 1574 |
contains [plane] = true; |
| 1575 |
|
| 1576 |
} |
| 1577 |
|
| 1578 |
} |
| 1579 |
|
| 1580 |
|
| 1581 |
} |
| 1582 |
|
| 1583 |
} |
| 1584 |
|
| 1585 |
for (plane = 0; plane < fColorPlanes; plane++) |
| 1586 |
{ |
| 1587 |
|
| 1588 |
if (!contains [plane]) |
| 1589 |
{ |
| 1590 |
|
| 1591 |
return false; |
| 1592 |
|
| 1593 |
} |
| 1594 |
|
| 1595 |
} |
| 1596 |
|
| 1597 |
} |
| 1598 |
|
| 1599 |
} |
| 1600 |
|
| 1601 |
return true; |
| 1602 |
|
| 1603 |
} |
| 1604 |
|
| 1605 |
/*****************************************************************************/ |
| 1606 |
|
| 1607 |
uint32 dng_mosaic_info::SizeForDownScale (const dng_point &downScale) const |
| 1608 |
{ |
| 1609 |
|
| 1610 |
uint32 sizeV = Max_uint32 (1, (fCroppedSize.v + (downScale.v >> 1)) / downScale.v); |
| 1611 |
uint32 sizeH = Max_uint32 (1, (fCroppedSize.h + (downScale.h >> 1)) / downScale.h); |
| 1612 |
|
| 1613 |
return Max_int32 (sizeV, sizeH); |
| 1614 |
|
| 1615 |
} |
| 1616 |
|
| 1617 |
/*****************************************************************************/ |
| 1618 |
|
| 1619 |
bool dng_mosaic_info::ValidSizeDownScale (const dng_point &downScale, |
| 1620 |
uint32 minSize) const |
| 1621 |
{ |
| 1622 |
|
| 1623 |
const int32 kMaxDownScale = 64; |
| 1624 |
|
| 1625 |
if (downScale.h > kMaxDownScale || |
| 1626 |
downScale.v > kMaxDownScale) |
| 1627 |
{ |
| 1628 |
|
| 1629 |
return false; |
| 1630 |
|
| 1631 |
} |
| 1632 |
|
| 1633 |
return SizeForDownScale (downScale) >= minSize; |
| 1634 |
|
| 1635 |
} |
| 1636 |
|
| 1637 |
/*****************************************************************************/ |
| 1638 |
|
| 1639 |
dng_point dng_mosaic_info::DownScale (uint32 minSize, |
| 1640 |
uint32 prefSize, |
| 1641 |
real64 cropFactor) const |
| 1642 |
{ |
| 1643 |
|
| 1644 |
dng_point bestScale (1, 1); |
| 1645 |
|
| 1646 |
if (prefSize && IsColorFilterArray ()) |
| 1647 |
{ |
| 1648 |
|
| 1649 |
// Adjust sizes for crop factor. |
| 1650 |
|
| 1651 |
minSize = Round_uint32 (minSize / cropFactor); |
| 1652 |
prefSize = Round_uint32 (prefSize / cropFactor); |
| 1653 |
|
| 1654 |
prefSize = Max_uint32 (prefSize, minSize); |
| 1655 |
|
| 1656 |
// Start by assuming we need the full size image. |
| 1657 |
|
| 1658 |
int32 bestSize = SizeForDownScale (bestScale); |
| 1659 |
|
| 1660 |
// Find size of nearly square cell. |
| 1661 |
|
| 1662 |
dng_point squareCell (1, 1); |
| 1663 |
|
| 1664 |
if (fAspectRatio < 1.0 / 1.8) |
| 1665 |
{ |
| 1666 |
|
| 1667 |
squareCell.h = Min_int32 (4, Round_int32 (1.0 / fAspectRatio)); |
| 1668 |
|
| 1669 |
} |
| 1670 |
|
| 1671 |
if (fAspectRatio > 1.8) |
| 1672 |
{ |
| 1673 |
|
| 1674 |
squareCell.v = Min_int32 (4, Round_int32 (fAspectRatio)); |
| 1675 |
|
| 1676 |
} |
| 1677 |
|
| 1678 |
// Find minimum safe cell size. |
| 1679 |
|
| 1680 |
dng_point testScale = squareCell; |
| 1681 |
|
| 1682 |
while (!IsSafeDownScale (testScale)) |
| 1683 |
{ |
| 1684 |
|
| 1685 |
testScale.v += squareCell.v; |
| 1686 |
testScale.h += squareCell.h; |
| 1687 |
|
| 1688 |
} |
| 1689 |
|
| 1690 |
// See if this scale is usable. |
| 1691 |
|
| 1692 |
if (!ValidSizeDownScale (testScale, minSize)) |
| 1693 |
{ |
| 1694 |
|
| 1695 |
// We cannot downsample at all... |
| 1696 |
|
| 1697 |
return bestScale; |
| 1698 |
|
| 1699 |
} |
| 1700 |
|
| 1701 |
// See if this is closer to the preferred size. |
| 1702 |
|
| 1703 |
int32 testSize = SizeForDownScale (testScale); |
| 1704 |
|
| 1705 |
if (Abs_int32 (testSize - (int32) prefSize) <= |
| 1706 |
Abs_int32 (bestSize - (int32) prefSize)) |
| 1707 |
{ |
| 1708 |
bestScale = testScale; |
| 1709 |
bestSize = testSize; |
| 1710 |
} |
| 1711 |
|
| 1712 |
else |
| 1713 |
{ |
| 1714 |
return bestScale; |
| 1715 |
} |
| 1716 |
|
| 1717 |
// Now keep adding square cells as long as possible. |
| 1718 |
|
| 1719 |
while (true) |
| 1720 |
{ |
| 1721 |
|
| 1722 |
testScale.v += squareCell.v; |
| 1723 |
testScale.h += squareCell.h; |
| 1724 |
|
| 1725 |
if (IsSafeDownScale (testScale)) |
| 1726 |
{ |
| 1727 |
|
| 1728 |
if (!ValidSizeDownScale (testScale, minSize)) |
| 1729 |
{ |
| 1730 |
return bestScale; |
| 1731 |
} |
| 1732 |
|
| 1733 |
// See if this is closer to the preferred size. |
| 1734 |
|
| 1735 |
testSize = SizeForDownScale (testScale); |
| 1736 |
|
| 1737 |
if (Abs_int32 (testSize - (int32) prefSize) <= |
| 1738 |
Abs_int32 (bestSize - (int32) prefSize)) |
| 1739 |
{ |
| 1740 |
bestScale = testScale; |
| 1741 |
bestSize = testSize; |
| 1742 |
} |
| 1743 |
|
| 1744 |
else |
| 1745 |
{ |
| 1746 |
return bestScale; |
| 1747 |
} |
| 1748 |
|
| 1749 |
} |
| 1750 |
|
| 1751 |
} |
| 1752 |
|
| 1753 |
} |
| 1754 |
|
| 1755 |
return bestScale; |
| 1756 |
|
| 1757 |
} |
| 1758 |
|
| 1759 |
/*****************************************************************************/ |
| 1760 |
|
| 1761 |
dng_point dng_mosaic_info::DstSize (const dng_point &downScale) const |
| 1762 |
{ |
| 1763 |
|
| 1764 |
if (downScale == dng_point (1, 1)) |
| 1765 |
{ |
| 1766 |
|
| 1767 |
dng_point scale = FullScale (); |
| 1768 |
|
| 1769 |
return dng_point (fSrcSize.v * scale.v, |
| 1770 |
fSrcSize.h * scale.h); |
| 1771 |
|
| 1772 |
} |
| 1773 |
|
| 1774 |
const int32 kMaxDownScale = 64; |
| 1775 |
|
| 1776 |
if (downScale.h > kMaxDownScale || |
| 1777 |
downScale.v > kMaxDownScale) |
| 1778 |
{ |
| 1779 |
|
| 1780 |
return dng_point (0, 0); |
| 1781 |
|
| 1782 |
} |
| 1783 |
|
| 1784 |
dng_point size; |
| 1785 |
|
| 1786 |
size.v = Max_int32 (1, (fSrcSize.v + (downScale.v >> 1)) / downScale.v); |
| 1787 |
size.h = Max_int32 (1, (fSrcSize.h + (downScale.h >> 1)) / downScale.h); |
| 1788 |
|
| 1789 |
return size; |
| 1790 |
|
| 1791 |
} |
| 1792 |
|
| 1793 |
/*****************************************************************************/ |
| 1794 |
|
| 1795 |
void dng_mosaic_info::InterpolateGeneric (dng_host &host, |
| 1796 |
dng_negative & /* negative */, |
| 1797 |
const dng_image &srcImage, |
| 1798 |
dng_image &dstImage, |
| 1799 |
uint32 srcPlane) const |
| 1800 |
{ |
| 1801 |
|
| 1802 |
// Find destination to source bit shifts. |
| 1803 |
|
| 1804 |
dng_point scale = FullScale (); |
| 1805 |
|
| 1806 |
uint32 srcShiftV = scale.v - 1; |
| 1807 |
uint32 srcShiftH = scale.h - 1; |
| 1808 |
|
| 1809 |
// Find tile sizes. |
| 1810 |
|
| 1811 |
const uint32 kMaxDstTileRows = 128; |
| 1812 |
const uint32 kMaxDstTileCols = 128; |
| 1813 |
|
| 1814 |
dng_point dstTileSize = dstImage.RepeatingTile ().Size (); |
| 1815 |
|
| 1816 |
dstTileSize.v = Min_int32 (dstTileSize.v, kMaxDstTileRows); |
| 1817 |
dstTileSize.h = Min_int32 (dstTileSize.h, kMaxDstTileCols); |
| 1818 |
|
| 1819 |
dng_point srcTileSize = dstTileSize; |
| 1820 |
|
| 1821 |
srcTileSize.v >>= srcShiftV; |
| 1822 |
srcTileSize.h >>= srcShiftH; |
| 1823 |
|
| 1824 |
srcTileSize.v += fCFAPatternSize.v * 2; |
| 1825 |
srcTileSize.h += fCFAPatternSize.h * 2; |
| 1826 |
|
| 1827 |
// Allocate source buffer. |
| 1828 |
|
| 1829 |
dng_pixel_buffer srcBuffer (dng_rect (srcTileSize), |
| 1830 |
srcPlane, |
| 1831 |
1, |
| 1832 |
srcImage.PixelType (), |
| 1833 |
pcInterleaved, |
| 1834 |
NULL); |
| 1835 |
|
| 1836 |
uint32 srcBufferSize = ComputeBufferSize (srcBuffer.fPixelType, |
| 1837 |
srcTileSize, |
| 1838 |
srcBuffer.fPlanes, |
| 1839 |
padNone); |
| 1840 |
|
| 1841 |
AutoPtr<dng_memory_block> srcData (host.Allocate (srcBufferSize)); |
| 1842 |
|
| 1843 |
srcBuffer.fData = srcData->Buffer (); |
| 1844 |
|
| 1845 |
// Allocate destination buffer. |
| 1846 |
|
| 1847 |
dng_pixel_buffer dstBuffer (dng_rect (dstTileSize), |
| 1848 |
0, |
| 1849 |
fColorPlanes, |
| 1850 |
dstImage.PixelType (), |
| 1851 |
pcRowInterleaved, |
| 1852 |
NULL); |
| 1853 |
|
| 1854 |
uint32 dstBufferSize = ComputeBufferSize (dstBuffer.fPixelType, |
| 1855 |
dstTileSize, |
| 1856 |
dstBuffer.fPlanes, |
| 1857 |
padNone); |
| 1858 |
|
| 1859 |
AutoPtr<dng_memory_block> dstData (host.Allocate (dstBufferSize)); |
| 1860 |
|
| 1861 |
dstBuffer.fData = dstData->Buffer (); |
| 1862 |
|
| 1863 |
// Create interpolator. |
| 1864 |
|
| 1865 |
AutoPtr<dng_bilinear_interpolator> interpolator (new dng_bilinear_interpolator (*this, |
| 1866 |
srcBuffer.fRowStep, |
| 1867 |
srcBuffer.fColStep)); |
| 1868 |
|
| 1869 |
// Iterate over destination tiles. |
| 1870 |
|
| 1871 |
dng_rect dstArea; |
| 1872 |
|
| 1873 |
dng_tile_iterator iter1 (dstImage, dstImage.Bounds ()); |
| 1874 |
|
| 1875 |
while (iter1.GetOneTile (dstArea)) |
| 1876 |
{ |
| 1877 |
|
| 1878 |
// Break into buffer sized tiles. |
| 1879 |
|
| 1880 |
dng_rect dstTile; |
| 1881 |
|
| 1882 |
dng_tile_iterator iter2 (dstTileSize, dstArea); |
| 1883 |
|
| 1884 |
while (iter2.GetOneTile (dstTile)) |
| 1885 |
{ |
| 1886 |
|
| 1887 |
host.SniffForAbort (); |
| 1888 |
|
| 1889 |
// Setup buffers for this tile. |
| 1890 |
|
| 1891 |
dng_rect srcTile (dstTile); |
| 1892 |
|
| 1893 |
srcTile.t >>= srcShiftV; |
| 1894 |
srcTile.b >>= srcShiftV; |
| 1895 |
|
| 1896 |
srcTile.l >>= srcShiftH; |
| 1897 |
srcTile.r >>= srcShiftH; |
| 1898 |
|
| 1899 |
srcTile.t -= fCFAPatternSize.v; |
| 1900 |
srcTile.b += fCFAPatternSize.v; |
| 1901 |
|
| 1902 |
srcTile.l -= fCFAPatternSize.h; |
| 1903 |
srcTile.r += fCFAPatternSize.h; |
| 1904 |
|
| 1905 |
srcBuffer.fArea = srcTile; |
| 1906 |
dstBuffer.fArea = dstTile; |
| 1907 |
|
| 1908 |
// Get source data. |
| 1909 |
|
| 1910 |
srcImage.Get (srcBuffer, |
| 1911 |
dng_image::edge_repeat, |
| 1912 |
fCFAPatternSize.v, |
| 1913 |
fCFAPatternSize.h); |
| 1914 |
|
| 1915 |
// Process data. |
| 1916 |
|
| 1917 |
interpolator->Interpolate (srcBuffer, |
| 1918 |
dstBuffer); |
| 1919 |
|
| 1920 |
// Save results. |
| 1921 |
|
| 1922 |
dstImage.Put (dstBuffer); |
| 1923 |
|
| 1924 |
} |
| 1925 |
|
| 1926 |
} |
| 1927 |
|
| 1928 |
} |
| 1929 |
|
| 1930 |
/*****************************************************************************/ |
| 1931 |
|
| 1932 |
void dng_mosaic_info::InterpolateFast (dng_host &host, |
| 1933 |
dng_negative & /* negative */, |
| 1934 |
const dng_image &srcImage, |
| 1935 |
dng_image &dstImage, |
| 1936 |
const dng_point &downScale, |
| 1937 |
uint32 srcPlane) const |
| 1938 |
{ |
| 1939 |
|
| 1940 |
// Create fast interpolator task. |
| 1941 |
|
| 1942 |
dng_fast_interpolator interpolator (*this, |
| 1943 |
srcImage, |
| 1944 |
dstImage, |
| 1945 |
downScale, |
| 1946 |
srcPlane); |
| 1947 |
|
| 1948 |
// Find area to process. |
| 1949 |
|
| 1950 |
dng_rect bounds = dstImage.Bounds (); |
| 1951 |
|
| 1952 |
// Do the interpolation. |
| 1953 |
|
| 1954 |
host.PerformAreaTask (interpolator, |
| 1955 |
bounds); |
| 1956 |
|
| 1957 |
} |
| 1958 |
|
| 1959 |
/*****************************************************************************/ |
| 1960 |
|
| 1961 |
void dng_mosaic_info::Interpolate (dng_host &host, |
| 1962 |
dng_negative &negative, |
| 1963 |
const dng_image &srcImage, |
| 1964 |
dng_image &dstImage, |
| 1965 |
const dng_point &downScale, |
| 1966 |
uint32 srcPlane, |
| 1967 |
dng_matrix *scaleTransforms) const |
| 1968 |
{ |
| 1969 |
|
| 1970 |
if (scaleTransforms && downScale != dng_point (1, 1)) |
| 1971 |
{ |
| 1972 |
|
| 1973 |
for (uint32 plane = 0; plane < dstImage.Planes (); plane++) |
| 1974 |
{ |
| 1975 |
|
| 1976 |
scaleTransforms [plane] = dng_matrix_3by3 (1.0 / downScale.v, 0.0, 0.0, |
| 1977 |
0.0, 1.0 / downScale.h, 0.0, |
| 1978 |
0.0, 0.0, 1.0); |
| 1979 |
|
| 1980 |
} |
| 1981 |
|
| 1982 |
} |
| 1983 |
|
| 1984 |
if (downScale == dng_point (1, 1)) |
| 1985 |
{ |
| 1986 |
|
| 1987 |
InterpolateGeneric (host, |
| 1988 |
negative, |
| 1989 |
srcImage, |
| 1990 |
dstImage, |
| 1991 |
srcPlane); |
| 1992 |
|
| 1993 |
} |
| 1994 |
|
| 1995 |
else |
| 1996 |
{ |
| 1997 |
|
| 1998 |
InterpolateFast (host, |
| 1999 |
negative, |
| 2000 |
srcImage, |
| 2001 |
dstImage, |
| 2002 |
downScale, |
| 2003 |
srcPlane); |
| 2004 |
|
| 2005 |
} |
| 2006 |
|
| 2007 |
} |
| 2008 |
|
| 2009 |
/*****************************************************************************/ |
| 2010 |
|
| 2011 |
bool dng_mosaic_info::SupportsPreservedBlackLevels () const |
| 2012 |
{ |
| 2013 |
|
| 2014 |
return false; |
| 2015 |
|
| 2016 |
} |
| 2017 |
|
| 2018 |
/*****************************************************************************/ |