LCOV - code coverage report
Current view: top level - frmts/vrt - vrtprocesseddatasetfunctions.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 677 744 91.0 %
Date: 2025-10-25 23:36:32 Functions: 28 28 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Virtual GDAL Datasets
       4             :  * Purpose:  Implementation of VRTProcessedDataset processing functions
       5             :  * Author:   Even Rouault <even.rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2024, Even Rouault <even.rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "cpl_float.h"
      14             : #include "cpl_minixml.h"
      15             : #include "cpl_string.h"
      16             : #include "gdal_cpp_functions.h"
      17             : #include "vrtdataset.h"
      18             : #include "vrtexpression.h"
      19             : 
      20             : #include <algorithm>
      21             : #include <functional>
      22             : #include <limits>
      23             : #include <map>
      24             : #include <optional>
      25             : #include <set>
      26             : #include <vector>
      27             : 
      28             : /************************************************************************/
      29             : /*                               GetDstValue()                          */
      30             : /************************************************************************/
      31             : 
      32             : /** Return a destination value given an initial value, the destination no data
      33             :  * value and its replacement value
      34             :  */
      35        3178 : static inline double GetDstValue(double dfVal, double dfDstNoData,
      36             :                                  double dfReplacementDstNodata,
      37             :                                  GDALDataType eIntendedDstDT,
      38             :                                  bool bDstIntendedDTIsInteger)
      39             : {
      40        3178 :     if (bDstIntendedDTIsInteger && std::round(dfVal) == dfDstNoData)
      41             :     {
      42           1 :         return dfReplacementDstNodata;
      43             :     }
      44        3177 :     else if (eIntendedDstDT == GDT_Float16 &&
      45        3177 :              static_cast<GFloat16>(dfVal) == static_cast<GFloat16>(dfDstNoData))
      46             :     {
      47           0 :         return dfReplacementDstNodata;
      48             :     }
      49        3177 :     else if (eIntendedDstDT == GDT_Float32 &&
      50           0 :              static_cast<float>(dfVal) == static_cast<float>(dfDstNoData))
      51             :     {
      52           0 :         return dfReplacementDstNodata;
      53             :     }
      54        3177 :     else if (eIntendedDstDT == GDT_Float64 && dfVal == dfDstNoData)
      55             :     {
      56           1 :         return dfReplacementDstNodata;
      57             :     }
      58             :     else
      59             :     {
      60        3176 :         return dfVal;
      61             :     }
      62             : }
      63             : 
      64             : /************************************************************************/
      65             : /*                    BandAffineCombinationData                         */
      66             : /************************************************************************/
      67             : 
      68             : namespace
      69             : {
      70             : /** Working structure for 'BandAffineCombination' builtin function. */
      71             : struct BandAffineCombinationData
      72             : {
      73             :     static constexpr const char *const EXPECTED_SIGNATURE =
      74             :         "BandAffineCombination";
      75             :     //! Signature (to make sure callback functions are called with the right argument)
      76             :     const std::string m_osSignature = EXPECTED_SIGNATURE;
      77             : 
      78             :     /** Replacement nodata value */
      79             :     std::vector<double> m_adfReplacementDstNodata{};
      80             : 
      81             :     /** Intended destination data type. */
      82             :     GDALDataType m_eIntendedDstDT = GDT_Float64;
      83             : 
      84             :     /** Affine transformation coefficients.
      85             :      * m_aadfCoefficients[i][0] is the constant term for the i(th) dst band
      86             :      * m_aadfCoefficients[i][j] is the weight of the j(th) src band for the
      87             :      * i(th) dst vand.
      88             :      * Said otherwise dst[i] = m_aadfCoefficients[i][0] +
      89             :      *      sum(m_aadfCoefficients[i][j + 1] * src[j] for j in 0...nSrcBands-1)
      90             :      */
      91             :     std::vector<std::vector<double>> m_aadfCoefficients{};
      92             : 
      93             :     //! Minimum clamping value.
      94             :     double m_dfClampMin = std::numeric_limits<double>::quiet_NaN();
      95             : 
      96             :     //! Maximum clamping value.
      97             :     double m_dfClampMax = std::numeric_limits<double>::quiet_NaN();
      98             : };
      99             : }  // namespace
     100             : 
     101             : /************************************************************************/
     102             : /*               SetOutputValuesForInNoDataAndOutNoData()               */
     103             : /************************************************************************/
     104             : 
     105          44 : static std::vector<double> SetOutputValuesForInNoDataAndOutNoData(
     106             :     int nInBands, double *padfInNoData, int *pnOutBands,
     107             :     double **ppadfOutNoData, bool bSrcNodataSpecified, double dfSrcNoData,
     108             :     bool bDstNodataSpecified, double dfDstNoData, bool bIsFinalStep)
     109             : {
     110          44 :     if (bSrcNodataSpecified)
     111             :     {
     112           3 :         std::vector<double> adfNoData(nInBands, dfSrcNoData);
     113           3 :         memcpy(padfInNoData, adfNoData.data(),
     114           3 :                adfNoData.size() * sizeof(double));
     115             :     }
     116             : 
     117          44 :     std::vector<double> adfDstNoData;
     118          44 :     if (bDstNodataSpecified)
     119             :     {
     120           3 :         adfDstNoData.resize(*pnOutBands, dfDstNoData);
     121             :     }
     122          41 :     else if (bIsFinalStep)
     123             :     {
     124             :         adfDstNoData =
     125          35 :             std::vector<double>(*ppadfOutNoData, *ppadfOutNoData + *pnOutBands);
     126             :     }
     127             :     else
     128             :     {
     129             :         adfDstNoData =
     130           6 :             std::vector<double>(padfInNoData, padfInNoData + nInBands);
     131           6 :         adfDstNoData.resize(*pnOutBands, *padfInNoData);
     132             :     }
     133             : 
     134          44 :     if (*ppadfOutNoData == nullptr)
     135             :     {
     136           6 :         *ppadfOutNoData =
     137           6 :             static_cast<double *>(CPLMalloc(*pnOutBands * sizeof(double)));
     138             :     }
     139          44 :     memcpy(*ppadfOutNoData, adfDstNoData.data(), *pnOutBands * sizeof(double));
     140             : 
     141          44 :     return adfDstNoData;
     142             : }
     143             : 
     144             : /************************************************************************/
     145             : /*                    BandAffineCombinationInit()                       */
     146             : /************************************************************************/
     147             : 
     148             : /** Init function for 'BandAffineCombination' builtin function. */
     149          38 : static CPLErr BandAffineCombinationInit(
     150             :     const char * /*pszFuncName*/, void * /*pUserData*/,
     151             :     CSLConstList papszFunctionArgs, int nInBands, GDALDataType eInDT,
     152             :     double *padfInNoData, int *pnOutBands, GDALDataType *peOutDT,
     153             :     double **ppadfOutNoData, const char * /* pszVRTPath */,
     154             :     VRTPDWorkingDataPtr *ppWorkingData)
     155             : {
     156          38 :     CPLAssert(eInDT == GDT_Float64);
     157             : 
     158          38 :     *peOutDT = eInDT;
     159          38 :     *ppWorkingData = nullptr;
     160             : 
     161          76 :     auto data = std::make_unique<BandAffineCombinationData>();
     162             : 
     163          76 :     std::map<int, std::vector<double>> oMapCoefficients{};
     164          38 :     double dfSrcNoData = std::numeric_limits<double>::quiet_NaN();
     165          38 :     bool bSrcNodataSpecified = false;
     166          38 :     double dfDstNoData = std::numeric_limits<double>::quiet_NaN();
     167          38 :     bool bDstNodataSpecified = false;
     168          38 :     double dfReplacementDstNodata = std::numeric_limits<double>::quiet_NaN();
     169          38 :     bool bReplacementDstNodataSpecified = false;
     170             : 
     171         195 :     for (const auto &[pszKey, pszValue] :
     172         232 :          cpl::IterateNameValue(papszFunctionArgs))
     173             :     {
     174          98 :         if (EQUAL(pszKey, "src_nodata"))
     175             :         {
     176           2 :             bSrcNodataSpecified = true;
     177           2 :             dfSrcNoData = CPLAtof(pszValue);
     178             :         }
     179          96 :         else if (EQUAL(pszKey, "dst_nodata"))
     180             :         {
     181           2 :             bDstNodataSpecified = true;
     182           2 :             dfDstNoData = CPLAtof(pszValue);
     183             :         }
     184          94 :         else if (EQUAL(pszKey, "replacement_nodata"))
     185             :         {
     186           1 :             bReplacementDstNodataSpecified = true;
     187           1 :             dfReplacementDstNodata = CPLAtof(pszValue);
     188             :         }
     189          93 :         else if (EQUAL(pszKey, "dst_intended_datatype"))
     190             :         {
     191           1 :             for (GDALDataType eDT = GDT_Byte; eDT < GDT_TypeCount;
     192           0 :                  eDT = static_cast<GDALDataType>(eDT + 1))
     193             :             {
     194           1 :                 if (EQUAL(GDALGetDataTypeName(eDT), pszValue))
     195             :                 {
     196           1 :                     data->m_eIntendedDstDT = eDT;
     197           1 :                     break;
     198             :                 }
     199             :             }
     200             :         }
     201          92 :         else if (STARTS_WITH_CI(pszKey, "coefficients_"))
     202             :         {
     203          88 :             const int nTargetBand = atoi(pszKey + strlen("coefficients_"));
     204          88 :             if (nTargetBand <= 0 || nTargetBand > 65536)
     205             :             {
     206           0 :                 CPLError(CE_Failure, CPLE_AppDefined,
     207             :                          "Invalid band in argument '%s'", pszKey);
     208           1 :                 return CE_Failure;
     209             :             }
     210          88 :             const CPLStringList aosTokens(CSLTokenizeString2(pszValue, ",", 0));
     211          88 :             if (aosTokens.size() != 1 + nInBands)
     212             :             {
     213           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     214             :                          "Argument %s has %d values, whereas %d are expected",
     215             :                          pszKey, aosTokens.size(), 1 + nInBands);
     216           1 :                 return CE_Failure;
     217             :             }
     218          87 :             std::vector<double> adfValues;
     219         401 :             for (int i = 0; i < aosTokens.size(); ++i)
     220             :             {
     221         314 :                 adfValues.push_back(CPLAtof(aosTokens[i]));
     222             :             }
     223          87 :             oMapCoefficients[nTargetBand - 1] = std::move(adfValues);
     224             :         }
     225           4 :         else if (EQUAL(pszKey, "min"))
     226             :         {
     227           2 :             data->m_dfClampMin = CPLAtof(pszValue);
     228             :         }
     229           2 :         else if (EQUAL(pszKey, "max"))
     230             :         {
     231           2 :             data->m_dfClampMax = CPLAtof(pszValue);
     232             :         }
     233             :         else
     234             :         {
     235           0 :             CPLError(CE_Warning, CPLE_AppDefined,
     236             :                      "Unrecognized argument name %s. Ignored", pszKey);
     237             :         }
     238             :     }
     239             : 
     240          37 :     const bool bIsFinalStep = *pnOutBands != 0;
     241          37 :     if (bIsFinalStep)
     242             :     {
     243          31 :         if (*pnOutBands != static_cast<int>(oMapCoefficients.size()))
     244             :         {
     245           2 :             CPLError(CE_Failure, CPLE_AppDefined,
     246             :                      "Final step expect %d bands, but only %d coefficient_XX "
     247             :                      "are provided",
     248           2 :                      *pnOutBands, static_cast<int>(oMapCoefficients.size()));
     249           2 :             return CE_Failure;
     250             :         }
     251             :     }
     252             :     else
     253             :     {
     254           6 :         *pnOutBands = static_cast<int>(oMapCoefficients.size());
     255             :     }
     256             : 
     257             :     const std::vector<double> adfDstNoData =
     258             :         SetOutputValuesForInNoDataAndOutNoData(
     259             :             nInBands, padfInNoData, pnOutBands, ppadfOutNoData,
     260             :             bSrcNodataSpecified, dfSrcNoData, bDstNodataSpecified, dfDstNoData,
     261          70 :             bIsFinalStep);
     262             : 
     263          35 :     if (bReplacementDstNodataSpecified)
     264             :     {
     265           1 :         data->m_adfReplacementDstNodata.resize(*pnOutBands,
     266             :                                                dfReplacementDstNodata);
     267             :     }
     268             :     else
     269             :     {
     270         116 :         for (double dfVal : adfDstNoData)
     271             :         {
     272          82 :             data->m_adfReplacementDstNodata.emplace_back(
     273          82 :                 GDALGetNoDataReplacementValue(data->m_eIntendedDstDT, dfVal));
     274             :         }
     275             :     }
     276             : 
     277             :     // Check we have a set of coefficient for all output bands and
     278             :     // convert the map to a vector
     279         117 :     for (auto &oIter : oMapCoefficients)
     280             :     {
     281          84 :         const int iExpected = static_cast<int>(data->m_aadfCoefficients.size());
     282          84 :         if (oIter.first != iExpected)
     283             :         {
     284           2 :             CPLError(CE_Failure, CPLE_AppDefined,
     285             :                      "Argument coefficients_%d is missing", iExpected + 1);
     286           2 :             return CE_Failure;
     287             :         }
     288          82 :         data->m_aadfCoefficients.emplace_back(std::move(oIter.second));
     289             :     }
     290          33 :     *ppWorkingData = data.release();
     291          33 :     return CE_None;
     292             : }
     293             : 
     294             : /************************************************************************/
     295             : /*                    BandAffineCombinationFree()                       */
     296             : /************************************************************************/
     297             : 
     298             : /** Free function for 'BandAffineCombination' builtin function. */
     299          33 : static void BandAffineCombinationFree(const char * /*pszFuncName*/,
     300             :                                       void * /*pUserData*/,
     301             :                                       VRTPDWorkingDataPtr pWorkingData)
     302             : {
     303          33 :     BandAffineCombinationData *data =
     304             :         static_cast<BandAffineCombinationData *>(pWorkingData);
     305          33 :     CPLAssert(data->m_osSignature ==
     306             :               BandAffineCombinationData::EXPECTED_SIGNATURE);
     307          33 :     CPL_IGNORE_RET_VAL(data->m_osSignature);
     308          33 :     delete data;
     309          33 : }
     310             : 
     311             : /************************************************************************/
     312             : /*                    BandAffineCombinationProcess()                    */
     313             : /************************************************************************/
     314             : 
     315             : /** Processing function for 'BandAffineCombination' builtin function. */
     316          41 : static CPLErr BandAffineCombinationProcess(
     317             :     const char * /*pszFuncName*/, void * /*pUserData*/,
     318             :     VRTPDWorkingDataPtr pWorkingData, CSLConstList /* papszFunctionArgs*/,
     319             :     int nBufXSize, int nBufYSize, const void *pInBuffer, size_t nInBufferSize,
     320             :     GDALDataType eInDT, int nInBands, const double *CPL_RESTRICT padfInNoData,
     321             :     void *pOutBuffer, size_t nOutBufferSize, GDALDataType eOutDT, int nOutBands,
     322             :     const double *CPL_RESTRICT padfOutNoData, double /*dfSrcXOff*/,
     323             :     double /*dfSrcYOff*/, double /*dfSrcXSize*/, double /*dfSrcYSize*/,
     324             :     const double /*adfSrcGT*/[], const char * /* pszVRTPath */,
     325             :     CSLConstList /*papszExtra*/)
     326             : {
     327          41 :     const size_t nElts = static_cast<size_t>(nBufXSize) * nBufYSize;
     328             : 
     329          41 :     CPL_IGNORE_RET_VAL(eInDT);
     330          41 :     CPLAssert(eInDT == GDT_Float64);
     331          41 :     CPL_IGNORE_RET_VAL(eOutDT);
     332          41 :     CPLAssert(eOutDT == GDT_Float64);
     333          41 :     CPL_IGNORE_RET_VAL(nInBufferSize);
     334          41 :     CPLAssert(nInBufferSize == nElts * nInBands * sizeof(double));
     335          41 :     CPL_IGNORE_RET_VAL(nOutBufferSize);
     336          41 :     CPLAssert(nOutBufferSize == nElts * nOutBands * sizeof(double));
     337             : 
     338          41 :     const BandAffineCombinationData *data =
     339             :         static_cast<BandAffineCombinationData *>(pWorkingData);
     340          41 :     CPLAssert(data->m_osSignature ==
     341             :               BandAffineCombinationData::EXPECTED_SIGNATURE);
     342          41 :     const double *CPL_RESTRICT padfSrc = static_cast<const double *>(pInBuffer);
     343          41 :     double *CPL_RESTRICT padfDst = static_cast<double *>(pOutBuffer);
     344             :     const bool bDstIntendedDTIsInteger =
     345          41 :         GDALDataTypeIsInteger(data->m_eIntendedDstDT);
     346          41 :     const double dfClampMin = data->m_dfClampMin;
     347          41 :     const double dfClampMax = data->m_dfClampMax;
     348        1919 :     for (size_t i = 0; i < nElts; ++i)
     349             :     {
     350        5068 :         for (int iDst = 0; iDst < nOutBands; ++iDst)
     351             :         {
     352        3190 :             const auto &adfCoefficients = data->m_aadfCoefficients[iDst];
     353        3190 :             double dfVal = adfCoefficients[0];
     354        3190 :             bool bSetNoData = false;
     355        7940 :             for (int iSrc = 0; iSrc < nInBands; ++iSrc)
     356             :             {
     357             :                 // written this way to work with a NaN value
     358        4762 :                 if (!(padfSrc[iSrc] != padfInNoData[iSrc]))
     359             :                 {
     360          12 :                     bSetNoData = true;
     361          12 :                     break;
     362             :                 }
     363        4750 :                 dfVal += adfCoefficients[iSrc + 1] * padfSrc[iSrc];
     364             :             }
     365        3190 :             if (bSetNoData)
     366             :             {
     367          12 :                 *padfDst = padfOutNoData[iDst];
     368             :             }
     369             :             else
     370             :             {
     371        9534 :                 double dfDstVal = GetDstValue(
     372        3178 :                     dfVal, padfOutNoData[iDst],
     373        3178 :                     data->m_adfReplacementDstNodata[iDst],
     374        3178 :                     data->m_eIntendedDstDT, bDstIntendedDTIsInteger);
     375        3178 :                 if (dfDstVal < dfClampMin)
     376           2 :                     dfDstVal = dfClampMin;
     377        3178 :                 if (dfDstVal > dfClampMax)
     378           2 :                     dfDstVal = dfClampMax;
     379        3178 :                 *padfDst = dfDstVal;
     380             :             }
     381        3190 :             ++padfDst;
     382             :         }
     383        1878 :         padfSrc += nInBands;
     384             :     }
     385             : 
     386          41 :     return CE_None;
     387             : }
     388             : 
     389             : /************************************************************************/
     390             : /*                                LUTData                               */
     391             : /************************************************************************/
     392             : 
     393             : namespace
     394             : {
     395             : /** Working structure for 'LUT' builtin function. */
     396             : struct LUTData
     397             : {
     398             :     static constexpr const char *const EXPECTED_SIGNATURE = "LUT";
     399             :     //! Signature (to make sure callback functions are called with the right argument)
     400             :     const std::string m_osSignature = EXPECTED_SIGNATURE;
     401             : 
     402             :     //! m_aadfLUTInputs[i][j] is the j(th) input value for that LUT of band i.
     403             :     std::vector<std::vector<double>> m_aadfLUTInputs{};
     404             : 
     405             :     //! m_aadfLUTOutputs[i][j] is the j(th) output value for that LUT of band i.
     406             :     std::vector<std::vector<double>> m_aadfLUTOutputs{};
     407             : 
     408             :     /************************************************************************/
     409             :     /*                              LookupValue()                           */
     410             :     /************************************************************************/
     411             : 
     412          18 :     double LookupValue(int iBand, double dfInput) const
     413             :     {
     414          18 :         const auto &adfInput = m_aadfLUTInputs[iBand];
     415          18 :         const auto &afdOutput = m_aadfLUTOutputs[iBand];
     416             : 
     417             :         // Find the index of the first element in the LUT input array that
     418             :         // is not smaller than the input value.
     419             :         int i = static_cast<int>(
     420          18 :             std::lower_bound(adfInput.data(), adfInput.data() + adfInput.size(),
     421          18 :                              dfInput) -
     422          18 :             adfInput.data());
     423             : 
     424          18 :         if (i == 0)
     425           6 :             return afdOutput[0];
     426             : 
     427             :         // If the index is beyond the end of the LUT input array, the input
     428             :         // value is larger than all the values in the array.
     429          12 :         if (i == static_cast<int>(adfInput.size()))
     430           6 :             return afdOutput.back();
     431             : 
     432           6 :         if (adfInput[i] == dfInput)
     433           0 :             return afdOutput[i];
     434             : 
     435             :         // Otherwise, interpolate.
     436           6 :         return afdOutput[i - 1] + (dfInput - adfInput[i - 1]) *
     437           6 :                                       ((afdOutput[i] - afdOutput[i - 1]) /
     438           6 :                                        (adfInput[i] - adfInput[i - 1]));
     439             :     }
     440             : };
     441             : }  // namespace
     442             : 
     443             : /************************************************************************/
     444             : /*                                LUTInit()                             */
     445             : /************************************************************************/
     446             : 
     447             : /** Init function for 'LUT' builtin function. */
     448           6 : static CPLErr LUTInit(const char * /*pszFuncName*/, void * /*pUserData*/,
     449             :                       CSLConstList papszFunctionArgs, int nInBands,
     450             :                       GDALDataType eInDT, double *padfInNoData, int *pnOutBands,
     451             :                       GDALDataType *peOutDT, double **ppadfOutNoData,
     452             :                       const char * /* pszVRTPath */,
     453             :                       VRTPDWorkingDataPtr *ppWorkingData)
     454             : {
     455           6 :     CPLAssert(eInDT == GDT_Float64);
     456             : 
     457           6 :     const bool bIsFinalStep = *pnOutBands != 0;
     458           6 :     *peOutDT = eInDT;
     459           6 :     *ppWorkingData = nullptr;
     460             : 
     461           6 :     if (!bIsFinalStep)
     462             :     {
     463           0 :         *pnOutBands = nInBands;
     464             :     }
     465             : 
     466          12 :     auto data = std::make_unique<LUTData>();
     467             : 
     468           6 :     double dfSrcNoData = std::numeric_limits<double>::quiet_NaN();
     469           6 :     bool bSrcNodataSpecified = false;
     470           6 :     double dfDstNoData = std::numeric_limits<double>::quiet_NaN();
     471           6 :     bool bDstNodataSpecified = false;
     472             : 
     473          12 :     std::map<int, std::pair<std::vector<double>, std::vector<double>>> oMap{};
     474             : 
     475          22 :     for (const auto &[pszKey, pszValue] :
     476          26 :          cpl::IterateNameValue(papszFunctionArgs))
     477             :     {
     478          12 :         if (EQUAL(pszKey, "src_nodata"))
     479             :         {
     480           1 :             bSrcNodataSpecified = true;
     481           1 :             dfSrcNoData = CPLAtof(pszValue);
     482             :         }
     483          11 :         else if (EQUAL(pszKey, "dst_nodata"))
     484             :         {
     485           1 :             bDstNodataSpecified = true;
     486           1 :             dfDstNoData = CPLAtof(pszValue);
     487             :         }
     488          10 :         else if (STARTS_WITH_CI(pszKey, "lut_"))
     489             :         {
     490          10 :             const int nBand = atoi(pszKey + strlen("lut_"));
     491          10 :             if (nBand <= 0 || nBand > nInBands)
     492             :             {
     493           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     494             :                          "Invalid band in argument '%s'", pszKey);
     495           2 :                 return CE_Failure;
     496             :             }
     497           9 :             const CPLStringList aosTokens(CSLTokenizeString2(pszValue, ",", 0));
     498           9 :             std::vector<double> adfInputValues;
     499           9 :             std::vector<double> adfOutputValues;
     500          26 :             for (int i = 0; i < aosTokens.size(); ++i)
     501             :             {
     502             :                 const CPLStringList aosTokens2(
     503          18 :                     CSLTokenizeString2(aosTokens[i], ":", 0));
     504          18 :                 if (aosTokens2.size() != 2)
     505             :                 {
     506           1 :                     CPLError(CE_Failure, CPLE_AppDefined,
     507             :                              "Invalid value for argument '%s'", pszKey);
     508           1 :                     return CE_Failure;
     509             :                 }
     510          17 :                 adfInputValues.push_back(CPLAtof(aosTokens2[0]));
     511          17 :                 adfOutputValues.push_back(CPLAtof(aosTokens2[1]));
     512             :             }
     513          16 :             oMap[nBand - 1] = std::pair(std::move(adfInputValues),
     514          16 :                                         std::move(adfOutputValues));
     515             :         }
     516             :         else
     517             :         {
     518           0 :             CPLError(CE_Warning, CPLE_AppDefined,
     519             :                      "Unrecognized argument name %s. Ignored", pszKey);
     520             :         }
     521             :     }
     522             : 
     523           4 :     SetOutputValuesForInNoDataAndOutNoData(
     524             :         nInBands, padfInNoData, pnOutBands, ppadfOutNoData, bSrcNodataSpecified,
     525             :         dfSrcNoData, bDstNodataSpecified, dfDstNoData, bIsFinalStep);
     526             : 
     527           4 :     int iExpected = 0;
     528             :     // Check we have values for all bands and convert to vector
     529          11 :     for (auto &oIter : oMap)
     530             :     {
     531           7 :         if (oIter.first != iExpected)
     532             :         {
     533           0 :             CPLError(CE_Failure, CPLE_AppDefined, "Argument lut_%d is missing",
     534             :                      iExpected + 1);
     535           0 :             return CE_Failure;
     536             :         }
     537           7 :         ++iExpected;
     538           7 :         data->m_aadfLUTInputs.emplace_back(std::move(oIter.second.first));
     539           7 :         data->m_aadfLUTOutputs.emplace_back(std::move(oIter.second.second));
     540             :     }
     541             : 
     542           4 :     if (static_cast<int>(oMap.size()) < *pnOutBands)
     543             :     {
     544           1 :         CPLError(CE_Failure, CPLE_AppDefined, "Missing lut_XX element(s)");
     545           1 :         return CE_Failure;
     546             :     }
     547             : 
     548           3 :     *ppWorkingData = data.release();
     549           3 :     return CE_None;
     550             : }
     551             : 
     552             : /************************************************************************/
     553             : /*                                LUTFree()                             */
     554             : /************************************************************************/
     555             : 
     556             : /** Free function for 'LUT' builtin function. */
     557           3 : static void LUTFree(const char * /*pszFuncName*/, void * /*pUserData*/,
     558             :                     VRTPDWorkingDataPtr pWorkingData)
     559             : {
     560           3 :     LUTData *data = static_cast<LUTData *>(pWorkingData);
     561           3 :     CPLAssert(data->m_osSignature == LUTData::EXPECTED_SIGNATURE);
     562           3 :     CPL_IGNORE_RET_VAL(data->m_osSignature);
     563           3 :     delete data;
     564           3 : }
     565             : 
     566             : /************************************************************************/
     567             : /*                             LUTProcess()                             */
     568             : /************************************************************************/
     569             : 
     570             : /** Processing function for 'LUT' builtin function. */
     571             : static CPLErr
     572           3 : LUTProcess(const char * /*pszFuncName*/, void * /*pUserData*/,
     573             :            VRTPDWorkingDataPtr pWorkingData,
     574             :            CSLConstList /* papszFunctionArgs*/, int nBufXSize, int nBufYSize,
     575             :            const void *pInBuffer, size_t nInBufferSize, GDALDataType eInDT,
     576             :            int nInBands, const double *CPL_RESTRICT padfInNoData,
     577             :            void *pOutBuffer, size_t nOutBufferSize, GDALDataType eOutDT,
     578             :            int nOutBands, const double *CPL_RESTRICT padfOutNoData,
     579             :            double /*dfSrcXOff*/, double /*dfSrcYOff*/, double /*dfSrcXSize*/,
     580             :            double /*dfSrcYSize*/, const double /*adfSrcGT*/[],
     581             :            const char * /* pszVRTPath */, CSLConstList /*papszExtra*/)
     582             : {
     583           3 :     const size_t nElts = static_cast<size_t>(nBufXSize) * nBufYSize;
     584             : 
     585           3 :     CPL_IGNORE_RET_VAL(eInDT);
     586           3 :     CPLAssert(eInDT == GDT_Float64);
     587           3 :     CPL_IGNORE_RET_VAL(eOutDT);
     588           3 :     CPLAssert(eOutDT == GDT_Float64);
     589           3 :     CPL_IGNORE_RET_VAL(nInBufferSize);
     590           3 :     CPLAssert(nInBufferSize == nElts * nInBands * sizeof(double));
     591           3 :     CPL_IGNORE_RET_VAL(nOutBufferSize);
     592           3 :     CPLAssert(nOutBufferSize == nElts * nOutBands * sizeof(double));
     593           3 :     CPLAssert(nInBands == nOutBands);
     594           3 :     CPL_IGNORE_RET_VAL(nOutBands);
     595             : 
     596           3 :     const LUTData *data = static_cast<LUTData *>(pWorkingData);
     597           3 :     CPLAssert(data->m_osSignature == LUTData::EXPECTED_SIGNATURE);
     598           3 :     const double *CPL_RESTRICT padfSrc = static_cast<const double *>(pInBuffer);
     599           3 :     double *CPL_RESTRICT padfDst = static_cast<double *>(pOutBuffer);
     600          14 :     for (size_t i = 0; i < nElts; ++i)
     601             :     {
     602          33 :         for (int iBand = 0; iBand < nInBands; ++iBand)
     603             :         {
     604             :             // written this way to work with a NaN value
     605          22 :             if (!(*padfSrc != padfInNoData[iBand]))
     606           4 :                 *padfDst = padfOutNoData[iBand];
     607             :             else
     608          18 :                 *padfDst = data->LookupValue(iBand, *padfSrc);
     609          22 :             ++padfSrc;
     610          22 :             ++padfDst;
     611             :         }
     612             :     }
     613             : 
     614           3 :     return CE_None;
     615             : }
     616             : 
     617             : /************************************************************************/
     618             : /*                        LocalScaleOffsetData                          */
     619             : /************************************************************************/
     620             : 
     621             : namespace
     622             : {
     623             : /** Working structure for 'LocalScaleOffset' builtin function. */
     624             : struct LocalScaleOffsetData
     625             : {
     626             :     static constexpr const char *const EXPECTED_SIGNATURE = "LocalScaleOffset";
     627             :     //! Signature (to make sure callback functions are called with the right argument)
     628             :     const std::string m_osSignature = EXPECTED_SIGNATURE;
     629             : 
     630             :     //! Nodata value for gain dataset(s)
     631             :     double m_dfGainNodata = std::numeric_limits<double>::quiet_NaN();
     632             : 
     633             :     //! Nodata value for offset dataset(s)
     634             :     double m_dfOffsetNodata = std::numeric_limits<double>::quiet_NaN();
     635             : 
     636             :     //! Minimum clamping value.
     637             :     double m_dfClampMin = std::numeric_limits<double>::quiet_NaN();
     638             : 
     639             :     //! Maximum clamping value.
     640             :     double m_dfClampMax = std::numeric_limits<double>::quiet_NaN();
     641             : 
     642             :     //! Map from gain/offset dataset name to datasets
     643             :     std::map<std::string, std::unique_ptr<GDALDataset>> m_oDatasetMap{};
     644             : 
     645             :     //! Vector of size nInBands that point to the raster band from which to read gains.
     646             :     std::vector<GDALRasterBand *> m_oGainBands{};
     647             : 
     648             :     //! Vector of size nInBands that point to the raster band from which to read offsets.
     649             :     std::vector<GDALRasterBand *> m_oOffsetBands{};
     650             : 
     651             :     //! Working buffer that contain gain values.
     652             :     std::vector<VRTProcessedDataset::NoInitByte> m_abyGainBuffer{};
     653             : 
     654             :     //! Working buffer that contain offset values.
     655             :     std::vector<VRTProcessedDataset::NoInitByte> m_abyOffsetBuffer{};
     656             : };
     657             : }  // namespace
     658             : 
     659             : /************************************************************************/
     660             : /*                           CheckAllBands()                            */
     661             : /************************************************************************/
     662             : 
     663             : /** Return true if the key of oMap is the sequence of all integers between
     664             :  * 0 and nExpectedBandCount-1.
     665             :  */
     666             : template <class T>
     667          28 : static bool CheckAllBands(const std::map<int, T> &oMap, int nExpectedBandCount)
     668             : {
     669          28 :     int iExpected = 0;
     670          60 :     for (const auto &kv : oMap)
     671             :     {
     672          32 :         if (kv.first != iExpected)
     673           0 :             return false;
     674          32 :         ++iExpected;
     675             :     }
     676          28 :     return iExpected == nExpectedBandCount;
     677             : }
     678             : 
     679             : /************************************************************************/
     680             : /*                        LocalScaleOffsetInit()                        */
     681             : /************************************************************************/
     682             : 
     683             : /** Init function for 'LocalScaleOffset' builtin function. */
     684             : static CPLErr
     685          11 : LocalScaleOffsetInit(const char * /*pszFuncName*/, void * /*pUserData*/,
     686             :                      CSLConstList papszFunctionArgs, int nInBands,
     687             :                      GDALDataType eInDT, double *padfInNoData, int *pnOutBands,
     688             :                      GDALDataType *peOutDT, double **ppadfOutNoData,
     689             :                      const char *pszVRTPath, VRTPDWorkingDataPtr *ppWorkingData)
     690             : {
     691          11 :     CPLAssert(eInDT == GDT_Float64);
     692             : 
     693          11 :     const bool bIsFinalStep = *pnOutBands != 0;
     694          11 :     *peOutDT = eInDT;
     695          11 :     *ppWorkingData = nullptr;
     696             : 
     697          11 :     if (!bIsFinalStep)
     698             :     {
     699           0 :         *pnOutBands = nInBands;
     700             :     }
     701             : 
     702          22 :     auto data = std::make_unique<LocalScaleOffsetData>();
     703             : 
     704          11 :     bool bNodataSpecified = false;
     705          11 :     double dfNoData = std::numeric_limits<double>::quiet_NaN();
     706             : 
     707          11 :     bool bGainNodataSpecified = false;
     708          11 :     bool bOffsetNodataSpecified = false;
     709             : 
     710          22 :     std::map<int, std::string> oGainDatasetNameMap;
     711          22 :     std::map<int, int> oGainDatasetBandMap;
     712             : 
     713          22 :     std::map<int, std::string> oOffsetDatasetNameMap;
     714          22 :     std::map<int, int> oOffsetDatasetBandMap;
     715             : 
     716          11 :     bool bRelativeToVRT = false;
     717             : 
     718          84 :     for (const auto &[pszKey, pszValue] :
     719          91 :          cpl::IterateNameValue(papszFunctionArgs))
     720             :     {
     721          44 :         if (EQUAL(pszKey, "relativeToVRT"))
     722             :         {
     723           0 :             bRelativeToVRT = CPLTestBool(pszValue);
     724             :         }
     725          44 :         else if (EQUAL(pszKey, "nodata"))
     726             :         {
     727           0 :             bNodataSpecified = true;
     728           0 :             dfNoData = CPLAtof(pszValue);
     729             :         }
     730          44 :         else if (EQUAL(pszKey, "gain_nodata"))
     731             :         {
     732           0 :             bGainNodataSpecified = true;
     733           0 :             data->m_dfGainNodata = CPLAtof(pszValue);
     734             :         }
     735          44 :         else if (EQUAL(pszKey, "offset_nodata"))
     736             :         {
     737           0 :             bOffsetNodataSpecified = true;
     738           0 :             data->m_dfOffsetNodata = CPLAtof(pszValue);
     739             :         }
     740          44 :         else if (STARTS_WITH_CI(pszKey, "gain_dataset_filename_"))
     741             :         {
     742          12 :             const int nBand = atoi(pszKey + strlen("gain_dataset_filename_"));
     743          12 :             if (nBand <= 0 || nBand > nInBands)
     744             :             {
     745           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     746             :                          "Invalid band in argument '%s'", pszKey);
     747           4 :                 return CE_Failure;
     748             :             }
     749          11 :             oGainDatasetNameMap[nBand - 1] = pszValue;
     750             :         }
     751          32 :         else if (STARTS_WITH_CI(pszKey, "gain_dataset_band_"))
     752             :         {
     753          11 :             const int nBand = atoi(pszKey + strlen("gain_dataset_band_"));
     754          11 :             if (nBand <= 0 || nBand > nInBands)
     755             :             {
     756           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     757             :                          "Invalid band in argument '%s'", pszKey);
     758           1 :                 return CE_Failure;
     759             :             }
     760          10 :             oGainDatasetBandMap[nBand - 1] = atoi(pszValue);
     761             :         }
     762          21 :         else if (STARTS_WITH_CI(pszKey, "offset_dataset_filename_"))
     763             :         {
     764          10 :             const int nBand = atoi(pszKey + strlen("offset_dataset_filename_"));
     765          10 :             if (nBand <= 0 || nBand > nInBands)
     766             :             {
     767           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     768             :                          "Invalid band in argument '%s'", pszKey);
     769           1 :                 return CE_Failure;
     770             :             }
     771           9 :             oOffsetDatasetNameMap[nBand - 1] = pszValue;
     772             :         }
     773          11 :         else if (STARTS_WITH_CI(pszKey, "offset_dataset_band_"))
     774             :         {
     775           9 :             const int nBand = atoi(pszKey + strlen("offset_dataset_band_"));
     776           9 :             if (nBand <= 0 || nBand > nInBands)
     777             :             {
     778           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     779             :                          "Invalid band in argument '%s'", pszKey);
     780           1 :                 return CE_Failure;
     781             :             }
     782           8 :             oOffsetDatasetBandMap[nBand - 1] = atoi(pszValue);
     783             :         }
     784           2 :         else if (EQUAL(pszKey, "min"))
     785             :         {
     786           1 :             data->m_dfClampMin = CPLAtof(pszValue);
     787             :         }
     788           1 :         else if (EQUAL(pszKey, "max"))
     789             :         {
     790           1 :             data->m_dfClampMax = CPLAtof(pszValue);
     791             :         }
     792             :         else
     793             :         {
     794           0 :             CPLError(CE_Warning, CPLE_AppDefined,
     795             :                      "Unrecognized argument name %s. Ignored", pszKey);
     796             :         }
     797             :     }
     798             : 
     799           7 :     if (!CheckAllBands(oGainDatasetNameMap, nInBands))
     800             :     {
     801           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     802             :                  "Missing gain_dataset_filename_XX element(s)");
     803           0 :         return CE_Failure;
     804             :     }
     805           7 :     if (!CheckAllBands(oGainDatasetBandMap, nInBands))
     806             :     {
     807           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     808             :                  "Missing gain_dataset_band_XX element(s)");
     809           0 :         return CE_Failure;
     810             :     }
     811           7 :     if (!CheckAllBands(oOffsetDatasetNameMap, nInBands))
     812             :     {
     813           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     814             :                  "Missing offset_dataset_filename_XX element(s)");
     815           0 :         return CE_Failure;
     816             :     }
     817           7 :     if (!CheckAllBands(oOffsetDatasetBandMap, nInBands))
     818             :     {
     819           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     820             :                  "Missing offset_dataset_band_XX element(s)");
     821           0 :         return CE_Failure;
     822             :     }
     823             : 
     824           7 :     data->m_oGainBands.resize(nInBands);
     825           7 :     data->m_oOffsetBands.resize(nInBands);
     826             : 
     827           7 :     constexpr int IDX_GAIN = 0;
     828           7 :     constexpr int IDX_OFFSET = 1;
     829          15 :     for (int i : {IDX_GAIN, IDX_OFFSET})
     830             :     {
     831          11 :         const auto &oMapNames =
     832             :             (i == IDX_GAIN) ? oGainDatasetNameMap : oOffsetDatasetNameMap;
     833          11 :         const auto &oMapBands =
     834             :             (i == IDX_GAIN) ? oGainDatasetBandMap : oOffsetDatasetBandMap;
     835          21 :         for (const auto &kv : oMapNames)
     836             :         {
     837          13 :             const int nInBandIdx = kv.first;
     838             :             const auto osFilename = GDALDataset::BuildFilename(
     839          13 :                 kv.second.c_str(), pszVRTPath, bRelativeToVRT);
     840          13 :             auto oIter = data->m_oDatasetMap.find(osFilename);
     841          13 :             if (oIter == data->m_oDatasetMap.end())
     842             :             {
     843             :                 auto poDS = std::unique_ptr<GDALDataset>(GDALDataset::Open(
     844             :                     osFilename.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
     845          11 :                     nullptr, nullptr, nullptr));
     846          11 :                 if (!poDS)
     847           1 :                     return CE_Failure;
     848          10 :                 GDALGeoTransform auxGT;
     849          10 :                 if (poDS->GetGeoTransform(auxGT) != CE_None)
     850             :                 {
     851           1 :                     CPLError(CE_Failure, CPLE_AppDefined,
     852             :                              "%s lacks a geotransform", osFilename.c_str());
     853           1 :                     return CE_Failure;
     854             :                 }
     855           9 :                 oIter = data->m_oDatasetMap
     856           9 :                             .insert(std::pair(osFilename, std::move(poDS)))
     857             :                             .first;
     858             :             }
     859          11 :             auto poDS = oIter->second.get();
     860          11 :             const auto oIterBand = oMapBands.find(nInBandIdx);
     861          11 :             CPLAssert(oIterBand != oMapBands.end());
     862          11 :             const int nAuxBand = oIterBand->second;
     863          11 :             if (nAuxBand <= 0 || nAuxBand > poDS->GetRasterCount())
     864             :             {
     865           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
     866             :                          "Invalid band number (%d) for a %s dataset", nAuxBand,
     867             :                          (i == IDX_GAIN) ? "gain" : "offset");
     868           1 :                 return CE_Failure;
     869             :             }
     870          10 :             auto poAuxBand = poDS->GetRasterBand(nAuxBand);
     871          10 :             int bAuxBandHasNoData = false;
     872             :             const double dfAuxNoData =
     873          10 :                 poAuxBand->GetNoDataValue(&bAuxBandHasNoData);
     874          10 :             if (i == IDX_GAIN)
     875             :             {
     876           5 :                 data->m_oGainBands[nInBandIdx] = poAuxBand;
     877           5 :                 if (!bGainNodataSpecified && bAuxBandHasNoData)
     878           2 :                     data->m_dfGainNodata = dfAuxNoData;
     879             :             }
     880             :             else
     881             :             {
     882           5 :                 data->m_oOffsetBands[nInBandIdx] = poAuxBand;
     883           5 :                 if (!bOffsetNodataSpecified && bAuxBandHasNoData)
     884           2 :                     data->m_dfOffsetNodata = dfAuxNoData;
     885             :             }
     886             :         }
     887             :     }
     888             : 
     889           4 :     SetOutputValuesForInNoDataAndOutNoData(
     890             :         nInBands, padfInNoData, pnOutBands, ppadfOutNoData, bNodataSpecified,
     891             :         dfNoData, bNodataSpecified, dfNoData, bIsFinalStep);
     892             : 
     893           4 :     *ppWorkingData = data.release();
     894           4 :     return CE_None;
     895             : }
     896             : 
     897             : /************************************************************************/
     898             : /*                       LocalScaleOffsetFree()                         */
     899             : /************************************************************************/
     900             : 
     901             : /** Free function for 'LocalScaleOffset' builtin function. */
     902           4 : static void LocalScaleOffsetFree(const char * /*pszFuncName*/,
     903             :                                  void * /*pUserData*/,
     904             :                                  VRTPDWorkingDataPtr pWorkingData)
     905             : {
     906           4 :     LocalScaleOffsetData *data =
     907             :         static_cast<LocalScaleOffsetData *>(pWorkingData);
     908           4 :     CPLAssert(data->m_osSignature == LocalScaleOffsetData::EXPECTED_SIGNATURE);
     909           4 :     CPL_IGNORE_RET_VAL(data->m_osSignature);
     910           4 :     delete data;
     911           4 : }
     912             : 
     913             : /************************************************************************/
     914             : /*                          LoadAuxData()                               */
     915             : /************************************************************************/
     916             : 
     917             : // Load auxiliary corresponding offset, gain or trimming data.
     918          17 : static bool LoadAuxData(double dfULX, double dfULY, double dfLRX, double dfLRY,
     919             :                         size_t nElts, int nBufXSize, int nBufYSize,
     920             :                         const char *pszAuxType, GDALRasterBand *poAuxBand,
     921             :                         std::vector<VRTProcessedDataset::NoInitByte> &abyBuffer)
     922             : {
     923          17 :     GDALGeoTransform auxGT, auxInvGT;
     924             : 
     925             :     // Compute pixel/line coordinates from the georeferenced extent
     926          34 :     CPL_IGNORE_RET_VAL(poAuxBand->GetDataset()->GetGeoTransform(
     927          17 :         auxGT));  // return code already tested
     928          17 :     CPL_IGNORE_RET_VAL(auxGT.GetInverse(auxInvGT));
     929             :     const double dfULPixel =
     930          17 :         auxInvGT[0] + auxInvGT[1] * dfULX + auxInvGT[2] * dfULY;
     931             :     const double dfULLine =
     932          17 :         auxInvGT[3] + auxInvGT[4] * dfULX + auxInvGT[5] * dfULY;
     933             :     const double dfLRPixel =
     934          17 :         auxInvGT[0] + auxInvGT[1] * dfLRX + auxInvGT[2] * dfLRY;
     935             :     const double dfLRLine =
     936          17 :         auxInvGT[3] + auxInvGT[4] * dfLRX + auxInvGT[5] * dfLRY;
     937          17 :     if (dfULPixel >= dfLRPixel || dfULLine >= dfLRLine)
     938             :     {
     939           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     940             :                  "Unexpected computed %s pixel/line", pszAuxType);
     941           0 :         return false;
     942             :     }
     943          17 :     if (dfULPixel < -1 || dfULLine < -1)
     944             :     {
     945           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     946             :                  "Unexpected computed %s upper left (pixel,line)=(%f,%f)",
     947             :                  pszAuxType, dfULPixel, dfULLine);
     948           0 :         return false;
     949             :     }
     950          34 :     if (dfLRPixel > poAuxBand->GetXSize() + 1 ||
     951          17 :         dfLRLine > poAuxBand->GetYSize() + 1)
     952             :     {
     953           0 :         CPLError(CE_Failure, CPLE_AppDefined,
     954             :                  "Unexpected computed %s lower right (pixel,line)=(%f,%f)",
     955             :                  pszAuxType, dfLRPixel, dfLRLine);
     956           0 :         return false;
     957             :     }
     958             : 
     959          34 :     const int nAuxXOff = std::clamp(static_cast<int>(std::round(dfULPixel)), 0,
     960          17 :                                     poAuxBand->GetXSize() - 1);
     961          34 :     const int nAuxYOff = std::clamp(static_cast<int>(std::round(dfULLine)), 0,
     962          17 :                                     poAuxBand->GetYSize() - 1);
     963          51 :     const int nAuxX2Off = std::min(poAuxBand->GetXSize(),
     964          17 :                                    static_cast<int>(std::round(dfLRPixel)));
     965             :     const int nAuxY2Off =
     966          17 :         std::min(poAuxBand->GetYSize(), static_cast<int>(std::round(dfLRLine)));
     967             : 
     968             :     try
     969             :     {
     970          17 :         abyBuffer.resize(nElts * sizeof(float));
     971             :     }
     972           0 :     catch (const std::bad_alloc &)
     973             :     {
     974           0 :         CPLError(CE_Failure, CPLE_OutOfMemory,
     975             :                  "Out of memory allocating working buffer");
     976           0 :         return false;
     977             :     }
     978             :     GDALRasterIOExtraArg sExtraArg;
     979          17 :     INIT_RASTERIO_EXTRA_ARG(sExtraArg);
     980          17 :     sExtraArg.bFloatingPointWindowValidity = true;
     981          17 :     CPL_IGNORE_RET_VAL(sExtraArg.eResampleAlg);
     982          17 :     sExtraArg.eResampleAlg = GRIORA_Bilinear;
     983          17 :     sExtraArg.dfXOff = std::max(0.0, dfULPixel);
     984          17 :     sExtraArg.dfYOff = std::max(0.0, dfULLine);
     985          17 :     sExtraArg.dfXSize = std::min<double>(poAuxBand->GetXSize(), dfLRPixel) -
     986          17 :                         std::max(0.0, dfULPixel);
     987          17 :     sExtraArg.dfYSize = std::min<double>(poAuxBand->GetYSize(), dfLRLine) -
     988          17 :                         std::max(0.0, dfULLine);
     989          17 :     return (poAuxBand->RasterIO(
     990          17 :                 GF_Read, nAuxXOff, nAuxYOff, std::max(1, nAuxX2Off - nAuxXOff),
     991          17 :                 std::max(1, nAuxY2Off - nAuxYOff), abyBuffer.data(), nBufXSize,
     992          17 :                 nBufYSize, GDT_Float32, 0, 0, &sExtraArg) == CE_None);
     993             : }
     994             : 
     995             : /************************************************************************/
     996             : /*                      LocalScaleOffsetProcess()                       */
     997             : /************************************************************************/
     998             : 
     999             : /** Processing function for 'LocalScaleOffset' builtin function. */
    1000           7 : static CPLErr LocalScaleOffsetProcess(
    1001             :     const char * /*pszFuncName*/, void * /*pUserData*/,
    1002             :     VRTPDWorkingDataPtr pWorkingData, CSLConstList /* papszFunctionArgs*/,
    1003             :     int nBufXSize, int nBufYSize, const void *pInBuffer, size_t nInBufferSize,
    1004             :     GDALDataType eInDT, int nInBands, const double *CPL_RESTRICT padfInNoData,
    1005             :     void *pOutBuffer, size_t nOutBufferSize, GDALDataType eOutDT, int nOutBands,
    1006             :     const double *CPL_RESTRICT padfOutNoData, double dfSrcXOff,
    1007             :     double dfSrcYOff, double dfSrcXSize, double dfSrcYSize,
    1008             :     const double adfSrcGT[], const char * /* pszVRTPath */,
    1009             :     CSLConstList /*papszExtra*/)
    1010             : {
    1011           7 :     const size_t nElts = static_cast<size_t>(nBufXSize) * nBufYSize;
    1012             : 
    1013           7 :     CPL_IGNORE_RET_VAL(eInDT);
    1014           7 :     CPLAssert(eInDT == GDT_Float64);
    1015           7 :     CPL_IGNORE_RET_VAL(eOutDT);
    1016           7 :     CPLAssert(eOutDT == GDT_Float64);
    1017           7 :     CPL_IGNORE_RET_VAL(nInBufferSize);
    1018           7 :     CPLAssert(nInBufferSize == nElts * nInBands * sizeof(double));
    1019           7 :     CPL_IGNORE_RET_VAL(nOutBufferSize);
    1020           7 :     CPLAssert(nOutBufferSize == nElts * nOutBands * sizeof(double));
    1021           7 :     CPLAssert(nInBands == nOutBands);
    1022           7 :     CPL_IGNORE_RET_VAL(nOutBands);
    1023             : 
    1024           7 :     LocalScaleOffsetData *data =
    1025             :         static_cast<LocalScaleOffsetData *>(pWorkingData);
    1026           7 :     CPLAssert(data->m_osSignature == LocalScaleOffsetData::EXPECTED_SIGNATURE);
    1027           7 :     const double *CPL_RESTRICT padfSrc = static_cast<const double *>(pInBuffer);
    1028           7 :     double *CPL_RESTRICT padfDst = static_cast<double *>(pOutBuffer);
    1029             : 
    1030             :     // Compute georeferenced extent of input region
    1031           7 :     const double dfULX =
    1032           7 :         adfSrcGT[0] + adfSrcGT[1] * dfSrcXOff + adfSrcGT[2] * dfSrcYOff;
    1033           7 :     const double dfULY =
    1034           7 :         adfSrcGT[3] + adfSrcGT[4] * dfSrcXOff + adfSrcGT[5] * dfSrcYOff;
    1035           7 :     const double dfLRX = adfSrcGT[0] + adfSrcGT[1] * (dfSrcXOff + dfSrcXSize) +
    1036           7 :                          adfSrcGT[2] * (dfSrcYOff + dfSrcYSize);
    1037           7 :     const double dfLRY = adfSrcGT[3] + adfSrcGT[4] * (dfSrcXOff + dfSrcXSize) +
    1038           7 :                          adfSrcGT[5] * (dfSrcYOff + dfSrcYSize);
    1039             : 
    1040           7 :     auto &abyOffsetBuffer = data->m_abyGainBuffer;
    1041           7 :     auto &abyGainBuffer = data->m_abyOffsetBuffer;
    1042             : 
    1043          15 :     for (int iBand = 0; iBand < nInBands; ++iBand)
    1044             :     {
    1045           8 :         if (!LoadAuxData(dfULX, dfULY, dfLRX, dfLRY, nElts, nBufXSize,
    1046           8 :                          nBufYSize, "gain", data->m_oGainBands[iBand],
    1047          16 :                          abyGainBuffer) ||
    1048           8 :             !LoadAuxData(dfULX, dfULY, dfLRX, dfLRY, nElts, nBufXSize,
    1049           8 :                          nBufYSize, "offset", data->m_oOffsetBands[iBand],
    1050             :                          abyOffsetBuffer))
    1051             :         {
    1052           0 :             return CE_Failure;
    1053             :         }
    1054             : 
    1055           8 :         const double *CPL_RESTRICT padfSrcThisBand = padfSrc + iBand;
    1056           8 :         double *CPL_RESTRICT padfDstThisBand = padfDst + iBand;
    1057             :         const float *pafGain =
    1058           8 :             reinterpret_cast<const float *>(abyGainBuffer.data());
    1059             :         const float *pafOffset =
    1060           8 :             reinterpret_cast<const float *>(abyOffsetBuffer.data());
    1061           8 :         const double dfSrcNodata = padfInNoData[iBand];
    1062           8 :         const double dfDstNodata = padfOutNoData[iBand];
    1063           8 :         const double dfGainNodata = data->m_dfGainNodata;
    1064           8 :         const double dfOffsetNodata = data->m_dfOffsetNodata;
    1065           8 :         const double dfClampMin = data->m_dfClampMin;
    1066           8 :         const double dfClampMax = data->m_dfClampMax;
    1067       66084 :         for (size_t i = 0; i < nElts; ++i)
    1068             :         {
    1069       66076 :             const double dfSrcVal = *padfSrcThisBand;
    1070             :             // written this way to work with a NaN value
    1071       66076 :             if (!(dfSrcVal != dfSrcNodata))
    1072             :             {
    1073           2 :                 *padfDstThisBand = dfDstNodata;
    1074             :             }
    1075             :             else
    1076             :             {
    1077       66074 :                 const double dfGain = pafGain[i];
    1078       66074 :                 const double dfOffset = pafOffset[i];
    1079       66074 :                 if (!(dfGain != dfGainNodata) || !(dfOffset != dfOffsetNodata))
    1080             :                 {
    1081           4 :                     *padfDstThisBand = dfDstNodata;
    1082             :                 }
    1083             :                 else
    1084             :                 {
    1085       66070 :                     double dfUnscaled = dfSrcVal * dfGain - dfOffset;
    1086       66070 :                     if (dfUnscaled < dfClampMin)
    1087           2 :                         dfUnscaled = dfClampMin;
    1088       66070 :                     if (dfUnscaled > dfClampMax)
    1089           1 :                         dfUnscaled = dfClampMax;
    1090             : 
    1091       66070 :                     *padfDstThisBand = dfUnscaled;
    1092             :                 }
    1093             :             }
    1094       66076 :             padfSrcThisBand += nInBands;
    1095       66076 :             padfDstThisBand += nInBands;
    1096             :         }
    1097             :     }
    1098             : 
    1099           7 :     return CE_None;
    1100             : }
    1101             : 
    1102             : /************************************************************************/
    1103             : /*                           TrimmingData                               */
    1104             : /************************************************************************/
    1105             : 
    1106             : namespace
    1107             : {
    1108             : /** Working structure for 'Trimming' builtin function. */
    1109             : struct TrimmingData
    1110             : {
    1111             :     static constexpr const char *const EXPECTED_SIGNATURE = "Trimming";
    1112             :     //! Signature (to make sure callback functions are called with the right argument)
    1113             :     const std::string m_osSignature = EXPECTED_SIGNATURE;
    1114             : 
    1115             :     //! Nodata value for trimming dataset
    1116             :     double m_dfTrimmingNodata = std::numeric_limits<double>::quiet_NaN();
    1117             : 
    1118             :     //! Maximum saturating RGB output value.
    1119             :     double m_dfTopRGB = 0;
    1120             : 
    1121             :     //! Maximum threshold beyond which we give up saturation
    1122             :     double m_dfToneCeil = 0;
    1123             : 
    1124             :     //! Margin to allow for dynamics in brightest areas (in [0,1] range)
    1125             :     double m_dfTopMargin = 0;
    1126             : 
    1127             :     //! Index (zero-based) of input/output red band.
    1128             :     int m_nRedBand = 1 - 1;
    1129             : 
    1130             :     //! Index (zero-based) of input/output green band.
    1131             :     int m_nGreenBand = 2 - 1;
    1132             : 
    1133             :     //! Index (zero-based) of input/output blue band.
    1134             :     int m_nBlueBand = 3 - 1;
    1135             : 
    1136             :     //! Trimming dataset
    1137             :     std::unique_ptr<GDALDataset> m_poTrimmingDS{};
    1138             : 
    1139             :     //! Trimming raster band.
    1140             :     GDALRasterBand *m_poTrimmingBand = nullptr;
    1141             : 
    1142             :     //! Working buffer that contain trimming values.
    1143             :     std::vector<VRTProcessedDataset::NoInitByte> m_abyTrimmingBuffer{};
    1144             : };
    1145             : }  // namespace
    1146             : 
    1147             : /************************************************************************/
    1148             : /*                           TrimmingInit()                             */
    1149             : /************************************************************************/
    1150             : 
    1151             : /** Init function for 'Trimming' builtin function. */
    1152          12 : static CPLErr TrimmingInit(const char * /*pszFuncName*/, void * /*pUserData*/,
    1153             :                            CSLConstList papszFunctionArgs, int nInBands,
    1154             :                            GDALDataType eInDT, double *padfInNoData,
    1155             :                            int *pnOutBands, GDALDataType *peOutDT,
    1156             :                            double **ppadfOutNoData, const char *pszVRTPath,
    1157             :                            VRTPDWorkingDataPtr *ppWorkingData)
    1158             : {
    1159          12 :     CPLAssert(eInDT == GDT_Float64);
    1160             : 
    1161          12 :     const bool bIsFinalStep = *pnOutBands != 0;
    1162          12 :     *peOutDT = eInDT;
    1163          12 :     *ppWorkingData = nullptr;
    1164             : 
    1165          12 :     if (!bIsFinalStep)
    1166             :     {
    1167           0 :         *pnOutBands = nInBands;
    1168             :     }
    1169             : 
    1170          24 :     auto data = std::make_unique<TrimmingData>();
    1171             : 
    1172          12 :     bool bNodataSpecified = false;
    1173          12 :     double dfNoData = std::numeric_limits<double>::quiet_NaN();
    1174          24 :     std::string osTrimmingFilename;
    1175          12 :     bool bTrimmingNodataSpecified = false;
    1176          12 :     bool bRelativeToVRT = false;
    1177             : 
    1178          84 :     for (const auto &[pszKey, pszValue] :
    1179          90 :          cpl::IterateNameValue(papszFunctionArgs))
    1180             :     {
    1181          45 :         if (EQUAL(pszKey, "relativeToVRT"))
    1182             :         {
    1183           0 :             bRelativeToVRT = CPLTestBool(pszValue);
    1184             :         }
    1185          45 :         else if (EQUAL(pszKey, "nodata"))
    1186             :         {
    1187           0 :             bNodataSpecified = true;
    1188           0 :             dfNoData = CPLAtof(pszValue);
    1189             :         }
    1190          45 :         else if (EQUAL(pszKey, "trimming_nodata"))
    1191             :         {
    1192           0 :             bTrimmingNodataSpecified = true;
    1193           0 :             data->m_dfTrimmingNodata = CPLAtof(pszValue);
    1194             :         }
    1195          45 :         else if (EQUAL(pszKey, "trimming_dataset_filename"))
    1196             :         {
    1197          12 :             osTrimmingFilename = pszValue;
    1198             :         }
    1199          33 :         else if (EQUAL(pszKey, "red_band"))
    1200             :         {
    1201           5 :             const int nBand = atoi(pszValue) - 1;
    1202           5 :             if (nBand < 0 || nBand >= nInBands)
    1203             :             {
    1204           2 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1205             :                          "Invalid band in argument '%s'", pszKey);
    1206           6 :                 return CE_Failure;
    1207             :             }
    1208           3 :             data->m_nRedBand = nBand;
    1209             :         }
    1210          28 :         else if (EQUAL(pszKey, "green_band"))
    1211             :         {
    1212           5 :             const int nBand = atoi(pszValue) - 1;
    1213           5 :             if (nBand < 0 || nBand >= nInBands)
    1214             :             {
    1215           2 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1216             :                          "Invalid band in argument '%s'", pszKey);
    1217           2 :                 return CE_Failure;
    1218             :             }
    1219           3 :             data->m_nGreenBand = nBand;
    1220             :         }
    1221          23 :         else if (EQUAL(pszKey, "blue_band"))
    1222             :         {
    1223           5 :             const int nBand = atoi(pszValue) - 1;
    1224           5 :             if (nBand < 0 || nBand >= nInBands)
    1225             :             {
    1226           2 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1227             :                          "Invalid band in argument '%s'", pszKey);
    1228           2 :                 return CE_Failure;
    1229             :             }
    1230           3 :             data->m_nBlueBand = nBand;
    1231             :         }
    1232          18 :         else if (EQUAL(pszKey, "top_rgb"))
    1233             :         {
    1234           6 :             data->m_dfTopRGB = CPLAtof(pszValue);
    1235             :         }
    1236          12 :         else if (EQUAL(pszKey, "tone_ceil"))
    1237             :         {
    1238           6 :             data->m_dfToneCeil = CPLAtof(pszValue);
    1239             :         }
    1240           6 :         else if (EQUAL(pszKey, "top_margin"))
    1241             :         {
    1242           6 :             data->m_dfTopMargin = CPLAtof(pszValue);
    1243             :         }
    1244             :         else
    1245             :         {
    1246           0 :             CPLError(CE_Warning, CPLE_AppDefined,
    1247             :                      "Unrecognized argument name %s. Ignored", pszKey);
    1248             :         }
    1249             :     }
    1250             : 
    1251           6 :     if (data->m_nRedBand == data->m_nGreenBand ||
    1252          10 :         data->m_nRedBand == data->m_nBlueBand ||
    1253           4 :         data->m_nGreenBand == data->m_nBlueBand)
    1254             :     {
    1255           3 :         CPLError(
    1256             :             CE_Failure, CPLE_NotSupported,
    1257             :             "red_band, green_band and blue_band must have distinct values");
    1258           3 :         return CE_Failure;
    1259             :     }
    1260             : 
    1261             :     const auto osFilename = GDALDataset::BuildFilename(
    1262           6 :         osTrimmingFilename.c_str(), pszVRTPath, bRelativeToVRT);
    1263           3 :     data->m_poTrimmingDS.reset(GDALDataset::Open(
    1264             :         osFilename.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, nullptr,
    1265             :         nullptr, nullptr));
    1266           3 :     if (!data->m_poTrimmingDS)
    1267           1 :         return CE_Failure;
    1268           2 :     if (data->m_poTrimmingDS->GetRasterCount() != 1)
    1269             :     {
    1270           1 :         CPLError(CE_Failure, CPLE_NotSupported,
    1271             :                  "Trimming dataset should have a single band");
    1272           1 :         return CE_Failure;
    1273             :     }
    1274           1 :     data->m_poTrimmingBand = data->m_poTrimmingDS->GetRasterBand(1);
    1275             : 
    1276           1 :     GDALGeoTransform auxGT;
    1277           1 :     if (data->m_poTrimmingDS->GetGeoTransform(auxGT) != CE_None)
    1278             :     {
    1279           0 :         CPLError(CE_Failure, CPLE_AppDefined, "%s lacks a geotransform",
    1280             :                  osFilename.c_str());
    1281           0 :         return CE_Failure;
    1282             :     }
    1283           1 :     int bAuxBandHasNoData = false;
    1284             :     const double dfAuxNoData =
    1285           1 :         data->m_poTrimmingBand->GetNoDataValue(&bAuxBandHasNoData);
    1286           1 :     if (!bTrimmingNodataSpecified && bAuxBandHasNoData)
    1287           0 :         data->m_dfTrimmingNodata = dfAuxNoData;
    1288             : 
    1289           1 :     SetOutputValuesForInNoDataAndOutNoData(
    1290             :         nInBands, padfInNoData, pnOutBands, ppadfOutNoData, bNodataSpecified,
    1291             :         dfNoData, bNodataSpecified, dfNoData, bIsFinalStep);
    1292             : 
    1293           1 :     *ppWorkingData = data.release();
    1294           1 :     return CE_None;
    1295             : }
    1296             : 
    1297             : /************************************************************************/
    1298             : /*                           TrimmingFree()                             */
    1299             : /************************************************************************/
    1300             : 
    1301             : /** Free function for 'Trimming' builtin function. */
    1302           1 : static void TrimmingFree(const char * /*pszFuncName*/, void * /*pUserData*/,
    1303             :                          VRTPDWorkingDataPtr pWorkingData)
    1304             : {
    1305           1 :     TrimmingData *data = static_cast<TrimmingData *>(pWorkingData);
    1306           1 :     CPLAssert(data->m_osSignature == TrimmingData::EXPECTED_SIGNATURE);
    1307           1 :     CPL_IGNORE_RET_VAL(data->m_osSignature);
    1308           1 :     delete data;
    1309           1 : }
    1310             : 
    1311             : /************************************************************************/
    1312             : /*                         TrimmingProcess()                            */
    1313             : /************************************************************************/
    1314             : 
    1315             : /** Processing function for 'Trimming' builtin function. */
    1316           1 : static CPLErr TrimmingProcess(
    1317             :     const char * /*pszFuncName*/, void * /*pUserData*/,
    1318             :     VRTPDWorkingDataPtr pWorkingData, CSLConstList /* papszFunctionArgs*/,
    1319             :     int nBufXSize, int nBufYSize, const void *pInBuffer, size_t nInBufferSize,
    1320             :     GDALDataType eInDT, int nInBands, const double *CPL_RESTRICT padfInNoData,
    1321             :     void *pOutBuffer, size_t nOutBufferSize, GDALDataType eOutDT, int nOutBands,
    1322             :     const double *CPL_RESTRICT padfOutNoData, double dfSrcXOff,
    1323             :     double dfSrcYOff, double dfSrcXSize, double dfSrcYSize,
    1324             :     const double adfSrcGT[], const char * /* pszVRTPath */,
    1325             :     CSLConstList /*papszExtra*/)
    1326             : {
    1327           1 :     const size_t nElts = static_cast<size_t>(nBufXSize) * nBufYSize;
    1328             : 
    1329           1 :     CPL_IGNORE_RET_VAL(eInDT);
    1330           1 :     CPLAssert(eInDT == GDT_Float64);
    1331           1 :     CPL_IGNORE_RET_VAL(eOutDT);
    1332           1 :     CPLAssert(eOutDT == GDT_Float64);
    1333           1 :     CPL_IGNORE_RET_VAL(nInBufferSize);
    1334           1 :     CPLAssert(nInBufferSize == nElts * nInBands * sizeof(double));
    1335           1 :     CPL_IGNORE_RET_VAL(nOutBufferSize);
    1336           1 :     CPLAssert(nOutBufferSize == nElts * nOutBands * sizeof(double));
    1337           1 :     CPLAssert(nInBands == nOutBands);
    1338           1 :     CPL_IGNORE_RET_VAL(nOutBands);
    1339             : 
    1340           1 :     TrimmingData *data = static_cast<TrimmingData *>(pWorkingData);
    1341           1 :     CPLAssert(data->m_osSignature == TrimmingData::EXPECTED_SIGNATURE);
    1342           1 :     const double *CPL_RESTRICT padfSrc = static_cast<const double *>(pInBuffer);
    1343           1 :     double *CPL_RESTRICT padfDst = static_cast<double *>(pOutBuffer);
    1344             : 
    1345             :     // Compute georeferenced extent of input region
    1346           1 :     const double dfULX =
    1347           1 :         adfSrcGT[0] + adfSrcGT[1] * dfSrcXOff + adfSrcGT[2] * dfSrcYOff;
    1348           1 :     const double dfULY =
    1349           1 :         adfSrcGT[3] + adfSrcGT[4] * dfSrcXOff + adfSrcGT[5] * dfSrcYOff;
    1350           1 :     const double dfLRX = adfSrcGT[0] + adfSrcGT[1] * (dfSrcXOff + dfSrcXSize) +
    1351           1 :                          adfSrcGT[2] * (dfSrcYOff + dfSrcYSize);
    1352           1 :     const double dfLRY = adfSrcGT[3] + adfSrcGT[4] * (dfSrcXOff + dfSrcXSize) +
    1353           1 :                          adfSrcGT[5] * (dfSrcYOff + dfSrcYSize);
    1354             : 
    1355           1 :     if (!LoadAuxData(dfULX, dfULY, dfLRX, dfLRY, nElts, nBufXSize, nBufYSize,
    1356             :                      "trimming", data->m_poTrimmingBand,
    1357           1 :                      data->m_abyTrimmingBuffer))
    1358             :     {
    1359           0 :         return CE_Failure;
    1360             :     }
    1361             : 
    1362             :     const float *pafTrimming =
    1363           1 :         reinterpret_cast<const float *>(data->m_abyTrimmingBuffer.data());
    1364           1 :     const int nRedBand = data->m_nRedBand;
    1365           1 :     const int nGreenBand = data->m_nGreenBand;
    1366           1 :     const int nBlueBand = data->m_nBlueBand;
    1367           1 :     const double dfTopMargin = data->m_dfTopMargin;
    1368           1 :     const double dfTopRGB = data->m_dfTopRGB;
    1369           1 :     const double dfToneCeil = data->m_dfToneCeil;
    1370             : #if !defined(trimming_non_optimized_version)
    1371           1 :     const double dfInvToneCeil = 1.0 / dfToneCeil;
    1372             : #endif
    1373             :     const bool bRGBBandsAreFirst =
    1374           1 :         std::max(std::max(nRedBand, nGreenBand), nBlueBand) <= 2;
    1375           1 :     const double dfNoDataTrimming = data->m_dfTrimmingNodata;
    1376           1 :     const double dfNoDataRed = padfInNoData[nRedBand];
    1377           1 :     const double dfNoDataGreen = padfInNoData[nGreenBand];
    1378           1 :     const double dfNoDataBlue = padfInNoData[nBlueBand];
    1379           7 :     for (size_t i = 0; i < nElts; ++i)
    1380             :     {
    1381             :         // Extract local saturation value from trimming image
    1382           6 :         const double dfLocalMaxRGB = pafTrimming[i];
    1383             :         const double dfReducedRGB =
    1384           6 :             std::min((1.0 - dfTopMargin) * dfTopRGB / dfLocalMaxRGB, 1.0);
    1385             : 
    1386           6 :         const double dfRed = padfSrc[nRedBand];
    1387           6 :         const double dfGreen = padfSrc[nGreenBand];
    1388           6 :         const double dfBlue = padfSrc[nBlueBand];
    1389           6 :         bool bNoDataPixel = false;
    1390           6 :         if ((dfLocalMaxRGB != dfNoDataTrimming) && (dfRed != dfNoDataRed) &&
    1391           6 :             (dfGreen != dfNoDataGreen) && (dfBlue != dfNoDataBlue))
    1392             :         {
    1393             :             // RGB bands specific process
    1394           6 :             const double dfMaxRGB = std::max(std::max(dfRed, dfGreen), dfBlue);
    1395             : #if !defined(trimming_non_optimized_version)
    1396           6 :             const double dfRedTimesToneRed = std::min(dfRed, dfToneCeil);
    1397           6 :             const double dfGreenTimesToneGreen = std::min(dfGreen, dfToneCeil);
    1398           6 :             const double dfBlueTimesToneBlue = std::min(dfBlue, dfToneCeil);
    1399             :             const double dfInvToneMaxRGB =
    1400           6 :                 std::max(dfMaxRGB * dfInvToneCeil, 1.0);
    1401           6 :             const double dfReducedRGBTimesInvToneMaxRGB =
    1402             :                 dfReducedRGB * dfInvToneMaxRGB;
    1403           6 :             padfDst[nRedBand] = std::min(
    1404           6 :                 dfRedTimesToneRed * dfReducedRGBTimesInvToneMaxRGB, dfTopRGB);
    1405           6 :             padfDst[nGreenBand] =
    1406          12 :                 std::min(dfGreenTimesToneGreen * dfReducedRGBTimesInvToneMaxRGB,
    1407           6 :                          dfTopRGB);
    1408           6 :             padfDst[nBlueBand] = std::min(
    1409           6 :                 dfBlueTimesToneBlue * dfReducedRGBTimesInvToneMaxRGB, dfTopRGB);
    1410             : #else
    1411             :             // Original formulas. Slightly less optimized than the above ones.
    1412             :             const double dfToneMaxRGB = std::min(dfToneCeil / dfMaxRGB, 1.0);
    1413             :             const double dfToneRed = std::min(dfToneCeil / dfRed, 1.0);
    1414             :             const double dfToneGreen = std::min(dfToneCeil / dfGreen, 1.0);
    1415             :             const double dfToneBlue = std::min(dfToneCeil / dfBlue, 1.0);
    1416             :             padfDst[nRedBand] = std::min(
    1417             :                 dfReducedRGB * dfRed * dfToneRed / dfToneMaxRGB, dfTopRGB);
    1418             :             padfDst[nGreenBand] = std::min(
    1419             :                 dfReducedRGB * dfGreen * dfToneGreen / dfToneMaxRGB, dfTopRGB);
    1420             :             padfDst[nBlueBand] = std::min(
    1421             :                 dfReducedRGB * dfBlue * dfToneBlue / dfToneMaxRGB, dfTopRGB);
    1422             : #endif
    1423             : 
    1424             :             // Other bands processing (NIR, ...): only apply RGB reduction factor
    1425           6 :             if (bRGBBandsAreFirst)
    1426             :             {
    1427             :                 // optimization
    1428          12 :                 for (int iBand = 3; iBand < nInBands; ++iBand)
    1429             :                 {
    1430           6 :                     if (padfSrc[iBand] != padfInNoData[iBand])
    1431             :                     {
    1432           6 :                         padfDst[iBand] = dfReducedRGB * padfSrc[iBand];
    1433             :                     }
    1434             :                     else
    1435             :                     {
    1436           0 :                         bNoDataPixel = true;
    1437           0 :                         break;
    1438             :                     }
    1439             :                 }
    1440             :             }
    1441             :             else
    1442             :             {
    1443           0 :                 for (int iBand = 0; iBand < nInBands; ++iBand)
    1444             :                 {
    1445           0 :                     if (iBand != nRedBand && iBand != nGreenBand &&
    1446           0 :                         iBand != nBlueBand)
    1447             :                     {
    1448           0 :                         if (padfSrc[iBand] != padfInNoData[iBand])
    1449             :                         {
    1450           0 :                             padfDst[iBand] = dfReducedRGB * padfSrc[iBand];
    1451             :                         }
    1452             :                         else
    1453             :                         {
    1454           0 :                             bNoDataPixel = true;
    1455           0 :                             break;
    1456             :                         }
    1457             :                     }
    1458             :                 }
    1459           6 :             }
    1460             :         }
    1461             :         else
    1462             :         {
    1463           0 :             bNoDataPixel = true;
    1464             :         }
    1465           6 :         if (bNoDataPixel)
    1466             :         {
    1467           0 :             for (int iBand = 0; iBand < nInBands; ++iBand)
    1468             :             {
    1469           0 :                 padfDst[iBand] = padfOutNoData[iBand];
    1470             :             }
    1471             :         }
    1472             : 
    1473           6 :         padfSrc += nInBands;
    1474           6 :         padfDst += nInBands;
    1475             :     }
    1476             : 
    1477           1 :     return CE_None;
    1478             : }
    1479             : 
    1480             : /************************************************************************/
    1481             : /*                    ExpressionInit()                                  */
    1482             : /************************************************************************/
    1483             : 
    1484             : namespace
    1485             : {
    1486             : 
    1487             : class ExpressionData
    1488             : {
    1489             :   public:
    1490          19 :     ExpressionData(int nInBands, int nBatchSize, std::string_view osExpression,
    1491             :                    std::string_view osDialect)
    1492          19 :         : m_nInBands(nInBands), m_nNominalBatchSize(nBatchSize),
    1493          19 :           m_nBatchCount(DIV_ROUND_UP(nInBands, nBatchSize)), m_adfResults{},
    1494          38 :           m_osExpression(std::string(osExpression)),
    1495          38 :           m_osDialect(std::string(osDialect)), m_oNominalBatchEnv{},
    1496         114 :           m_oPartialBatchEnv{}
    1497             :     {
    1498          19 :     }
    1499             : 
    1500          19 :     CPLErr Compile()
    1501             :     {
    1502          38 :         auto eErr = m_oNominalBatchEnv.Initialize(m_osExpression, m_osDialect,
    1503          19 :                                                   m_nNominalBatchSize);
    1504          19 :         if (eErr != CE_None)
    1505             :         {
    1506           2 :             return eErr;
    1507             :         }
    1508             : 
    1509          17 :         const auto nPartialBatchSize = m_nInBands % m_nNominalBatchSize;
    1510          17 :         if (nPartialBatchSize)
    1511             :         {
    1512           1 :             eErr = m_oPartialBatchEnv.Initialize(m_osExpression, m_osDialect,
    1513             :                                                  nPartialBatchSize);
    1514             :         }
    1515             : 
    1516          17 :         return eErr;
    1517             :     }
    1518             : 
    1519          30 :     CPLErr Evaluate(const double *padfInputs, size_t nExpectedOutBands)
    1520             :     {
    1521          30 :         m_adfResults.clear();
    1522             : 
    1523          89 :         for (int iBatch = 0; iBatch < m_nBatchCount; iBatch++)
    1524             :         {
    1525          62 :             const auto nBandsRemaining =
    1526          62 :                 static_cast<int>(m_nInBands - (m_nNominalBatchSize * iBatch));
    1527             :             const auto nBatchSize =
    1528          62 :                 std::min(m_nNominalBatchSize, nBandsRemaining);
    1529             : 
    1530          62 :             auto &oEnv = GetEnv(nBatchSize);
    1531             : 
    1532          62 :             const double *pdfStart = padfInputs + iBatch * m_nNominalBatchSize;
    1533          62 :             const double *pdfEnd = pdfStart + nBatchSize;
    1534             : 
    1535          62 :             std::copy(pdfStart, pdfEnd, oEnv.m_adfValuesForPixel.begin());
    1536             : 
    1537          62 :             if (auto eErr = oEnv.m_poExpression->Evaluate(); eErr != CE_None)
    1538             :             {
    1539           3 :                 return eErr;
    1540             :             }
    1541             : 
    1542          59 :             const auto &adfResults = oEnv.m_poExpression->Results();
    1543          59 :             if (m_nBatchCount > 1)
    1544             :             {
    1545             :                 std::copy(adfResults.begin(), adfResults.end(),
    1546          38 :                           std::back_inserter(m_adfResults));
    1547             :             }
    1548             :         }
    1549             : 
    1550          27 :         if (nExpectedOutBands > 0)
    1551             :         {
    1552          23 :             if (Results().size() != static_cast<std::size_t>(nExpectedOutBands))
    1553             :             {
    1554           1 :                 CPLError(CE_Failure, CPLE_AppDefined,
    1555             :                          "Expression returned %d values but "
    1556             :                          "%d output bands were expected.",
    1557           1 :                          static_cast<int>(Results().size()),
    1558             :                          static_cast<int>(nExpectedOutBands));
    1559           1 :                 return CE_Failure;
    1560             :             }
    1561             :         }
    1562             : 
    1563          26 :         return CE_None;
    1564             :     }
    1565             : 
    1566          50 :     const std::vector<double> &Results() const
    1567             :     {
    1568          50 :         if (m_nBatchCount == 1)
    1569             :         {
    1570          41 :             return m_oNominalBatchEnv.m_poExpression->Results();
    1571             :         }
    1572             :         else
    1573             :         {
    1574           9 :             return m_adfResults;
    1575             :         }
    1576             :     }
    1577             : 
    1578             :   private:
    1579             :     const int m_nInBands;
    1580             :     const int m_nNominalBatchSize;
    1581             :     const int m_nBatchCount;
    1582             :     std::vector<double> m_adfResults;
    1583             : 
    1584             :     const CPLString m_osExpression;
    1585             :     const CPLString m_osDialect;
    1586             : 
    1587             :     struct InvocationEnv
    1588             :     {
    1589             :         std::vector<double> m_adfValuesForPixel;
    1590             :         std::unique_ptr<gdal::MathExpression> m_poExpression;
    1591             : 
    1592          20 :         CPLErr Initialize(const CPLString &osExpression,
    1593             :                           const CPLString &osDialect, int nBatchSize)
    1594             :         {
    1595             :             m_poExpression =
    1596          20 :                 gdal::MathExpression::Create(osExpression, osDialect.c_str());
    1597             :             // cppcheck-suppress knownConditionTrueFalse
    1598          20 :             if (m_poExpression == nullptr)
    1599             :             {
    1600           0 :                 return CE_Failure;
    1601             :             }
    1602             : 
    1603          20 :             m_adfValuesForPixel.resize(nBatchSize);
    1604             : 
    1605         131 :             for (int i = 0; i < nBatchSize; i++)
    1606             :             {
    1607         222 :                 std::string osVar = "B" + std::to_string(i + 1);
    1608         222 :                 m_poExpression->RegisterVariable(osVar,
    1609         111 :                                                  &m_adfValuesForPixel[i]);
    1610             :             }
    1611             : 
    1612          20 :             if (osExpression.ifind("BANDS") != std::string::npos)
    1613             :             {
    1614          11 :                 m_poExpression->RegisterVector("BANDS", &m_adfValuesForPixel);
    1615             :             }
    1616             : 
    1617          20 :             return m_poExpression->Compile();
    1618             :         }
    1619             :     };
    1620             : 
    1621          62 :     InvocationEnv &GetEnv(int nBatchSize)
    1622             :     {
    1623          62 :         if (nBatchSize == m_nNominalBatchSize)
    1624             :         {
    1625          60 :             return m_oNominalBatchEnv;
    1626             :         }
    1627             :         else
    1628             :         {
    1629           2 :             return m_oPartialBatchEnv;
    1630             :         }
    1631             :     }
    1632             : 
    1633             :     InvocationEnv m_oNominalBatchEnv;
    1634             :     InvocationEnv m_oPartialBatchEnv;
    1635             : };
    1636             : 
    1637             : }  // namespace
    1638             : 
    1639          19 : static CPLErr ExpressionInit(const char * /*pszFuncName*/, void * /*pUserData*/,
    1640             :                              CSLConstList papszFunctionArgs, int nInBands,
    1641             :                              GDALDataType eInDT, double * /* padfInNoData */,
    1642             :                              int *pnOutBands, GDALDataType *peOutDT,
    1643             :                              double ** /* ppadfOutNoData */,
    1644             :                              const char * /* pszVRTPath */,
    1645             :                              VRTPDWorkingDataPtr *ppWorkingData)
    1646             : {
    1647          19 :     CPLAssert(eInDT == GDT_Float64);
    1648             : 
    1649          19 :     *peOutDT = eInDT;
    1650          19 :     *ppWorkingData = nullptr;
    1651             : 
    1652             :     const char *pszBatchSize =
    1653          19 :         CSLFetchNameValue(papszFunctionArgs, "batch_size");
    1654          19 :     auto nBatchSize = nInBands;
    1655             : 
    1656          19 :     if (pszBatchSize != nullptr)
    1657             :     {
    1658           4 :         nBatchSize = std::min(nInBands, std::atoi(pszBatchSize));
    1659             :     }
    1660             : 
    1661          19 :     if (nBatchSize < 1)
    1662             :     {
    1663           0 :         CPLError(CE_Failure, CPLE_IllegalArg, "batch_size must be at least 1");
    1664           0 :         return CE_Failure;
    1665             :     }
    1666             : 
    1667          19 :     const char *pszDialect = CSLFetchNameValue(papszFunctionArgs, "dialect");
    1668          19 :     if (pszDialect == nullptr)
    1669             :     {
    1670           0 :         pszDialect = "muparser";
    1671             :     }
    1672             : 
    1673             :     const char *pszExpression =
    1674          19 :         CSLFetchNameValue(papszFunctionArgs, "expression");
    1675             : 
    1676             :     auto data = std::make_unique<ExpressionData>(nInBands, nBatchSize,
    1677          38 :                                                  pszExpression, pszDialect);
    1678             : 
    1679          19 :     if (auto eErr = data->Compile(); eErr != CE_None)
    1680             :     {
    1681           2 :         return eErr;
    1682             :     }
    1683             : 
    1684          17 :     if (*pnOutBands == 0)
    1685             :     {
    1686           4 :         std::vector<double> aDummyValues(nInBands);
    1687           4 :         if (auto eErr = data->Evaluate(aDummyValues.data(), 0); eErr != CE_None)
    1688             :         {
    1689           0 :             return eErr;
    1690             :         }
    1691             : 
    1692           4 :         *pnOutBands = static_cast<int>(data->Results().size());
    1693             :     }
    1694             : 
    1695          17 :     *ppWorkingData = data.release();
    1696             : 
    1697          17 :     return CE_None;
    1698             : }
    1699             : 
    1700          17 : static void ExpressionFree(const char * /* pszFuncName */,
    1701             :                            void * /* pUserData */,
    1702             :                            VRTPDWorkingDataPtr pWorkingData)
    1703             : {
    1704          17 :     ExpressionData *data = static_cast<ExpressionData *>(pWorkingData);
    1705          17 :     delete data;
    1706          17 : }
    1707             : 
    1708          17 : static CPLErr ExpressionProcess(
    1709             :     const char * /* pszFuncName */, void * /* pUserData */,
    1710             :     VRTPDWorkingDataPtr pWorkingData, CSLConstList /* papszFunctionArgs */,
    1711             :     int nBufXSize, int nBufYSize, const void *pInBuffer,
    1712             :     size_t /* nInBufferSize */, GDALDataType eInDT, int nInBands,
    1713             :     const double *CPL_RESTRICT /* padfInNoData */, void *pOutBuffer,
    1714             :     size_t /* nOutBufferSize */, GDALDataType eOutDT, int nOutBands,
    1715             :     const double *CPL_RESTRICT /* padfOutNoData */, double /* dfSrcXOff */,
    1716             :     double /* dfSrcYOff */, double /* dfSrcXSize */, double /* dfSrcYSize */,
    1717             :     const double /* adfSrcGT */[], const char * /* pszVRTPath "*/,
    1718             :     CSLConstList /* papszExtra */)
    1719             : {
    1720          17 :     ExpressionData *expr = static_cast<ExpressionData *>(pWorkingData);
    1721             : 
    1722          17 :     const size_t nElts = static_cast<size_t>(nBufXSize) * nBufYSize;
    1723             : 
    1724          17 :     CPL_IGNORE_RET_VAL(eInDT);
    1725          17 :     CPLAssert(eInDT == GDT_Float64);
    1726          17 :     const double *CPL_RESTRICT padfSrc = static_cast<const double *>(pInBuffer);
    1727             : 
    1728          17 :     CPLAssert(eOutDT == GDT_Float64);
    1729          17 :     CPL_IGNORE_RET_VAL(eOutDT);
    1730          17 :     double *CPL_RESTRICT padfDst = static_cast<double *>(pOutBuffer);
    1731             : 
    1732          39 :     for (size_t i = 0; i < nElts; i++)
    1733             :     {
    1734          26 :         if (auto eErr = expr->Evaluate(padfSrc, nOutBands); eErr != CE_None)
    1735             :         {
    1736           4 :             return eErr;
    1737             :         }
    1738             : 
    1739          22 :         const auto &adfResults = expr->Results();
    1740          22 :         std::copy(adfResults.begin(), adfResults.end(), padfDst);
    1741             : 
    1742          22 :         padfDst += nOutBands;
    1743          22 :         padfSrc += nInBands;
    1744             :     }
    1745             : 
    1746          13 :     return CE_None;
    1747             : }
    1748             : 
    1749             : /************************************************************************/
    1750             : /*              GDALVRTRegisterDefaultProcessedDatasetFuncs()           */
    1751             : /************************************************************************/
    1752             : 
    1753             : /** Register builtin functions that can be used in a VRTProcessedDataset.
    1754             :  */
    1755        1555 : void GDALVRTRegisterDefaultProcessedDatasetFuncs()
    1756             : {
    1757        1555 :     GDALVRTRegisterProcessedDatasetFunc(
    1758             :         "BandAffineCombination", nullptr,
    1759             :         "<ProcessedDatasetFunctionArgumentsList>"
    1760             :         "   <Argument name='src_nodata' type='double' "
    1761             :         "description='Override input nodata value'/>"
    1762             :         "   <Argument name='dst_nodata' type='double' "
    1763             :         "description='Override output nodata value'/>"
    1764             :         "   <Argument name='replacement_nodata' "
    1765             :         "description='value to substitute to a valid computed value that "
    1766             :         "would be nodata' type='double'/>"
    1767             :         "   <Argument name='dst_intended_datatype' type='string' "
    1768             :         "description='Intented datatype of output (which might be "
    1769             :         "different than the working data type)'/>"
    1770             :         "   <Argument name='coefficients_{band}' "
    1771             :         "description='Comma-separated coefficients for combining bands. "
    1772             :         "First one is constant term' "
    1773             :         "type='double_list' required='true'/>"
    1774             :         "   <Argument name='min' description='clamp min value' type='double'/>"
    1775             :         "   <Argument name='max' description='clamp max value' type='double'/>"
    1776             :         "</ProcessedDatasetFunctionArgumentsList>",
    1777             :         GDT_Float64, nullptr, 0, nullptr, 0, BandAffineCombinationInit,
    1778             :         BandAffineCombinationFree, BandAffineCombinationProcess, nullptr);
    1779             : 
    1780        1555 :     GDALVRTRegisterProcessedDatasetFunc(
    1781             :         "LUT", nullptr,
    1782             :         "<ProcessedDatasetFunctionArgumentsList>"
    1783             :         "   <Argument name='src_nodata' type='double' "
    1784             :         "description='Override input nodata value'/>"
    1785             :         "   <Argument name='dst_nodata' type='double' "
    1786             :         "description='Override output nodata value'/>"
    1787             :         "   <Argument name='lut_{band}' "
    1788             :         "description='List of the form [src value 1]:[dest value 1],"
    1789             :         "[src value 2]:[dest value 2],...' "
    1790             :         "type='string' required='true'/>"
    1791             :         "</ProcessedDatasetFunctionArgumentsList>",
    1792             :         GDT_Float64, nullptr, 0, nullptr, 0, LUTInit, LUTFree, LUTProcess,
    1793             :         nullptr);
    1794             : 
    1795        1555 :     GDALVRTRegisterProcessedDatasetFunc(
    1796             :         "LocalScaleOffset", nullptr,
    1797             :         "<ProcessedDatasetFunctionArgumentsList>"
    1798             :         "   <Argument name='relativeToVRT' "
    1799             :         "description='Whether gain and offset filenames are relative to "
    1800             :         "the VRT' type='boolean' default='false'/>"
    1801             :         "   <Argument name='gain_dataset_filename_{band}' "
    1802             :         "description='Filename to the gain dataset' "
    1803             :         "type='string' required='true'/>"
    1804             :         "   <Argument name='gain_dataset_band_{band}' "
    1805             :         "description='Band of the gain dataset' "
    1806             :         "type='integer' required='true'/>"
    1807             :         "   <Argument name='offset_dataset_filename_{band}' "
    1808             :         "description='Filename to the offset dataset' "
    1809             :         "type='string' required='true'/>"
    1810             :         "   <Argument name='offset_dataset_band_{band}' "
    1811             :         "description='Band of the offset dataset' "
    1812             :         "type='integer' required='true'/>"
    1813             :         "   <Argument name='min' description='clamp min value' type='double'/>"
    1814             :         "   <Argument name='max' description='clamp max value' type='double'/>"
    1815             :         "   <Argument name='nodata' type='double' "
    1816             :         "description='Override dataset nodata value'/>"
    1817             :         "   <Argument name='gain_nodata' type='double' "
    1818             :         "description='Override gain dataset nodata value'/>"
    1819             :         "   <Argument name='offset_nodata' type='double' "
    1820             :         "description='Override offset dataset nodata value'/>"
    1821             :         "</ProcessedDatasetFunctionArgumentsList>",
    1822             :         GDT_Float64, nullptr, 0, nullptr, 0, LocalScaleOffsetInit,
    1823             :         LocalScaleOffsetFree, LocalScaleOffsetProcess, nullptr);
    1824             : 
    1825        1555 :     GDALVRTRegisterProcessedDatasetFunc(
    1826             :         "Trimming", nullptr,
    1827             :         "<ProcessedDatasetFunctionArgumentsList>"
    1828             :         "   <Argument name='relativeToVRT' "
    1829             :         "description='Whether trimming_dataset_filename is relative to the VRT'"
    1830             :         " type='boolean' default='false'/>"
    1831             :         "   <Argument name='trimming_dataset_filename' "
    1832             :         "description='Filename to the trimming dataset' "
    1833             :         "type='string' required='true'/>"
    1834             :         "   <Argument name='red_band' type='integer' default='1'/>"
    1835             :         "   <Argument name='green_band' type='integer' default='2'/>"
    1836             :         "   <Argument name='blue_band' type='integer' default='3'/>"
    1837             :         "   <Argument name='top_rgb' "
    1838             :         "description='Maximum saturating RGB output value' "
    1839             :         "type='double' required='true'/>"
    1840             :         "   <Argument name='tone_ceil' "
    1841             :         "description='Maximum threshold beyond which we give up saturation' "
    1842             :         "type='double' required='true'/>"
    1843             :         "   <Argument name='top_margin' "
    1844             :         "description='Margin to allow for dynamics in brightest areas "
    1845             :         "(between 0 and 1, should be close to 0)' "
    1846             :         "type='double' required='true'/>"
    1847             :         "   <Argument name='nodata' type='double' "
    1848             :         "description='Override dataset nodata value'/>"
    1849             :         "   <Argument name='trimming_nodata' type='double' "
    1850             :         "description='Override trimming dataset nodata value'/>"
    1851             :         "</ProcessedDatasetFunctionArgumentsList>",
    1852             :         GDT_Float64, nullptr, 0, nullptr, 0, TrimmingInit, TrimmingFree,
    1853             :         TrimmingProcess, nullptr);
    1854             : 
    1855        1555 :     GDALVRTRegisterProcessedDatasetFunc(
    1856             :         "Expression", nullptr,
    1857             :         "<ProcessedDatasetFunctionArgumentsList>"
    1858             :         "    <Argument name='expression' description='the expression to "
    1859             :         "evaluate' type='string' required='true' />"
    1860             :         "    <Argument name='dialect' description='expression dialect' "
    1861             :         "type='string' />"
    1862             :         "    <Argument name='batch_size' description='batch size' "
    1863             :         "type='integer' />"
    1864             :         "</ProcessedDatasetFunctionArgumentsList>",
    1865             :         GDT_Float64, nullptr, 0, nullptr, 0, ExpressionInit, ExpressionFree,
    1866             :         ExpressionProcess, nullptr);
    1867        1555 : }

Generated by: LCOV version 1.14