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 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
|
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: Convert nearly black or nearly white border to exact black/white.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
* ****************************************************************************
* Copyright (c) 2006, MapShots Inc (www.mapshots.com)
* Copyright (c) 2007-2013, Even Rouault <even dot rouault at spatialys.com>
*
* 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 "cpl_port.h"
#include "gdal_utils.h"
#include "gdal_utils_priv.h"
#include "commonutils.h"
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <memory>
#include <vector>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_progress.h"
#include "cpl_string.h"
#include "gdal.h"
typedef std::vector<int> Color;
typedef std::vector<Color> Colors;
struct GDALNearblackOptions
{
/*! output format. Use the short format name. */
char *pszFormat;
/*! the progress function to use */
GDALProgressFunc pfnProgress;
/*! pointer to the progress data variable */
void *pProgressData;
int nMaxNonBlack;
int nNearDist;
bool bNearWhite;
bool bSetAlpha;
bool bSetMask;
Colors oColors;
char **papszCreationOptions;
};
static void ProcessLine(GByte *pabyLine, GByte *pabyMask, int iStart, int iEnd,
int nSrcBands, int nDstBands, int nNearDist,
int nMaxNonBlack, bool bNearWhite,
const Colors *poColors, int *panLastLineCounts,
bool bDoHorizontalCheck, bool bDoVerticalCheck,
bool bBottomUp, int iLineFromTopOrBottom);
/************************************************************************/
/* GDALNearblack() */
/************************************************************************/
/* clang-format off */
/**
* Convert nearly black/white borders to exact value.
*
* This is the equivalent of the
* <a href="/programs/nearblack.html">nearblack</a> utility.
*
* GDALNearblackOptions* must be allocated and freed with
* GDALNearblackOptionsNew() and GDALNearblackOptionsFree() respectively.
* pszDest and hDstDS cannot be used at the same time.
*
* In-place update (i.e. hDstDS == hSrcDataset) is possible for formats that
* support it, and if the dataset is opened in update mode.
*
* @param pszDest the destination dataset path or NULL.
* @param hDstDS the destination dataset or NULL. Might be equal to hSrcDataset.
* @param hSrcDataset the source dataset handle.
* @param psOptionsIn the options struct returned by GDALNearblackOptionsNew()
* or NULL.
* @param pbUsageError pointer to a integer output variable to store if any
* usage error has occurred or NULL.
* @return the output dataset (new dataset that must be closed using
* GDALClose(), or hDstDS when it is not NULL) or NULL in case of error.
*
* @since GDAL 2.1
*/
/* clang-format on */
GDALDatasetH CPL_DLL GDALNearblack(const char *pszDest, GDALDatasetH hDstDS,
GDALDatasetH hSrcDataset,
const GDALNearblackOptions *psOptionsIn,
int *pbUsageError)
{
if (pszDest == nullptr && hDstDS == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"pszDest == NULL && hDstDS == NULL");
if (pbUsageError)
*pbUsageError = TRUE;
return nullptr;
}
if (hSrcDataset == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined, "hSrcDataset== NULL");
if (pbUsageError)
*pbUsageError = TRUE;
return nullptr;
}
GDALNearblackOptions *psOptionsToFree = nullptr;
const GDALNearblackOptions *psOptions;
if (psOptionsIn)
{
psOptions = psOptionsIn;
}
else
{
psOptionsToFree = GDALNearblackOptionsNew(nullptr, nullptr);
psOptions = psOptionsToFree;
}
const bool bCloseOutDSOnError = hDstDS == nullptr;
if (pszDest == nullptr)
pszDest = GDALGetDescription(hDstDS);
const int nXSize = GDALGetRasterXSize(hSrcDataset);
const int nYSize = GDALGetRasterYSize(hSrcDataset);
int nBands = GDALGetRasterCount(hSrcDataset);
int nDstBands = nBands;
const int nMaxNonBlack = psOptions->nMaxNonBlack;
const int nNearDist = psOptions->nNearDist;
const bool bNearWhite = psOptions->bNearWhite;
const bool bSetAlpha = psOptions->bSetAlpha;
bool bSetMask = psOptions->bSetMask;
Colors oColors = psOptions->oColors;
/* -------------------------------------------------------------------- */
/* Do we need to create output file? */
/* -------------------------------------------------------------------- */
if (hDstDS == nullptr)
{
CPLString osFormat;
if (psOptions->pszFormat == nullptr)
{
osFormat = GetOutputDriverForRaster(pszDest);
if (osFormat.empty())
{
GDALNearblackOptionsFree(psOptionsToFree);
return nullptr;
}
}
else
{
osFormat = psOptions->pszFormat;
}
GDALDriverH hDriver = GDALGetDriverByName(osFormat);
if (hDriver == nullptr)
{
GDALNearblackOptionsFree(psOptionsToFree);
return nullptr;
}
if (bSetAlpha)
{
// TODO(winkey): There should be a way to preserve alpha
// band data not in the collar.
if (nBands == 4)
nBands--;
else
nDstBands++;
}
if (bSetMask)
{
if (nBands == 4)
{
nDstBands = 3;
nBands = 3;
}
}
hDstDS = GDALCreate(hDriver, pszDest, nXSize, nYSize, nDstBands,
GDT_Byte, psOptions->papszCreationOptions);
if (hDstDS == nullptr)
{
GDALNearblackOptionsFree(psOptionsToFree);
return nullptr;
}
double adfGeoTransform[6] = {};
if (GDALGetGeoTransform(hSrcDataset, adfGeoTransform) == CE_None)
{
GDALSetGeoTransform(hDstDS, adfGeoTransform);
GDALSetProjection(hDstDS, GDALGetProjectionRef(hSrcDataset));
}
}
else
{
if (psOptions->papszCreationOptions != nullptr)
{
CPLError(CE_Warning, CPLE_AppDefined,
"Warning: creation options are ignored when writing to "
"an existing file.");
}
/***** check the input and output datasets are the same size *****/
if (GDALGetRasterXSize(hDstDS) != nXSize ||
GDALGetRasterYSize(hDstDS) != nYSize)
{
CPLError(CE_Failure, CPLE_AppDefined,
"The dimensions of the output dataset don't match "
"the dimensions of the input dataset.");
GDALNearblackOptionsFree(psOptionsToFree);
return nullptr;
}
if (bSetAlpha)
{
if (nBands != 4 &&
(nBands < 2 ||
GDALGetRasterColorInterpretation(
GDALGetRasterBand(hDstDS, nBands)) != GCI_AlphaBand))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Last band is not an alpha band.");
GDALNearblackOptionsFree(psOptionsToFree);
return nullptr;
}
nBands--;
}
if (bSetMask)
{
if (nBands == 4)
{
nDstBands = 3;
nBands = 3;
}
}
}
/***** set a color if there are no colors set? *****/
if (oColors.empty())
{
Color oColor;
/***** loop over the bands to get the right number of values *****/
for (int iBand = 0; iBand < nBands; iBand++)
{
// black or white?
oColor.push_back(bNearWhite ? 255 : 0);
}
/***** add the color to the colors *****/
oColors.push_back(oColor);
assert(!oColors.empty());
}
/***** does the number of bands match the number of color values? *****/
if (static_cast<int>(oColors.front().size()) != nBands)
{
CPLError(CE_Failure, CPLE_AppDefined,
"-color args must have the same number of values as "
"the non alpha input band count.\n");
GDALNearblackOptionsFree(psOptionsToFree);
if (bCloseOutDSOnError)
GDALClose(hDstDS);
return nullptr;
}
for (int iBand = 0; iBand < nBands; iBand++)
{
GDALRasterBandH hBand = GDALGetRasterBand(hSrcDataset, iBand + 1);
if (GDALGetRasterDataType(hBand) != GDT_Byte)
{
CPLError(CE_Warning, CPLE_AppDefined,
"Band %d is not of type GDT_Byte. "
"It can lead to unexpected results.",
iBand + 1);
}
if (GDALGetRasterColorTable(hBand) != nullptr)
{
CPLError(
CE_Warning, CPLE_AppDefined,
"Band %d has a color table, which is ignored by nearblack. "
"It can lead to unexpected results.",
iBand + 1);
}
}
GDALRasterBandH hMaskBand = nullptr;
if (bSetMask)
{
// If there isn't already a mask band on the output file create one.
if (GMF_PER_DATASET != GDALGetMaskFlags(GDALGetRasterBand(hDstDS, 1)))
{
if (CE_None != GDALCreateDatasetMaskBand(hDstDS, GMF_PER_DATASET))
{
CPLError(CE_Failure, CPLE_AppDefined,
"Failed to create mask band on output DS");
bSetMask = false;
}
}
if (bSetMask)
{
hMaskBand = GDALGetMaskBand(GDALGetRasterBand(hDstDS, 1));
}
}
/* -------------------------------------------------------------------- */
/* Allocate a line buffer. */
/* -------------------------------------------------------------------- */
GByte *pabyLine = static_cast<GByte *>(CPLMalloc(nXSize * nDstBands));
GByte *pabyMask = nullptr;
if (bSetMask)
pabyMask = static_cast<GByte *>(CPLMalloc(nXSize));
int *panLastLineCounts = static_cast<int *>(CPLCalloc(sizeof(int), nXSize));
/* -------------------------------------------------------------------- */
/* Processing data one line at a time. */
/* -------------------------------------------------------------------- */
int iLine;
for (iLine = 0; iLine < nYSize; iLine++)
{
CPLErr eErr = GDALDatasetRasterIO(
hSrcDataset, GF_Read, 0, iLine, nXSize, 1, pabyLine, nXSize, 1,
GDT_Byte, nBands, nullptr, nDstBands, nXSize * nDstBands, 1);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
if (bSetAlpha)
{
for (int iCol = 0; iCol < nXSize; iCol++)
{
pabyLine[iCol * nDstBands + nDstBands - 1] = 255;
}
}
if (bSetMask)
{
for (int iCol = 0; iCol < nXSize; iCol++)
{
pabyMask[iCol] = 255;
}
}
ProcessLine(pabyLine, pabyMask, 0, nXSize - 1, nBands, nDstBands,
nNearDist, nMaxNonBlack, bNearWhite, &oColors,
panLastLineCounts,
true, // bDoHorizontalCheck
true, // bDoVerticalCheck
false, // bBottomUp
iLine);
ProcessLine(pabyLine, pabyMask, nXSize - 1, 0, nBands, nDstBands,
nNearDist, nMaxNonBlack, bNearWhite, &oColors,
panLastLineCounts,
true, // bDoHorizontalCheck
false, // bDoVerticalCheck
false, // bBottomUp
iLine);
eErr = GDALDatasetRasterIO(hDstDS, GF_Write, 0, iLine, nXSize, 1,
pabyLine, nXSize, 1, GDT_Byte, nDstBands,
nullptr, nDstBands, nXSize * nDstBands, 1);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
/***** write out the mask band line *****/
if (bSetMask)
{
eErr = GDALRasterIO(hMaskBand, GF_Write, 0, iLine, nXSize, 1,
pabyMask, nXSize, 1, GDT_Byte, 0, 0);
if (eErr != CE_None)
{
CPLError(CE_Warning, CPLE_AppDefined,
"ERROR writing out line to mask band.");
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
}
if (!(psOptions->pfnProgress(
0.5 * ((iLine + 1) / static_cast<double>(nYSize)), nullptr,
psOptions->pProgressData)))
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
}
/* -------------------------------------------------------------------- */
/* Now process from the bottom back up .*/
/* -------------------------------------------------------------------- */
memset(panLastLineCounts, 0, sizeof(int) * nXSize);
for (iLine = nYSize - 1; hDstDS != nullptr && iLine >= 0; iLine--)
{
CPLErr eErr = GDALDatasetRasterIO(
hDstDS, GF_Read, 0, iLine, nXSize, 1, pabyLine, nXSize, 1, GDT_Byte,
nDstBands, nullptr, nDstBands, nXSize * nDstBands, 1);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
/***** read the mask band line back in *****/
if (bSetMask)
{
eErr = GDALRasterIO(hMaskBand, GF_Read, 0, iLine, nXSize, 1,
pabyMask, nXSize, 1, GDT_Byte, 0, 0);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
}
ProcessLine(pabyLine, pabyMask, 0, nXSize - 1, nBands, nDstBands,
nNearDist, nMaxNonBlack, bNearWhite, &oColors,
panLastLineCounts,
true, // bDoHorizontalCheck
true, // bDoVerticalCheck
true, // bBottomUp
nYSize - 1 - iLine);
ProcessLine(pabyLine, pabyMask, nXSize - 1, 0, nBands, nDstBands,
nNearDist, nMaxNonBlack, bNearWhite, &oColors,
panLastLineCounts,
true, // bDoHorizontalCheck
false, // bDoVerticalCheck
true, // bBottomUp
nYSize - 1 - iLine);
eErr = GDALDatasetRasterIO(hDstDS, GF_Write, 0, iLine, nXSize, 1,
pabyLine, nXSize, 1, GDT_Byte, nDstBands,
nullptr, nDstBands, nXSize * nDstBands, 1);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
/***** write out the mask band line *****/
if (bSetMask)
{
eErr = GDALRasterIO(hMaskBand, GF_Write, 0, iLine, nXSize, 1,
pabyMask, nXSize, 1, GDT_Byte, 0, 0);
if (eErr != CE_None)
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
}
if (!(psOptions->pfnProgress(0.5 + 0.5 * (nYSize - iLine) /
static_cast<double>(nYSize),
nullptr, psOptions->pProgressData)))
{
if (bCloseOutDSOnError)
GDALClose(hDstDS);
hDstDS = nullptr;
break;
}
}
CPLFree(pabyLine);
if (bSetMask)
CPLFree(pabyMask);
CPLFree(panLastLineCounts);
GDALNearblackOptionsFree(psOptionsToFree);
return hDstDS;
}
/************************************************************************/
/* ProcessLine() */
/* */
/* Process a single scanline of image data. */
/************************************************************************/
static void ProcessLine(GByte *pabyLine, GByte *pabyMask, int iStart, int iEnd,
int nSrcBands, int nDstBands, int nNearDist,
int nMaxNonBlack, bool bNearWhite,
const Colors *poColors, int *panLastLineCounts,
bool bDoHorizontalCheck, bool bDoVerticalCheck,
bool bBottomUp, int iLineFromTopOrBottom)
{
const GByte nReplacevalue = bNearWhite ? 255 : 0;
/* -------------------------------------------------------------------- */
/* Vertical checking. */
/* -------------------------------------------------------------------- */
if (bDoVerticalCheck)
{
const int nXSize = std::max(iStart + 1, iEnd + 1);
for (int i = 0; i < nXSize; i++)
{
// are we already terminated for this column?
if (panLastLineCounts[i] > nMaxNonBlack)
continue;
/***** is the pixel valid data? ****/
bool bIsNonBlack = false;
/***** loop over the colors *****/
for (int iColor = 0; iColor < static_cast<int>(poColors->size());
iColor++)
{
Color oColor = (*poColors)[iColor];
bIsNonBlack = false;
/***** loop over the bands *****/
for (int iBand = 0; iBand < nSrcBands; iBand++)
{
const int nPix = pabyLine[i * nDstBands + iBand];
if (oColor[iBand] - nPix > nNearDist ||
nPix > nNearDist + oColor[iBand])
{
bIsNonBlack = true;
break;
}
}
if (!bIsNonBlack)
break;
}
if (bIsNonBlack)
{
panLastLineCounts[i]++;
if (panLastLineCounts[i] > nMaxNonBlack)
continue;
if (iLineFromTopOrBottom == 0 && nMaxNonBlack > 0)
{
// if there's a valid value just at the top or bottom
// of the raster, then ignore the nMaxNonBlack setting
panLastLineCounts[i] = nMaxNonBlack + 1;
continue;
}
}
// else
// panLastLineCounts[i] = 0; // not sure this even makes sense
/***** replace the pixel values *****/
for (int iBand = 0; iBand < nSrcBands; iBand++)
pabyLine[i * nDstBands + iBand] = nReplacevalue;
/***** alpha *****/
if (nDstBands > nSrcBands)
pabyLine[i * nDstBands + nDstBands - 1] = 0;
/***** mask *****/
if (pabyMask != nullptr)
pabyMask[i] = 0;
}
}
/* -------------------------------------------------------------------- */
/* Horizontal Checking. */
/* -------------------------------------------------------------------- */
if (bDoHorizontalCheck)
{
int nNonBlackPixels = 0;
/***** on a bottom up pass assume nMaxNonBlack is 0 *****/
if (bBottomUp)
nMaxNonBlack = 0;
const int iDir = iStart < iEnd ? 1 : -1;
bool bDoTest = TRUE;
for (int i = iStart; i != iEnd; i += iDir)
{
/***** not seen any valid data? *****/
if (bDoTest)
{
/***** is the pixel valid data? ****/
bool bIsNonBlack = false;
/***** loop over the colors *****/
for (int iColor = 0;
iColor < static_cast<int>(poColors->size()); iColor++)
{
Color oColor = (*poColors)[iColor];
bIsNonBlack = false;
/***** loop over the bands *****/
for (int iBand = 0; iBand < nSrcBands; iBand++)
{
const int nPix = pabyLine[i * nDstBands + iBand];
if (oColor[iBand] - nPix > nNearDist ||
nPix > nNearDist + oColor[iBand])
{
bIsNonBlack = true;
break;
}
}
if (bIsNonBlack == false)
break;
}
if (bIsNonBlack)
{
/***** use nNonBlackPixels in grey areas *****/
/***** from the vertical pass's grey areas ****/
if (panLastLineCounts[i] <= nMaxNonBlack)
nNonBlackPixels = panLastLineCounts[i];
else
nNonBlackPixels++;
}
if (nNonBlackPixels > nMaxNonBlack)
{
bDoTest = false;
continue;
}
if (bIsNonBlack && nMaxNonBlack > 0 && i == iStart)
{
// if there's a valid value just at the left or right
// of the raster, then ignore the nMaxNonBlack setting
bDoTest = false;
continue;
}
/***** replace the pixel values *****/
for (int iBand = 0; iBand < nSrcBands; iBand++)
pabyLine[i * nDstBands + iBand] = nReplacevalue;
/***** alpha *****/
if (nDstBands > nSrcBands)
pabyLine[i * nDstBands + nDstBands - 1] = 0;
/***** mask *****/
if (pabyMask != nullptr)
pabyMask[i] = 0;
}
/***** seen valid data but test if the *****/
/***** vertical pass saw any non valid data *****/
else if (panLastLineCounts[i] == 0)
{
bDoTest = true;
nNonBlackPixels = 0;
}
}
}
}
/************************************************************************/
/* IsInt() */
/************************************************************************/
static bool IsInt(const char *pszArg)
{
if (pszArg[0] == '-')
pszArg++;
if (*pszArg == '\0')
return false;
while (*pszArg != '\0')
{
if (*pszArg < '0' || *pszArg > '9')
return false;
pszArg++;
}
return true;
}
/************************************************************************/
/* GDALNearblackOptionsNew() */
/************************************************************************/
/**
* Allocates a GDALNearblackOptions struct.
*
* @param papszArgv NULL terminated list of options (potentially including
* filename and open options too), or NULL. The accepted options are the ones of
* the <a href="/programs/nearblack.html">nearblack</a> utility.
* @param psOptionsForBinary (output) may be NULL (and should generally be
* NULL), otherwise (gdal_translate_bin.cpp use case) must be allocated with
* GDALNearblackOptionsForBinaryNew() prior to this
* function. Will be filled with potentially present filename, open options,...
* @return pointer to the allocated GDALNearblackOptions struct. Must be freed
* with GDALNearblackOptionsFree().
*
* @since GDAL 2.1
*/
GDALNearblackOptions *
GDALNearblackOptionsNew(char **papszArgv,
GDALNearblackOptionsForBinary *psOptionsForBinary)
{
GDALNearblackOptions *psOptions = new GDALNearblackOptions;
psOptions->pszFormat = nullptr;
psOptions->pfnProgress = GDALDummyProgress;
psOptions->pProgressData = nullptr;
psOptions->papszCreationOptions = nullptr;
psOptions->nMaxNonBlack = 2;
psOptions->nNearDist = 15;
psOptions->bNearWhite = false;
psOptions->bSetAlpha = false;
psOptions->bSetMask = false;
/* -------------------------------------------------------------------- */
/* Handle command line arguments. */
/* -------------------------------------------------------------------- */
const int argc = CSLCount(papszArgv);
for (int i = 0; papszArgv != nullptr && i < argc; i++)
{
if (i < argc - 1 &&
(EQUAL(papszArgv[i], "-of") || EQUAL(papszArgv[i], "-f")))
{
++i;
CPLFree(psOptions->pszFormat);
psOptions->pszFormat = CPLStrdup(papszArgv[i]);
}
else if (EQUAL(papszArgv[i], "-q") || EQUAL(papszArgv[i], "-quiet"))
{
if (psOptionsForBinary)
psOptionsForBinary->bQuiet = TRUE;
}
else if (i + 1 < argc && EQUAL(papszArgv[i], "-co"))
{
psOptions->papszCreationOptions =
CSLAddString(psOptions->papszCreationOptions, papszArgv[++i]);
}
else if (i + 1 < argc && EQUAL(papszArgv[i], "-o"))
{
i++;
if (psOptionsForBinary)
{
CPLFree(psOptionsForBinary->pszOutFile);
psOptionsForBinary->pszOutFile = CPLStrdup(papszArgv[i]);
}
}
else if (EQUAL(papszArgv[i], "-white"))
{
psOptions->bNearWhite = true;
}
/***** -color c1,c2,c3...cn *****/
else if (i + 1 < argc && EQUAL(papszArgv[i], "-color"))
{
Color oColor;
/***** tokenize the arg on , *****/
char **papszTokens = CSLTokenizeString2(papszArgv[++i], ",", 0);
/***** loop over the tokens *****/
for (int iToken = 0; papszTokens && papszTokens[iToken]; iToken++)
{
/***** ensure the token is an int and add it to the color *****/
if (IsInt(papszTokens[iToken]))
{
oColor.push_back(atoi(papszTokens[iToken]));
}
else
{
CPLError(CE_Failure, CPLE_AppDefined,
"Colors must be valid integers.");
CSLDestroy(papszTokens);
GDALNearblackOptionsFree(psOptions);
return nullptr;
}
}
CSLDestroy(papszTokens);
/***** check if the number of bands is consistent *****/
if (!psOptions->oColors.empty() &&
psOptions->oColors.front().size() != oColor.size())
{
CPLError(
CE_Failure, CPLE_AppDefined,
"all -color args must have the same number of values.\n");
GDALNearblackOptionsFree(psOptions);
return nullptr;
}
/***** add the color to the colors *****/
psOptions->oColors.push_back(oColor);
}
else if (i + 1 < argc && EQUAL(papszArgv[i], "-nb"))
{
psOptions->nMaxNonBlack = atoi(papszArgv[++i]);
}
else if (i + 1 < argc && EQUAL(papszArgv[i], "-near"))
{
psOptions->nNearDist = atoi(papszArgv[++i]);
}
else if (EQUAL(papszArgv[i], "-setalpha"))
{
psOptions->bSetAlpha = true;
}
else if (EQUAL(papszArgv[i], "-setmask"))
{
psOptions->bSetMask = true;
}
else if (papszArgv[i][0] == '-')
{
CPLError(CE_Failure, CPLE_NotSupported, "Unknown option name '%s'",
papszArgv[i]);
GDALNearblackOptionsFree(psOptions);
return nullptr;
}
else if (psOptionsForBinary && psOptionsForBinary->pszInFile == nullptr)
{
psOptionsForBinary->pszInFile = CPLStrdup(papszArgv[i]);
}
else
{
CPLError(CE_Failure, CPLE_NotSupported,
"Too many command options '%s'", papszArgv[i]);
GDALNearblackOptionsFree(psOptions);
return nullptr;
}
}
return psOptions;
}
/************************************************************************/
/* GDALNearblackOptionsFree() */
/************************************************************************/
/**
* Frees the GDALNearblackOptions struct.
*
* @param psOptions the options struct for GDALNearblack().
*
* @since GDAL 2.1
*/
void GDALNearblackOptionsFree(GDALNearblackOptions *psOptions)
{
if (psOptions == nullptr)
return;
CPLFree(psOptions->pszFormat);
CSLDestroy(psOptions->papszCreationOptions);
delete psOptions;
}
/************************************************************************/
/* GDALNearblackOptionsSetProgress() */
/************************************************************************/
/**
* Set a progress function.
*
* @param psOptions the options struct for GDALNearblack().
* @param pfnProgress the progress callback.
* @param pProgressData the user data for the progress callback.
*
* @since GDAL 2.1
*/
void GDALNearblackOptionsSetProgress(GDALNearblackOptions *psOptions,
GDALProgressFunc pfnProgress,
void *pProgressData)
{
psOptions->pfnProgress = pfnProgress ? pfnProgress : GDALDummyProgress;
psOptions->pProgressData = pProgressData;
}
|