| 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_resample.h" |
| 10 |
|
| 11 |
#include "dng_assertions.h" |
| 12 |
#include "dng_bottlenecks.h" |
| 13 |
#include "dng_filter_task.h" |
| 14 |
#include "dng_host.h" |
| 15 |
#include "dng_image.h" |
| 16 |
#include "dng_memory.h" |
| 17 |
#include "dng_pixel_buffer.h" |
| 18 |
#include "dng_safe_arithmetic.h" |
| 19 |
#include "dng_tag_types.h" |
| 20 |
#include "dng_utils.h" |
| 21 |
|
| 22 |
/******************************************************************************/ |
| 23 |
|
| 24 |
real64 dng_resample_bicubic::Extent () const |
| 25 |
{ |
| 26 |
|
| 27 |
return 2.0; |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/******************************************************************************/ |
| 32 |
|
| 33 |
real64 dng_resample_bicubic::Evaluate (real64 x) const |
| 34 |
{ |
| 35 |
|
| 36 |
const real64 A = -0.75; |
| 37 |
|
| 38 |
x = Abs_real64 (x); |
| 39 |
|
| 40 |
if (x >= 2.0) |
| 41 |
return 0.0; |
| 42 |
|
| 43 |
else if (x >= 1.0) |
| 44 |
return (((A * x - 5.0 * A) * x + 8.0 * A) * x - 4.0 * A); |
| 45 |
|
| 46 |
else |
| 47 |
return (((A + 2.0) * x - (A + 3.0)) * x * x + 1.0); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/******************************************************************************/ |
| 52 |
|
| 53 |
const dng_resample_function & dng_resample_bicubic::Get () |
| 54 |
{ |
| 55 |
|
| 56 |
static dng_resample_bicubic static_dng_resample_bicubic; |
| 57 |
|
| 58 |
return static_dng_resample_bicubic; |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
/*****************************************************************************/ |
| 63 |
|
| 64 |
dng_resample_coords::dng_resample_coords () |
| 65 |
|
| 66 |
: fOrigin (0) |
| 67 |
, fCoords () |
| 68 |
|
| 69 |
{ |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/*****************************************************************************/ |
| 74 |
|
| 75 |
dng_resample_coords::~dng_resample_coords () |
| 76 |
{ |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/*****************************************************************************/ |
| 81 |
|
| 82 |
void dng_resample_coords::Initialize (int32 srcOrigin, |
| 83 |
int32 dstOrigin, |
| 84 |
uint32 srcCount, |
| 85 |
uint32 dstCount, |
| 86 |
dng_memory_allocator &allocator) |
| 87 |
{ |
| 88 |
|
| 89 |
fOrigin = dstOrigin; |
| 90 |
|
| 91 |
uint32 dstEntries = 0; |
| 92 |
uint32 bufferSize = 0; |
| 93 |
|
| 94 |
if (!RoundUpUint32ToMultiple (dstCount, 8, &dstEntries) || |
| 95 |
!SafeUint32Mult (dstEntries, sizeof (int32), &bufferSize)) |
| 96 |
{ |
| 97 |
|
| 98 |
ThrowOverflow ("Arithmetic overflow computing size for coordinate " |
| 99 |
"buffer"); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
fCoords.Reset (allocator.Allocate (bufferSize)); |
| 104 |
|
| 105 |
int32 *coords = fCoords->Buffer_int32 (); |
| 106 |
|
| 107 |
real64 invScale = (real64) srcCount / |
| 108 |
(real64) dstCount; |
| 109 |
|
| 110 |
for (uint32 j = 0; j < dstCount; j++) |
| 111 |
{ |
| 112 |
|
| 113 |
real64 x = (real64) j + 0.5; |
| 114 |
|
| 115 |
real64 y = x * invScale - 0.5 + (real64) srcOrigin; |
| 116 |
|
| 117 |
coords [j] = Round_int32 (y * (real64) kResampleSubsampleCount); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
// Pad out table by replicating last entry. |
| 122 |
|
| 123 |
for (uint32 k = dstCount; k < dstEntries; k++) |
| 124 |
{ |
| 125 |
|
| 126 |
coords [k] = coords [dstCount - 1]; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/*****************************************************************************/ |
| 133 |
|
| 134 |
dng_resample_weights::dng_resample_weights () |
| 135 |
|
| 136 |
: fRadius (0) |
| 137 |
|
| 138 |
, fWeightStep (0) |
| 139 |
|
| 140 |
, fWeights32 () |
| 141 |
, fWeights16 () |
| 142 |
|
| 143 |
{ |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/*****************************************************************************/ |
| 148 |
|
| 149 |
dng_resample_weights::~dng_resample_weights () |
| 150 |
{ |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/*****************************************************************************/ |
| 155 |
|
| 156 |
void dng_resample_weights::Initialize (real64 scale, |
| 157 |
const dng_resample_function &kernel, |
| 158 |
dng_memory_allocator &allocator) |
| 159 |
{ |
| 160 |
|
| 161 |
uint32 j; |
| 162 |
|
| 163 |
// We only adjust the kernel size for scale factors less than 1.0. |
| 164 |
|
| 165 |
scale = Min_real64 (scale, 1.0); |
| 166 |
|
| 167 |
// Find radius of this kernel. |
| 168 |
|
| 169 |
fRadius = (uint32) (kernel.Extent () / scale + 0.9999); |
| 170 |
|
| 171 |
// Width is twice the radius. |
| 172 |
|
| 173 |
uint32 width = fRadius * 2; |
| 174 |
|
| 175 |
// Round to each set to weights to a multiple of 8 entries. |
| 176 |
|
| 177 |
if (!RoundUpUint32ToMultiple (width, 8, &fWeightStep)) |
| 178 |
{ |
| 179 |
|
| 180 |
ThrowOverflow ("Arithmetic overflow computing fWeightStep"); |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
// Allocate and zero weight tables. |
| 185 |
|
| 186 |
uint32 bufferSize = 0; |
| 187 |
|
| 188 |
if (!SafeUint32Mult (fWeightStep, kResampleSubsampleCount, &bufferSize) || |
| 189 |
!SafeUint32Mult (bufferSize, (uint32) sizeof (real32), &bufferSize)) |
| 190 |
{ |
| 191 |
|
| 192 |
ThrowOverflow ("Arithmetic overflow computing buffer size."); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
fWeights32.Reset (allocator.Allocate (bufferSize)); |
| 197 |
|
| 198 |
DoZeroBytes (fWeights32->Buffer (), |
| 199 |
fWeights32->LogicalSize ()); |
| 200 |
|
| 201 |
if (!SafeUint32Mult (fWeightStep, kResampleSubsampleCount, &bufferSize) || |
| 202 |
!SafeUint32Mult (bufferSize, (uint32) sizeof (int16), &bufferSize)) |
| 203 |
{ |
| 204 |
|
| 205 |
ThrowOverflow ("Arithmetic overflow computing buffer size."); |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
fWeights16.Reset (allocator.Allocate (bufferSize)); |
| 210 |
|
| 211 |
DoZeroBytes (fWeights16->Buffer (), |
| 212 |
fWeights16->LogicalSize ()); |
| 213 |
|
| 214 |
// Compute kernel for each subsample values. |
| 215 |
|
| 216 |
for (uint32 sample = 0; sample < kResampleSubsampleCount; sample++) |
| 217 |
{ |
| 218 |
|
| 219 |
real64 fract = sample * (1.0 / (real64) kResampleSubsampleCount); |
| 220 |
|
| 221 |
real32 *w32 = fWeights32->Buffer_real32 () + fWeightStep * sample; |
| 222 |
|
| 223 |
// Evaluate kernel function for 32 bit weights. |
| 224 |
|
| 225 |
{ |
| 226 |
|
| 227 |
real64 t32 = 0.0; |
| 228 |
|
| 229 |
for (j = 0; j < width; j++) |
| 230 |
{ |
| 231 |
|
| 232 |
int32 k = (int32) j - (int32) fRadius + 1; |
| 233 |
|
| 234 |
real64 x = (k - fract) * scale; |
| 235 |
|
| 236 |
w32 [j] = (real32) kernel.Evaluate (x); |
| 237 |
|
| 238 |
t32 += w32 [j]; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
// Scale 32 bit weights so total of weights is 1.0. |
| 243 |
|
| 244 |
real32 s32 = (real32) (1.0 / t32); |
| 245 |
|
| 246 |
for (j = 0; j < width; j++) |
| 247 |
{ |
| 248 |
|
| 249 |
w32 [j] *= s32; |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
// Round off 32 bit weights to 16 bit weights. |
| 256 |
|
| 257 |
{ |
| 258 |
|
| 259 |
int16 *w16 = fWeights16->Buffer_int16 () + fWeightStep * sample; |
| 260 |
|
| 261 |
int32 t16 = 0; |
| 262 |
|
| 263 |
for (j = 0; j < width; j++) |
| 264 |
{ |
| 265 |
|
| 266 |
w16 [j] = (int16) Round_int32 (w32 [j] * 16384.0); |
| 267 |
|
| 268 |
t16 += w16 [j]; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
// Adjust center entry for any round off error so total is |
| 273 |
// exactly 16384. |
| 274 |
|
| 275 |
w16 [fRadius - (fract >= 0.5 ? 0 : 1)] += (int16) (16384 - t16); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
/*****************************************************************************/ |
| 284 |
|
| 285 |
dng_resample_weights_2d::dng_resample_weights_2d () |
| 286 |
|
| 287 |
: fRadius (0) |
| 288 |
|
| 289 |
, fRowStep (0) |
| 290 |
, fColStep (0) |
| 291 |
|
| 292 |
, fWeights32 () |
| 293 |
, fWeights16 () |
| 294 |
|
| 295 |
{ |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
/*****************************************************************************/ |
| 300 |
|
| 301 |
dng_resample_weights_2d::~dng_resample_weights_2d () |
| 302 |
{ |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
/*****************************************************************************/ |
| 307 |
|
| 308 |
void dng_resample_weights_2d::Initialize (const dng_resample_function &kernel, |
| 309 |
dng_memory_allocator &allocator) |
| 310 |
{ |
| 311 |
|
| 312 |
// Find radius of this kernel. Unlike with 1d resample weights (see |
| 313 |
// dng_resample_weights), we never scale up the kernel size. |
| 314 |
|
| 315 |
fRadius = (uint32) (kernel.Extent () + 0.9999); |
| 316 |
|
| 317 |
// Width is twice the radius. |
| 318 |
|
| 319 |
uint32 width = 0; |
| 320 |
uint32 widthSqr = 0; |
| 321 |
uint32 step = 0; |
| 322 |
|
| 323 |
if (!SafeUint32Mult (fRadius, 2, &width) || |
| 324 |
!SafeUint32Mult (width, width, &widthSqr) || |
| 325 |
!RoundUpUint32ToMultiple (widthSqr, 8, &step) || |
| 326 |
!SafeUint32Mult (step, kResampleSubsampleCount2D, &fRowStep)) |
| 327 |
{ |
| 328 |
|
| 329 |
ThrowOverflow ("Arithmetic overflow computing row step."); |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
fColStep = step; |
| 334 |
|
| 335 |
// Allocate and zero weight tables. |
| 336 |
|
| 337 |
uint32 bufferSize = 0; |
| 338 |
|
| 339 |
if (!SafeUint32Mult (step, kResampleSubsampleCount2D, &bufferSize) || |
| 340 |
!SafeUint32Mult (bufferSize, kResampleSubsampleCount2D, &bufferSize) || |
| 341 |
!SafeUint32Mult (bufferSize, (uint32) sizeof (real32), &bufferSize)) |
| 342 |
{ |
| 343 |
|
| 344 |
ThrowOverflow ("Arithmetic overflow computing buffer size."); |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
fWeights32.Reset (allocator.Allocate (bufferSize)); |
| 349 |
|
| 350 |
DoZeroBytes (fWeights32->Buffer (), |
| 351 |
fWeights32->LogicalSize ()); |
| 352 |
|
| 353 |
if (!SafeUint32Mult (step, kResampleSubsampleCount2D, &bufferSize) || |
| 354 |
!SafeUint32Mult (bufferSize, kResampleSubsampleCount2D, &bufferSize) || |
| 355 |
!SafeUint32Mult (bufferSize, (uint32) sizeof (int16), &bufferSize)) |
| 356 |
{ |
| 357 |
|
| 358 |
ThrowOverflow ("Arithmetic overflow computing buffer size."); |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
fWeights16.Reset (allocator.Allocate (bufferSize)); |
| 363 |
|
| 364 |
DoZeroBytes (fWeights16->Buffer (), |
| 365 |
fWeights16->LogicalSize ()); |
| 366 |
|
| 367 |
// Compute kernel for each subsample values. |
| 368 |
|
| 369 |
for (uint32 y = 0; y < kResampleSubsampleCount2D; y++) |
| 370 |
{ |
| 371 |
|
| 372 |
real64 yFract = y * (1.0 / (real64) kResampleSubsampleCount2D); |
| 373 |
|
| 374 |
for (uint32 x = 0; x < kResampleSubsampleCount2D; x++) |
| 375 |
{ |
| 376 |
|
| 377 |
real64 xFract = x * (1.0 / (real64) kResampleSubsampleCount2D); |
| 378 |
|
| 379 |
real32 *w32 = (real32 *) Weights32 (dng_point ((int32) y, |
| 380 |
(int32) x)); |
| 381 |
|
| 382 |
// Evaluate kernel function for 32 bit weights. |
| 383 |
|
| 384 |
{ |
| 385 |
|
| 386 |
real64 t32 = 0.0; |
| 387 |
|
| 388 |
uint32 index = 0; |
| 389 |
|
| 390 |
for (uint32 i = 0; i < width; i++) |
| 391 |
{ |
| 392 |
|
| 393 |
int32 yInt = ((int32) i) - (int32) fRadius + 1; |
| 394 |
real64 yPos = yInt - yFract; |
| 395 |
|
| 396 |
for (uint32 j = 0; j < width; j++) |
| 397 |
{ |
| 398 |
|
| 399 |
int32 xInt = ((int32) j) - (int32) fRadius + 1; |
| 400 |
real64 xPos = xInt - xFract; |
| 401 |
|
| 402 |
#if 0 |
| 403 |
|
| 404 |
// Radial. |
| 405 |
|
| 406 |
real64 dy2 = yPos * yPos; |
| 407 |
real64 dx2 = xPos * xPos; |
| 408 |
|
| 409 |
real64 r = sqrt (dx2 + dy2); |
| 410 |
|
| 411 |
w32 [index] = (real32) kernel.Evaluate (r); |
| 412 |
|
| 413 |
#else |
| 414 |
|
| 415 |
// Separable. |
| 416 |
|
| 417 |
w32 [index] = (real32) kernel.Evaluate (xPos) * |
| 418 |
(real32) kernel.Evaluate (yPos); |
| 419 |
|
| 420 |
#endif |
| 421 |
|
| 422 |
t32 += w32 [index]; |
| 423 |
|
| 424 |
index++; |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
} |
| 429 |
|
| 430 |
// Scale 32 bit weights so total of weights is 1.0. |
| 431 |
|
| 432 |
const real32 s32 = (real32) (1.0 / t32); |
| 433 |
|
| 434 |
for (uint32 i = 0; i < widthSqr; i++) |
| 435 |
{ |
| 436 |
|
| 437 |
w32 [i] *= s32; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
} |
| 442 |
|
| 443 |
// Round off 32 bit weights to 16 bit weights. |
| 444 |
|
| 445 |
{ |
| 446 |
|
| 447 |
int16 *w16 = (int16 *) Weights16 (dng_point ((int32) y, |
| 448 |
(int32) x)); |
| 449 |
|
| 450 |
int32 t16 = 0; |
| 451 |
|
| 452 |
for (uint32 j = 0; j < widthSqr; j++) |
| 453 |
{ |
| 454 |
|
| 455 |
w16 [j] = (int16) Round_int32 (w32 [j] * 16384.0); |
| 456 |
|
| 457 |
t16 += w16 [j]; |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
// Adjust one of the center entries for any round off error so total |
| 462 |
// is exactly 16384. |
| 463 |
|
| 464 |
const uint32 xOffset = fRadius - ((xFract >= 0.5) ? 0 : 1); |
| 465 |
const uint32 yOffset = fRadius - ((yFract >= 0.5) ? 0 : 1); |
| 466 |
const uint32 centerOffset = width * yOffset + xOffset; |
| 467 |
|
| 468 |
w16 [centerOffset] += (int16) (16384 - t16); |
| 469 |
|
| 470 |
} |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
/*****************************************************************************/ |
| 479 |
|
| 480 |
class dng_resample_task: public dng_filter_task |
| 481 |
{ |
| 482 |
|
| 483 |
protected: |
| 484 |
|
| 485 |
dng_rect fSrcBounds; |
| 486 |
dng_rect fDstBounds; |
| 487 |
|
| 488 |
const dng_resample_function &fKernel; |
| 489 |
|
| 490 |
real64 fRowScale; |
| 491 |
real64 fColScale; |
| 492 |
|
| 493 |
dng_resample_coords fRowCoords; |
| 494 |
dng_resample_coords fColCoords; |
| 495 |
|
| 496 |
dng_resample_weights fWeightsV; |
| 497 |
dng_resample_weights fWeightsH; |
| 498 |
|
| 499 |
dng_point fSrcTileSize; |
| 500 |
|
| 501 |
AutoPtr<dng_memory_block> fTempBuffer [kMaxMPThreads]; |
| 502 |
|
| 503 |
public: |
| 504 |
|
| 505 |
dng_resample_task (const dng_image &srcImage, |
| 506 |
dng_image &dstImage, |
| 507 |
const dng_rect &srcBounds, |
| 508 |
const dng_rect &dstBounds, |
| 509 |
const dng_resample_function &kernel); |
| 510 |
|
| 511 |
virtual dng_rect SrcArea (const dng_rect &dstArea); |
| 512 |
|
| 513 |
virtual dng_point SrcTileSize (const dng_point &dstTileSize); |
| 514 |
|
| 515 |
virtual void Start (uint32 threadCount, |
| 516 |
const dng_rect &dstArea, |
| 517 |
const dng_point &tileSize, |
| 518 |
dng_memory_allocator *allocator, |
| 519 |
dng_abort_sniffer *sniffer); |
| 520 |
|
| 521 |
virtual void ProcessArea (uint32 threadIndex, |
| 522 |
dng_pixel_buffer &srcBuffer, |
| 523 |
dng_pixel_buffer &dstBuffer); |
| 524 |
|
| 525 |
}; |
| 526 |
|
| 527 |
/*****************************************************************************/ |
| 528 |
|
| 529 |
dng_resample_task::dng_resample_task (const dng_image &srcImage, |
| 530 |
dng_image &dstImage, |
| 531 |
const dng_rect &srcBounds, |
| 532 |
const dng_rect &dstBounds, |
| 533 |
const dng_resample_function &kernel) |
| 534 |
|
| 535 |
: dng_filter_task ("dng_resample_task", |
| 536 |
srcImage, |
| 537 |
dstImage) |
| 538 |
|
| 539 |
, fSrcBounds (srcBounds) |
| 540 |
, fDstBounds (dstBounds) |
| 541 |
|
| 542 |
, fKernel (kernel) |
| 543 |
|
| 544 |
, fRowScale ((srcBounds.H () != 0) ? dstBounds.H () / (real64) srcBounds.H () : 0) |
| 545 |
, fColScale ((srcBounds.W () != 0) ? dstBounds.W () / (real64) srcBounds.W () : 0) |
| 546 |
|
| 547 |
, fRowCoords () |
| 548 |
, fColCoords () |
| 549 |
|
| 550 |
, fWeightsV () |
| 551 |
, fWeightsH () |
| 552 |
|
| 553 |
, fSrcTileSize () |
| 554 |
|
| 555 |
{ |
| 556 |
|
| 557 |
if (srcImage.PixelSize () <= 2 && |
| 558 |
dstImage.PixelSize () <= 2 && |
| 559 |
srcImage.PixelRange () == dstImage.PixelRange ()) |
| 560 |
{ |
| 561 |
fSrcPixelType = ttShort; |
| 562 |
fDstPixelType = ttShort; |
| 563 |
} |
| 564 |
|
| 565 |
else |
| 566 |
{ |
| 567 |
fSrcPixelType = ttFloat; |
| 568 |
fDstPixelType = ttFloat; |
| 569 |
} |
| 570 |
|
| 571 |
fUnitCell = dng_point (8, 8); |
| 572 |
|
| 573 |
fMaxTileSize.v = Pin_int32 (fUnitCell.v, |
| 574 |
Round_int32 (fMaxTileSize.v * fRowScale), |
| 575 |
fMaxTileSize.v); |
| 576 |
|
| 577 |
fMaxTileSize.h = Pin_int32 (fUnitCell.h, |
| 578 |
Round_int32 (fMaxTileSize.h * fColScale), |
| 579 |
fMaxTileSize.h); |
| 580 |
|
| 581 |
} |
| 582 |
|
| 583 |
/*****************************************************************************/ |
| 584 |
|
| 585 |
dng_rect dng_resample_task::SrcArea (const dng_rect &dstArea) |
| 586 |
{ |
| 587 |
|
| 588 |
int32 offsetV = fWeightsV.Offset (); |
| 589 |
int32 offsetH = fWeightsH.Offset (); |
| 590 |
|
| 591 |
int32 widthV = ConvertUint32ToInt32 (fWeightsV.Width ()); |
| 592 |
int32 widthH = ConvertUint32ToInt32 (fWeightsH.Width ()); |
| 593 |
|
| 594 |
dng_rect srcArea; |
| 595 |
|
| 596 |
srcArea.t = SafeInt32Add (fRowCoords.Pixel (dstArea.t), offsetV); |
| 597 |
srcArea.l = SafeInt32Add (fColCoords.Pixel (dstArea.l), offsetH); |
| 598 |
|
| 599 |
srcArea.b = SafeInt32Add (SafeInt32Add |
| 600 |
(fRowCoords.Pixel (SafeInt32Sub (dstArea.b, 1)), |
| 601 |
offsetV), |
| 602 |
widthV); |
| 603 |
|
| 604 |
srcArea.r = SafeInt32Add (SafeInt32Add |
| 605 |
(fColCoords.Pixel (SafeInt32Sub (dstArea.r, 1)), |
| 606 |
offsetH), |
| 607 |
widthH); |
| 608 |
|
| 609 |
return srcArea; |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
/*****************************************************************************/ |
| 614 |
|
| 615 |
dng_point dng_resample_task::SrcTileSize (const dng_point & /* dstTileSize */) |
| 616 |
{ |
| 617 |
|
| 618 |
return fSrcTileSize; |
| 619 |
|
| 620 |
} |
| 621 |
|
| 622 |
/*****************************************************************************/ |
| 623 |
|
| 624 |
void dng_resample_task::Start (uint32 threadCount, |
| 625 |
const dng_rect &dstArea, |
| 626 |
const dng_point &tileSize, |
| 627 |
dng_memory_allocator *allocator, |
| 628 |
dng_abort_sniffer *sniffer) |
| 629 |
{ |
| 630 |
|
| 631 |
// Compute sub-pixel resolution coordinates in the source image for |
| 632 |
// each row and column of the destination area. |
| 633 |
|
| 634 |
fRowCoords.Initialize (fSrcBounds.t, |
| 635 |
fDstBounds.t, |
| 636 |
fSrcBounds.H (), |
| 637 |
fDstBounds.H (), |
| 638 |
*allocator); |
| 639 |
|
| 640 |
fColCoords.Initialize (fSrcBounds.l, |
| 641 |
fDstBounds.l, |
| 642 |
fSrcBounds.W (), |
| 643 |
fDstBounds.W (), |
| 644 |
*allocator); |
| 645 |
|
| 646 |
// Compute resampling kernels. |
| 647 |
|
| 648 |
fWeightsV.Initialize (fRowScale, |
| 649 |
fKernel, |
| 650 |
*allocator); |
| 651 |
|
| 652 |
fWeightsH.Initialize (fColScale, |
| 653 |
fKernel, |
| 654 |
*allocator); |
| 655 |
|
| 656 |
// Find upper bound on source source tile. |
| 657 |
|
| 658 |
fSrcTileSize.v = Round_int32 (tileSize.v / fRowScale) + fWeightsV.Width () + 2; |
| 659 |
fSrcTileSize.h = Round_int32 (tileSize.h / fColScale) + fWeightsH.Width () + 2; |
| 660 |
|
| 661 |
// Allocate temp buffers. |
| 662 |
|
| 663 |
uint32 tempBufferSize = 0; |
| 664 |
|
| 665 |
if (!RoundUpUint32ToMultiple (fSrcTileSize.h, 8, &tempBufferSize) || |
| 666 |
!SafeUint32Mult (tempBufferSize, |
| 667 |
static_cast<uint32> (sizeof (real32)), |
| 668 |
&tempBufferSize)) |
| 669 |
{ |
| 670 |
|
| 671 |
ThrowOverflow ("Arithmetic overflow computing buffer size."); |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
for (uint32 threadIndex = 0; threadIndex < threadCount; threadIndex++) |
| 676 |
{ |
| 677 |
|
| 678 |
fTempBuffer [threadIndex] . Reset (allocator->Allocate (tempBufferSize)); |
| 679 |
|
| 680 |
} |
| 681 |
|
| 682 |
// Allocate the pixel buffers. |
| 683 |
|
| 684 |
dng_filter_task::Start (threadCount, |
| 685 |
dstArea, |
| 686 |
tileSize, |
| 687 |
allocator, |
| 688 |
sniffer); |
| 689 |
|
| 690 |
} |
| 691 |
|
| 692 |
/*****************************************************************************/ |
| 693 |
|
| 694 |
void dng_resample_task::ProcessArea (uint32 threadIndex, |
| 695 |
dng_pixel_buffer &srcBuffer, |
| 696 |
dng_pixel_buffer &dstBuffer) |
| 697 |
{ |
| 698 |
|
| 699 |
dng_rect srcArea = srcBuffer.fArea; |
| 700 |
dng_rect dstArea = dstBuffer.fArea; |
| 701 |
|
| 702 |
uint32 srcCols = srcArea.W (); |
| 703 |
uint32 dstCols = dstArea.W (); |
| 704 |
|
| 705 |
uint32 widthV = fWeightsV.Width (); |
| 706 |
uint32 widthH = fWeightsH.Width (); |
| 707 |
|
| 708 |
int32 offsetV = fWeightsV.Offset (); |
| 709 |
int32 offsetH = fWeightsH.Offset (); |
| 710 |
|
| 711 |
uint32 stepH = fWeightsH.Step (); |
| 712 |
|
| 713 |
const int32 *rowCoords = fRowCoords.Coords (0 ); |
| 714 |
const int32 *colCoords = fColCoords.Coords (dstArea.l); |
| 715 |
|
| 716 |
if (fSrcPixelType == ttFloat) |
| 717 |
{ |
| 718 |
|
| 719 |
const real32 *weightsH = fWeightsH.Weights32 (0); |
| 720 |
|
| 721 |
real32 *tPtr = fTempBuffer [threadIndex]->Buffer_real32 (); |
| 722 |
|
| 723 |
real32 *ttPtr = tPtr + offsetH - srcArea.l; |
| 724 |
|
| 725 |
for (int32 dstRow = dstArea.t; dstRow < dstArea.b; dstRow++) |
| 726 |
{ |
| 727 |
|
| 728 |
int32 rowCoord = rowCoords [dstRow]; |
| 729 |
|
| 730 |
int32 rowFract = rowCoord & kResampleSubsampleMask; |
| 731 |
|
| 732 |
const real32 *weightsV = fWeightsV.Weights32 (rowFract); |
| 733 |
|
| 734 |
int32 srcRow = (rowCoord >> kResampleSubsampleBits) + offsetV; |
| 735 |
|
| 736 |
for (uint32 plane = 0; plane < dstBuffer.fPlanes; plane++) |
| 737 |
{ |
| 738 |
|
| 739 |
const real32 *sPtr = srcBuffer.ConstPixel_real32 (srcRow, |
| 740 |
srcArea.l, |
| 741 |
plane); |
| 742 |
|
| 743 |
DoResampleDown32 (sPtr, |
| 744 |
tPtr, |
| 745 |
srcCols, |
| 746 |
srcBuffer.fRowStep, |
| 747 |
weightsV, |
| 748 |
widthV); |
| 749 |
|
| 750 |
real32 *dPtr = dstBuffer.DirtyPixel_real32 (dstRow, |
| 751 |
dstArea.l, |
| 752 |
plane); |
| 753 |
|
| 754 |
DoResampleAcross32 (ttPtr, |
| 755 |
dPtr, |
| 756 |
dstCols, |
| 757 |
colCoords, |
| 758 |
weightsH, |
| 759 |
widthH, |
| 760 |
stepH); |
| 761 |
|
| 762 |
} |
| 763 |
|
| 764 |
} |
| 765 |
|
| 766 |
} |
| 767 |
|
| 768 |
else |
| 769 |
{ |
| 770 |
|
| 771 |
const int16 *weightsH = fWeightsH.Weights16 (0); |
| 772 |
|
| 773 |
uint16 *tPtr = fTempBuffer [threadIndex]->Buffer_uint16 (); |
| 774 |
|
| 775 |
uint16 *ttPtr = tPtr + offsetH - srcArea.l; |
| 776 |
|
| 777 |
uint32 pixelRange = fDstImage.PixelRange (); |
| 778 |
|
| 779 |
for (int32 dstRow = dstArea.t; dstRow < dstArea.b; dstRow++) |
| 780 |
{ |
| 781 |
|
| 782 |
int32 rowCoord = rowCoords [dstRow]; |
| 783 |
|
| 784 |
int32 rowFract = rowCoord & kResampleSubsampleMask; |
| 785 |
|
| 786 |
const int16 *weightsV = fWeightsV.Weights16 (rowFract); |
| 787 |
|
| 788 |
int32 srcRow = (rowCoord >> kResampleSubsampleBits) + offsetV; |
| 789 |
|
| 790 |
for (uint32 plane = 0; plane < dstBuffer.fPlanes; plane++) |
| 791 |
{ |
| 792 |
|
| 793 |
const uint16 *sPtr = srcBuffer.ConstPixel_uint16 (srcRow, |
| 794 |
srcArea.l, |
| 795 |
plane); |
| 796 |
|
| 797 |
DoResampleDown16 (sPtr, |
| 798 |
tPtr, |
| 799 |
srcCols, |
| 800 |
srcBuffer.fRowStep, |
| 801 |
weightsV, |
| 802 |
widthV, |
| 803 |
pixelRange); |
| 804 |
|
| 805 |
uint16 *dPtr = dstBuffer.DirtyPixel_uint16 (dstRow, |
| 806 |
dstArea.l, |
| 807 |
plane); |
| 808 |
|
| 809 |
DoResampleAcross16 (ttPtr, |
| 810 |
dPtr, |
| 811 |
dstCols, |
| 812 |
colCoords, |
| 813 |
weightsH, |
| 814 |
widthH, |
| 815 |
stepH, |
| 816 |
pixelRange); |
| 817 |
|
| 818 |
} |
| 819 |
|
| 820 |
} |
| 821 |
|
| 822 |
} |
| 823 |
|
| 824 |
} |
| 825 |
|
| 826 |
/*****************************************************************************/ |
| 827 |
|
| 828 |
void ResampleImage (dng_host &host, |
| 829 |
const dng_image &srcImage, |
| 830 |
dng_image &dstImage, |
| 831 |
const dng_rect &srcBounds, |
| 832 |
const dng_rect &dstBounds, |
| 833 |
const dng_resample_function &kernel) |
| 834 |
{ |
| 835 |
|
| 836 |
dng_resample_task task (srcImage, |
| 837 |
dstImage, |
| 838 |
srcBounds, |
| 839 |
dstBounds, |
| 840 |
kernel); |
| 841 |
|
| 842 |
host.PerformAreaTask (task, |
| 843 |
dstBounds); |
| 844 |
|
| 845 |
} |
| 846 |
|
| 847 |
/*****************************************************************************/ |