Skip to content

Commit

Permalink
nearblack: use GDALArgumentParser
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Mar 12, 2024
1 parent 662bc11 commit dfd3f70
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 202 deletions.
16 changes: 9 additions & 7 deletions apps/gdal_utils_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ struct GDALDEMProcessingOptionsForBinary
int bQuiet;
};

struct GDALNearblackOptionsForBinary
{
char *pszInFile;
char *pszOutFile;
int bQuiet;
};

struct GDALBuildVRTOptionsForBinary
{
int nSrcFiles;
Expand Down Expand Up @@ -230,6 +223,15 @@ struct GDALTileIndexOptionsForBinary
bool bQuiet = false;
};

struct GDALNearblackOptionsForBinary
{
std::string osInFile{};
std::string osOutFile{};
bool bQuiet = false;
};

std::string CPL_DLL GDALNearblackOptionsGetParserUsage();

#endif /* #ifndef DOXYGEN_SKIP */

#endif /* GDAL_UTILS_PRIV_H_INCLUDED */
83 changes: 15 additions & 68 deletions apps/nearblack_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,16 @@
/* Usage() */
/************************************************************************/

static void Usage(bool bIsError, const char *pszErrorMsg = nullptr)
static void Usage(const char *pszErrorMsg = nullptr)
{
fprintf(bIsError ? stderr : stdout,
"Usage: nearblack [--help] [--help-general]\n"
" [-of <format>] [-white | [-color "
"<c1>,<c2>,<c3>...<cn>]...]\n"
" [-near <dist>] [-nb <non_black_pixels>]\n"
" [-setalpha] [-setmask] [-alg twopasses|floodfill]\n"
" [-o <outfile>] [-q] [-co <NAME>=<VALUE>]... <infile>\n");
fprintf(stderr, "%s\n\n", GDALNearblackOptionsGetParserUsage().c_str());

if (pszErrorMsg != nullptr)
fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);

exit(bIsError ? 1 : 0);
exit(1);
}

/************************************************************************/
/* GDALNearblackOptionsForBinaryNew() */
/************************************************************************/

static GDALNearblackOptionsForBinary *GDALNearblackOptionsForBinaryNew()
{
return static_cast<GDALNearblackOptionsForBinary *>(
CPLCalloc(1, sizeof(GDALNearblackOptionsForBinary)));
}

/************************************************************************/
/* GDALNearblackOptionsForBinaryFree() */
/************************************************************************/

static void GDALNearblackOptionsForBinaryFree(
GDALNearblackOptionsForBinary *psOptionsForBinary)
{
if (psOptionsForBinary)
{
CPLFree(psOptionsForBinary->pszInFile);
CPLFree(psOptionsForBinary->pszOutFile);
CPLFree(psOptionsForBinary);
}
}
/************************************************************************/
/* main() */
/************************************************************************/
Expand All @@ -98,44 +68,23 @@ MAIN_START(argc, argv)
if (argc < 1)
exit(-argc);

for (int i = 0; i < argc; i++)
{
if (EQUAL(argv[i], "--utility_version"))
{
printf("%s was compiled against GDAL %s and "
"is running against GDAL %s\n",
argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(argv);
return 0;
}
else if (EQUAL(argv[i], "--help"))
{
Usage(false);
}
}

GDALNearblackOptionsForBinary *psOptionsForBinary =
GDALNearblackOptionsForBinaryNew();
GDALNearblackOptionsForBinary sOptionsForBinary;
GDALNearblackOptions *psOptions =
GDALNearblackOptionsNew(argv + 1, psOptionsForBinary);
GDALNearblackOptionsNew(argv + 1, &sOptionsForBinary);
CSLDestroy(argv);

if (psOptions == nullptr)
{
Usage(true);
Usage();
}

if (!(psOptionsForBinary->bQuiet))
if (!(sOptionsForBinary.bQuiet))
{
GDALNearblackOptionsSetProgress(psOptions, GDALTermProgress, nullptr);
}

if (psOptionsForBinary->pszInFile == nullptr)
Usage(true, "No input file specified.");

if (psOptionsForBinary->pszOutFile == nullptr)
psOptionsForBinary->pszOutFile =
CPLStrdup(psOptionsForBinary->pszInFile);
if (sOptionsForBinary.osOutFile.empty())
sOptionsForBinary.osOutFile = sOptionsForBinary.osInFile;

/* -------------------------------------------------------------------- */
/* Open input file. */
Expand All @@ -144,26 +93,25 @@ MAIN_START(argc, argv)
GDALDatasetH hOutDS = nullptr;
bool bCloseRetDS = false;

if (strcmp(psOptionsForBinary->pszOutFile, psOptionsForBinary->pszInFile) ==
0)
if (sOptionsForBinary.osOutFile == sOptionsForBinary.osInFile)
{
hInDS = GDALOpen(psOptionsForBinary->pszInFile, GA_Update);
hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_Update);
hOutDS = hInDS;
}
else
{
hInDS = GDALOpen(psOptionsForBinary->pszInFile, GA_ReadOnly);
hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_ReadOnly);
bCloseRetDS = true;
}

if (hInDS == nullptr)
exit(1);

int bUsageError = FALSE;
GDALDatasetH hRetDS = GDALNearblack(psOptionsForBinary->pszOutFile, hOutDS,
hInDS, psOptions, &bUsageError);
GDALDatasetH hRetDS = GDALNearblack(sOptionsForBinary.osOutFile.c_str(),
hOutDS, hInDS, psOptions, &bUsageError);
if (bUsageError)
Usage(true);
Usage();
int nRetCode = hRetDS ? 0 : 1;

if (GDALClose(hInDS) != CE_None)
Expand All @@ -174,7 +122,6 @@ MAIN_START(argc, argv)
nRetCode = 1;
}
GDALNearblackOptionsFree(psOptions);
GDALNearblackOptionsForBinaryFree(psOptionsForBinary);

GDALDestroyDriverManager();

Expand Down
Loading

0 comments on commit dfd3f70

Please sign in to comment.