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 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "cpl_string.h" 30 : #include "gdal_version.h" 31 : #include "commonutils.h" 32 : #include "gdal_utils_priv.h" 33 : 34 : /************************************************************************/ 35 : /* Usage() */ 36 : /************************************************************************/ 37 : 38 0 : static void Usage(const char *pszErrorMsg = nullptr) 39 : { 40 0 : fprintf(stderr, "%s\n\n", GDALNearblackGetParserUsage().c_str()); 41 : 42 0 : if (pszErrorMsg != nullptr) 43 0 : fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg); 44 : 45 0 : exit(1); 46 : } 47 : 48 : /************************************************************************/ 49 : /* main() */ 50 : /************************************************************************/ 51 : 52 9 : MAIN_START(argc, argv) 53 : { 54 : /* Check strict compilation and runtime library version as we use C++ API */ 55 9 : if (!GDAL_CHECK_VERSION(argv[0])) 56 0 : exit(1); 57 : 58 9 : EarlySetConfigOptions(argc, argv); 59 : 60 : /* -------------------------------------------------------------------- */ 61 : /* Generic arg processing. */ 62 : /* -------------------------------------------------------------------- */ 63 9 : GDALAllRegister(); 64 : 65 9 : if (CPLGetConfigOption("GDAL_CACHEMAX", nullptr) == nullptr) 66 9 : GDALSetCacheMax(100000000); 67 9 : argc = GDALGeneralCmdLineProcessor(argc, &argv, 0); 68 9 : if (argc < 1) 69 0 : exit(-argc); 70 : 71 9 : GDALNearblackOptionsForBinary sOptionsForBinary; 72 : GDALNearblackOptions *psOptions = 73 9 : GDALNearblackOptionsNew(argv + 1, &sOptionsForBinary); 74 8 : CSLDestroy(argv); 75 : 76 8 : if (psOptions == nullptr) 77 : { 78 0 : Usage(); 79 : } 80 : 81 8 : if (!(sOptionsForBinary.bQuiet)) 82 : { 83 7 : GDALNearblackOptionsSetProgress(psOptions, GDALTermProgress, nullptr); 84 : } 85 : 86 8 : if (sOptionsForBinary.osOutFile.empty()) 87 3 : sOptionsForBinary.osOutFile = sOptionsForBinary.osInFile; 88 : 89 : /* -------------------------------------------------------------------- */ 90 : /* Open input file. */ 91 : /* -------------------------------------------------------------------- */ 92 8 : GDALDatasetH hInDS = nullptr; 93 8 : GDALDatasetH hOutDS = nullptr; 94 8 : bool bCloseRetDS = false; 95 : 96 8 : if (sOptionsForBinary.osOutFile == sOptionsForBinary.osInFile) 97 : { 98 3 : hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_Update); 99 3 : hOutDS = hInDS; 100 : } 101 : else 102 : { 103 5 : hInDS = GDALOpen(sOptionsForBinary.osInFile.c_str(), GA_ReadOnly); 104 5 : bCloseRetDS = true; 105 : } 106 : 107 8 : if (hInDS == nullptr) 108 0 : exit(1); 109 : 110 8 : int bUsageError = FALSE; 111 8 : GDALDatasetH hRetDS = GDALNearblack(sOptionsForBinary.osOutFile.c_str(), 112 : hOutDS, hInDS, psOptions, &bUsageError); 113 8 : if (bUsageError) 114 0 : Usage(); 115 8 : int nRetCode = hRetDS ? 0 : 1; 116 : 117 8 : if (GDALClose(hInDS) != CE_None) 118 0 : nRetCode = 1; 119 8 : if (bCloseRetDS) 120 : { 121 5 : if (GDALClose(hRetDS) != CE_None) 122 0 : nRetCode = 1; 123 : } 124 8 : GDALNearblackOptionsFree(psOptions); 125 : 126 8 : GDALDestroyDriverManager(); 127 : 128 8 : return nRetCode; 129 : } 130 : 131 0 : MAIN_END