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

Generated by: LCOV version 1.14