| 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_image.h" |
| 10 |
|
| 11 |
#include "dng_assertions.h" |
| 12 |
#include "dng_exceptions.h" |
| 13 |
#include "dng_orientation.h" |
| 14 |
#include "dng_pixel_buffer.h" |
| 15 |
#include "dng_tag_types.h" |
| 16 |
#include "dng_tile_iterator.h" |
| 17 |
#include "dng_utils.h" |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
dng_tile_buffer::dng_tile_buffer (const dng_image &image, |
| 22 |
const dng_rect &tile, |
| 23 |
bool dirty) |
| 24 |
|
| 25 |
: fImage (image) |
| 26 |
, fRefData (NULL) |
| 27 |
|
| 28 |
{ |
| 29 |
|
| 30 |
fImage.AcquireTileBuffer (*this, |
| 31 |
tile, |
| 32 |
dirty); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/*****************************************************************************/ |
| 37 |
|
| 38 |
dng_tile_buffer::~dng_tile_buffer () |
| 39 |
{ |
| 40 |
|
| 41 |
fImage.ReleaseTileBuffer (*this); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/*****************************************************************************/ |
| 46 |
|
| 47 |
dng_const_tile_buffer::dng_const_tile_buffer (const dng_image &image, |
| 48 |
const dng_rect &tile) |
| 49 |
|
| 50 |
: dng_tile_buffer (image, tile, false) |
| 51 |
|
| 52 |
{ |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/*****************************************************************************/ |
| 57 |
|
| 58 |
dng_const_tile_buffer::~dng_const_tile_buffer () |
| 59 |
{ |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/*****************************************************************************/ |
| 64 |
|
| 65 |
dng_dirty_tile_buffer::dng_dirty_tile_buffer (dng_image &image, |
| 66 |
const dng_rect &tile) |
| 67 |
|
| 68 |
: dng_tile_buffer (image, tile, true) |
| 69 |
|
| 70 |
{ |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/*****************************************************************************/ |
| 75 |
|
| 76 |
dng_dirty_tile_buffer::~dng_dirty_tile_buffer () |
| 77 |
{ |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/*****************************************************************************/ |
| 82 |
|
| 83 |
dng_image::dng_image (const dng_rect &bounds, |
| 84 |
uint32 planes, |
| 85 |
uint32 pixelType) |
| 86 |
|
| 87 |
: fBounds (bounds) |
| 88 |
, fPlanes (planes) |
| 89 |
, fPixelType (pixelType) |
| 90 |
|
| 91 |
{ |
| 92 |
|
| 93 |
if (bounds.IsEmpty () || planes == 0 || PixelSize () == 0) |
| 94 |
{ |
| 95 |
|
| 96 |
#if qDNGValidate |
| 97 |
|
| 98 |
ReportError ("Fuzz: Attempt to create zero size image"); |
| 99 |
|
| 100 |
#endif |
| 101 |
|
| 102 |
ThrowBadFormat (); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
// Allow up to 2 * kMaxImageSide to deal with intermediate image objects |
| 107 |
// (e.g., rotated and padded). |
| 108 |
|
| 109 |
static const uint32 kLimit = 2 * kMaxImageSide; |
| 110 |
|
| 111 |
if (bounds.W () > kLimit || |
| 112 |
bounds.H () > kLimit) |
| 113 |
{ |
| 114 |
|
| 115 |
ThrowBadFormat ("dng_image bounds too large"); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/*****************************************************************************/ |
| 122 |
|
| 123 |
dng_image::~dng_image () |
| 124 |
{ |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/*****************************************************************************/ |
| 129 |
|
| 130 |
dng_image * dng_image::Clone () const |
| 131 |
{ |
| 132 |
|
| 133 |
ThrowProgramError ("Clone is not supported by this dng_image subclass"); |
| 134 |
|
| 135 |
return NULL; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/*****************************************************************************/ |
| 140 |
|
| 141 |
void dng_image::SetPixelType (uint32 pixelType) |
| 142 |
{ |
| 143 |
|
| 144 |
if (TagTypeSize (pixelType) != PixelSize ()) |
| 145 |
{ |
| 146 |
|
| 147 |
ThrowProgramError ("Cannot change pixel size for existing image"); |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
fPixelType = pixelType; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/*****************************************************************************/ |
| 156 |
|
| 157 |
uint32 dng_image::PixelSize () const |
| 158 |
{ |
| 159 |
|
| 160 |
return TagTypeSize (PixelType ()); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
/*****************************************************************************/ |
| 165 |
|
| 166 |
uint32 dng_image::PixelRange () const |
| 167 |
{ |
| 168 |
|
| 169 |
switch (fPixelType) |
| 170 |
{ |
| 171 |
|
| 172 |
case ttByte: |
| 173 |
case ttSByte: |
| 174 |
{ |
| 175 |
return 0x0FF; |
| 176 |
} |
| 177 |
|
| 178 |
case ttShort: |
| 179 |
case ttSShort: |
| 180 |
{ |
| 181 |
return 0x0FFFF; |
| 182 |
} |
| 183 |
|
| 184 |
case ttLong: |
| 185 |
case ttSLong: |
| 186 |
{ |
| 187 |
return 0xFFFFFFFF; |
| 188 |
} |
| 189 |
|
| 190 |
default: |
| 191 |
break; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
return 0; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/*****************************************************************************/ |
| 200 |
|
| 201 |
dng_rect dng_image::RepeatingTile () const |
| 202 |
{ |
| 203 |
|
| 204 |
return fBounds; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
/*****************************************************************************/ |
| 209 |
|
| 210 |
void dng_image::AcquireTileBuffer (dng_tile_buffer & /* buffer */, |
| 211 |
const dng_rect & /* area */, |
| 212 |
bool /* dirty */) const |
| 213 |
{ |
| 214 |
|
| 215 |
ThrowProgramError (); |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/*****************************************************************************/ |
| 220 |
|
| 221 |
void dng_image::ReleaseTileBuffer (dng_tile_buffer & /* buffer */) const |
| 222 |
{ |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
/*****************************************************************************/ |
| 227 |
|
| 228 |
void dng_image::DoGet (dng_pixel_buffer &buffer) const |
| 229 |
{ |
| 230 |
|
| 231 |
dng_rect tile; |
| 232 |
|
| 233 |
dng_tile_iterator iter (*this, buffer.fArea); |
| 234 |
|
| 235 |
while (iter.GetOneTile (tile)) |
| 236 |
{ |
| 237 |
|
| 238 |
dng_const_tile_buffer tileBuffer (*this, tile); |
| 239 |
|
| 240 |
buffer.CopyArea (tileBuffer, |
| 241 |
tile, |
| 242 |
buffer.fPlane, |
| 243 |
buffer.fPlanes); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
/*****************************************************************************/ |
| 250 |
|
| 251 |
void dng_image::DoPut (const dng_pixel_buffer &buffer) |
| 252 |
{ |
| 253 |
|
| 254 |
dng_rect tile; |
| 255 |
|
| 256 |
dng_tile_iterator iter (*this, buffer.fArea); |
| 257 |
|
| 258 |
while (iter.GetOneTile (tile)) |
| 259 |
{ |
| 260 |
|
| 261 |
dng_dirty_tile_buffer tileBuffer (*this, tile); |
| 262 |
|
| 263 |
tileBuffer.CopyArea (buffer, |
| 264 |
tile, |
| 265 |
buffer.fPlane, |
| 266 |
buffer.fPlanes); |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/*****************************************************************************/ |
| 273 |
|
| 274 |
void dng_image::GetRepeat (dng_pixel_buffer &buffer, |
| 275 |
const dng_rect &srcArea, |
| 276 |
const dng_rect &dstArea) const |
| 277 |
{ |
| 278 |
|
| 279 |
// If we already have the entire srcArea in the |
| 280 |
// buffer, we can just repeat that. |
| 281 |
|
| 282 |
if ((srcArea & buffer.fArea) == srcArea) |
| 283 |
{ |
| 284 |
|
| 285 |
buffer.RepeatArea (srcArea, |
| 286 |
dstArea); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
// Else we first need to get the srcArea into the buffer area. |
| 291 |
|
| 292 |
else |
| 293 |
{ |
| 294 |
|
| 295 |
// Find repeating pattern size. |
| 296 |
|
| 297 |
dng_point repeat = srcArea.Size (); |
| 298 |
|
| 299 |
// Find pattern phase at top-left corner of destination area. |
| 300 |
|
| 301 |
dng_point phase = dng_pixel_buffer::RepeatPhase (srcArea, |
| 302 |
dstArea); |
| 303 |
|
| 304 |
// Find new source area at top-left of dstArea. |
| 305 |
|
| 306 |
dng_rect newArea = srcArea + (dstArea.TL () - |
| 307 |
srcArea.TL ()); |
| 308 |
|
| 309 |
// Find quadrant split coordinates. |
| 310 |
|
| 311 |
int32 splitV = newArea.t + repeat.v - phase.v; |
| 312 |
int32 splitH = newArea.l + repeat.h - phase.h; |
| 313 |
|
| 314 |
// Top-left quadrant. |
| 315 |
|
| 316 |
dng_rect dst1 (dng_rect (newArea.t, |
| 317 |
newArea.l, |
| 318 |
splitV, |
| 319 |
splitH) & dstArea); |
| 320 |
|
| 321 |
if (dst1.NotEmpty ()) |
| 322 |
{ |
| 323 |
|
| 324 |
dng_pixel_buffer temp (buffer); |
| 325 |
|
| 326 |
temp.fArea = dst1 + (srcArea.TL () - |
| 327 |
dstArea.TL () + |
| 328 |
dng_point (phase.v, phase.h)); |
| 329 |
|
| 330 |
temp.fData = buffer.DirtyPixel (dst1.t, |
| 331 |
dst1.l, |
| 332 |
buffer.fPlane); |
| 333 |
|
| 334 |
DoGet (temp); |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
// Top-right quadrant. |
| 339 |
|
| 340 |
dng_rect dst2 (dng_rect (newArea.t, |
| 341 |
splitH, |
| 342 |
splitV, |
| 343 |
newArea.r) & dstArea); |
| 344 |
|
| 345 |
if (dst2.NotEmpty ()) |
| 346 |
{ |
| 347 |
|
| 348 |
dng_pixel_buffer temp (buffer); |
| 349 |
|
| 350 |
temp.fArea = dst2 + (srcArea.TL () - |
| 351 |
dstArea.TL () + |
| 352 |
dng_point (phase.v, -phase.h)); |
| 353 |
|
| 354 |
temp.fData = buffer.DirtyPixel (dst2.t, |
| 355 |
dst2.l, |
| 356 |
buffer.fPlane); |
| 357 |
|
| 358 |
DoGet (temp); |
| 359 |
|
| 360 |
} |
| 361 |
|
| 362 |
// Bottom-left quadrant. |
| 363 |
|
| 364 |
dng_rect dst3 (dng_rect (splitV, |
| 365 |
newArea.l, |
| 366 |
newArea.b, |
| 367 |
splitH) & dstArea); |
| 368 |
|
| 369 |
if (dst3.NotEmpty ()) |
| 370 |
{ |
| 371 |
|
| 372 |
dng_pixel_buffer temp (buffer); |
| 373 |
|
| 374 |
temp.fArea = dst3 + (srcArea.TL () - |
| 375 |
dstArea.TL () + |
| 376 |
dng_point (-phase.v, phase.h)); |
| 377 |
|
| 378 |
temp.fData = buffer.DirtyPixel (dst3.t, |
| 379 |
dst3.l, |
| 380 |
buffer.fPlane); |
| 381 |
|
| 382 |
DoGet (temp); |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
// Bottom-right quadrant. |
| 387 |
|
| 388 |
dng_rect dst4 (dng_rect (splitV, |
| 389 |
splitH, |
| 390 |
newArea.b, |
| 391 |
newArea.r) & dstArea); |
| 392 |
|
| 393 |
if (dst4.NotEmpty ()) |
| 394 |
{ |
| 395 |
|
| 396 |
dng_pixel_buffer temp (buffer); |
| 397 |
|
| 398 |
temp.fArea = dst4 + (srcArea.TL () - |
| 399 |
dstArea.TL () + |
| 400 |
dng_point (-phase.v, -phase.h)); |
| 401 |
|
| 402 |
temp.fData = buffer.DirtyPixel (dst4.t, |
| 403 |
dst4.l, |
| 404 |
buffer.fPlane); |
| 405 |
|
| 406 |
DoGet (temp); |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
// Replicate this new source area. |
| 411 |
|
| 412 |
buffer.RepeatArea (newArea, |
| 413 |
dstArea); |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
/*****************************************************************************/ |
| 420 |
|
| 421 |
void dng_image::GetEdge (dng_pixel_buffer &buffer, |
| 422 |
edge_option edgeOption, |
| 423 |
const dng_rect &srcArea, |
| 424 |
const dng_rect &dstArea) const |
| 425 |
{ |
| 426 |
|
| 427 |
switch (edgeOption) |
| 428 |
{ |
| 429 |
|
| 430 |
case edge_zero: |
| 431 |
{ |
| 432 |
|
| 433 |
buffer.SetZero (dstArea, |
| 434 |
buffer.fPlane, |
| 435 |
buffer.fPlanes); |
| 436 |
|
| 437 |
break; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
case edge_repeat: |
| 442 |
{ |
| 443 |
|
| 444 |
GetRepeat (buffer, |
| 445 |
srcArea, |
| 446 |
dstArea); |
| 447 |
|
| 448 |
break; |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
case edge_repeat_zero_last: |
| 453 |
{ |
| 454 |
|
| 455 |
if (buffer.fPlanes > 1) |
| 456 |
{ |
| 457 |
|
| 458 |
dng_pixel_buffer buffer1 (buffer); |
| 459 |
|
| 460 |
buffer1.fPlanes--; |
| 461 |
|
| 462 |
GetEdge (buffer1, |
| 463 |
edge_repeat, |
| 464 |
srcArea, |
| 465 |
dstArea); |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
dng_pixel_buffer buffer2 (buffer); |
| 470 |
|
| 471 |
buffer2.fPlane = buffer.fPlanes - 1; |
| 472 |
buffer2.fPlanes = 1; |
| 473 |
|
| 474 |
buffer2.fData = buffer.DirtyPixel (buffer2.fArea.t, |
| 475 |
buffer2.fArea.l, |
| 476 |
buffer2.fPlane); |
| 477 |
|
| 478 |
GetEdge (buffer2, |
| 479 |
edge_zero, |
| 480 |
srcArea, |
| 481 |
dstArea); |
| 482 |
|
| 483 |
break; |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
default: |
| 488 |
{ |
| 489 |
|
| 490 |
ThrowProgramError (); |
| 491 |
|
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
} |
| 497 |
|
| 498 |
/*****************************************************************************/ |
| 499 |
|
| 500 |
static void WrapLeft (dng_pixel_buffer &buffer, |
| 501 |
const dng_image &image, |
| 502 |
const dng_rect &dstArea, |
| 503 |
const int32 srcTop, |
| 504 |
const int32 srcBottom) |
| 505 |
{ |
| 506 |
|
| 507 |
const dng_rect imageBounds = image.Bounds (); |
| 508 |
|
| 509 |
int32 remainingPixels = (int32) dstArea.W (); |
| 510 |
|
| 511 |
int32 maxPixelsPerIter = (int32) imageBounds.W (); |
| 512 |
|
| 513 |
int32 dstLeft = Min_int32 (imageBounds.l, dstArea.r); |
| 514 |
|
| 515 |
DNG_REQUIRE (imageBounds.W () > 0, "WrapLeft: imageBounds.W"); |
| 516 |
|
| 517 |
int32 dstLeftPhase = (imageBounds.l - dstLeft) % imageBounds.W (); |
| 518 |
|
| 519 |
while (remainingPixels > 0) |
| 520 |
{ |
| 521 |
|
| 522 |
int32 srcRight = imageBounds.r - dstLeftPhase; |
| 523 |
|
| 524 |
int32 pixelCount = Min_int32 (remainingPixels, |
| 525 |
maxPixelsPerIter - dstLeftPhase); |
| 526 |
|
| 527 |
dstLeft -= pixelCount; |
| 528 |
|
| 529 |
dng_rect srcArea (srcTop, |
| 530 |
srcRight - pixelCount, |
| 531 |
srcBottom, |
| 532 |
srcRight); |
| 533 |
|
| 534 |
auto tmpBuffer = buffer; |
| 535 |
|
| 536 |
void *ptr = tmpBuffer.DirtyPixel (dstArea.t, |
| 537 |
dstLeft); |
| 538 |
|
| 539 |
tmpBuffer.fArea = srcArea; |
| 540 |
|
| 541 |
tmpBuffer.fData = ptr; |
| 542 |
|
| 543 |
image.Get (tmpBuffer, |
| 544 |
dng_image::edge_none); |
| 545 |
|
| 546 |
remainingPixels -= pixelCount; |
| 547 |
|
| 548 |
dstLeftPhase = 0; // Only valid/needed if remainingPixels > 0. |
| 549 |
|
| 550 |
} |
| 551 |
|
| 552 |
} |
| 553 |
|
| 554 |
/*****************************************************************************/ |
| 555 |
|
| 556 |
static void WrapRight (dng_pixel_buffer &buffer, |
| 557 |
const dng_image &image, |
| 558 |
const dng_rect &dstArea, |
| 559 |
const int32 srcTop, |
| 560 |
const int32 srcBottom) |
| 561 |
{ |
| 562 |
|
| 563 |
const dng_rect imageBounds = image.Bounds (); |
| 564 |
|
| 565 |
int32 remainingPixels = (int32) dstArea.W (); |
| 566 |
|
| 567 |
int32 maxPixelsPerIter = (int32) imageBounds.W (); |
| 568 |
|
| 569 |
int32 dstLeft = Max_int32 (imageBounds.r, dstArea.l); |
| 570 |
|
| 571 |
DNG_REQUIRE (imageBounds.W () > 0, "WrapRight: imageBounds.W"); |
| 572 |
|
| 573 |
int32 dstLeftPhase = (dstLeft - imageBounds.r) % imageBounds.W (); |
| 574 |
|
| 575 |
while (remainingPixels > 0) |
| 576 |
{ |
| 577 |
|
| 578 |
int32 srcLeft = imageBounds.l + dstLeftPhase; |
| 579 |
|
| 580 |
int32 pixelCount = Min_int32 (remainingPixels, |
| 581 |
maxPixelsPerIter - dstLeftPhase); |
| 582 |
|
| 583 |
dng_rect srcArea (srcTop, |
| 584 |
srcLeft, |
| 585 |
srcBottom, |
| 586 |
srcLeft + pixelCount); |
| 587 |
|
| 588 |
auto tmpBuffer = buffer; |
| 589 |
|
| 590 |
void *ptr = tmpBuffer.DirtyPixel (dstArea.t, |
| 591 |
dstLeft); |
| 592 |
|
| 593 |
tmpBuffer.fArea = srcArea; |
| 594 |
|
| 595 |
tmpBuffer.fData = ptr; |
| 596 |
|
| 597 |
image.Get (tmpBuffer, |
| 598 |
dng_image::edge_none); |
| 599 |
|
| 600 |
dstLeft += pixelCount; |
| 601 |
|
| 602 |
remainingPixels -= pixelCount; |
| 603 |
|
| 604 |
dstLeftPhase = 0; // Only valid/needed if remainingPixels > 0. |
| 605 |
|
| 606 |
} |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
/*****************************************************************************/ |
| 611 |
|
| 612 |
static void WrapCornerHorizontal (dng_pixel_buffer &buffer, |
| 613 |
const dng_image &image, |
| 614 |
const dng_rect &dstArea, |
| 615 |
uint32 repeatV, |
| 616 |
bool isTop, |
| 617 |
bool isLeft) |
| 618 |
{ |
| 619 |
|
| 620 |
if (dstArea.IsEmpty ()) |
| 621 |
{ |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
const dng_rect imageBounds = image.Bounds (); |
| 626 |
|
| 627 |
uint32 repeatSize = repeatV; |
| 628 |
|
| 629 |
repeatSize = Max_uint32 (repeatSize, 1); |
| 630 |
|
| 631 |
repeatSize = Min_uint32 (repeatSize, imageBounds.H ()); |
| 632 |
|
| 633 |
repeatSize = Min_uint32 (repeatSize, dstArea.H ()); |
| 634 |
|
| 635 |
auto tmpArea = dstArea; |
| 636 |
|
| 637 |
const int32 iRepeatSize = (int32) repeatSize; |
| 638 |
|
| 639 |
if (isTop) |
| 640 |
{ |
| 641 |
tmpArea.t = tmpArea.b - iRepeatSize; |
| 642 |
} |
| 643 |
|
| 644 |
else |
| 645 |
{ |
| 646 |
tmpArea.b = tmpArea.t + iRepeatSize; |
| 647 |
} |
| 648 |
|
| 649 |
if (isLeft) |
| 650 |
{ |
| 651 |
|
| 652 |
WrapLeft (buffer, |
| 653 |
image, |
| 654 |
tmpArea, |
| 655 |
isTop ? (imageBounds.t) : (imageBounds.b - iRepeatSize), |
| 656 |
isTop ? (imageBounds.t + iRepeatSize) : (imageBounds.b)); |
| 657 |
|
| 658 |
} |
| 659 |
|
| 660 |
else |
| 661 |
{ |
| 662 |
|
| 663 |
WrapRight (buffer, |
| 664 |
image, |
| 665 |
tmpArea, |
| 666 |
isTop ? (imageBounds.t) : (imageBounds.b - iRepeatSize), |
| 667 |
isTop ? (imageBounds.t + iRepeatSize) : (imageBounds.b)); |
| 668 |
|
| 669 |
} |
| 670 |
|
| 671 |
buffer.RepeatArea (tmpArea, |
| 672 |
dstArea); |
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
/*****************************************************************************/ |
| 677 |
|
| 678 |
void dng_image::Get (dng_pixel_buffer &buffer, |
| 679 |
edge_option edgeOption, |
| 680 |
uint32 repeatV, |
| 681 |
uint32 repeatH) const |
| 682 |
{ |
| 683 |
|
| 684 |
// Find the overlap with the image bounds. |
| 685 |
|
| 686 |
dng_rect overlap = buffer.fArea & fBounds; |
| 687 |
|
| 688 |
// Move the overlapping pixels. |
| 689 |
|
| 690 |
if (overlap.NotEmpty ()) |
| 691 |
{ |
| 692 |
|
| 693 |
dng_pixel_buffer temp (buffer); |
| 694 |
|
| 695 |
temp.fArea = overlap; |
| 696 |
|
| 697 |
temp.fData = buffer.DirtyPixel (overlap.t, |
| 698 |
overlap.l, |
| 699 |
buffer.fPlane); |
| 700 |
|
| 701 |
DoGet (temp); |
| 702 |
|
| 703 |
} |
| 704 |
|
| 705 |
// Throw if unsupported edge wrap options are found. |
| 706 |
|
| 707 |
if (edgeOption == edge_wrap_vertical || |
| 708 |
edgeOption == edge_wrap_all) |
| 709 |
{ |
| 710 |
|
| 711 |
ThrowNotYetImplemented ("Unsupported edge option"); |
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
// See if we need to pad the edge values. |
| 716 |
|
| 717 |
if ((edgeOption != edge_none) && (overlap != buffer.fArea)) |
| 718 |
{ |
| 719 |
|
| 720 |
dng_rect areaT (buffer.fArea); |
| 721 |
dng_rect areaL (buffer.fArea); |
| 722 |
dng_rect areaB (buffer.fArea); |
| 723 |
dng_rect areaR (buffer.fArea); |
| 724 |
|
| 725 |
areaT.b = Min_int32 (areaT.b, fBounds.t); |
| 726 |
areaL.r = Min_int32 (areaL.r, fBounds.l); |
| 727 |
areaB.t = Max_int32 (areaB.t, fBounds.b); |
| 728 |
areaR.l = Max_int32 (areaR.l, fBounds.r); |
| 729 |
|
| 730 |
dng_rect areaH (buffer.fArea); |
| 731 |
dng_rect areaV (buffer.fArea); |
| 732 |
|
| 733 |
areaH.l = Max_int32 (areaH.l, fBounds.l); |
| 734 |
areaH.r = Min_int32 (areaH.r, fBounds.r); |
| 735 |
|
| 736 |
areaV.t = Max_int32 (areaV.t, fBounds.t); |
| 737 |
areaV.b = Min_int32 (areaV.b, fBounds.b); |
| 738 |
|
| 739 |
// Top left. |
| 740 |
|
| 741 |
dng_rect areaTL = areaT & areaL; |
| 742 |
|
| 743 |
if (areaTL.NotEmpty ()) |
| 744 |
{ |
| 745 |
|
| 746 |
if (edgeOption == edge_wrap_horizontal) |
| 747 |
{ |
| 748 |
|
| 749 |
WrapCornerHorizontal (buffer, |
| 750 |
*this, |
| 751 |
areaTL, |
| 752 |
repeatV, |
| 753 |
true, // is top? |
| 754 |
true); // is left? |
| 755 |
|
| 756 |
} |
| 757 |
|
| 758 |
else |
| 759 |
{ |
| 760 |
|
| 761 |
GetEdge (buffer, |
| 762 |
edgeOption, |
| 763 |
dng_rect (fBounds.t, |
| 764 |
fBounds.l, |
| 765 |
fBounds.t + (int32) repeatV, |
| 766 |
fBounds.l + (int32) repeatH), |
| 767 |
areaTL); |
| 768 |
|
| 769 |
} |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
// Top middle. |
| 774 |
|
| 775 |
dng_rect areaTM = areaT & areaH; |
| 776 |
|
| 777 |
if (areaTM.NotEmpty ()) |
| 778 |
{ |
| 779 |
|
| 780 |
edge_option tmpEdgeOption = edgeOption; |
| 781 |
|
| 782 |
if (edgeOption == edge_wrap_horizontal) |
| 783 |
{ |
| 784 |
tmpEdgeOption = edge_repeat; |
| 785 |
} |
| 786 |
|
| 787 |
GetEdge (buffer, |
| 788 |
tmpEdgeOption, |
| 789 |
dng_rect (fBounds.t, |
| 790 |
areaTM.l, |
| 791 |
fBounds.t + (int32) repeatV, |
| 792 |
areaTM.r), |
| 793 |
areaTM); |
| 794 |
|
| 795 |
} |
| 796 |
|
| 797 |
// Top right. |
| 798 |
|
| 799 |
dng_rect areaTR = areaT & areaR; |
| 800 |
|
| 801 |
if (areaTR.NotEmpty ()) |
| 802 |
{ |
| 803 |
|
| 804 |
if (edgeOption == edge_wrap_horizontal) |
| 805 |
{ |
| 806 |
|
| 807 |
WrapCornerHorizontal (buffer, |
| 808 |
*this, |
| 809 |
areaTR, |
| 810 |
repeatV, |
| 811 |
true, // is top? |
| 812 |
false); // is left? |
| 813 |
|
| 814 |
} |
| 815 |
|
| 816 |
else |
| 817 |
{ |
| 818 |
|
| 819 |
GetEdge (buffer, |
| 820 |
edgeOption, |
| 821 |
dng_rect (fBounds.t, |
| 822 |
fBounds.r - (int32) repeatH, |
| 823 |
fBounds.t + (int32) repeatV, |
| 824 |
fBounds.r), |
| 825 |
areaTR); |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
} |
| 830 |
|
| 831 |
// Left middle. |
| 832 |
|
| 833 |
dng_rect areaLM = areaL & areaV; |
| 834 |
|
| 835 |
if (areaLM.NotEmpty ()) |
| 836 |
{ |
| 837 |
|
| 838 |
if (edgeOption == edge_wrap_horizontal) |
| 839 |
{ |
| 840 |
|
| 841 |
WrapLeft (buffer, |
| 842 |
*this, |
| 843 |
areaLM, |
| 844 |
areaLM.t, |
| 845 |
areaLM.b); |
| 846 |
|
| 847 |
} |
| 848 |
|
| 849 |
else |
| 850 |
{ |
| 851 |
|
| 852 |
GetEdge (buffer, |
| 853 |
edgeOption, |
| 854 |
dng_rect (areaLM.t, |
| 855 |
fBounds.l, |
| 856 |
areaLM.b, |
| 857 |
fBounds.l + (int32) repeatH), |
| 858 |
areaLM); |
| 859 |
|
| 860 |
} |
| 861 |
|
| 862 |
} |
| 863 |
|
| 864 |
// Right middle. |
| 865 |
|
| 866 |
dng_rect areaRM = areaR & areaV; |
| 867 |
|
| 868 |
if (areaRM.NotEmpty ()) |
| 869 |
{ |
| 870 |
|
| 871 |
if (edgeOption == edge_wrap_horizontal) |
| 872 |
{ |
| 873 |
|
| 874 |
WrapRight (buffer, |
| 875 |
*this, |
| 876 |
areaRM, |
| 877 |
areaRM.t, |
| 878 |
areaRM.b); |
| 879 |
|
| 880 |
} |
| 881 |
|
| 882 |
else |
| 883 |
{ |
| 884 |
|
| 885 |
GetEdge (buffer, |
| 886 |
edgeOption, |
| 887 |
dng_rect (areaRM.t, |
| 888 |
fBounds.r - (int32) repeatH, |
| 889 |
areaRM.b, |
| 890 |
fBounds.r), |
| 891 |
areaRM); |
| 892 |
|
| 893 |
} |
| 894 |
|
| 895 |
} |
| 896 |
|
| 897 |
// Bottom left. |
| 898 |
|
| 899 |
dng_rect areaBL = areaB & areaL; |
| 900 |
|
| 901 |
if (areaBL.NotEmpty ()) |
| 902 |
{ |
| 903 |
|
| 904 |
if (edgeOption == edge_wrap_horizontal) |
| 905 |
{ |
| 906 |
|
| 907 |
WrapCornerHorizontal (buffer, |
| 908 |
*this, |
| 909 |
areaBL, |
| 910 |
repeatV, |
| 911 |
false, // is top? |
| 912 |
true); // is left? |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
else |
| 917 |
{ |
| 918 |
|
| 919 |
GetEdge (buffer, |
| 920 |
edgeOption, |
| 921 |
dng_rect (fBounds.b - (int32) repeatV, |
| 922 |
fBounds.l, |
| 923 |
fBounds.b, |
| 924 |
fBounds.l + (int32) repeatH), |
| 925 |
areaBL); |
| 926 |
|
| 927 |
} |
| 928 |
|
| 929 |
} |
| 930 |
|
| 931 |
// Bottom middle. |
| 932 |
|
| 933 |
dng_rect areaBM = areaB & areaH; |
| 934 |
|
| 935 |
if (areaBM.NotEmpty ()) |
| 936 |
{ |
| 937 |
|
| 938 |
edge_option tmpEdgeOption = edgeOption; |
| 939 |
|
| 940 |
if (edgeOption == edge_wrap_horizontal) |
| 941 |
{ |
| 942 |
tmpEdgeOption = edge_repeat; |
| 943 |
} |
| 944 |
|
| 945 |
GetEdge (buffer, |
| 946 |
tmpEdgeOption, |
| 947 |
dng_rect (fBounds.b - (int32) repeatV, |
| 948 |
areaBM.l, |
| 949 |
fBounds.b, |
| 950 |
areaBM.r), |
| 951 |
areaBM); |
| 952 |
|
| 953 |
} |
| 954 |
|
| 955 |
// Bottom right. |
| 956 |
|
| 957 |
dng_rect areaBR = areaB & areaR; |
| 958 |
|
| 959 |
if (areaBR.NotEmpty ()) |
| 960 |
{ |
| 961 |
|
| 962 |
if (edgeOption == edge_wrap_horizontal) |
| 963 |
{ |
| 964 |
|
| 965 |
WrapCornerHorizontal (buffer, |
| 966 |
*this, |
| 967 |
areaBR, |
| 968 |
repeatV, |
| 969 |
false, // is top? |
| 970 |
false); // is left? |
| 971 |
|
| 972 |
} |
| 973 |
|
| 974 |
else |
| 975 |
{ |
| 976 |
|
| 977 |
GetEdge (buffer, |
| 978 |
edgeOption, |
| 979 |
dng_rect (fBounds.b - (int32) repeatV, |
| 980 |
fBounds.r - (int32) repeatH, |
| 981 |
fBounds.b, |
| 982 |
fBounds.r), |
| 983 |
areaBR); |
| 984 |
|
| 985 |
} |
| 986 |
|
| 987 |
} |
| 988 |
|
| 989 |
} |
| 990 |
|
| 991 |
} |
| 992 |
|
| 993 |
/*****************************************************************************/ |
| 994 |
|
| 995 |
void dng_image::Put (const dng_pixel_buffer &buffer) |
| 996 |
{ |
| 997 |
|
| 998 |
// Move the overlapping pixels. |
| 999 |
|
| 1000 |
dng_rect overlap = buffer.fArea & fBounds; |
| 1001 |
|
| 1002 |
if (overlap.NotEmpty ()) |
| 1003 |
{ |
| 1004 |
|
| 1005 |
dng_pixel_buffer temp (buffer); |
| 1006 |
|
| 1007 |
temp.fArea = overlap; |
| 1008 |
|
| 1009 |
temp.fData = (void *) buffer.ConstPixel (overlap.t, |
| 1010 |
overlap.l, |
| 1011 |
buffer.fPlane); |
| 1012 |
|
| 1013 |
// Move the overlapping planes. |
| 1014 |
|
| 1015 |
if (temp.fPlane < Planes ()) |
| 1016 |
{ |
| 1017 |
|
| 1018 |
temp.fPlanes = Min_uint32 (temp.fPlanes, |
| 1019 |
Planes () - temp.fPlane); |
| 1020 |
|
| 1021 |
DoPut (temp); |
| 1022 |
|
| 1023 |
} |
| 1024 |
|
| 1025 |
} |
| 1026 |
|
| 1027 |
} |
| 1028 |
|
| 1029 |
/*****************************************************************************/ |
| 1030 |
|
| 1031 |
void dng_image::Trim (const dng_rect &r) |
| 1032 |
{ |
| 1033 |
|
| 1034 |
if (r != Bounds ()) |
| 1035 |
{ |
| 1036 |
|
| 1037 |
ThrowProgramError ("Trim is not support by this dng_image subclass"); |
| 1038 |
|
| 1039 |
} |
| 1040 |
|
| 1041 |
} |
| 1042 |
|
| 1043 |
/*****************************************************************************/ |
| 1044 |
|
| 1045 |
void dng_image::Rotate (const dng_orientation &orientation) |
| 1046 |
{ |
| 1047 |
|
| 1048 |
if (orientation != dng_orientation::Normal ()) |
| 1049 |
{ |
| 1050 |
|
| 1051 |
ThrowProgramError ("Rotate is not support by this dng_image subclass"); |
| 1052 |
|
| 1053 |
} |
| 1054 |
|
| 1055 |
} |
| 1056 |
|
| 1057 |
/*****************************************************************************/ |
| 1058 |
|
| 1059 |
void dng_image::Offset (const dng_point &offset) |
| 1060 |
{ |
| 1061 |
|
| 1062 |
if (offset != dng_point (0, 0)) |
| 1063 |
{ |
| 1064 |
|
| 1065 |
ThrowProgramError ("Offset is not support by this dng_image subclass"); |
| 1066 |
|
| 1067 |
} |
| 1068 |
|
| 1069 |
} |
| 1070 |
|
| 1071 |
/*****************************************************************************/ |
| 1072 |
|
| 1073 |
void dng_image::DoCopyArea (const dng_image &src, |
| 1074 |
const dng_rect &area, |
| 1075 |
uint32 srcPlane, |
| 1076 |
uint32 dstPlane, |
| 1077 |
uint32 planes) |
| 1078 |
{ |
| 1079 |
|
| 1080 |
if (&src == this) |
| 1081 |
return; |
| 1082 |
|
| 1083 |
dng_tile_iterator destIter(*this, area); |
| 1084 |
dng_rect destTileArea; |
| 1085 |
|
| 1086 |
while (destIter.GetOneTile(destTileArea)) |
| 1087 |
{ |
| 1088 |
dng_tile_iterator srcIter(src, destTileArea); |
| 1089 |
dng_rect srcTileArea; |
| 1090 |
|
| 1091 |
while (srcIter.GetOneTile(srcTileArea)) |
| 1092 |
{ |
| 1093 |
|
| 1094 |
dng_dirty_tile_buffer destTile(*this, srcTileArea); |
| 1095 |
dng_const_tile_buffer srcTile(src, srcTileArea); |
| 1096 |
|
| 1097 |
destTile.CopyArea (srcTile, srcTileArea, srcPlane, dstPlane, planes); |
| 1098 |
|
| 1099 |
} |
| 1100 |
|
| 1101 |
} |
| 1102 |
|
| 1103 |
} |
| 1104 |
|
| 1105 |
/*****************************************************************************/ |
| 1106 |
|
| 1107 |
bool dng_image::EqualArea (const dng_image &src, |
| 1108 |
const dng_rect &area, |
| 1109 |
uint32 plane, |
| 1110 |
uint32 planes) const |
| 1111 |
{ |
| 1112 |
|
| 1113 |
if (&src == this) |
| 1114 |
return true; |
| 1115 |
|
| 1116 |
dng_tile_iterator destIter (*this, area); |
| 1117 |
|
| 1118 |
dng_rect destTileArea; |
| 1119 |
|
| 1120 |
while (destIter.GetOneTile (destTileArea)) |
| 1121 |
{ |
| 1122 |
|
| 1123 |
dng_tile_iterator srcIter (src, destTileArea); |
| 1124 |
|
| 1125 |
dng_rect srcTileArea; |
| 1126 |
|
| 1127 |
while (srcIter.GetOneTile (srcTileArea)) |
| 1128 |
{ |
| 1129 |
|
| 1130 |
dng_const_tile_buffer destTile (*this, srcTileArea); |
| 1131 |
dng_const_tile_buffer srcTile (src , srcTileArea); |
| 1132 |
|
| 1133 |
if (!destTile.EqualArea (srcTile, srcTileArea, plane, planes)) |
| 1134 |
{ |
| 1135 |
return false; |
| 1136 |
} |
| 1137 |
|
| 1138 |
} |
| 1139 |
|
| 1140 |
} |
| 1141 |
|
| 1142 |
return true; |
| 1143 |
|
| 1144 |
} |
| 1145 |
|
| 1146 |
/*****************************************************************************/ |
| 1147 |
|
| 1148 |
void dng_image::SetConstant (uint32 value, |
| 1149 |
const dng_rect &area) |
| 1150 |
{ |
| 1151 |
|
| 1152 |
dng_tile_iterator iter (*this, area); |
| 1153 |
|
| 1154 |
dng_rect tileArea; |
| 1155 |
|
| 1156 |
while (iter.GetOneTile (tileArea)) |
| 1157 |
{ |
| 1158 |
|
| 1159 |
dng_dirty_tile_buffer buffer (*this, tileArea); |
| 1160 |
|
| 1161 |
buffer.SetConstant (tileArea, |
| 1162 |
0, |
| 1163 |
fPlanes, |
| 1164 |
value); |
| 1165 |
|
| 1166 |
} |
| 1167 |
|
| 1168 |
} |
| 1169 |
|
| 1170 |
/*****************************************************************************/ |