LCOV - code coverage report
Current view: top level - apps - nearblack_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 35 48 72.9 %
Date: 2024-11-21 22:18:42 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL Utilities
       4             :  * Purpose:  Convert nearly black or nearly white border to exact black/white.
       5             :  * Authors:  Even Rouault, <even dot rouault at spatialys dot com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2015, Even Rouault <even dot rouault at spatialys dot com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "cpl_string.h"
      14             : #include "gdal_version.h"
      15             : #include "commonutils.h"
      16             : #include "gdal_utils_priv.h"
      17             : 
      18             : /************************************************************************/
      19             : /*                               Usage()                                */
      20             : /************************************************************************/
      21             : 
      22           0 : static void Usage(const char *pszErrorMsg = nullptr)
      23             : {
      24           0 :     fprintf(stderr, "%s\n\n", GDALNearblackGetParserUsage().c_str());
      25             : 
      26           0 :     if (pszErrorMsg != nullptr)
      27           0 :         fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
      28             : 
      29           0 :     exit(1);
      30             : }
      31             : 
      32             : /************************************************************************/
      33             : /*                                main()                                */
      34             : /************************************************************************/
      35             : 
      36           9 : MAIN_START(argc, argv)
      37             : {
      38             :     /* Check strict compilation and runtime library version as we use C++ API */
      39           9 :     if (!GDAL_CHECK_VERSION(argv[0]))
      40           0 :         exit(1);
      41             : 
      42           9 :     EarlySetConfigOptions(argc, argv);
      43             : 
      44             :     /* -------------------------------------------------------------------- */
      45             :     /*      Generic arg processing.                                         */
      46             :     /* -------------------------------------------------------------------- */
      47           9 :     GDALAllRegister();
      48             : 
      49           9 :     if (CPLGetConfigOption("GDAL_CACHEMAX", nullptr) == nullptr)
      50           9 :         GDALSetCacheMax(100000000);
      51           9 :     argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
      52           9 :     if (argc < 1)
      53           0 :         exit(-argc);
      54             : 
      55           9 :     GDALNearblackOptionsForBinary sOptionsForBinary;
      56             :     GDALNearblackOptions *psOptions =
      57           9 :         GDALNearblackOptionsNew(argv + 1, &sOptionsForBinary);
      58           8 :     CSLDestroy(argv);
      59             : 
      60           8 :     if (psOptions == nullptr)
      61             :     {
      62           0 :         Usage();
      63             :     }
      64             : 
      65           8 :     if (!(sOptionsForBinary.bQuiet))
      66             :     {
      67           7 :         GDALNearblackOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
      68             :     }
      69             : 
      70           8 :     if (sOptionsForBinary.osOutFile.empty())
      71           3 :         sOptionsForBinary.osOutFile = sOptionsForBinary.osInFile;
      72             : 
      73             :     /* -------------------------------------------------------------------- */
      74             :     /*      Open input file.                                                */
      75             :     /* -------------------------------------------------------------------- */
      76           8 :     GDALDatasetH hInDS = nullptr;
      77           8 :     GDALDatasetH hOutDS = nullptr;
      78           8 :     bool bCloseRetDS = false;
      79             : 
      80           8 :     if (sOptionsForBinary.osOutFile == sOptionsForBinary.osInFile)
      81             :     {
      82           3 :         hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_Update);
      83           3 :         hOutDS = hInDS;
      84             :     }
      85             :     else
      86             :     {
      87           5 :         hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_ReadOnly);
      88           5 :         bCloseRetDS = true;
      89             :     }
      90             : 
      91           8 :     if (hInDS == nullptr)
      92           0 :         exit(1);
      93             : 
      94           8 :     int bUsageError = FALSE;
      95           8 :     GDALDatasetH hRetDS = GDALNearblack(sOptionsForBinary.osOutFile.c_str(),
      96             :                                         hOutDS, hInDS, psOptions, &bUsageError);
      97           8 :     if (bUsageError)
      98           0 :         Usage();
      99           8 :     int nRetCode = hRetDS ? 0 : 1;
     100             : 
     101           8 :     if (GDALClose(hInDS) != CE_None)
     102           0 :         nRetCode = 1;
     103           8 :     if (bCloseRetDS)
     104             :     {
     105           5 :         if (GDALClose(hRetDS) != CE_None)
     106           0 :             nRetCode = 1;
     107             :     }
     108           8 :     GDALNearblackOptionsFree(psOptions);
     109             : 
     110           8 :     GDALDestroyDriverManager();
     111             : 
     112           8 :     return nRetCode;
     113             : }
     114             : 
     115           0 : MAIN_END

Generated by: LCOV version 1.14