1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
|
/******************************************************************************
* $Id: rasterfill.cpp 26341 2013-08-19 19:23:33Z rouault $
*
* Project: GDAL
* Purpose: Interpolate in nodata areas.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2008, Frank Warmerdam
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "gdal_alg.h"
#include "cpl_conv.h"
#include "cpl_string.h"
CPL_CVSID("$Id: rasterfill.cpp 26341 2013-08-19 19:23:33Z rouault $");
/************************************************************************/
/* GDALFilterLine() */
/* */
/* Apply 3x3 filtering one one scanline with masking for which */
/* pixels are to be interpolated (ThisFMask) and which window */
/* pixels are valid to include in the interpolation (TMask). */
/************************************************************************/
static void
GDALFilterLine( float *pafLastLine, float *pafThisLine, float *pafNextLine,
float *pafOutLine,
GByte *pabyLastTMask, GByte *pabyThisTMask, GByte*pabyNextTMask,
GByte *pabyThisFMask, int nXSize )
{
int iX;
for( iX = 0; iX < nXSize; iX++ )
{
if( !pabyThisFMask[iX] )
{
pafOutLine[iX] = pafThisLine[iX];
continue;
}
CPLAssert( pabyThisTMask[iX] );
double dfValSum = 0.0;
double dfWeightSum = 0.0;
// Previous line
if( pafLastLine != NULL )
{
if( iX > 0 && pabyLastTMask[iX-1] )
{
dfValSum += pafLastLine[iX-1];
dfWeightSum += 1.0;
}
if( pabyLastTMask[iX] )
{
dfValSum += pafLastLine[iX];
dfWeightSum += 1.0;
}
if( iX < nXSize-1 && pabyLastTMask[iX+1] )
{
dfValSum += pafLastLine[iX+1];
dfWeightSum += 1.0;
}
}
// Current Line
if( iX > 0 && pabyThisTMask[iX-1] )
{
dfValSum += pafThisLine[iX-1];
dfWeightSum += 1.0;
}
if( pabyThisTMask[iX] )
{
dfValSum += pafThisLine[iX];
dfWeightSum += 1.0;
}
if( iX < nXSize-1 && pabyThisTMask[iX+1] )
{
dfValSum += pafThisLine[iX+1];
dfWeightSum += 1.0;
}
// Next line
if( pafNextLine != NULL )
{
if( iX > 0 && pabyNextTMask[iX-1] )
{
dfValSum += pafNextLine[iX-1];
dfWeightSum += 1.0;
}
if( pabyNextTMask[iX] )
{
dfValSum += pafNextLine[iX];
dfWeightSum += 1.0;
}
if( iX < nXSize-1 && pabyNextTMask[iX+1] )
{
dfValSum += pafNextLine[iX+1];
dfWeightSum += 1.0;
}
}
pafOutLine[iX] = (float) (dfValSum / dfWeightSum);
}
}
/************************************************************************/
/* GDALMultiFilter() */
/* */
/* Apply multiple iterations of a 3x3 smoothing filter over a */
/* band with masking controlling what pixels should be */
/* filtered (FiltMaskBand non zero) and which pixels can be */
/* considered valid contributors to the filter */
/* (TargetMaskBand non zero). */
/* */
/* This implementation attempts to apply many iterations in */
/* one IO pass by managing the filtering over a rolling buffer */
/* of nIternations+2 scanlines. While possibly clever this */
/* makes the algorithm implementation largely */
/* incomprehensible. */
/************************************************************************/
static CPLErr
GDALMultiFilter( GDALRasterBandH hTargetBand,
GDALRasterBandH hTargetMaskBand,
GDALRasterBandH hFiltMaskBand,
int nIterations,
GDALProgressFunc pfnProgress,
void * pProgressArg )
{
float *paf3PassLineBuf;
GByte *pabyTMaskBuf;
GByte *pabyFMaskBuf;
float *pafThisPass, *pafLastPass, *pafSLastPass;
int nBufLines = nIterations + 2;
int iPassCounter = 0;
int nNewLine; // the line being loaded this time (zero based scanline)
int nXSize = GDALGetRasterBandXSize( hTargetBand );
int nYSize = GDALGetRasterBandYSize( hTargetBand );
CPLErr eErr = CE_None;
/* -------------------------------------------------------------------- */
/* Report starting progress value. */
/* -------------------------------------------------------------------- */
if( !pfnProgress( 0.0, "Smoothing Filter...", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
return CE_Failure;
}
/* -------------------------------------------------------------------- */
/* Allocate rotating buffers. */
/* -------------------------------------------------------------------- */
pabyTMaskBuf = (GByte *) VSIMalloc2(nXSize, nBufLines);
pabyFMaskBuf = (GByte *) VSIMalloc2(nXSize, nBufLines);
paf3PassLineBuf = (float *) VSIMalloc3(nXSize, nBufLines, 3 * sizeof(float));
if (pabyTMaskBuf == NULL || pabyFMaskBuf == NULL || paf3PassLineBuf == NULL)
{
CPLError(CE_Failure, CPLE_OutOfMemory,
"Could not allocate enough memory for temporary buffers");
eErr = CE_Failure;
goto end;
}
/* -------------------------------------------------------------------- */
/* Process rotating buffers. */
/* -------------------------------------------------------------------- */
for( nNewLine = 0;
eErr == CE_None && nNewLine < nYSize+nIterations;
nNewLine++ )
{
/* -------------------------------------------------------------------- */
/* Rotate pass buffers. */
/* -------------------------------------------------------------------- */
iPassCounter = (iPassCounter + 1) % 3;
pafSLastPass = paf3PassLineBuf
+ ((iPassCounter+0)%3) * nXSize*nBufLines;
pafLastPass = paf3PassLineBuf
+ ((iPassCounter+1)%3) * nXSize*nBufLines;
pafThisPass = paf3PassLineBuf
+ ((iPassCounter+2)%3) * nXSize*nBufLines;
/* -------------------------------------------------------------------- */
/* Where does the new line go in the rotating buffer? */
/* -------------------------------------------------------------------- */
int iBufOffset = nNewLine % nBufLines;
/* -------------------------------------------------------------------- */
/* Read the new data line if it is't off the bottom of the */
/* image. */
/* -------------------------------------------------------------------- */
if( nNewLine < nYSize )
{
eErr =
GDALRasterIO( hTargetMaskBand, GF_Read,
0, nNewLine, nXSize, 1,
pabyTMaskBuf + nXSize * iBufOffset, nXSize, 1,
GDT_Byte, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hFiltMaskBand, GF_Read,
0, nNewLine, nXSize, 1,
pabyFMaskBuf + nXSize * iBufOffset, nXSize, 1,
GDT_Byte, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hTargetBand, GF_Read,
0, nNewLine, nXSize, 1,
pafThisPass + nXSize * iBufOffset, nXSize, 1,
GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
}
/* -------------------------------------------------------------------- */
/* Loop over the loaded data, applying the filter to all loaded */
/* lines with neighbours. */
/* -------------------------------------------------------------------- */
int iFLine;
for( iFLine = nNewLine-1;
eErr == CE_None && iFLine >= nNewLine-nIterations;
iFLine-- )
{
int iLastOffset, iThisOffset, iNextOffset;
iLastOffset = (iFLine-1) % nBufLines;
iThisOffset = (iFLine ) % nBufLines;
iNextOffset = (iFLine+1) % nBufLines;
// default to preserving the old value.
if( iFLine >= 0 )
memcpy( pafThisPass + iThisOffset * nXSize,
pafLastPass + iThisOffset * nXSize,
sizeof(float) * nXSize );
// currently this skips the first and last line. Eventually
// we will enable these too. TODO
if( iFLine < 1 || iFLine >= nYSize-1 )
{
continue;
}
GDALFilterLine(
pafSLastPass + iLastOffset * nXSize,
pafLastPass + iThisOffset * nXSize,
pafThisPass + iNextOffset * nXSize,
pafThisPass + iThisOffset * nXSize,
pabyTMaskBuf + iLastOffset * nXSize,
pabyTMaskBuf + iThisOffset * nXSize,
pabyTMaskBuf + iNextOffset * nXSize,
pabyFMaskBuf + iThisOffset * nXSize,
nXSize );
}
/* -------------------------------------------------------------------- */
/* Write out the top data line that will be rolling out of our */
/* buffer. */
/* -------------------------------------------------------------------- */
int iLineToSave = nNewLine - nIterations;
if( iLineToSave >= 0 && eErr == CE_None )
{
iBufOffset = iLineToSave % nBufLines;
eErr =
GDALRasterIO( hTargetBand, GF_Write,
0, iLineToSave, nXSize, 1,
pafThisPass + nXSize * iBufOffset, nXSize, 1,
GDT_Float32, 0, 0 );
}
/* -------------------------------------------------------------------- */
/* Report progress. */
/* -------------------------------------------------------------------- */
if( eErr == CE_None
&& !pfnProgress( (nNewLine+1) / (double) (nYSize+nIterations),
"Smoothing Filter...", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
eErr = CE_Failure;
}
}
/* -------------------------------------------------------------------- */
/* Cleanup */
/* -------------------------------------------------------------------- */
end:
CPLFree( pabyTMaskBuf );
CPLFree( pabyFMaskBuf );
CPLFree( paf3PassLineBuf );
return eErr;
}
/************************************************************************/
/* QUAD_CHECK() */
/* */
/* macro for checking whether a point is nearer than the */
/* existing closest point. */
/************************************************************************/
#define QUAD_CHECK(quad_dist, quad_value, \
target_x, target_y, origin_x, origin_y, target_value ) \
\
if( quad_value != nNoDataVal ) \
{ \
double dfDx = (double)target_x - (double)origin_x; \
double dfDy = (double)target_y - (double)origin_y; \
double dfDistSq = dfDx * dfDx + dfDy * dfDy; \
\
if( dfDistSq < quad_dist*quad_dist ) \
{ \
CPLAssert( dfDistSq > 0.0 ); \
quad_dist = sqrt(dfDistSq); \
quad_value = target_value; \
} \
}
/************************************************************************/
/* GDALFillNodata() */
/************************************************************************/
/**
* Fill selected raster regions by interpolation from the edges.
*
* This algorithm will interpolate values for all designated
* nodata pixels (marked by zeros in hMaskBand). For each pixel
* a four direction conic search is done to find values to interpolate
* from (using inverse distance weighting). Once all values are
* interpolated, zero or more smoothing iterations (3x3 average
* filters on interpolated pixels) are applied to smooth out
* artifacts.
*
* This algorithm is generally suitable for interpolating missing
* regions of fairly continuously varying rasters (such as elevation
* models for instance). It is also suitable for filling small holes
* and cracks in more irregularly varying images (like airphotos). It
* is generally not so great for interpolating a raster from sparse
* point data - see the algorithms defined in gdal_grid.h for that case.
*
* @param hTargetBand the raster band to be modified in place.
* @param hMaskBand a mask band indicating pixels to be interpolated (zero valued
* @param dfMaxSearchDist the maximum number of pixels to search in all
* directions to find values to interpolate from.
* @param bDeprecatedOption unused argument, should be zero.
* @param nSmoothingIterations the number of 3x3 smoothing filter passes to
* run (0 or more).
* @param papszOptions additional name=value options in a string list (none
* supported at this time - just pass NULL).
* @param pfnProgress the progress function to report completion.
* @param pProgressArg callback data for progress function.
*
* @return CE_None on success or CE_Failure if something goes wrong.
*/
CPLErr CPL_STDCALL
GDALFillNodata( GDALRasterBandH hTargetBand,
GDALRasterBandH hMaskBand,
double dfMaxSearchDist,
int bDeprecatedOption,
int nSmoothingIterations,
char **papszOptions,
GDALProgressFunc pfnProgress,
void * pProgressArg )
{
VALIDATE_POINTER1( hTargetBand, "GDALFillNodata", CE_Failure );
int nXSize = GDALGetRasterBandXSize( hTargetBand );
int nYSize = GDALGetRasterBandYSize( hTargetBand );
CPLErr eErr = CE_None;
// Special "x" pixel values identifying pixels as special.
GUInt32 nNoDataVal;
GDALDataType eType;
if( dfMaxSearchDist == 0.0 )
dfMaxSearchDist = MAX(nXSize,nYSize) + 1;
int nMaxSearchDist = (int) floor(dfMaxSearchDist);
if( nXSize > 65533 || nYSize > 65533 )
{
eType = GDT_UInt32;
nNoDataVal = 4000002;
}
else
{
eType = GDT_UInt16;
nNoDataVal = 65535;
}
if( hMaskBand == NULL )
hMaskBand = GDALGetMaskBand( hTargetBand );
/* If there are smoothing iterations, reserve 10% of the progress for them */
double dfProgressRatio = (nSmoothingIterations > 0) ? 0.9 : 1.0;
/* -------------------------------------------------------------------- */
/* Initialize progress counter. */
/* -------------------------------------------------------------------- */
if( pfnProgress == NULL )
pfnProgress = GDALDummyProgress;
if( !pfnProgress( 0.0, "Filling...", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
return CE_Failure;
}
/* -------------------------------------------------------------------- */
/* Create a work file to hold the Y "last value" indices. */
/* -------------------------------------------------------------------- */
GDALDriverH hDriver = GDALGetDriverByName( "GTiff" );
if (hDriver == NULL)
{
CPLError(CE_Failure, CPLE_AppDefined,
"GDALFillNodata needs GTiff driver");
return CE_Failure;
}
GDALDatasetH hYDS;
GDALRasterBandH hYBand;
static const char *apszOptions[] = { "COMPRESS=LZW", "BIGTIFF=IF_SAFER",
NULL };
CPLString osTmpFile = CPLGenerateTempFilename("");
CPLString osYTmpFile = osTmpFile + "fill_y_work.tif";
hYDS = GDALCreate( hDriver, osYTmpFile, nXSize, nYSize, 1,
eType, (char **) apszOptions );
if( hYDS == NULL )
return CE_Failure;
hYBand = GDALGetRasterBand( hYDS, 1 );
/* -------------------------------------------------------------------- */
/* Create a work file to hold the pixel value associated with */
/* the "last xy value" pixel. */
/* -------------------------------------------------------------------- */
GDALDatasetH hValDS;
GDALRasterBandH hValBand;
CPLString osValTmpFile = osTmpFile + "fill_val_work.tif";
hValDS = GDALCreate( hDriver, osValTmpFile, nXSize, nYSize, 1,
GDALGetRasterDataType( hTargetBand ),
(char **) apszOptions );
if( hValDS == NULL )
return CE_Failure;
hValBand = GDALGetRasterBand( hValDS, 1 );
/* -------------------------------------------------------------------- */
/* Create a mask file to make it clear what pixels can be filtered */
/* on the filtering pass. */
/* -------------------------------------------------------------------- */
GDALDatasetH hFiltMaskDS;
GDALRasterBandH hFiltMaskBand;
CPLString osFiltMaskTmpFile = osTmpFile + "fill_filtmask_work.tif";
hFiltMaskDS =
GDALCreate( hDriver, osFiltMaskTmpFile, nXSize, nYSize, 1,
GDT_Byte, (char **) apszOptions );
if( hFiltMaskDS == NULL )
return CE_Failure;
hFiltMaskBand = GDALGetRasterBand( hFiltMaskDS, 1 );
/* -------------------------------------------------------------------- */
/* Allocate buffers for last scanline and this scanline. */
/* -------------------------------------------------------------------- */
GUInt32 *panLastY, *panThisY, *panTopDownY;
float *pafLastValue, *pafThisValue, *pafScanline, *pafTopDownValue;
GByte *pabyMask, *pabyFiltMask;
int iX;
int iY;
panLastY = (GUInt32 *) VSICalloc(nXSize,sizeof(GUInt32));
panThisY = (GUInt32 *) VSICalloc(nXSize,sizeof(GUInt32));
panTopDownY = (GUInt32 *) VSICalloc(nXSize,sizeof(GUInt32));
pafLastValue = (float *) VSICalloc(nXSize,sizeof(float));
pafThisValue = (float *) VSICalloc(nXSize,sizeof(float));
pafTopDownValue = (float *) VSICalloc(nXSize,sizeof(float));
pafScanline = (float *) VSICalloc(nXSize,sizeof(float));
pabyMask = (GByte *) VSICalloc(nXSize,1);
pabyFiltMask = (GByte *) VSICalloc(nXSize,1);
if (panLastY == NULL || panThisY == NULL || panTopDownY == NULL ||
pafLastValue == NULL || pafThisValue == NULL || pafTopDownValue == NULL ||
pafScanline == NULL || pabyMask == NULL || pabyFiltMask == NULL)
{
CPLError(CE_Failure, CPLE_OutOfMemory,
"Could not allocate enough memory for temporary buffers");
eErr = CE_Failure;
goto end;
}
for( iX = 0; iX < nXSize; iX++ )
{
panLastY[iX] = nNoDataVal;
}
/* ==================================================================== */
/* Make first pass from top to bottom collecting the "last */
/* known value" for each column and writing it out to the work */
/* files. */
/* ==================================================================== */
for( iY = 0; iY < nYSize && eErr == CE_None; iY++ )
{
/* -------------------------------------------------------------------- */
/* Read data and mask for this line. */
/* -------------------------------------------------------------------- */
eErr =
GDALRasterIO( hMaskBand, GF_Read, 0, iY, nXSize, 1,
pabyMask, nXSize, 1, GDT_Byte, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hTargetBand, GF_Read, 0, iY, nXSize, 1,
pafScanline, nXSize, 1, GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
/* -------------------------------------------------------------------- */
/* Figure out the most recent pixel for each column. */
/* -------------------------------------------------------------------- */
for( iX = 0; iX < nXSize; iX++ )
{
if( pabyMask[iX] )
{
pafThisValue[iX] = pafScanline[iX];
panThisY[iX] = iY;
}
else if( iY <= dfMaxSearchDist + panLastY[iX] )
{
pafThisValue[iX] = pafLastValue[iX];
panThisY[iX] = panLastY[iX];
}
else
{
panThisY[iX] = nNoDataVal;
}
}
/* -------------------------------------------------------------------- */
/* Write out best index/value to working files. */
/* -------------------------------------------------------------------- */
eErr = GDALRasterIO( hYBand, GF_Write, 0, iY, nXSize, 1,
panThisY, nXSize, 1, GDT_UInt32, 0, 0 );
if( eErr != CE_None )
break;
eErr = GDALRasterIO( hValBand, GF_Write, 0, iY, nXSize, 1,
pafThisValue, nXSize, 1, GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
/* -------------------------------------------------------------------- */
/* Flip this/last buffers. */
/* -------------------------------------------------------------------- */
{
float *pafTmp = pafThisValue;
pafThisValue = pafLastValue;
pafLastValue = pafTmp;
GUInt32 *panTmp = panThisY;
panThisY = panLastY;
panLastY = panTmp;
}
/* -------------------------------------------------------------------- */
/* report progress. */
/* -------------------------------------------------------------------- */
if( eErr == CE_None
&& !pfnProgress( dfProgressRatio * (0.5*(iY+1) / (double)nYSize),
"Filling...", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
eErr = CE_Failure;
}
}
/* ==================================================================== */
/* Now we will do collect similar this/last information from */
/* bottom to top and use it in combination with the top to */
/* bottom search info to interpolate. */
/* ==================================================================== */
for( iY = nYSize-1; iY >= 0 && eErr == CE_None; iY-- )
{
eErr =
GDALRasterIO( hMaskBand, GF_Read, 0, iY, nXSize, 1,
pabyMask, nXSize, 1, GDT_Byte, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hTargetBand, GF_Read, 0, iY, nXSize, 1,
pafScanline, nXSize, 1, GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
/* -------------------------------------------------------------------- */
/* Figure out the most recent pixel for each column. */
/* -------------------------------------------------------------------- */
for( iX = 0; iX < nXSize; iX++ )
{
if( pabyMask[iX] )
{
pafThisValue[iX] = pafScanline[iX];
panThisY[iX] = iY;
}
else if( panLastY[iX] - iY <= dfMaxSearchDist )
{
pafThisValue[iX] = pafLastValue[iX];
panThisY[iX] = panLastY[iX];
}
else
{
panThisY[iX] = nNoDataVal;
}
}
/* -------------------------------------------------------------------- */
/* Load the last y and corresponding value from the top down pass. */
/* -------------------------------------------------------------------- */
eErr =
GDALRasterIO( hYBand, GF_Read, 0, iY, nXSize, 1,
panTopDownY, nXSize, 1, GDT_UInt32, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hValBand, GF_Read, 0, iY, nXSize, 1,
pafTopDownValue, nXSize, 1, GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
/* -------------------------------------------------------------------- */
/* Attempt to interpolate any pixels that are nodata. */
/* -------------------------------------------------------------------- */
memset( pabyFiltMask, 0, nXSize );
for( iX = 0; iX < nXSize; iX++ )
{
int iStep, iQuad;
int nThisMaxSearchDist = nMaxSearchDist;
// If this was a valid target - no change.
if( pabyMask[iX] )
continue;
// Quadrants 0:topleft, 1:bottomleft, 2:topright, 3:bottomright
double adfQuadDist[4];
double adfQuadValue[4];
for( iQuad = 0; iQuad < 4; iQuad++ )
{
adfQuadDist[iQuad] = dfMaxSearchDist + 1.0;
adfQuadValue[iQuad] = 0.0;
}
// Step left and right by one pixel searching for the closest
// target value for each quadrant.
for( iStep = 0; iStep < nThisMaxSearchDist; iStep++ )
{
int iLeftX = MAX(0,iX - iStep);
int iRightX = MIN(nXSize-1,iX + iStep);
// top left includes current line
QUAD_CHECK(adfQuadDist[0],adfQuadValue[0],
iLeftX, panTopDownY[iLeftX], iX, iY,
pafTopDownValue[iLeftX] );
// bottom left
QUAD_CHECK(adfQuadDist[1],adfQuadValue[1],
iLeftX, panLastY[iLeftX], iX, iY,
pafLastValue[iLeftX] );
// top right and bottom right do no include center pixel.
if( iStep == 0 )
continue;
// top right includes current line
QUAD_CHECK(adfQuadDist[2],adfQuadValue[2],
iRightX, panTopDownY[iRightX], iX, iY,
pafTopDownValue[iRightX] );
// bottom right
QUAD_CHECK(adfQuadDist[3],adfQuadValue[3],
iRightX, panLastY[iRightX], iX, iY,
pafLastValue[iRightX] );
// every four steps, recompute maximum distance.
if( (iStep & 0x3) == 0 )
nThisMaxSearchDist = (int) floor(
MAX(MAX(adfQuadDist[0],adfQuadDist[1]),
MAX(adfQuadDist[2],adfQuadDist[3])) );
}
double dfWeightSum = 0.0;
double dfValueSum = 0.0;
for( iQuad = 0; iQuad < 4; iQuad++ )
{
if( adfQuadDist[iQuad] <= dfMaxSearchDist )
{
double dfWeight = 1.0 / adfQuadDist[iQuad];
dfWeightSum += dfWeight;
dfValueSum += adfQuadValue[iQuad] * dfWeight;
}
}
if( dfWeightSum > 0.0 )
{
pabyMask[iX] = 255;
pabyFiltMask[iX] = 255;
pafScanline[iX] = (float) (dfValueSum / dfWeightSum);
}
}
/* -------------------------------------------------------------------- */
/* Write out the updated data and mask information. */
/* -------------------------------------------------------------------- */
eErr =
GDALRasterIO( hTargetBand, GF_Write, 0, iY, nXSize, 1,
pafScanline, nXSize, 1, GDT_Float32, 0, 0 );
if( eErr != CE_None )
break;
eErr =
GDALRasterIO( hFiltMaskBand, GF_Write, 0, iY, nXSize, 1,
pabyFiltMask, nXSize, 1, GDT_Byte, 0, 0 );
if( eErr != CE_None )
break;
/* -------------------------------------------------------------------- */
/* Flip this/last buffers. */
/* -------------------------------------------------------------------- */
{
float *pafTmp = pafThisValue;
pafThisValue = pafLastValue;
pafLastValue = pafTmp;
GUInt32 *panTmp = panThisY;
panThisY = panLastY;
panLastY = panTmp;
}
/* -------------------------------------------------------------------- */
/* report progress. */
/* -------------------------------------------------------------------- */
if( eErr == CE_None
&& !pfnProgress( dfProgressRatio*(0.5+0.5*(nYSize-iY) / (double)nYSize),
"Filling...", pProgressArg ) )
{
CPLError( CE_Failure, CPLE_UserInterrupt, "User terminated" );
eErr = CE_Failure;
}
}
/* ==================================================================== */
/* Now we will do iterative average filters over the */
/* interpolated values to smooth things out and make linear */
/* artifacts less obvious. */
/* ==================================================================== */
if( eErr == CE_None && nSmoothingIterations > 0 )
{
// force masks to be to flushed and recomputed.
GDALFlushRasterCache( hMaskBand );
void *pScaledProgress;
pScaledProgress =
GDALCreateScaledProgress( dfProgressRatio, 1.0, pfnProgress, NULL );
eErr = GDALMultiFilter( hTargetBand, hMaskBand, hFiltMaskBand,
nSmoothingIterations,
GDALScaledProgress, pScaledProgress );
GDALDestroyScaledProgress( pScaledProgress );
}
/* -------------------------------------------------------------------- */
/* Close and clean up temporary files. Free working buffers */
/* -------------------------------------------------------------------- */
end:
CPLFree(panLastY);
CPLFree(panThisY);
CPLFree(panTopDownY);
CPLFree(pafLastValue);
CPLFree(pafThisValue);
CPLFree(pafTopDownValue);
CPLFree(pafScanline);
CPLFree(pabyMask);
CPLFree(pabyFiltMask);
GDALClose( hYDS );
GDALClose( hValDS );
GDALClose( hFiltMaskDS );
GDALDeleteDataset( hDriver, osYTmpFile );
GDALDeleteDataset( hDriver, osValTmpFile );
GDALDeleteDataset( hDriver, osFiltMaskTmpFile );
return eErr;
}
|