Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: "resize" step of "raster pipeline" 5 : * Author: Even Rouault <even dot rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "gdalalg_raster_resize.h" 14 : 15 : #include "gdal_priv.h" 16 : #include "gdal_utils.h" 17 : 18 : //! @cond Doxygen_Suppress 19 : 20 : #ifndef _ 21 : #define _(x) (x) 22 : #endif 23 : 24 : /************************************************************************/ 25 : /* GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm() */ 26 : /************************************************************************/ 27 : 28 10 : GDALRasterResizeAlgorithm::GDALRasterResizeAlgorithm(bool standaloneStep) 29 : : GDALRasterPipelineStepAlgorithm(NAME, DESCRIPTION, HELP_URL, 30 10 : standaloneStep) 31 : { 32 20 : auto &sizeArg = AddArg("size", 0, _("Target size in pixels"), &m_size) 33 10 : .SetMinCount(2) 34 10 : .SetMaxCount(2) 35 10 : .SetRequired() 36 10 : .SetRepeatedArgAllowed(false) 37 10 : .SetDisplayHintAboutRepetition(false) 38 20 : .SetMetaVar("<width>,<height>") 39 10 : .SetMutualExclusionGroup("resolution-size"); 40 : sizeArg.AddValidationAction( 41 2 : [&sizeArg]() 42 : { 43 2 : const auto &val = sizeArg.Get<std::vector<int>>(); 44 2 : CPLAssert(val.size() == 2); 45 2 : if (!(val[0] >= 0 && val[1] >= 0)) 46 : { 47 0 : CPLError(CE_Failure, CPLE_AppDefined, 48 : "Target size should be positive or 0."); 49 0 : return false; 50 : } 51 2 : return true; 52 10 : }); 53 : 54 20 : AddArg("resampling", 'r', _("Resampling method"), &m_resampling) 55 : .SetChoices("nearest", "bilinear", "cubic", "cubicspline", "lanczos", 56 10 : "average", "mode") 57 10 : .SetDefault("nearest") 58 10 : .SetHiddenChoices("near"); 59 10 : } 60 : 61 : /************************************************************************/ 62 : /* GDALRasterResizeAlgorithm::RunStep() */ 63 : /************************************************************************/ 64 : 65 2 : bool GDALRasterResizeAlgorithm::RunStep(GDALProgressFunc, void *) 66 : { 67 2 : CPLAssert(m_inputDataset.GetDatasetRef()); 68 2 : CPLAssert(m_outputDataset.GetName().empty()); 69 2 : CPLAssert(!m_outputDataset.GetDatasetRef()); 70 : 71 4 : CPLStringList aosOptions; 72 2 : aosOptions.AddString("-of"); 73 2 : aosOptions.AddString("VRT"); 74 2 : if (!m_size.empty()) 75 : { 76 2 : aosOptions.AddString("-outsize"); 77 2 : aosOptions.AddString(CPLSPrintf("%d", m_size[0])); 78 2 : aosOptions.AddString(CPLSPrintf("%d", m_size[1])); 79 : } 80 2 : if (!m_resampling.empty()) 81 : { 82 2 : aosOptions.AddString("-r"); 83 2 : aosOptions.AddString(m_resampling.c_str()); 84 : } 85 : 86 : GDALTranslateOptions *psOptions = 87 2 : GDALTranslateOptionsNew(aosOptions.List(), nullptr); 88 : 89 : auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle( 90 : GDALTranslate("", GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()), 91 2 : psOptions, nullptr))); 92 2 : GDALTranslateOptionsFree(psOptions); 93 2 : const bool bRet = poOutDS != nullptr; 94 2 : if (poOutDS) 95 : { 96 2 : m_outputDataset.Set(std::move(poOutDS)); 97 : } 98 : 99 4 : return bRet; 100 : } 101 : 102 : //! @endcond