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