Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: Virtual GDAL Datasets
4 : * Purpose: Implementation of VRTSimpleSource, VRTFuncSource and
5 : * VRTAveragedSource.
6 : * Author: Frank Warmerdam <warmerdam@pobox.com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2001, Frank Warmerdam <warmerdam@pobox.com>
10 : * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
11 : *
12 : * SPDX-License-Identifier: MIT
13 : ****************************************************************************/
14 :
15 : #include "gdal_vrt.h"
16 : #include "vrtdataset.h"
17 :
18 : #include <cassert>
19 : #include <climits>
20 : #include <cmath>
21 : #include <cstddef>
22 : #include <cstdio>
23 : #include <cstdlib>
24 : #include <cstring>
25 : #include <algorithm>
26 : #include <limits>
27 : #include <string>
28 :
29 : #include "cpl_conv.h"
30 : #include "cpl_error.h"
31 : #include "cpl_hash_set.h"
32 : #include "cpl_minixml.h"
33 : #include "cpl_progress.h"
34 : #include "cpl_string.h"
35 : #include "cpl_vsi.h"
36 : #include "gdal.h"
37 : #include "gdal_priv.h"
38 : #include "gdal_proxy.h"
39 : #include "gdal_priv_templates.hpp"
40 : #include "gdalsubdatasetinfo.h"
41 :
42 : /*! @cond Doxygen_Suppress */
43 :
44 : // #define DEBUG_VERBOSE 1
45 :
46 : /************************************************************************/
47 : /* ==================================================================== */
48 : /* VRTSource */
49 : /* ==================================================================== */
50 : /************************************************************************/
51 :
52 247758 : VRTSource::~VRTSource()
53 : {
54 247758 : }
55 :
56 : /************************************************************************/
57 : /* GetFileList() */
58 : /************************************************************************/
59 :
60 0 : void VRTSource::GetFileList(char *** /* ppapszFileList */, int * /* pnSize */,
61 : int * /* pnMaxSize */, CPLHashSet * /* hSetFiles */)
62 : {
63 0 : }
64 :
65 : /************************************************************************/
66 : /* ==================================================================== */
67 : /* VRTSimpleSource */
68 : /* ==================================================================== */
69 : /************************************************************************/
70 :
71 : /************************************************************************/
72 : /* VRTSimpleSource() */
73 : /************************************************************************/
74 :
75 : VRTSimpleSource::VRTSimpleSource() = default;
76 :
77 : /************************************************************************/
78 : /* VRTSimpleSource() */
79 : /************************************************************************/
80 :
81 106 : VRTSimpleSource::VRTSimpleSource(const VRTSimpleSource *poSrcSource,
82 106 : double dfXDstRatio, double dfYDstRatio)
83 : : VRTSource(*poSrcSource),
84 106 : m_poMapSharedSources(poSrcSource->m_poMapSharedSources),
85 106 : m_poRasterBand(poSrcSource->m_poRasterBand),
86 106 : m_poMaskBandMainBand(poSrcSource->m_poMaskBandMainBand),
87 106 : m_aosOpenOptionsOri(poSrcSource->m_aosOpenOptionsOri),
88 106 : m_aosOpenOptions(poSrcSource->m_aosOpenOptions),
89 106 : m_bSrcDSNameFromVRT(poSrcSource->m_bSrcDSNameFromVRT),
90 106 : m_nBand(poSrcSource->m_nBand),
91 106 : m_bGetMaskBand(poSrcSource->m_bGetMaskBand),
92 106 : m_dfSrcXOff(poSrcSource->m_dfSrcXOff),
93 106 : m_dfSrcYOff(poSrcSource->m_dfSrcYOff),
94 106 : m_dfSrcXSize(poSrcSource->m_dfSrcXSize),
95 106 : m_dfSrcYSize(poSrcSource->m_dfSrcYSize),
96 106 : m_nMaxValue(poSrcSource->m_nMaxValue), m_bRelativeToVRTOri(-1),
97 106 : m_nExplicitSharedStatus(poSrcSource->m_nExplicitSharedStatus),
98 106 : m_osSrcDSName(poSrcSource->m_osSrcDSName),
99 106 : m_bDropRefOnSrcBand(poSrcSource->m_bDropRefOnSrcBand)
100 : {
101 106 : if (!poSrcSource->IsSrcWinSet() && !poSrcSource->IsDstWinSet() &&
102 0 : (dfXDstRatio != 1.0 || dfYDstRatio != 1.0))
103 : {
104 2 : auto l_band = GetRasterBand();
105 2 : if (l_band)
106 : {
107 2 : m_dfSrcXOff = 0;
108 2 : m_dfSrcYOff = 0;
109 2 : m_dfSrcXSize = l_band->GetXSize();
110 2 : m_dfSrcYSize = l_band->GetYSize();
111 2 : m_dfDstXOff = 0;
112 2 : m_dfDstYOff = 0;
113 2 : m_dfDstXSize = l_band->GetXSize() * dfXDstRatio;
114 2 : m_dfDstYSize = l_band->GetYSize() * dfYDstRatio;
115 : }
116 : }
117 104 : else if (poSrcSource->IsDstWinSet())
118 : {
119 104 : m_dfDstXOff = poSrcSource->m_dfDstXOff * dfXDstRatio;
120 104 : m_dfDstYOff = poSrcSource->m_dfDstYOff * dfYDstRatio;
121 104 : m_dfDstXSize = poSrcSource->m_dfDstXSize * dfXDstRatio;
122 104 : m_dfDstYSize = poSrcSource->m_dfDstYSize * dfYDstRatio;
123 : }
124 :
125 106 : if (m_bDropRefOnSrcBand)
126 : {
127 106 : GDALDataset *poDS = GetSourceDataset();
128 106 : if (poDS)
129 102 : poDS->Reference();
130 : }
131 106 : }
132 :
133 : /************************************************************************/
134 : /* ~VRTSimpleSource() */
135 : /************************************************************************/
136 :
137 494500 : VRTSimpleSource::~VRTSimpleSource()
138 :
139 : {
140 247698 : if (m_bDropRefOnSrcBand)
141 : {
142 247214 : GDALDataset *poDS = GetSourceDataset();
143 247214 : if (poDS)
144 244685 : poDS->ReleaseRef();
145 : }
146 494500 : }
147 :
148 : /************************************************************************/
149 : /* GetSourceDataset() */
150 : /************************************************************************/
151 :
152 247320 : GDALDataset *VRTSimpleSource::GetSourceDataset() const
153 : {
154 247320 : GDALDataset *poDS = nullptr;
155 247320 : if (m_poMaskBandMainBand)
156 78 : poDS = m_poMaskBandMainBand->GetDataset();
157 247242 : else if (m_poRasterBand)
158 244709 : poDS = m_poRasterBand->GetDataset();
159 247320 : return poDS;
160 : }
161 :
162 : /************************************************************************/
163 : /* GetTypeStatic() */
164 : /************************************************************************/
165 :
166 270060 : const char *VRTSimpleSource::GetTypeStatic()
167 : {
168 : static const char *TYPE = "SimpleSource";
169 270060 : return TYPE;
170 : }
171 :
172 : /************************************************************************/
173 : /* GetType() */
174 : /************************************************************************/
175 :
176 32865 : const char *VRTSimpleSource::GetType() const
177 : {
178 32865 : return GetTypeStatic();
179 : }
180 :
181 : /************************************************************************/
182 : /* FlushCache() */
183 : /************************************************************************/
184 :
185 313382 : CPLErr VRTSimpleSource::FlushCache(bool bAtClosing)
186 :
187 : {
188 313382 : if (m_poMaskBandMainBand != nullptr)
189 : {
190 24 : return m_poMaskBandMainBand->FlushCache(bAtClosing);
191 : }
192 313358 : else if (m_poRasterBand != nullptr)
193 : {
194 310793 : return m_poRasterBand->FlushCache(bAtClosing);
195 : }
196 2565 : return CE_None;
197 : }
198 :
199 : /************************************************************************/
200 : /* UnsetPreservedRelativeFilenames() */
201 : /************************************************************************/
202 :
203 41 : void VRTSimpleSource::UnsetPreservedRelativeFilenames()
204 : {
205 120 : if (m_bRelativeToVRTOri &&
206 79 : !STARTS_WITH(m_osSourceFileNameOri.c_str(), "http://") &&
207 38 : !STARTS_WITH(m_osSourceFileNameOri.c_str(), "https://"))
208 : {
209 38 : m_bRelativeToVRTOri = -1;
210 38 : m_osSourceFileNameOri = "";
211 : }
212 41 : }
213 :
214 : /************************************************************************/
215 : /* SetSrcBand() */
216 : /************************************************************************/
217 :
218 701 : void VRTSimpleSource::SetSrcBand(const char *pszFilename, int nBand)
219 :
220 : {
221 701 : m_nBand = nBand;
222 701 : m_osSrcDSName = pszFilename;
223 701 : }
224 :
225 : /************************************************************************/
226 : /* SetSrcBand() */
227 : /************************************************************************/
228 :
229 142549 : void VRTSimpleSource::SetSrcBand(GDALRasterBand *poNewSrcBand)
230 :
231 : {
232 142549 : m_poRasterBand = poNewSrcBand;
233 142549 : m_nBand = m_poRasterBand->GetBand();
234 142549 : auto poDS = poNewSrcBand->GetDataset();
235 142549 : if (poDS != nullptr)
236 : {
237 142549 : m_osSrcDSName = poDS->GetDescription();
238 142549 : m_aosOpenOptions = CSLDuplicate(poDS->GetOpenOptions());
239 142549 : m_aosOpenOptionsOri = m_aosOpenOptions;
240 : }
241 142549 : }
242 :
243 : /************************************************************************/
244 : /* SetSourceDatasetName() */
245 : /************************************************************************/
246 :
247 7 : void VRTSimpleSource::SetSourceDatasetName(const char *pszFilename,
248 : bool bRelativeToVRT)
249 : {
250 7 : CPLAssert(m_nBand >= 0);
251 7 : m_osSrcDSName = pszFilename;
252 7 : m_osSourceFileNameOri = pszFilename;
253 7 : m_bRelativeToVRTOri = bRelativeToVRT;
254 7 : }
255 :
256 : /************************************************************************/
257 : /* SetSrcMaskBand() */
258 : /************************************************************************/
259 :
260 : // poSrcBand is not the mask band, but the band from which the mask band is
261 : // taken.
262 54 : void VRTSimpleSource::SetSrcMaskBand(GDALRasterBand *poNewSrcBand)
263 :
264 : {
265 54 : m_poRasterBand = poNewSrcBand->GetMaskBand();
266 54 : m_poMaskBandMainBand = poNewSrcBand;
267 54 : m_nBand = poNewSrcBand->GetBand();
268 54 : auto poDS = poNewSrcBand->GetDataset();
269 54 : if (poDS != nullptr)
270 : {
271 54 : m_osSrcDSName = poDS->GetDescription();
272 54 : m_aosOpenOptions = CSLDuplicate(poDS->GetOpenOptions());
273 54 : m_aosOpenOptionsOri = m_aosOpenOptions;
274 : }
275 54 : m_bGetMaskBand = true;
276 54 : }
277 :
278 : /************************************************************************/
279 : /* RoundIfCloseToInt() */
280 : /************************************************************************/
281 :
282 2670550 : static double RoundIfCloseToInt(double dfValue)
283 : {
284 2670550 : double dfClosestInt = floor(dfValue + 0.5);
285 2670550 : return (fabs(dfValue - dfClosestInt) < 1e-3) ? dfClosestInt : dfValue;
286 : }
287 :
288 : /************************************************************************/
289 : /* SetSrcWindow() */
290 : /************************************************************************/
291 :
292 146068 : void VRTSimpleSource::SetSrcWindow(double dfNewXOff, double dfNewYOff,
293 : double dfNewXSize, double dfNewYSize)
294 :
295 : {
296 146068 : m_dfSrcXOff = RoundIfCloseToInt(dfNewXOff);
297 146068 : m_dfSrcYOff = RoundIfCloseToInt(dfNewYOff);
298 146068 : m_dfSrcXSize = RoundIfCloseToInt(dfNewXSize);
299 146068 : m_dfSrcYSize = RoundIfCloseToInt(dfNewYSize);
300 146068 : }
301 :
302 : /************************************************************************/
303 : /* SetDstWindow() */
304 : /************************************************************************/
305 :
306 146067 : void VRTSimpleSource::SetDstWindow(double dfNewXOff, double dfNewYOff,
307 : double dfNewXSize, double dfNewYSize)
308 :
309 : {
310 146067 : m_dfDstXOff = RoundIfCloseToInt(dfNewXOff);
311 146067 : m_dfDstYOff = RoundIfCloseToInt(dfNewYOff);
312 146067 : m_dfDstXSize = RoundIfCloseToInt(dfNewXSize);
313 146067 : m_dfDstYSize = RoundIfCloseToInt(dfNewYSize);
314 146067 : }
315 :
316 : /************************************************************************/
317 : /* GetDstWindow() */
318 : /************************************************************************/
319 :
320 545 : void VRTSimpleSource::GetDstWindow(double &dfDstXOff, double &dfDstYOff,
321 : double &dfDstXSize, double &dfDstYSize) const
322 : {
323 545 : dfDstXOff = m_dfDstXOff;
324 545 : dfDstYOff = m_dfDstYOff;
325 545 : dfDstXSize = m_dfDstXSize;
326 545 : dfDstYSize = m_dfDstYSize;
327 545 : }
328 :
329 : /************************************************************************/
330 : /* DstWindowIntersects() */
331 : /************************************************************************/
332 :
333 1100 : bool VRTSimpleSource::DstWindowIntersects(double dfXOff, double dfYOff,
334 : double dfXSize, double dfYSize) const
335 : {
336 2191 : return IsDstWinSet() && m_dfDstXOff + m_dfDstXSize > dfXOff &&
337 1091 : m_dfDstYOff + m_dfDstYSize > dfYOff &&
338 2191 : m_dfDstXOff < dfXOff + dfXSize && m_dfDstYOff < dfYOff + dfYSize;
339 : }
340 :
341 : /************************************************************************/
342 : /* IsSlowSource() */
343 : /************************************************************************/
344 :
345 2493 : static bool IsSlowSource(const char *pszSrcName)
346 : {
347 4986 : return strstr(pszSrcName, "/vsicurl/http") != nullptr ||
348 4986 : strstr(pszSrcName, "/vsicurl/ftp") != nullptr ||
349 2493 : (strstr(pszSrcName, "/vsicurl?") != nullptr &&
350 2493 : strstr(pszSrcName, "&url=http") != nullptr);
351 : }
352 :
353 : /************************************************************************/
354 : /* AddSourceFilenameNode() */
355 : /************************************************************************/
356 :
357 : /* static */
358 2493 : std::pair<std::string, bool> VRTSimpleSource::ComputeSourceNameAndRelativeFlag(
359 : const char *pszVRTPath, const std::string &osSourceNameIn)
360 : {
361 2493 : std::string osSourceFilename = osSourceNameIn;
362 2493 : int bRelativeToVRT = false;
363 :
364 : // If this isn't actually a file, don't even try to know if it is a
365 : // relative path. It can't be !, and unfortunately CPLIsFilenameRelative()
366 : // can only work with strings that are filenames To be clear
367 : // NITF_TOC_ENTRY:CADRG_JOG-A_250K_1_0:some_path isn't a relative file
368 : // path.
369 : VSIStatBufL sStat;
370 2493 : if (VSIStatExL(osSourceFilename.c_str(), &sStat, VSI_STAT_EXISTS_FLAG) != 0)
371 : {
372 : // Try subdatasetinfo API first
373 : // Note: this will become the only branch when subdatasetinfo will become
374 : // available for NITF_IM, RASTERLITE and TILEDB
375 63 : const auto oSubDSInfo{GDALGetSubdatasetInfo(osSourceFilename.c_str())};
376 63 : if (oSubDSInfo && !oSubDSInfo->GetPathComponent().empty())
377 : {
378 8 : auto path{oSubDSInfo->GetPathComponent()};
379 : std::string relPath{CPLExtractRelativePath(pszVRTPath, path.c_str(),
380 8 : &bRelativeToVRT)};
381 4 : osSourceFilename = oSubDSInfo->ModifyPathComponent(relPath);
382 4 : GDALDestroySubdatasetInfo(oSubDSInfo);
383 : }
384 : else
385 : {
386 280 : for (const char *pszSyntax :
387 339 : GDALDataset::apszSpecialSubDatasetSyntax)
388 : {
389 283 : CPLString osPrefix(pszSyntax);
390 283 : osPrefix.resize(strchr(pszSyntax, ':') - pszSyntax + 1);
391 283 : if (pszSyntax[osPrefix.size()] == '"')
392 56 : osPrefix += '"';
393 283 : if (EQUALN(osSourceFilename.c_str(), osPrefix, osPrefix.size()))
394 : {
395 3 : if (STARTS_WITH_CI(pszSyntax + osPrefix.size(), "{ANY}"))
396 : {
397 : const char *pszLastPart =
398 3 : strrchr(osSourceFilename.c_str(), ':') + 1;
399 : // CSV:z:/foo.xyz
400 0 : if ((pszLastPart[0] == '/' || pszLastPart[0] == '\\') &&
401 6 : pszLastPart - osSourceFilename.c_str() >= 3 &&
402 3 : pszLastPart[-3] == ':')
403 0 : pszLastPart -= 2;
404 3 : CPLString osPrefixFilename(osSourceFilename);
405 3 : osPrefixFilename.resize(pszLastPart -
406 3 : osSourceFilename.c_str());
407 : osSourceFilename = CPLExtractRelativePath(
408 3 : pszVRTPath, pszLastPart, &bRelativeToVRT);
409 3 : osSourceFilename = osPrefixFilename + osSourceFilename;
410 : }
411 0 : else if (STARTS_WITH_CI(pszSyntax + osPrefix.size(),
412 : "{FILENAME}"))
413 : {
414 0 : CPLString osFilename(osSourceFilename.c_str() +
415 0 : osPrefix.size());
416 0 : size_t nPos = 0;
417 0 : if (osFilename.size() >= 3 && osFilename[1] == ':' &&
418 0 : (osFilename[2] == '\\' || osFilename[2] == '/'))
419 0 : nPos = 2;
420 0 : nPos = osFilename.find(
421 0 : pszSyntax[osPrefix.size() + strlen("{FILENAME}")],
422 : nPos);
423 0 : if (nPos != std::string::npos)
424 : {
425 0 : const CPLString osSuffix = osFilename.substr(nPos);
426 0 : osFilename.resize(nPos);
427 : osSourceFilename = CPLExtractRelativePath(
428 0 : pszVRTPath, osFilename, &bRelativeToVRT);
429 : osSourceFilename =
430 0 : osPrefix + osSourceFilename + osSuffix;
431 : }
432 : }
433 3 : break;
434 : }
435 : }
436 : }
437 : }
438 : else
439 : {
440 4860 : std::string osVRTFilename = pszVRTPath;
441 4860 : std::string osSourceDataset = osSourceNameIn;
442 : ;
443 2430 : char *pszCurDir = CPLGetCurrentDir();
444 2430 : if (CPLIsFilenameRelative(osSourceDataset.c_str()) &&
445 2430 : !CPLIsFilenameRelative(osVRTFilename.c_str()) &&
446 : pszCurDir != nullptr)
447 : {
448 194 : osSourceDataset = CPLFormFilenameSafe(
449 97 : pszCurDir, osSourceDataset.c_str(), nullptr);
450 : }
451 2333 : else if (!CPLIsFilenameRelative(osSourceDataset.c_str()) &&
452 2333 : CPLIsFilenameRelative(osVRTFilename.c_str()) &&
453 : pszCurDir != nullptr)
454 : {
455 : osVRTFilename =
456 1 : CPLFormFilenameSafe(pszCurDir, osVRTFilename.c_str(), nullptr);
457 : }
458 2430 : CPLFree(pszCurDir);
459 : osSourceFilename = CPLExtractRelativePath(
460 2430 : osVRTFilename.c_str(), osSourceDataset.c_str(), &bRelativeToVRT);
461 : }
462 :
463 7479 : return {osSourceFilename, static_cast<bool>(bRelativeToVRT)};
464 : }
465 :
466 : /************************************************************************/
467 : /* AddSourceFilenameNode() */
468 : /************************************************************************/
469 :
470 2535 : void VRTSimpleSource::AddSourceFilenameNode(const char *pszVRTPath,
471 : CPLXMLNode *psSrc)
472 : {
473 :
474 2535 : bool bRelativeToVRT = false;
475 5070 : std::string osSourceFilename;
476 :
477 2535 : if (m_bRelativeToVRTOri >= 0)
478 : {
479 42 : osSourceFilename = m_osSourceFileNameOri;
480 42 : bRelativeToVRT = CPL_TO_BOOL(m_bRelativeToVRTOri);
481 : }
482 2493 : else if (IsSlowSource(m_osSrcDSName))
483 : {
484 : // Testing the existence of remote resources can be excruciating
485 : // slow, so let's just suppose they exist.
486 0 : osSourceFilename = m_osSrcDSName;
487 0 : bRelativeToVRT = false;
488 : }
489 : else
490 : {
491 2493 : std::tie(osSourceFilename, bRelativeToVRT) =
492 4986 : ComputeSourceNameAndRelativeFlag(pszVRTPath, m_osSrcDSName);
493 : }
494 :
495 2535 : CPLSetXMLValue(psSrc, "SourceFilename", osSourceFilename.c_str());
496 :
497 2535 : CPLCreateXMLNode(CPLCreateXMLNode(CPLGetXMLNode(psSrc, "SourceFilename"),
498 : CXT_Attribute, "relativeToVRT"),
499 : CXT_Text, bRelativeToVRT ? "1" : "0");
500 :
501 : // Determine if we must write the shared attribute. The config option
502 : // will override the m_nExplicitSharedStatus value
503 2535 : const char *pszShared = CPLGetConfigOption("VRT_SHARED_SOURCE", nullptr);
504 2535 : if ((pszShared == nullptr && m_nExplicitSharedStatus == 0) ||
505 0 : (pszShared != nullptr && !CPLTestBool(pszShared)))
506 : {
507 0 : CPLCreateXMLNode(
508 : CPLCreateXMLNode(CPLGetXMLNode(psSrc, "SourceFilename"),
509 : CXT_Attribute, "shared"),
510 : CXT_Text, "0");
511 : }
512 2535 : }
513 :
514 : /************************************************************************/
515 : /* SerializeToXML() */
516 : /************************************************************************/
517 :
518 2539 : CPLXMLNode *VRTSimpleSource::SerializeToXML(const char *pszVRTPath)
519 :
520 : {
521 : CPLXMLNode *const psSrc =
522 2539 : CPLCreateXMLNode(nullptr, CXT_Element, GetTypeStatic());
523 :
524 2539 : if (!m_osResampling.empty())
525 : {
526 26 : CPLCreateXMLNode(CPLCreateXMLNode(psSrc, CXT_Attribute, "resampling"),
527 : CXT_Text, m_osResampling.c_str());
528 : }
529 :
530 2539 : if (!m_osName.empty())
531 : {
532 22 : CPLAddXMLAttributeAndValue(psSrc, "name", m_osName.c_str());
533 : }
534 :
535 2539 : if (m_bSrcDSNameFromVRT)
536 : {
537 1 : CPLAddXMLChild(psSrc, CPLParseXMLString(m_osSrcDSName.c_str()));
538 : }
539 : else
540 : {
541 2538 : bool bDone = false;
542 2538 : if (m_osSrcDSName.empty() && m_poRasterBand)
543 : {
544 52 : auto poSrcDS = m_poRasterBand->GetDataset();
545 52 : if (poSrcDS)
546 : {
547 52 : VRTDataset *poSrcVRTDS = nullptr;
548 : // For GDALComputedDataset
549 52 : void *pHandle = poSrcDS->GetInternalHandle("VRT_DATASET");
550 52 : if (pHandle && poSrcDS->GetInternalHandle(nullptr) == nullptr)
551 : {
552 0 : poSrcVRTDS = static_cast<VRTDataset *>(pHandle);
553 : }
554 : else
555 : {
556 52 : poSrcVRTDS = dynamic_cast<VRTDataset *>(poSrcDS);
557 : }
558 52 : if (poSrcVRTDS)
559 : {
560 3 : poSrcVRTDS->UnsetPreservedRelativeFilenames();
561 3 : CPLAddXMLChild(psSrc,
562 3 : poSrcVRTDS->SerializeToXML(pszVRTPath));
563 3 : bDone = true;
564 : }
565 : }
566 : }
567 2538 : if (!bDone)
568 : {
569 2535 : AddSourceFilenameNode(pszVRTPath, psSrc);
570 : }
571 : }
572 :
573 2539 : GDALSerializeOpenOptionsToXML(psSrc, m_aosOpenOptionsOri.List());
574 :
575 2539 : if (m_bGetMaskBand)
576 19 : CPLSetXMLValue(psSrc, "SourceBand", CPLSPrintf("mask,%d", m_nBand));
577 : else
578 2520 : CPLSetXMLValue(psSrc, "SourceBand", CPLSPrintf("%d", m_nBand));
579 :
580 : // TODO: in a later version, no longer emit SourceProperties, which
581 : // is no longer used by GDAL 3.4
582 2539 : if (m_poRasterBand)
583 : {
584 : /* Write a few additional useful properties of the dataset */
585 : /* so that we can use a proxy dataset when re-opening. See XMLInit() */
586 : /* below */
587 2426 : CPLSetXMLValue(psSrc, "SourceProperties.#RasterXSize",
588 2426 : CPLSPrintf("%d", m_poRasterBand->GetXSize()));
589 2426 : CPLSetXMLValue(psSrc, "SourceProperties.#RasterYSize",
590 2426 : CPLSPrintf("%d", m_poRasterBand->GetYSize()));
591 2426 : CPLSetXMLValue(
592 : psSrc, "SourceProperties.#DataType",
593 2426 : GDALGetDataTypeName(m_poRasterBand->GetRasterDataType()));
594 :
595 2426 : int nBlockXSize = 0;
596 2426 : int nBlockYSize = 0;
597 2426 : m_poRasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
598 :
599 2426 : CPLSetXMLValue(psSrc, "SourceProperties.#BlockXSize",
600 : CPLSPrintf("%d", nBlockXSize));
601 2426 : CPLSetXMLValue(psSrc, "SourceProperties.#BlockYSize",
602 : CPLSPrintf("%d", nBlockYSize));
603 : }
604 :
605 2539 : if (IsSrcWinSet())
606 : {
607 2525 : CPLSetXMLValue(psSrc, "SrcRect.#xOff",
608 : CPLSPrintf("%.15g", m_dfSrcXOff));
609 2525 : CPLSetXMLValue(psSrc, "SrcRect.#yOff",
610 : CPLSPrintf("%.15g", m_dfSrcYOff));
611 2525 : CPLSetXMLValue(psSrc, "SrcRect.#xSize",
612 : CPLSPrintf("%.15g", m_dfSrcXSize));
613 2525 : CPLSetXMLValue(psSrc, "SrcRect.#ySize",
614 : CPLSPrintf("%.15g", m_dfSrcYSize));
615 : }
616 :
617 2539 : if (IsDstWinSet())
618 : {
619 2525 : CPLSetXMLValue(psSrc, "DstRect.#xOff",
620 : CPLSPrintf("%.15g", m_dfDstXOff));
621 2525 : CPLSetXMLValue(psSrc, "DstRect.#yOff",
622 : CPLSPrintf("%.15g", m_dfDstYOff));
623 2525 : CPLSetXMLValue(psSrc, "DstRect.#xSize",
624 : CPLSPrintf("%.15g", m_dfDstXSize));
625 2525 : CPLSetXMLValue(psSrc, "DstRect.#ySize",
626 : CPLSPrintf("%.15g", m_dfDstYSize));
627 : }
628 :
629 2539 : return psSrc;
630 : }
631 :
632 : /************************************************************************/
633 : /* XMLInit() */
634 : /************************************************************************/
635 :
636 103859 : CPLErr VRTSimpleSource::XMLInit(const CPLXMLNode *psSrc, const char *pszVRTPath,
637 : VRTMapSharedResources &oMapSharedSources)
638 :
639 : {
640 103859 : m_poMapSharedSources = &oMapSharedSources;
641 :
642 103859 : m_osResampling = CPLGetXMLValue(psSrc, "resampling", "");
643 103859 : m_osName = CPLGetXMLValue(psSrc, "name", "");
644 :
645 : /* -------------------------------------------------------------------- */
646 : /* Prepare filename. */
647 : /* -------------------------------------------------------------------- */
648 : const CPLXMLNode *psSourceFileNameNode =
649 103859 : CPLGetXMLNode(psSrc, "SourceFilename");
650 103859 : const CPLXMLNode *psSourceVRTDataset = CPLGetXMLNode(psSrc, "VRTDataset");
651 : const char *pszFilename =
652 103859 : psSourceFileNameNode ? CPLGetXMLValue(psSourceFileNameNode, nullptr, "")
653 103859 : : "";
654 :
655 103859 : if (pszFilename[0] == '\0' && !psSourceVRTDataset)
656 : {
657 3 : CPLError(CE_Warning, CPLE_AppDefined,
658 : "Missing <SourceFilename> or <VRTDataset> element in <%s>.",
659 3 : psSrc->pszValue);
660 3 : return CE_Failure;
661 : }
662 :
663 : // Backup original filename and relativeToVRT so as to be able to
664 : // serialize them identically again (#5985)
665 103856 : m_osSourceFileNameOri = pszFilename;
666 103856 : if (pszFilename[0])
667 : {
668 103850 : m_bRelativeToVRTOri =
669 103850 : atoi(CPLGetXMLValue(psSourceFileNameNode, "relativetoVRT", "0"));
670 : const char *pszShared =
671 103850 : CPLGetXMLValue(psSourceFileNameNode, "shared", nullptr);
672 103850 : if (pszShared == nullptr)
673 : {
674 103848 : pszShared = CPLGetConfigOption("VRT_SHARED_SOURCE", nullptr);
675 : }
676 103850 : if (pszShared != nullptr)
677 : {
678 8 : m_nExplicitSharedStatus = CPLTestBool(pszShared);
679 : }
680 :
681 207700 : m_osSrcDSName = GDALDataset::BuildFilename(
682 207700 : pszFilename, pszVRTPath, CPL_TO_BOOL(m_bRelativeToVRTOri));
683 : }
684 6 : else if (psSourceVRTDataset)
685 : {
686 : CPLXMLNode sNode;
687 6 : sNode.eType = psSourceVRTDataset->eType;
688 6 : sNode.pszValue = psSourceVRTDataset->pszValue;
689 6 : sNode.psNext = nullptr;
690 6 : sNode.psChild = psSourceVRTDataset->psChild;
691 6 : char *pszXML = CPLSerializeXMLTree(&sNode);
692 6 : if (pszXML)
693 : {
694 6 : m_bSrcDSNameFromVRT = true;
695 6 : m_osSrcDSName = pszXML;
696 6 : CPLFree(pszXML);
697 : }
698 : }
699 :
700 103856 : const char *pszSourceBand = CPLGetXMLValue(psSrc, "SourceBand", "1");
701 103856 : m_bGetMaskBand = false;
702 103856 : if (STARTS_WITH_CI(pszSourceBand, "mask"))
703 : {
704 23 : m_bGetMaskBand = true;
705 23 : if (pszSourceBand[4] == ',')
706 23 : m_nBand = atoi(pszSourceBand + 5);
707 : else
708 0 : m_nBand = 1;
709 : }
710 : else
711 : {
712 103833 : m_nBand = atoi(pszSourceBand);
713 : }
714 103856 : if (!GDALCheckBandCount(m_nBand, 0))
715 : {
716 0 : CPLError(CE_Warning, CPLE_AppDefined,
717 : "Invalid <SourceBand> element in VRTRasterBand.");
718 0 : return CE_Failure;
719 : }
720 :
721 103856 : m_aosOpenOptions = GDALDeserializeOpenOptionsFromXML(psSrc);
722 103856 : m_aosOpenOptionsOri = m_aosOpenOptions;
723 103856 : if (strstr(m_osSrcDSName.c_str(), "<VRTDataset") != nullptr)
724 8 : m_aosOpenOptions.SetNameValue("ROOT_PATH", pszVRTPath);
725 :
726 103856 : return ParseSrcRectAndDstRect(psSrc);
727 : }
728 :
729 : /************************************************************************/
730 : /* ParseSrcRectAndDstRect() */
731 : /************************************************************************/
732 :
733 103879 : CPLErr VRTSimpleSource::ParseSrcRectAndDstRect(const CPLXMLNode *psSrc)
734 : {
735 22940 : const auto GetAttrValue = [](const CPLXMLNode *psNode,
736 : const char *pszAttrName, double dfDefaultVal)
737 : {
738 22940 : if (const char *pszVal = CPLGetXMLValue(psNode, pszAttrName, nullptr))
739 22940 : return CPLAtof(pszVal);
740 : else
741 0 : return dfDefaultVal;
742 : };
743 :
744 : /* -------------------------------------------------------------------- */
745 : /* Set characteristics. */
746 : /* -------------------------------------------------------------------- */
747 103879 : const CPLXMLNode *const psSrcRect = CPLGetXMLNode(psSrc, "SrcRect");
748 103879 : if (psSrcRect)
749 : {
750 2868 : double xOff = GetAttrValue(psSrcRect, "xOff", UNINIT_WINDOW);
751 2868 : double yOff = GetAttrValue(psSrcRect, "yOff", UNINIT_WINDOW);
752 2868 : double xSize = GetAttrValue(psSrcRect, "xSize", UNINIT_WINDOW);
753 2868 : double ySize = GetAttrValue(psSrcRect, "ySize", UNINIT_WINDOW);
754 : // Test written that way to catch NaN values
755 2868 : if (!(xOff >= INT_MIN && xOff <= INT_MAX) ||
756 2868 : !(yOff >= INT_MIN && yOff <= INT_MAX) ||
757 2868 : !(xSize > 0 || xSize == UNINIT_WINDOW) || xSize > INT_MAX ||
758 2867 : !(ySize > 0 || ySize == UNINIT_WINDOW) || ySize > INT_MAX)
759 : {
760 1 : CPLError(CE_Failure, CPLE_AppDefined, "Wrong values in SrcRect");
761 1 : return CE_Failure;
762 : }
763 2867 : SetSrcWindow(xOff, yOff, xSize, ySize);
764 : }
765 : else
766 : {
767 101011 : m_dfSrcXOff = UNINIT_WINDOW;
768 101011 : m_dfSrcYOff = UNINIT_WINDOW;
769 101011 : m_dfSrcXSize = UNINIT_WINDOW;
770 101011 : m_dfSrcYSize = UNINIT_WINDOW;
771 : }
772 :
773 103878 : const CPLXMLNode *const psDstRect = CPLGetXMLNode(psSrc, "DstRect");
774 103878 : if (psDstRect)
775 : {
776 2867 : double xOff = GetAttrValue(psDstRect, "xOff", UNINIT_WINDOW);
777 : ;
778 2867 : double yOff = GetAttrValue(psDstRect, "yOff", UNINIT_WINDOW);
779 2867 : double xSize = GetAttrValue(psDstRect, "xSize", UNINIT_WINDOW);
780 : ;
781 2867 : double ySize = GetAttrValue(psDstRect, "ySize", UNINIT_WINDOW);
782 : // Test written that way to catch NaN values
783 2867 : if (!(xOff >= INT_MIN && xOff <= INT_MAX) ||
784 2867 : !(yOff >= INT_MIN && yOff <= INT_MAX) ||
785 2867 : !(xSize > 0 || xSize == UNINIT_WINDOW) || xSize > INT_MAX ||
786 2867 : !(ySize > 0 || ySize == UNINIT_WINDOW) || ySize > INT_MAX)
787 : {
788 1 : CPLError(CE_Failure, CPLE_AppDefined, "Wrong values in DstRect");
789 1 : return CE_Failure;
790 : }
791 2866 : SetDstWindow(xOff, yOff, xSize, ySize);
792 : }
793 : else
794 : {
795 101011 : m_dfDstXOff = UNINIT_WINDOW;
796 101011 : m_dfDstYOff = UNINIT_WINDOW;
797 101011 : m_dfDstXSize = UNINIT_WINDOW;
798 101011 : m_dfDstYSize = UNINIT_WINDOW;
799 : }
800 :
801 103877 : return CE_None;
802 : }
803 :
804 : /************************************************************************/
805 : /* GetFileList() */
806 : /************************************************************************/
807 :
808 114 : void VRTSimpleSource::GetFileList(char ***ppapszFileList, int *pnSize,
809 : int *pnMaxSize, CPLHashSet *hSetFiles)
810 : {
811 114 : if (!m_osSrcDSName.empty() && !m_bSrcDSNameFromVRT)
812 : {
813 114 : const char *pszFilename = m_osSrcDSName.c_str();
814 :
815 : /* --------------------------------------------------------------------
816 : */
817 : /* Is it already in the list ? */
818 : /* --------------------------------------------------------------------
819 : */
820 114 : if (CPLHashSetLookup(hSetFiles, pszFilename) != nullptr)
821 15 : return;
822 :
823 : /* --------------------------------------------------------------------
824 : */
825 : /* Grow array if necessary */
826 : /* --------------------------------------------------------------------
827 : */
828 99 : if (*pnSize + 1 >= *pnMaxSize)
829 : {
830 68 : *pnMaxSize = std::max(*pnSize + 2, 2 + 2 * (*pnMaxSize));
831 68 : *ppapszFileList = static_cast<char **>(
832 68 : CPLRealloc(*ppapszFileList, sizeof(char *) * (*pnMaxSize)));
833 : }
834 :
835 : /* --------------------------------------------------------------------
836 : */
837 : /* Add the string to the list */
838 : /* --------------------------------------------------------------------
839 : */
840 99 : (*ppapszFileList)[*pnSize] = CPLStrdup(pszFilename);
841 99 : (*ppapszFileList)[(*pnSize + 1)] = nullptr;
842 99 : CPLHashSetInsert(hSetFiles, (*ppapszFileList)[*pnSize]);
843 :
844 99 : (*pnSize)++;
845 : }
846 : }
847 :
848 : /************************************************************************/
849 : /* OpenSource() */
850 : /************************************************************************/
851 :
852 102062 : void VRTSimpleSource::OpenSource() const
853 : {
854 102062 : CPLAssert(m_poRasterBand == nullptr);
855 :
856 : /* ----------------------------------------------------------------- */
857 : /* Create a proxy dataset */
858 : /* ----------------------------------------------------------------- */
859 102062 : GDALProxyPoolDataset *proxyDS = nullptr;
860 102062 : std::string osKeyMapSharedSources;
861 102062 : if (m_poMapSharedSources)
862 : {
863 101802 : osKeyMapSharedSources = m_osSrcDSName;
864 101811 : for (int i = 0; i < m_aosOpenOptions.size(); ++i)
865 : {
866 9 : osKeyMapSharedSources += "||";
867 9 : osKeyMapSharedSources += m_aosOpenOptions[i];
868 : }
869 :
870 101802 : proxyDS = cpl::down_cast<GDALProxyPoolDataset *>(
871 101802 : m_poMapSharedSources->Get(osKeyMapSharedSources));
872 : }
873 :
874 102062 : if (proxyDS == nullptr)
875 : {
876 3175 : int bShared = true;
877 3175 : if (m_nExplicitSharedStatus != -1)
878 8 : bShared = m_nExplicitSharedStatus;
879 :
880 3175 : const CPLString osUniqueHandle(CPLSPrintf("%p", m_poMapSharedSources));
881 3175 : proxyDS = GDALProxyPoolDataset::Create(
882 : m_osSrcDSName, m_aosOpenOptions.List(), GA_ReadOnly, bShared,
883 : osUniqueHandle.c_str());
884 3175 : if (proxyDS == nullptr)
885 15 : return;
886 : }
887 : else
888 : {
889 98887 : proxyDS->Reference();
890 : }
891 :
892 102047 : if (m_bGetMaskBand)
893 : {
894 : GDALProxyPoolRasterBand *poMaskBand =
895 15 : cpl::down_cast<GDALProxyPoolRasterBand *>(
896 15 : proxyDS->GetRasterBand(m_nBand));
897 15 : poMaskBand->AddSrcMaskBandDescriptionFromUnderlying();
898 : }
899 :
900 : /* -------------------------------------------------------------------- */
901 : /* Get the raster band. */
902 : /* -------------------------------------------------------------------- */
903 :
904 102047 : m_poRasterBand = proxyDS->GetRasterBand(m_nBand);
905 102047 : if (m_poRasterBand == nullptr || !ValidateOpenedBand(m_poRasterBand))
906 : {
907 2 : proxyDS->ReleaseRef();
908 2 : return;
909 : }
910 :
911 102045 : if (m_bGetMaskBand)
912 : {
913 15 : m_poRasterBand = m_poRasterBand->GetMaskBand();
914 15 : if (m_poRasterBand == nullptr)
915 : {
916 0 : proxyDS->ReleaseRef();
917 0 : return;
918 : }
919 15 : m_poMaskBandMainBand = m_poRasterBand;
920 : }
921 :
922 102045 : if (m_poMapSharedSources)
923 : {
924 101785 : m_poMapSharedSources->Insert(osKeyMapSharedSources, proxyDS);
925 : }
926 : }
927 :
928 : /************************************************************************/
929 : /* GetRasterBand() */
930 : /************************************************************************/
931 :
932 972252 : GDALRasterBand *VRTSimpleSource::GetRasterBand() const
933 : {
934 972252 : if (m_poRasterBand == nullptr)
935 101982 : OpenSource();
936 972252 : return m_poRasterBand;
937 : }
938 :
939 : /************************************************************************/
940 : /* GetMaskBandMainBand() */
941 : /************************************************************************/
942 :
943 5887 : GDALRasterBand *VRTSimpleSource::GetMaskBandMainBand()
944 : {
945 5887 : if (m_poRasterBand == nullptr)
946 80 : OpenSource();
947 5887 : return m_poMaskBandMainBand;
948 : }
949 :
950 : /************************************************************************/
951 : /* IsSameExceptBandNumber() */
952 : /************************************************************************/
953 :
954 3222 : bool VRTSimpleSource::IsSameExceptBandNumber(
955 : const VRTSimpleSource *poOtherSource) const
956 : {
957 6444 : return m_dfSrcXOff == poOtherSource->m_dfSrcXOff &&
958 3222 : m_dfSrcYOff == poOtherSource->m_dfSrcYOff &&
959 3222 : m_dfSrcXSize == poOtherSource->m_dfSrcXSize &&
960 3222 : m_dfSrcYSize == poOtherSource->m_dfSrcYSize &&
961 3222 : m_dfDstXOff == poOtherSource->m_dfDstXOff &&
962 3222 : m_dfDstYOff == poOtherSource->m_dfDstYOff &&
963 3222 : m_dfDstXSize == poOtherSource->m_dfDstXSize &&
964 9666 : m_dfDstYSize == poOtherSource->m_dfDstYSize &&
965 6444 : m_osSrcDSName == poOtherSource->m_osSrcDSName;
966 : }
967 :
968 : /************************************************************************/
969 : /* SrcToDst() */
970 : /* */
971 : /* Note: this is a no-op if the both src and dst windows are unset */
972 : /************************************************************************/
973 :
974 19108 : void VRTSimpleSource::SrcToDst(double dfX, double dfY, double &dfXOut,
975 : double &dfYOut) const
976 :
977 : {
978 19108 : dfXOut = ((dfX - m_dfSrcXOff) / m_dfSrcXSize) * m_dfDstXSize + m_dfDstXOff;
979 19108 : dfYOut = ((dfY - m_dfSrcYOff) / m_dfSrcYSize) * m_dfDstYSize + m_dfDstYOff;
980 19108 : }
981 :
982 : /************************************************************************/
983 : /* DstToSrc() */
984 : /* */
985 : /* Note: this is a no-op if the both src and dst windows are unset */
986 : /************************************************************************/
987 :
988 4022310 : void VRTSimpleSource::DstToSrc(double dfX, double dfY, double &dfXOut,
989 : double &dfYOut) const
990 :
991 : {
992 4022310 : dfXOut = ((dfX - m_dfDstXOff) / m_dfDstXSize) * m_dfSrcXSize + m_dfSrcXOff;
993 4022310 : dfYOut = ((dfY - m_dfDstYOff) / m_dfDstYSize) * m_dfSrcYSize + m_dfSrcYOff;
994 4022310 : }
995 :
996 : /************************************************************************/
997 : /* GetSrcDstWindow() */
998 : /************************************************************************/
999 :
1000 0 : int VRTSimpleSource::GetSrcDstWindow(
1001 : double dfXOff, double dfYOff, double dfXSize, double dfYSize, int nBufXSize,
1002 : int nBufYSize, double *pdfReqXOff, double *pdfReqYOff, double *pdfReqXSize,
1003 : double *pdfReqYSize, int *pnReqXOff, int *pnReqYOff, int *pnReqXSize,
1004 : int *pnReqYSize, int *pnOutXOff, int *pnOutYOff, int *pnOutXSize,
1005 : int *pnOutYSize, bool &bErrorOut)
1006 :
1007 : {
1008 0 : return GetSrcDstWindow(
1009 : dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
1010 : GRIORA_Bilinear, // to stick with legacy behavior
1011 : pdfReqXOff, pdfReqYOff, pdfReqXSize, pdfReqYSize, pnReqXOff, pnReqYOff,
1012 : pnReqXSize, pnReqYSize, pnOutXOff, pnOutYOff, pnOutXSize, pnOutYSize,
1013 0 : bErrorOut);
1014 : }
1015 :
1016 402051 : int VRTSimpleSource::GetSrcDstWindow(
1017 : double dfXOff, double dfYOff, double dfXSize, double dfYSize, int nBufXSize,
1018 : int nBufYSize, GDALRIOResampleAlg eResampleAlg, double *pdfReqXOff,
1019 : double *pdfReqYOff, double *pdfReqXSize, double *pdfReqYSize,
1020 : int *pnReqXOff, int *pnReqYOff, int *pnReqXSize, int *pnReqYSize,
1021 : int *pnOutXOff, int *pnOutYOff, int *pnOutXSize, int *pnOutYSize,
1022 : bool &bErrorOut)
1023 :
1024 : {
1025 402051 : bErrorOut = false;
1026 :
1027 402051 : if (m_dfSrcXSize == 0.0 || m_dfSrcYSize == 0.0 || m_dfDstXSize == 0.0 ||
1028 402051 : m_dfDstYSize == 0.0)
1029 : {
1030 0 : return FALSE;
1031 : }
1032 :
1033 402051 : const bool bDstWinSet = IsDstWinSet();
1034 :
1035 : #ifdef DEBUG
1036 402051 : const bool bSrcWinSet = IsSrcWinSet();
1037 :
1038 402051 : if (bSrcWinSet != bDstWinSet)
1039 : {
1040 0 : return FALSE;
1041 : }
1042 : #endif
1043 :
1044 : /* -------------------------------------------------------------------- */
1045 : /* If the input window completely misses the portion of the */
1046 : /* virtual dataset provided by this source we have nothing to do. */
1047 : /* -------------------------------------------------------------------- */
1048 402051 : if (bDstWinSet)
1049 : {
1050 298889 : if (dfXOff >= m_dfDstXOff + m_dfDstXSize ||
1051 287861 : dfYOff >= m_dfDstYOff + m_dfDstYSize ||
1052 284253 : dfXOff + dfXSize <= m_dfDstXOff || dfYOff + dfYSize <= m_dfDstYOff)
1053 26520 : return FALSE;
1054 : }
1055 :
1056 : /* -------------------------------------------------------------------- */
1057 : /* This request window corresponds to the whole output buffer. */
1058 : /* -------------------------------------------------------------------- */
1059 375531 : *pnOutXOff = 0;
1060 375531 : *pnOutYOff = 0;
1061 375531 : *pnOutXSize = nBufXSize;
1062 375531 : *pnOutYSize = nBufYSize;
1063 :
1064 : /* -------------------------------------------------------------------- */
1065 : /* If the input window extents outside the portion of the on */
1066 : /* the virtual file that this source can set, then clip down */
1067 : /* the requested window. */
1068 : /* -------------------------------------------------------------------- */
1069 375531 : bool bModifiedX = false;
1070 375531 : bool bModifiedY = false;
1071 375531 : double dfRXOff = dfXOff;
1072 375531 : double dfRYOff = dfYOff;
1073 375531 : double dfRXSize = dfXSize;
1074 375531 : double dfRYSize = dfYSize;
1075 :
1076 375531 : if (bDstWinSet)
1077 : {
1078 272369 : if (dfRXOff < m_dfDstXOff)
1079 : {
1080 3826 : dfRXSize = dfRXSize + dfRXOff - m_dfDstXOff;
1081 3826 : dfRXOff = m_dfDstXOff;
1082 3826 : bModifiedX = true;
1083 : }
1084 :
1085 272369 : if (dfRYOff < m_dfDstYOff)
1086 : {
1087 392 : dfRYSize = dfRYSize + dfRYOff - m_dfDstYOff;
1088 392 : dfRYOff = m_dfDstYOff;
1089 392 : bModifiedY = true;
1090 : }
1091 :
1092 272369 : if (dfRXOff + dfRXSize > m_dfDstXOff + m_dfDstXSize)
1093 : {
1094 5963 : dfRXSize = m_dfDstXOff + m_dfDstXSize - dfRXOff;
1095 5963 : bModifiedX = true;
1096 : }
1097 :
1098 272369 : if (dfRYOff + dfRYSize > m_dfDstYOff + m_dfDstYSize)
1099 : {
1100 383 : dfRYSize = m_dfDstYOff + m_dfDstYSize - dfRYOff;
1101 383 : bModifiedY = true;
1102 : }
1103 : }
1104 :
1105 : /* -------------------------------------------------------------------- */
1106 : /* Translate requested region in virtual file into the source */
1107 : /* band coordinates. */
1108 : /* -------------------------------------------------------------------- */
1109 375531 : const double dfScaleX = m_dfSrcXSize / m_dfDstXSize;
1110 375531 : const double dfScaleY = m_dfSrcYSize / m_dfDstYSize;
1111 :
1112 375531 : *pdfReqXOff = (dfRXOff - m_dfDstXOff) * dfScaleX + m_dfSrcXOff;
1113 375531 : *pdfReqYOff = (dfRYOff - m_dfDstYOff) * dfScaleY + m_dfSrcYOff;
1114 375531 : *pdfReqXSize = dfRXSize * dfScaleX;
1115 375531 : *pdfReqYSize = dfRYSize * dfScaleY;
1116 :
1117 751062 : if (!std::isfinite(*pdfReqXOff) || !std::isfinite(*pdfReqYOff) ||
1118 375531 : !std::isfinite(*pdfReqXSize) || !std::isfinite(*pdfReqYSize) ||
1119 1126590 : *pdfReqXOff > INT_MAX || *pdfReqYOff > INT_MAX || *pdfReqXSize < 0 ||
1120 375531 : *pdfReqYSize < 0)
1121 : {
1122 0 : return FALSE;
1123 : }
1124 :
1125 : /* -------------------------------------------------------------------- */
1126 : /* Clamp within the bounds of the available source data. */
1127 : /* -------------------------------------------------------------------- */
1128 375531 : if (*pdfReqXOff < 0)
1129 : {
1130 9 : *pdfReqXSize += *pdfReqXOff;
1131 9 : *pdfReqXOff = 0;
1132 9 : bModifiedX = true;
1133 : }
1134 375531 : if (*pdfReqYOff < 0)
1135 : {
1136 9 : *pdfReqYSize += *pdfReqYOff;
1137 9 : *pdfReqYOff = 0;
1138 9 : bModifiedY = true;
1139 : }
1140 :
1141 375531 : constexpr double EPSILON = 1e-10;
1142 375531 : if (eResampleAlg == GRIORA_NearestNeighbour &&
1143 367131 : (std::fabs(m_dfSrcXOff - std::round(m_dfSrcXOff)) > EPSILON ||
1144 367067 : std::fabs(m_dfSrcYOff - std::round(m_dfSrcYOff)) > EPSILON ||
1145 367065 : std::fabs(m_dfDstXOff - std::round(m_dfDstXOff)) > EPSILON ||
1146 367060 : std::fabs(m_dfDstYOff - std::round(m_dfDstYOff)) > EPSILON))
1147 : {
1148 : // Align the behavior with https://github.com/OSGeo/gdal/blob/0e5bb914b80d049198d9a85e04b22c9b0590cc36/gcore/rasterio.cpp#L799
1149 : // in the way we round coordinates
1150 : // Add small epsilon to avoid some numeric precision issues.
1151 : // Covered by test_vrt_read_multithreaded_non_integer_coordinates_nearest test
1152 79 : *pnReqXOff = static_cast<int>(*pdfReqXOff + 0.5 + EPSILON);
1153 79 : *pnReqYOff = static_cast<int>(*pdfReqYOff + 0.5 + EPSILON);
1154 : }
1155 : else
1156 : {
1157 375452 : *pnReqXOff = static_cast<int>(*pdfReqXOff);
1158 375452 : *pnReqYOff = static_cast<int>(*pdfReqYOff);
1159 : }
1160 :
1161 375531 : constexpr double EPS = 1e-3;
1162 375531 : constexpr double ONE_MINUS_EPS = 1.0 - EPS;
1163 375531 : if (*pdfReqXOff - *pnReqXOff > ONE_MINUS_EPS)
1164 : {
1165 3 : (*pnReqXOff)++;
1166 3 : *pdfReqXOff = *pnReqXOff;
1167 : }
1168 375531 : if (*pdfReqYOff - *pnReqYOff > ONE_MINUS_EPS)
1169 : {
1170 16 : (*pnReqYOff)++;
1171 16 : *pdfReqYOff = *pnReqYOff;
1172 : }
1173 :
1174 375531 : if (*pdfReqXSize > INT_MAX)
1175 0 : *pnReqXSize = INT_MAX;
1176 : else
1177 375531 : *pnReqXSize = static_cast<int>(floor(*pdfReqXSize + 0.5));
1178 :
1179 375531 : if (*pdfReqYSize > INT_MAX)
1180 0 : *pnReqYSize = INT_MAX;
1181 : else
1182 375531 : *pnReqYSize = static_cast<int>(floor(*pdfReqYSize + 0.5));
1183 :
1184 : /* -------------------------------------------------------------------- */
1185 : /* Clamp within the bounds of the available source data. */
1186 : /* -------------------------------------------------------------------- */
1187 :
1188 375531 : if (*pnReqXSize == 0)
1189 1849 : *pnReqXSize = 1;
1190 375531 : if (*pnReqYSize == 0)
1191 25387 : *pnReqYSize = 1;
1192 :
1193 375531 : auto l_band = GetRasterBand();
1194 375531 : if (!l_band)
1195 : {
1196 10 : bErrorOut = true;
1197 10 : return FALSE;
1198 : }
1199 751042 : if (*pnReqXSize > INT_MAX - *pnReqXOff ||
1200 375521 : *pnReqXOff + *pnReqXSize > l_band->GetXSize())
1201 : {
1202 276 : *pnReqXSize = l_band->GetXSize() - *pnReqXOff;
1203 276 : bModifiedX = true;
1204 : }
1205 375521 : if (*pdfReqXOff + *pdfReqXSize > l_band->GetXSize())
1206 : {
1207 274 : *pdfReqXSize = l_band->GetXSize() - *pdfReqXOff;
1208 274 : bModifiedX = true;
1209 : }
1210 :
1211 751042 : if (*pnReqYSize > INT_MAX - *pnReqYOff ||
1212 375521 : *pnReqYOff + *pnReqYSize > l_band->GetYSize())
1213 : {
1214 48 : *pnReqYSize = l_band->GetYSize() - *pnReqYOff;
1215 48 : bModifiedY = true;
1216 : }
1217 375521 : if (*pdfReqYOff + *pdfReqYSize > l_band->GetYSize())
1218 : {
1219 66 : *pdfReqYSize = l_band->GetYSize() - *pdfReqYOff;
1220 66 : bModifiedY = true;
1221 : }
1222 :
1223 : /* -------------------------------------------------------------------- */
1224 : /* Don't do anything if the requesting region is completely off */
1225 : /* the source image. */
1226 : /* -------------------------------------------------------------------- */
1227 751035 : if (*pnReqXOff >= l_band->GetXSize() || *pnReqYOff >= l_band->GetYSize() ||
1228 751035 : *pnReqXSize <= 0 || *pnReqYSize <= 0)
1229 : {
1230 15 : return FALSE;
1231 : }
1232 :
1233 : /* -------------------------------------------------------------------- */
1234 : /* If we haven't had to modify the source rectangle, then the */
1235 : /* destination rectangle must be the whole region. */
1236 : /* -------------------------------------------------------------------- */
1237 375506 : if (bModifiedX || bModifiedY)
1238 : {
1239 : /* --------------------------------------------------------------------
1240 : */
1241 : /* Now transform this possibly reduced request back into the */
1242 : /* destination buffer coordinates in case the output region is */
1243 : /* less than the whole buffer. */
1244 : /* --------------------------------------------------------------------
1245 : */
1246 9554 : double dfDstULX = 0.0;
1247 9554 : double dfDstULY = 0.0;
1248 9554 : double dfDstLRX = 0.0;
1249 9554 : double dfDstLRY = 0.0;
1250 :
1251 9554 : SrcToDst(*pdfReqXOff, *pdfReqYOff, dfDstULX, dfDstULY);
1252 9554 : SrcToDst(*pdfReqXOff + *pdfReqXSize, *pdfReqYOff + *pdfReqYSize,
1253 : dfDstLRX, dfDstLRY);
1254 : #if DEBUG_VERBOSE
1255 : CPLDebug("VRT", "dfDstULX=%g dfDstULY=%g dfDstLRX=%g dfDstLRY=%g",
1256 : dfDstULX, dfDstULY, dfDstLRX, dfDstLRY);
1257 : #endif
1258 :
1259 9554 : if (bModifiedX)
1260 : {
1261 9418 : const double dfScaleWinToBufX = nBufXSize / dfXSize;
1262 :
1263 9418 : const double dfOutXOff = (dfDstULX - dfXOff) * dfScaleWinToBufX;
1264 9418 : if (dfOutXOff <= 0)
1265 5594 : *pnOutXOff = 0;
1266 3824 : else if (dfOutXOff > INT_MAX)
1267 0 : *pnOutXOff = INT_MAX;
1268 : else
1269 3824 : *pnOutXOff = static_cast<int>(dfOutXOff + EPS);
1270 :
1271 : // Apply correction on floating-point source window
1272 : {
1273 9418 : double dfDstDeltaX =
1274 9418 : (dfOutXOff - *pnOutXOff) / dfScaleWinToBufX;
1275 9418 : double dfSrcDeltaX = dfDstDeltaX / m_dfDstXSize * m_dfSrcXSize;
1276 9418 : *pdfReqXOff -= dfSrcDeltaX;
1277 18836 : *pdfReqXSize = std::min(*pdfReqXSize + dfSrcDeltaX,
1278 9418 : static_cast<double>(INT_MAX));
1279 : }
1280 :
1281 9418 : double dfOutRightXOff = (dfDstLRX - dfXOff) * dfScaleWinToBufX;
1282 9418 : if (dfOutRightXOff < dfOutXOff)
1283 3 : return FALSE;
1284 9418 : if (dfOutRightXOff > INT_MAX)
1285 0 : dfOutRightXOff = INT_MAX;
1286 9418 : const int nOutRightXOff =
1287 9418 : static_cast<int>(ceil(dfOutRightXOff - EPS));
1288 9418 : *pnOutXSize = nOutRightXOff - *pnOutXOff;
1289 :
1290 9418 : if (*pnOutXSize > INT_MAX - *pnOutXOff ||
1291 9418 : *pnOutXOff + *pnOutXSize > nBufXSize)
1292 0 : *pnOutXSize = nBufXSize - *pnOutXOff;
1293 :
1294 : // Apply correction on floating-point source window
1295 : {
1296 9418 : double dfDstDeltaX =
1297 9418 : (nOutRightXOff - dfOutRightXOff) / dfScaleWinToBufX;
1298 9418 : double dfSrcDeltaX = dfDstDeltaX / m_dfDstXSize * m_dfSrcXSize;
1299 18836 : *pdfReqXSize = std::min(*pdfReqXSize + dfSrcDeltaX,
1300 9418 : static_cast<double>(INT_MAX));
1301 : }
1302 : }
1303 :
1304 9554 : if (bModifiedY)
1305 : {
1306 714 : const double dfScaleWinToBufY = nBufYSize / dfYSize;
1307 :
1308 714 : const double dfOutYOff = (dfDstULY - dfYOff) * dfScaleWinToBufY;
1309 714 : if (dfOutYOff <= 0)
1310 305 : *pnOutYOff = 0;
1311 409 : else if (dfOutYOff > INT_MAX)
1312 0 : *pnOutYOff = INT_MAX;
1313 : else
1314 409 : *pnOutYOff = static_cast<int>(dfOutYOff + EPS);
1315 :
1316 : // Apply correction on floating-point source window
1317 : {
1318 714 : double dfDstDeltaY =
1319 714 : (dfOutYOff - *pnOutYOff) / dfScaleWinToBufY;
1320 714 : double dfSrcDeltaY = dfDstDeltaY / m_dfDstYSize * m_dfSrcYSize;
1321 714 : *pdfReqYOff -= dfSrcDeltaY;
1322 1428 : *pdfReqYSize = std::min(*pdfReqYSize + dfSrcDeltaY,
1323 714 : static_cast<double>(INT_MAX));
1324 : }
1325 :
1326 714 : double dfOutTopYOff = (dfDstLRY - dfYOff) * dfScaleWinToBufY;
1327 714 : if (dfOutTopYOff < dfOutYOff)
1328 0 : return FALSE;
1329 714 : if (dfOutTopYOff > INT_MAX)
1330 0 : dfOutTopYOff = INT_MAX;
1331 714 : const int nOutTopYOff = static_cast<int>(ceil(dfOutTopYOff - EPS));
1332 714 : *pnOutYSize = nOutTopYOff - *pnOutYOff;
1333 :
1334 714 : if (*pnOutYSize > INT_MAX - *pnOutYOff ||
1335 714 : *pnOutYOff + *pnOutYSize > nBufYSize)
1336 0 : *pnOutYSize = nBufYSize - *pnOutYOff;
1337 :
1338 : // Apply correction on floating-point source window
1339 : {
1340 714 : double dfDstDeltaY =
1341 714 : (nOutTopYOff - dfOutTopYOff) / dfScaleWinToBufY;
1342 714 : double dfSrcDeltaY = dfDstDeltaY / m_dfDstYSize * m_dfSrcYSize;
1343 1428 : *pdfReqYSize = std::min(*pdfReqYSize + dfSrcDeltaY,
1344 714 : static_cast<double>(INT_MAX));
1345 : }
1346 : }
1347 :
1348 9554 : if (*pnOutXSize < 1 || *pnOutYSize < 1)
1349 3 : return FALSE;
1350 : }
1351 :
1352 375503 : *pdfReqXOff = RoundIfCloseToInt(*pdfReqXOff);
1353 375503 : *pdfReqYOff = RoundIfCloseToInt(*pdfReqYOff);
1354 375503 : *pdfReqXSize = RoundIfCloseToInt(*pdfReqXSize);
1355 375503 : *pdfReqYSize = RoundIfCloseToInt(*pdfReqYSize);
1356 :
1357 375503 : return TRUE;
1358 : }
1359 :
1360 : /************************************************************************/
1361 : /* NeedMaxValAdjustment() */
1362 : /************************************************************************/
1363 :
1364 356405 : int VRTSimpleSource::NeedMaxValAdjustment() const
1365 : {
1366 356405 : if (!m_nMaxValue)
1367 356383 : return FALSE;
1368 :
1369 22 : auto l_band = GetRasterBand();
1370 22 : if (!l_band)
1371 0 : return FALSE;
1372 : const char *pszNBITS =
1373 22 : l_band->GetMetadataItem(GDALMD_NBITS, GDAL_MDD_IMAGE_STRUCTURE);
1374 22 : const int nBits = (pszNBITS) ? atoi(pszNBITS) : 0;
1375 22 : if (nBits >= 1 && nBits <= 31)
1376 : {
1377 0 : const int nBandMaxValue = static_cast<int>((1U << nBits) - 1);
1378 0 : return nBandMaxValue > m_nMaxValue;
1379 : }
1380 22 : return TRUE;
1381 : }
1382 :
1383 : /************************************************************************/
1384 : /* CopyWordIn() */
1385 : /************************************************************************/
1386 :
1387 : template <class DstType>
1388 4400 : static void CopyWordIn(const void *pSrcVal, GDALDataType eSrcType,
1389 : DstType *pDstVal, GDALDataType eDstType)
1390 : {
1391 4400 : switch (eSrcType)
1392 : {
1393 800 : case GDT_UInt8:
1394 800 : GDALCopyWord(*static_cast<const uint8_t *>(pSrcVal), *pDstVal);
1395 800 : break;
1396 0 : case GDT_Int8:
1397 0 : GDALCopyWord(*static_cast<const int8_t *>(pSrcVal), *pDstVal);
1398 0 : break;
1399 0 : case GDT_UInt16:
1400 0 : GDALCopyWord(*static_cast<const uint16_t *>(pSrcVal), *pDstVal);
1401 0 : break;
1402 0 : case GDT_Int16:
1403 0 : GDALCopyWord(*static_cast<const int16_t *>(pSrcVal), *pDstVal);
1404 0 : break;
1405 0 : case GDT_UInt32:
1406 0 : GDALCopyWord(*static_cast<const uint32_t *>(pSrcVal), *pDstVal);
1407 0 : break;
1408 3600 : case GDT_Int32:
1409 3600 : GDALCopyWord(*static_cast<const int32_t *>(pSrcVal), *pDstVal);
1410 3600 : break;
1411 0 : case GDT_UInt64:
1412 0 : GDALCopyWord(*static_cast<const uint64_t *>(pSrcVal), *pDstVal);
1413 0 : break;
1414 0 : case GDT_Int64:
1415 0 : GDALCopyWord(*static_cast<const int64_t *>(pSrcVal), *pDstVal);
1416 0 : break;
1417 0 : case GDT_Float16:
1418 0 : GDALCopyWord(*static_cast<const GFloat16 *>(pSrcVal), *pDstVal);
1419 0 : break;
1420 0 : case GDT_Float32:
1421 0 : GDALCopyWord(*static_cast<const float *>(pSrcVal), *pDstVal);
1422 0 : break;
1423 0 : case GDT_Float64:
1424 0 : GDALCopyWord(*static_cast<const double *>(pSrcVal), *pDstVal);
1425 0 : break;
1426 0 : case GDT_CInt16:
1427 : case GDT_CInt32:
1428 : case GDT_CFloat16:
1429 : case GDT_CFloat32:
1430 : case GDT_CFloat64:
1431 0 : GDALCopyWords(pSrcVal, eSrcType, 0, pDstVal, eDstType, 0, 1);
1432 0 : break;
1433 0 : case GDT_Unknown:
1434 : case GDT_TypeCount:
1435 0 : CPLAssert(false);
1436 : }
1437 4400 : }
1438 :
1439 : /************************************************************************/
1440 : /* CopyWordOut() */
1441 : /************************************************************************/
1442 :
1443 : template <class SrcType>
1444 10566399 : static void CopyWordOut(const SrcType *pSrcVal, GDALDataType eSrcType,
1445 : void *pDstVal, GDALDataType eDstType)
1446 : {
1447 10566399 : switch (eDstType)
1448 : {
1449 2645910 : case GDT_UInt8:
1450 2645910 : GDALCopyWord(*pSrcVal, *static_cast<uint8_t *>(pDstVal));
1451 2645910 : break;
1452 62 : case GDT_Int8:
1453 62 : GDALCopyWord(*pSrcVal, *static_cast<int8_t *>(pDstVal));
1454 62 : break;
1455 2073100 : case GDT_UInt16:
1456 2073100 : GDALCopyWord(*pSrcVal, *static_cast<uint16_t *>(pDstVal));
1457 2073100 : break;
1458 5088270 : case GDT_Int16:
1459 5088270 : GDALCopyWord(*pSrcVal, *static_cast<int16_t *>(pDstVal));
1460 5088270 : break;
1461 22 : case GDT_UInt32:
1462 22 : GDALCopyWord(*pSrcVal, *static_cast<uint32_t *>(pDstVal));
1463 22 : break;
1464 367222 : case GDT_Int32:
1465 367222 : GDALCopyWord(*pSrcVal, *static_cast<int32_t *>(pDstVal));
1466 367222 : break;
1467 22 : case GDT_UInt64:
1468 22 : GDALCopyWord(*pSrcVal, *static_cast<uint64_t *>(pDstVal));
1469 22 : break;
1470 42 : case GDT_Int64:
1471 42 : GDALCopyWord(*pSrcVal, *static_cast<int64_t *>(pDstVal));
1472 42 : break;
1473 0 : case GDT_Float16:
1474 0 : GDALCopyWord(*pSrcVal, *static_cast<GFloat16 *>(pDstVal));
1475 0 : break;
1476 382740 : case GDT_Float32:
1477 382740 : GDALCopyWord(*pSrcVal, *static_cast<float *>(pDstVal));
1478 382740 : break;
1479 7969 : case GDT_Float64:
1480 7969 : GDALCopyWord(*pSrcVal, *static_cast<double *>(pDstVal));
1481 7969 : break;
1482 1019 : case GDT_CInt16:
1483 : case GDT_CInt32:
1484 : case GDT_CFloat16:
1485 : case GDT_CFloat32:
1486 : case GDT_CFloat64:
1487 1019 : GDALCopyWords(pSrcVal, eSrcType, 0, pDstVal, eDstType, 0, 1);
1488 1019 : break;
1489 0 : case GDT_Unknown:
1490 : case GDT_TypeCount:
1491 0 : CPLAssert(false);
1492 : }
1493 10566399 : }
1494 :
1495 : /************************************************************************/
1496 : /* GDALClampValueToType() */
1497 : /************************************************************************/
1498 :
1499 : template <class T>
1500 188805 : inline void GDALClampValueToType(T *pValue, GDALDataType eClampingType)
1501 : {
1502 188805 : switch (eClampingType)
1503 : {
1504 38063 : case GDT_UInt8:
1505 38063 : *pValue = GDALClampValueToType<T, uint8_t>(*pValue);
1506 38063 : break;
1507 420 : case GDT_Int8:
1508 420 : *pValue = GDALClampValueToType<T, int8_t>(*pValue);
1509 420 : break;
1510 27480 : case GDT_UInt16:
1511 27480 : *pValue = GDALClampValueToType<T, uint16_t>(*pValue);
1512 27480 : break;
1513 36094 : case GDT_Int16:
1514 : case GDT_CInt16:
1515 36094 : *pValue = GDALClampValueToType<T, int16_t>(*pValue);
1516 36094 : break;
1517 24388 : case GDT_UInt32:
1518 24388 : *pValue = GDALClampValueToType<T, uint32_t>(*pValue);
1519 24388 : break;
1520 20636 : case GDT_Int32:
1521 : case GDT_CInt32:
1522 20636 : *pValue = GDALClampValueToType<T, int32_t>(*pValue);
1523 20636 : break;
1524 0 : case GDT_UInt64:
1525 0 : *pValue = GDALClampValueToType<T, uint64_t>(*pValue);
1526 0 : break;
1527 0 : case GDT_Int64:
1528 0 : *pValue = GDALClampValueToType<T, int64_t>(*pValue);
1529 0 : break;
1530 0 : case GDT_Float16:
1531 : case GDT_CFloat16:
1532 0 : *pValue = GDALClampValueToType<T, GFloat16>(*pValue);
1533 0 : break;
1534 26984 : case GDT_Float32:
1535 : case GDT_CFloat32:
1536 26984 : *pValue = GDALClampValueToType<T, float>(*pValue);
1537 26984 : break;
1538 14740 : case GDT_Float64:
1539 : case GDT_CFloat64:
1540 14740 : *pValue = GDALClampValueToType<T, double>(*pValue);
1541 14740 : break;
1542 0 : case GDT_Unknown:
1543 : case GDT_TypeCount:
1544 0 : CPLAssert(false);
1545 : break;
1546 : }
1547 188805 : }
1548 :
1549 : /************************************************************************/
1550 : /* GDALClampValueToType() */
1551 : /************************************************************************/
1552 :
1553 184565 : inline void GDALClampValueToType(void *pValue, GDALDataType eValueType,
1554 : GDALDataType eClampingType)
1555 : {
1556 184565 : switch (eValueType)
1557 : {
1558 37645 : case GDT_UInt8:
1559 37645 : GDALClampValueToType(static_cast<uint8_t *>(pValue), eClampingType);
1560 37645 : break;
1561 400 : case GDT_Int8:
1562 400 : GDALClampValueToType(static_cast<int8_t *>(pValue), eClampingType);
1563 400 : break;
1564 27480 : case GDT_UInt16:
1565 27480 : GDALClampValueToType(static_cast<uint16_t *>(pValue),
1566 : eClampingType);
1567 27480 : break;
1568 36094 : case GDT_Int16:
1569 36094 : GDALClampValueToType(static_cast<int16_t *>(pValue), eClampingType);
1570 36094 : break;
1571 24388 : case GDT_UInt32:
1572 24388 : GDALClampValueToType(static_cast<uint32_t *>(pValue),
1573 : eClampingType);
1574 24388 : break;
1575 21042 : case GDT_Int32:
1576 21042 : GDALClampValueToType(static_cast<int32_t *>(pValue), eClampingType);
1577 21042 : break;
1578 0 : case GDT_UInt64:
1579 0 : GDALClampValueToType(static_cast<uint64_t *>(pValue),
1580 : eClampingType);
1581 0 : break;
1582 0 : case GDT_Int64:
1583 0 : GDALClampValueToType(static_cast<int64_t *>(pValue), eClampingType);
1584 0 : break;
1585 0 : case GDT_Float16:
1586 0 : GDALClampValueToType(static_cast<GFloat16 *>(pValue),
1587 : eClampingType);
1588 0 : break;
1589 18776 : case GDT_Float32:
1590 18776 : GDALClampValueToType(static_cast<float *>(pValue), eClampingType);
1591 18776 : break;
1592 14740 : case GDT_Float64:
1593 14740 : GDALClampValueToType(static_cast<double *>(pValue), eClampingType);
1594 14740 : break;
1595 0 : case GDT_CInt16:
1596 0 : GDALClampValueToType(static_cast<int16_t *>(pValue) + 0,
1597 : eClampingType);
1598 0 : GDALClampValueToType(static_cast<int16_t *>(pValue) + 1,
1599 : eClampingType);
1600 0 : break;
1601 0 : case GDT_CInt32:
1602 0 : GDALClampValueToType(static_cast<int32_t *>(pValue) + 0,
1603 : eClampingType);
1604 0 : GDALClampValueToType(static_cast<int32_t *>(pValue) + 1,
1605 : eClampingType);
1606 0 : break;
1607 0 : case GDT_CFloat16:
1608 0 : GDALClampValueToType(static_cast<GFloat16 *>(pValue) + 0,
1609 : eClampingType);
1610 0 : GDALClampValueToType(static_cast<GFloat16 *>(pValue) + 1,
1611 : eClampingType);
1612 0 : break;
1613 4000 : case GDT_CFloat32:
1614 4000 : GDALClampValueToType(static_cast<float *>(pValue) + 0,
1615 : eClampingType);
1616 4000 : GDALClampValueToType(static_cast<float *>(pValue) + 1,
1617 : eClampingType);
1618 4000 : break;
1619 0 : case GDT_CFloat64:
1620 0 : GDALClampValueToType(static_cast<double *>(pValue) + 0,
1621 : eClampingType);
1622 0 : GDALClampValueToType(static_cast<double *>(pValue) + 1,
1623 : eClampingType);
1624 0 : break;
1625 0 : case GDT_Unknown:
1626 : case GDT_TypeCount:
1627 0 : CPLAssert(false);
1628 : break;
1629 : }
1630 184565 : }
1631 :
1632 : /************************************************************************/
1633 : /* RasterIO() */
1634 : /************************************************************************/
1635 :
1636 355686 : CPLErr VRTSimpleSource::RasterIO(GDALDataType eVRTBandDataType, int nXOff,
1637 : int nYOff, int nXSize, int nYSize, void *pData,
1638 : int nBufXSize, int nBufYSize,
1639 : GDALDataType eBufType, GSpacing nPixelSpace,
1640 : GSpacing nLineSpace,
1641 : GDALRasterIOExtraArg *psExtraArgIn,
1642 : WorkingState & /*oWorkingState*/)
1643 :
1644 : {
1645 : GDALRasterIOExtraArg sExtraArg;
1646 355686 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
1647 355686 : GDALRasterIOExtraArg *psExtraArg = &sExtraArg;
1648 :
1649 355686 : double dfXOff = nXOff;
1650 355686 : double dfYOff = nYOff;
1651 355686 : double dfXSize = nXSize;
1652 355686 : double dfYSize = nYSize;
1653 355686 : if (psExtraArgIn != nullptr && psExtraArgIn->bFloatingPointWindowValidity)
1654 : {
1655 130 : dfXOff = psExtraArgIn->dfXOff;
1656 130 : dfYOff = psExtraArgIn->dfYOff;
1657 130 : dfXSize = psExtraArgIn->dfXSize;
1658 130 : dfYSize = psExtraArgIn->dfYSize;
1659 : }
1660 :
1661 : // The window we will actually request from the source raster band.
1662 355686 : double dfReqXOff = 0.0;
1663 355686 : double dfReqYOff = 0.0;
1664 355686 : double dfReqXSize = 0.0;
1665 355686 : double dfReqYSize = 0.0;
1666 355686 : int nReqXOff = 0;
1667 355686 : int nReqYOff = 0;
1668 355686 : int nReqXSize = 0;
1669 355686 : int nReqYSize = 0;
1670 :
1671 : // The window we will actual set _within_ the pData buffer.
1672 355686 : int nOutXOff = 0;
1673 355686 : int nOutYOff = 0;
1674 355686 : int nOutXSize = 0;
1675 355686 : int nOutYSize = 0;
1676 :
1677 355686 : if (!m_osResampling.empty())
1678 : {
1679 203262 : psExtraArg->eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
1680 : }
1681 152424 : else if (psExtraArgIn != nullptr)
1682 : {
1683 152424 : psExtraArg->eResampleAlg = psExtraArgIn->eResampleAlg;
1684 : }
1685 :
1686 355686 : bool bError = false;
1687 355686 : if (!GetSrcDstWindow(dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
1688 : psExtraArg->eResampleAlg, &dfReqXOff, &dfReqYOff,
1689 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
1690 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
1691 : &nOutXSize, &nOutYSize, bError))
1692 : {
1693 9698 : return bError ? CE_Failure : CE_None;
1694 : }
1695 : #if DEBUG_VERBOSE
1696 : CPLDebug("VRT",
1697 : "nXOff=%d, nYOff=%d, nXSize=%d, nYSize=%d, nBufXSize=%d, "
1698 : "nBufYSize=%d,\n"
1699 : "dfReqXOff=%g, dfReqYOff=%g, dfReqXSize=%g, dfReqYSize=%g,\n"
1700 : "nReqXOff=%d, nReqYOff=%d, nReqXSize=%d, nReqYSize=%d,\n"
1701 : "nOutXOff=%d, nOutYOff=%d, nOutXSize=%d, nOutYSize=%d",
1702 : nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize, dfReqXOff,
1703 : dfReqYOff, dfReqXSize, dfReqYSize, nReqXOff, nReqYOff, nReqXSize,
1704 : nReqYSize, nOutXOff, nOutYOff, nOutXSize, nOutYSize);
1705 : #endif
1706 :
1707 : /* -------------------------------------------------------------------- */
1708 : /* Actually perform the IO request. */
1709 : /* -------------------------------------------------------------------- */
1710 345988 : psExtraArg->bFloatingPointWindowValidity = TRUE;
1711 345988 : psExtraArg->dfXOff = dfReqXOff;
1712 345988 : psExtraArg->dfYOff = dfReqYOff;
1713 345988 : psExtraArg->dfXSize = dfReqXSize;
1714 345988 : psExtraArg->dfYSize = dfReqYSize;
1715 345988 : if (psExtraArgIn)
1716 : {
1717 345988 : psExtraArg->pfnProgress = psExtraArgIn->pfnProgress;
1718 345988 : psExtraArg->pProgressData = psExtraArgIn->pProgressData;
1719 345988 : if (psExtraArgIn->nVersion >= 2)
1720 : {
1721 345988 : psExtraArg->bUseOnlyThisScale = psExtraArgIn->bUseOnlyThisScale;
1722 : }
1723 345988 : psExtraArg->bOperateInBufType =
1724 345988 : GDAL_GET_OPERATE_IN_BUF_TYPE(*psExtraArgIn);
1725 : }
1726 :
1727 345988 : GByte *pabyOut = static_cast<unsigned char *>(pData) +
1728 345988 : nOutXOff * nPixelSpace +
1729 345988 : static_cast<GPtrDiff_t>(nOutYOff) * nLineSpace;
1730 :
1731 345988 : auto l_band = GetRasterBand();
1732 345988 : if (!l_band)
1733 0 : return CE_Failure;
1734 :
1735 345988 : CPLErr eErr = CE_Failure;
1736 345988 : if (GDALDataTypeIsConversionLossy(l_band->GetRasterDataType(),
1737 345988 : eVRTBandDataType))
1738 : {
1739 1209 : if (!psExtraArg->bOperateInBufType)
1740 : {
1741 1 : const int nBandDTSize = GDALGetDataTypeSizeBytes(eVRTBandDataType);
1742 : void *pTemp =
1743 1 : VSI_MALLOC3_VERBOSE(nOutXSize, nOutYSize, nBandDTSize);
1744 1 : if (pTemp)
1745 : {
1746 1 : eErr = l_band->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize,
1747 : nReqYSize, pTemp, nOutXSize, nOutYSize,
1748 : eVRTBandDataType, 0, 0, psExtraArg);
1749 1 : if (eErr == CE_None)
1750 : {
1751 1 : GByte *pabyTemp = static_cast<GByte *>(pTemp);
1752 3 : for (int iY = 0; iY < nOutYSize; iY++)
1753 : {
1754 2 : GDALCopyWords(
1755 2 : pabyTemp + static_cast<size_t>(iY) * nBandDTSize *
1756 2 : nOutXSize,
1757 : eVRTBandDataType, nBandDTSize,
1758 2 : pabyOut + static_cast<GPtrDiff_t>(iY * nLineSpace),
1759 : eBufType, static_cast<int>(nPixelSpace), nOutXSize);
1760 : }
1761 : }
1762 1 : VSIFree(pTemp);
1763 : }
1764 : }
1765 : else
1766 : {
1767 : eErr =
1768 1208 : l_band->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize,
1769 : nReqYSize, pabyOut, nOutXSize, nOutYSize,
1770 : eBufType, nPixelSpace, nLineSpace, psExtraArg);
1771 1208 : if (eErr == CE_None)
1772 : {
1773 4145 : for (int j = 0; j < nOutYSize; j++)
1774 : {
1775 179594 : for (int i = 0; i < nOutXSize; i++)
1776 : {
1777 176657 : void *pDst = pabyOut + j * nLineSpace + i * nPixelSpace;
1778 176657 : GDALClampValueToType(pDst, eBufType, eVRTBandDataType);
1779 : }
1780 : }
1781 : }
1782 : }
1783 : }
1784 : else
1785 : {
1786 344779 : eErr = l_band->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize,
1787 : nReqYSize, pabyOut, nOutXSize, nOutYSize,
1788 : eBufType, nPixelSpace, nLineSpace, psExtraArg);
1789 : }
1790 :
1791 345988 : if (NeedMaxValAdjustment())
1792 : {
1793 234 : for (int j = 0; j < nOutYSize; j++)
1794 : {
1795 4620 : for (int i = 0; i < nOutXSize; i++)
1796 : {
1797 4400 : GByte *pDst = pabyOut + j * nLineSpace + i * nPixelSpace;
1798 4400 : int nVal = 0;
1799 4400 : CopyWordIn(pDst, eBufType, &nVal, GDT_Int32);
1800 4400 : if (nVal > m_nMaxValue)
1801 800 : nVal = m_nMaxValue;
1802 4400 : CopyWordOut(&nVal, GDT_Int32, pDst, eBufType);
1803 : }
1804 : }
1805 : }
1806 :
1807 345988 : if (psExtraArg->pfnProgress)
1808 529 : psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
1809 :
1810 345988 : return eErr;
1811 : }
1812 :
1813 : /************************************************************************/
1814 : /* GetMinimum() */
1815 : /************************************************************************/
1816 :
1817 52 : double VRTSimpleSource::GetMinimum(int nXSize, int nYSize, int *pbSuccess)
1818 : {
1819 : // The window we will actually request from the source raster band.
1820 52 : double dfReqXOff = 0.0;
1821 52 : double dfReqYOff = 0.0;
1822 52 : double dfReqXSize = 0.0;
1823 52 : double dfReqYSize = 0.0;
1824 52 : int nReqXOff = 0;
1825 52 : int nReqYOff = 0;
1826 52 : int nReqXSize = 0;
1827 52 : int nReqYSize = 0;
1828 :
1829 : // The window we will actual set _within_ the pData buffer.
1830 52 : int nOutXOff = 0;
1831 52 : int nOutYOff = 0;
1832 52 : int nOutXSize = 0;
1833 52 : int nOutYSize = 0;
1834 :
1835 52 : bool bError = false;
1836 52 : auto l_band = GetRasterBand();
1837 104 : if (!l_band ||
1838 52 : !GetSrcDstWindow(0, 0, nXSize, nYSize, nXSize, nYSize,
1839 : GRIORA_NearestNeighbour, &dfReqXOff, &dfReqYOff,
1840 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
1841 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
1842 52 : &nOutXSize, &nOutYSize, bError) ||
1843 156 : nReqXOff != 0 || nReqYOff != 0 || nReqXSize != l_band->GetXSize() ||
1844 52 : nReqYSize != l_band->GetYSize())
1845 : {
1846 0 : *pbSuccess = FALSE;
1847 0 : return 0;
1848 : }
1849 :
1850 52 : const double dfVal = l_band->GetMinimum(pbSuccess);
1851 52 : if (NeedMaxValAdjustment() && dfVal > m_nMaxValue)
1852 2 : return m_nMaxValue;
1853 50 : return dfVal;
1854 : }
1855 :
1856 : /************************************************************************/
1857 : /* GetMaximum() */
1858 : /************************************************************************/
1859 :
1860 51 : double VRTSimpleSource::GetMaximum(int nXSize, int nYSize, int *pbSuccess)
1861 : {
1862 : // The window we will actually request from the source raster band.
1863 51 : double dfReqXOff = 0.0;
1864 51 : double dfReqYOff = 0.0;
1865 51 : double dfReqXSize = 0.0;
1866 51 : double dfReqYSize = 0.0;
1867 51 : int nReqXOff = 0;
1868 51 : int nReqYOff = 0;
1869 51 : int nReqXSize = 0;
1870 51 : int nReqYSize = 0;
1871 :
1872 : // The window we will actual set _within_ the pData buffer.
1873 51 : int nOutXOff = 0;
1874 51 : int nOutYOff = 0;
1875 51 : int nOutXSize = 0;
1876 51 : int nOutYSize = 0;
1877 :
1878 51 : bool bError = false;
1879 51 : auto l_band = GetRasterBand();
1880 102 : if (!l_band ||
1881 51 : !GetSrcDstWindow(0, 0, nXSize, nYSize, nXSize, nYSize,
1882 : GRIORA_NearestNeighbour, &dfReqXOff, &dfReqYOff,
1883 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
1884 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
1885 51 : &nOutXSize, &nOutYSize, bError) ||
1886 153 : nReqXOff != 0 || nReqYOff != 0 || nReqXSize != l_band->GetXSize() ||
1887 51 : nReqYSize != l_band->GetYSize())
1888 : {
1889 0 : *pbSuccess = FALSE;
1890 0 : return 0;
1891 : }
1892 :
1893 51 : const double dfVal = l_band->GetMaximum(pbSuccess);
1894 51 : if (NeedMaxValAdjustment() && dfVal > m_nMaxValue)
1895 2 : return m_nMaxValue;
1896 49 : return dfVal;
1897 : }
1898 :
1899 : /************************************************************************/
1900 : /* GetHistogram() */
1901 : /************************************************************************/
1902 :
1903 4 : CPLErr VRTSimpleSource::GetHistogram(int nXSize, int nYSize, double dfMin,
1904 : double dfMax, int nBuckets,
1905 : GUIntBig *panHistogram,
1906 : int bIncludeOutOfRange, int bApproxOK,
1907 : GDALProgressFunc pfnProgress,
1908 : void *pProgressData)
1909 : {
1910 : // The window we will actually request from the source raster band.
1911 4 : double dfReqXOff = 0.0;
1912 4 : double dfReqYOff = 0.0;
1913 4 : double dfReqXSize = 0.0;
1914 4 : double dfReqYSize = 0.0;
1915 4 : int nReqXOff = 0;
1916 4 : int nReqYOff = 0;
1917 4 : int nReqXSize = 0;
1918 4 : int nReqYSize = 0;
1919 :
1920 : // The window we will actual set _within_ the pData buffer.
1921 4 : int nOutXOff = 0;
1922 4 : int nOutYOff = 0;
1923 4 : int nOutXSize = 0;
1924 4 : int nOutYSize = 0;
1925 :
1926 4 : bool bError = false;
1927 4 : auto l_band = GetRasterBand();
1928 4 : if (!l_band || NeedMaxValAdjustment() ||
1929 4 : !GetSrcDstWindow(0, 0, nXSize, nYSize, nXSize, nYSize,
1930 : GRIORA_NearestNeighbour, &dfReqXOff, &dfReqYOff,
1931 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
1932 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
1933 4 : &nOutXSize, &nOutYSize, bError) ||
1934 4 : nReqXOff != 0 || nReqYOff != 0 || nReqXSize != l_band->GetXSize() ||
1935 4 : nReqYSize != l_band->GetYSize() || nOutXOff != 0 || nOutYOff != 0 ||
1936 8 : nOutXSize != nXSize || nOutYSize != nYSize)
1937 : {
1938 1 : return CE_Failure;
1939 : }
1940 :
1941 3 : return l_band->GetHistogram(dfMin, dfMax, nBuckets, panHistogram,
1942 : bIncludeOutOfRange, bApproxOK, pfnProgress,
1943 3 : pProgressData);
1944 : }
1945 :
1946 : /************************************************************************/
1947 : /* DatasetRasterIO() */
1948 : /************************************************************************/
1949 :
1950 14017 : CPLErr VRTSimpleSource::DatasetRasterIO(
1951 : GDALDataType eVRTBandDataType, int nXOff, int nYOff, int nXSize, int nYSize,
1952 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
1953 : int nBandCount, const int *panBandMap, GSpacing nPixelSpace,
1954 : GSpacing nLineSpace, GSpacing nBandSpace,
1955 : GDALRasterIOExtraArg *psExtraArgIn)
1956 : {
1957 14017 : if (GetType() != VRTSimpleSource::GetTypeStatic())
1958 : {
1959 0 : CPLError(CE_Failure, CPLE_NotSupported,
1960 0 : "DatasetRasterIO() not implemented for %s", GetType());
1961 0 : return CE_Failure;
1962 : }
1963 :
1964 : GDALRasterIOExtraArg sExtraArg;
1965 14017 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
1966 14017 : GDALRasterIOExtraArg *psExtraArg = &sExtraArg;
1967 :
1968 14017 : double dfXOff = nXOff;
1969 14017 : double dfYOff = nYOff;
1970 14017 : double dfXSize = nXSize;
1971 14017 : double dfYSize = nYSize;
1972 14017 : if (psExtraArgIn != nullptr && psExtraArgIn->bFloatingPointWindowValidity)
1973 : {
1974 591 : dfXOff = psExtraArgIn->dfXOff;
1975 591 : dfYOff = psExtraArgIn->dfYOff;
1976 591 : dfXSize = psExtraArgIn->dfXSize;
1977 591 : dfYSize = psExtraArgIn->dfYSize;
1978 : }
1979 :
1980 : // The window we will actually request from the source raster band.
1981 14017 : double dfReqXOff = 0.0;
1982 14017 : double dfReqYOff = 0.0;
1983 14017 : double dfReqXSize = 0.0;
1984 14017 : double dfReqYSize = 0.0;
1985 14017 : int nReqXOff = 0;
1986 14017 : int nReqYOff = 0;
1987 14017 : int nReqXSize = 0;
1988 14017 : int nReqYSize = 0;
1989 :
1990 : // The window we will actual set _within_ the pData buffer.
1991 14017 : int nOutXOff = 0;
1992 14017 : int nOutYOff = 0;
1993 14017 : int nOutXSize = 0;
1994 14017 : int nOutYSize = 0;
1995 :
1996 14017 : if (!m_osResampling.empty())
1997 : {
1998 9236 : psExtraArg->eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
1999 : }
2000 4781 : else if (psExtraArgIn != nullptr)
2001 : {
2002 4781 : psExtraArg->eResampleAlg = psExtraArgIn->eResampleAlg;
2003 : }
2004 :
2005 14017 : bool bError = false;
2006 14017 : if (!GetSrcDstWindow(dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
2007 : psExtraArg->eResampleAlg, &dfReqXOff, &dfReqYOff,
2008 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
2009 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
2010 : &nOutXSize, &nOutYSize, bError))
2011 : {
2012 3783 : return bError ? CE_Failure : CE_None;
2013 : }
2014 :
2015 10234 : auto l_band = GetRasterBand();
2016 10234 : if (!l_band)
2017 0 : return CE_Failure;
2018 :
2019 10234 : GDALDataset *poDS = l_band->GetDataset();
2020 10234 : if (poDS == nullptr)
2021 0 : return CE_Failure;
2022 :
2023 10234 : psExtraArg->bFloatingPointWindowValidity = TRUE;
2024 10234 : psExtraArg->dfXOff = dfReqXOff;
2025 10234 : psExtraArg->dfYOff = dfReqYOff;
2026 10234 : psExtraArg->dfXSize = dfReqXSize;
2027 10234 : psExtraArg->dfYSize = dfReqYSize;
2028 10234 : if (psExtraArgIn)
2029 : {
2030 10234 : psExtraArg->pfnProgress = psExtraArgIn->pfnProgress;
2031 10234 : psExtraArg->pProgressData = psExtraArgIn->pProgressData;
2032 10234 : if (psExtraArgIn->nVersion >= 2)
2033 : {
2034 10234 : psExtraArg->bUseOnlyThisScale = psExtraArgIn->bUseOnlyThisScale;
2035 : }
2036 10234 : psExtraArg->bOperateInBufType =
2037 10234 : GDAL_GET_OPERATE_IN_BUF_TYPE(*psExtraArgIn);
2038 : }
2039 :
2040 10234 : GByte *pabyOut = static_cast<unsigned char *>(pData) +
2041 10234 : nOutXOff * nPixelSpace +
2042 10234 : static_cast<GPtrDiff_t>(nOutYOff) * nLineSpace;
2043 :
2044 10234 : CPLErr eErr = CE_Failure;
2045 :
2046 10234 : if (GDALDataTypeIsConversionLossy(l_band->GetRasterDataType(),
2047 10234 : eVRTBandDataType))
2048 : {
2049 4 : if (!psExtraArg->bOperateInBufType)
2050 : {
2051 1 : const int nBandDTSize = GDALGetDataTypeSizeBytes(eVRTBandDataType);
2052 1 : void *pTemp = VSI_MALLOC3_VERBOSE(
2053 : nOutXSize, nOutYSize,
2054 : cpl::fits_on<int>(nBandDTSize * nBandCount));
2055 1 : if (pTemp)
2056 : {
2057 1 : eErr = poDS->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize,
2058 : nReqYSize, pTemp, nOutXSize, nOutYSize,
2059 : eVRTBandDataType, nBandCount, panBandMap,
2060 : 0, 0, 0, psExtraArg);
2061 1 : if (eErr == CE_None)
2062 : {
2063 1 : GByte *pabyTemp = static_cast<GByte *>(pTemp);
2064 1 : const size_t nSrcBandSpace =
2065 1 : static_cast<size_t>(nOutYSize) * nOutXSize *
2066 1 : nBandDTSize;
2067 3 : for (int iBand = 0; iBand < nBandCount; iBand++)
2068 : {
2069 6 : for (int iY = 0; iY < nOutYSize; iY++)
2070 : {
2071 4 : GDALCopyWords(pabyTemp + iBand * nSrcBandSpace +
2072 4 : static_cast<size_t>(iY) *
2073 4 : nBandDTSize * nOutXSize,
2074 : eVRTBandDataType, nBandDTSize,
2075 4 : pabyOut + static_cast<GPtrDiff_t>(
2076 4 : iY * nLineSpace +
2077 4 : iBand * nBandSpace),
2078 : eBufType,
2079 : static_cast<int>(nPixelSpace),
2080 : nOutXSize);
2081 : }
2082 : }
2083 : }
2084 1 : VSIFree(pTemp);
2085 : }
2086 : }
2087 : else
2088 : {
2089 3 : eErr = poDS->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize,
2090 : nReqYSize, pabyOut, nOutXSize, nOutYSize,
2091 : eBufType, nBandCount, panBandMap, nPixelSpace,
2092 : nLineSpace, nBandSpace, psExtraArg);
2093 3 : if (eErr == CE_None)
2094 : {
2095 9 : for (int k = 0; k < nBandCount; k++)
2096 : {
2097 180 : for (int j = 0; j < nOutYSize; j++)
2098 : {
2099 8082 : for (int i = 0; i < nOutXSize; i++)
2100 : {
2101 7908 : void *pDst = pabyOut + k * nBandSpace +
2102 7908 : j * nLineSpace + i * nPixelSpace;
2103 7908 : GDALClampValueToType(pDst, eBufType,
2104 : eVRTBandDataType);
2105 : }
2106 : }
2107 : }
2108 : }
2109 : }
2110 : }
2111 : else
2112 : {
2113 10230 : eErr = poDS->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
2114 : pabyOut, nOutXSize, nOutYSize, eBufType,
2115 : nBandCount, panBandMap, nPixelSpace, nLineSpace,
2116 : nBandSpace, psExtraArg);
2117 : }
2118 :
2119 10234 : if (NeedMaxValAdjustment())
2120 : {
2121 0 : for (int k = 0; k < nBandCount; k++)
2122 : {
2123 0 : for (int j = 0; j < nOutYSize; j++)
2124 : {
2125 0 : for (int i = 0; i < nOutXSize; i++)
2126 : {
2127 0 : void *pDst = pabyOut + k * nBandSpace + j * nLineSpace +
2128 0 : i * nPixelSpace;
2129 0 : int nVal = 0;
2130 0 : CopyWordIn(pDst, eBufType, &nVal, GDT_Int32);
2131 :
2132 0 : if (nVal > m_nMaxValue)
2133 0 : nVal = m_nMaxValue;
2134 :
2135 0 : CopyWordOut(&nVal, GDT_Int32, pDst, eBufType);
2136 : }
2137 : }
2138 : }
2139 : }
2140 :
2141 10234 : if (psExtraArg->pfnProgress)
2142 9669 : psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
2143 :
2144 10234 : return eErr;
2145 : }
2146 :
2147 : /************************************************************************/
2148 : /* SetResampling() */
2149 : /************************************************************************/
2150 :
2151 70643 : void VRTSimpleSource::SetResampling(const char *pszResampling)
2152 : {
2153 70643 : m_osResampling = (pszResampling) ? pszResampling : "";
2154 70643 : }
2155 :
2156 : /************************************************************************/
2157 : /* ==================================================================== */
2158 : /* VRTAveragedSource */
2159 : /* ==================================================================== */
2160 : /************************************************************************/
2161 :
2162 : /************************************************************************/
2163 : /* VRTAveragedSource() */
2164 : /************************************************************************/
2165 :
2166 16 : VRTAveragedSource::VRTAveragedSource()
2167 : {
2168 16 : }
2169 :
2170 : /************************************************************************/
2171 : /* GetTypeStatic() */
2172 : /************************************************************************/
2173 :
2174 103856 : const char *VRTAveragedSource::GetTypeStatic()
2175 : {
2176 : static const char *TYPE = "AveragedSource";
2177 103856 : return TYPE;
2178 : }
2179 :
2180 : /************************************************************************/
2181 : /* GetType() */
2182 : /************************************************************************/
2183 :
2184 11 : const char *VRTAveragedSource::GetType() const
2185 : {
2186 11 : return GetTypeStatic();
2187 : }
2188 :
2189 : /************************************************************************/
2190 : /* SerializeToXML() */
2191 : /************************************************************************/
2192 :
2193 0 : CPLXMLNode *VRTAveragedSource::SerializeToXML(const char *pszVRTPath)
2194 :
2195 : {
2196 0 : CPLXMLNode *const psSrc = VRTSimpleSource::SerializeToXML(pszVRTPath);
2197 :
2198 0 : if (psSrc == nullptr)
2199 0 : return nullptr;
2200 :
2201 0 : CPLFree(psSrc->pszValue);
2202 0 : psSrc->pszValue = CPLStrdup(GetTypeStatic());
2203 :
2204 0 : return psSrc;
2205 : }
2206 :
2207 : /************************************************************************/
2208 : /* SetNoDataValue() */
2209 : /************************************************************************/
2210 :
2211 0 : void VRTAveragedSource::SetNoDataValue(double dfNewNoDataValue)
2212 :
2213 : {
2214 0 : if (dfNewNoDataValue == VRT_NODATA_UNSET)
2215 : {
2216 0 : m_bNoDataSet = FALSE;
2217 0 : m_dfNoDataValue = VRT_NODATA_UNSET;
2218 0 : return;
2219 : }
2220 :
2221 0 : m_bNoDataSet = TRUE;
2222 0 : m_dfNoDataValue = dfNewNoDataValue;
2223 : }
2224 :
2225 : /************************************************************************/
2226 : /* RasterIO() */
2227 : /************************************************************************/
2228 :
2229 33 : CPLErr VRTAveragedSource::RasterIO(GDALDataType /*eVRTBandDataType*/, int nXOff,
2230 : int nYOff, int nXSize, int nYSize,
2231 : void *pData, int nBufXSize, int nBufYSize,
2232 : GDALDataType eBufType, GSpacing nPixelSpace,
2233 : GSpacing nLineSpace,
2234 : GDALRasterIOExtraArg *psExtraArgIn,
2235 : WorkingState & /*oWorkingState*/)
2236 :
2237 : {
2238 : GDALRasterIOExtraArg sExtraArg;
2239 33 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
2240 33 : GDALRasterIOExtraArg *psExtraArg = &sExtraArg;
2241 :
2242 33 : double dfXOff = nXOff;
2243 33 : double dfYOff = nYOff;
2244 33 : double dfXSize = nXSize;
2245 33 : double dfYSize = nYSize;
2246 33 : if (psExtraArgIn != nullptr && psExtraArgIn->bFloatingPointWindowValidity)
2247 : {
2248 0 : dfXOff = psExtraArgIn->dfXOff;
2249 0 : dfYOff = psExtraArgIn->dfYOff;
2250 0 : dfXSize = psExtraArgIn->dfXSize;
2251 0 : dfYSize = psExtraArgIn->dfYSize;
2252 : }
2253 :
2254 : // The window we will actually request from the source raster band.
2255 33 : double dfReqXOff = 0.0;
2256 33 : double dfReqYOff = 0.0;
2257 33 : double dfReqXSize = 0.0;
2258 33 : double dfReqYSize = 0.0;
2259 33 : int nReqXOff = 0;
2260 33 : int nReqYOff = 0;
2261 33 : int nReqXSize = 0;
2262 33 : int nReqYSize = 0;
2263 :
2264 : // The window we will actual set _within_ the pData buffer.
2265 33 : int nOutXOff = 0;
2266 33 : int nOutYOff = 0;
2267 33 : int nOutXSize = 0;
2268 33 : int nOutYSize = 0;
2269 :
2270 33 : if (!m_osResampling.empty())
2271 : {
2272 28 : psExtraArg->eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
2273 : }
2274 5 : else if (psExtraArgIn != nullptr)
2275 : {
2276 5 : psExtraArg->eResampleAlg = psExtraArgIn->eResampleAlg;
2277 : }
2278 :
2279 33 : bool bError = false;
2280 33 : if (!GetSrcDstWindow(dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
2281 : psExtraArg->eResampleAlg, &dfReqXOff, &dfReqYOff,
2282 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
2283 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
2284 : &nOutXSize, &nOutYSize, bError))
2285 : {
2286 0 : return bError ? CE_Failure : CE_None;
2287 : }
2288 :
2289 33 : auto l_band = GetRasterBand();
2290 33 : if (!l_band)
2291 0 : return CE_Failure;
2292 :
2293 : /* -------------------------------------------------------------------- */
2294 : /* Allocate a temporary buffer to whole the full resolution */
2295 : /* data from the area of interest. */
2296 : /* -------------------------------------------------------------------- */
2297 : float *const pafSrc = static_cast<float *>(
2298 33 : VSI_MALLOC3_VERBOSE(sizeof(float), nReqXSize, nReqYSize));
2299 33 : if (pafSrc == nullptr)
2300 : {
2301 0 : return CE_Failure;
2302 : }
2303 :
2304 : /* -------------------------------------------------------------------- */
2305 : /* Load it. */
2306 : /* -------------------------------------------------------------------- */
2307 33 : psExtraArg->bFloatingPointWindowValidity = TRUE;
2308 33 : psExtraArg->dfXOff = dfReqXOff;
2309 33 : psExtraArg->dfYOff = dfReqYOff;
2310 33 : psExtraArg->dfXSize = dfReqXSize;
2311 33 : psExtraArg->dfYSize = dfReqYSize;
2312 33 : if (psExtraArgIn)
2313 : {
2314 33 : psExtraArg->pfnProgress = psExtraArgIn->pfnProgress;
2315 33 : psExtraArg->pProgressData = psExtraArgIn->pProgressData;
2316 33 : if (psExtraArgIn->nVersion >= 2)
2317 : {
2318 33 : psExtraArg->bUseOnlyThisScale = psExtraArgIn->bUseOnlyThisScale;
2319 : }
2320 : }
2321 :
2322 33 : const CPLErr eErr = l_band->RasterIO(
2323 : GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize, pafSrc, nReqXSize,
2324 : nReqYSize, GDT_Float32, 0, 0, psExtraArg);
2325 :
2326 33 : if (eErr != CE_None)
2327 : {
2328 0 : VSIFree(pafSrc);
2329 0 : return eErr;
2330 : }
2331 :
2332 : /* -------------------------------------------------------------------- */
2333 : /* Do the averaging. */
2334 : /* -------------------------------------------------------------------- */
2335 5956 : for (int iBufLine = nOutYOff; iBufLine < nOutYOff + nOutYSize; iBufLine++)
2336 : {
2337 5923 : const double dfYDst =
2338 5923 : (iBufLine / static_cast<double>(nBufYSize)) * nYSize + nYOff;
2339 :
2340 2017080 : for (int iBufPixel = nOutXOff; iBufPixel < nOutXOff + nOutXSize;
2341 : iBufPixel++)
2342 : {
2343 : double dfXSrcStart, dfXSrcEnd, dfYSrcStart, dfYSrcEnd;
2344 : int iXSrcStart, iYSrcStart, iXSrcEnd, iYSrcEnd;
2345 :
2346 2011160 : const double dfXDst =
2347 2011160 : (iBufPixel / static_cast<double>(nBufXSize)) * nXSize + nXOff;
2348 :
2349 : // Compute the source image rectangle needed for this pixel.
2350 2011160 : DstToSrc(dfXDst, dfYDst, dfXSrcStart, dfYSrcStart);
2351 2011160 : DstToSrc(dfXDst + 1.0, dfYDst + 1.0, dfXSrcEnd, dfYSrcEnd);
2352 :
2353 : // Convert to integers, assuming that the center of the source
2354 : // pixel must be in our rect to get included.
2355 2011160 : if (dfXSrcEnd >= dfXSrcStart + 1)
2356 : {
2357 1049560 : iXSrcStart = static_cast<int>(floor(dfXSrcStart + 0.5));
2358 1049560 : iXSrcEnd = static_cast<int>(floor(dfXSrcEnd + 0.5));
2359 : }
2360 : else
2361 : {
2362 : /* If the resampling factor is less than 100%, the distance */
2363 : /* between the source pixel is < 1, so we stick to nearest */
2364 : /* neighbour */
2365 961600 : iXSrcStart = static_cast<int>(floor(dfXSrcStart));
2366 961600 : iXSrcEnd = iXSrcStart + 1;
2367 : }
2368 2011160 : if (dfYSrcEnd >= dfYSrcStart + 1)
2369 : {
2370 1049560 : iYSrcStart = static_cast<int>(floor(dfYSrcStart + 0.5));
2371 1049560 : iYSrcEnd = static_cast<int>(floor(dfYSrcEnd + 0.5));
2372 : }
2373 : else
2374 : {
2375 961600 : iYSrcStart = static_cast<int>(floor(dfYSrcStart));
2376 961600 : iYSrcEnd = iYSrcStart + 1;
2377 : }
2378 :
2379 : // Transform into the coordinate system of the source *buffer*
2380 2011160 : iXSrcStart -= nReqXOff;
2381 2011160 : iYSrcStart -= nReqYOff;
2382 2011160 : iXSrcEnd -= nReqXOff;
2383 2011160 : iYSrcEnd -= nReqYOff;
2384 :
2385 2011160 : double dfSum = 0.0;
2386 2011160 : int nPixelCount = 0;
2387 :
2388 4022510 : for (int iY = iYSrcStart; iY < iYSrcEnd; iY++)
2389 : {
2390 2011360 : if (iY < 0 || iY >= nReqYSize)
2391 0 : continue;
2392 :
2393 4023130 : for (int iX = iXSrcStart; iX < iXSrcEnd; iX++)
2394 : {
2395 2011780 : if (iX < 0 || iX >= nReqXSize)
2396 0 : continue;
2397 :
2398 2011780 : const float fSampledValue =
2399 2011780 : pafSrc[iX + static_cast<size_t>(iY) * nReqXSize];
2400 2011780 : if (std::isnan(fSampledValue))
2401 0 : continue;
2402 :
2403 4023550 : if (m_bNoDataSet &&
2404 2011780 : GDALIsValueInRange<float>(m_dfNoDataValue) &&
2405 0 : ARE_REAL_EQUAL(fSampledValue,
2406 0 : static_cast<float>(m_dfNoDataValue)))
2407 0 : continue;
2408 :
2409 2011780 : nPixelCount++;
2410 2011780 : dfSum += pafSrc[iX + static_cast<size_t>(iY) * nReqXSize];
2411 : }
2412 : }
2413 :
2414 2011160 : if (nPixelCount == 0)
2415 0 : continue;
2416 :
2417 : // Compute output value.
2418 2011160 : const float dfOutputValue = static_cast<float>(dfSum / nPixelCount);
2419 :
2420 : // Put it in the output buffer.
2421 2011160 : GByte *pDstLocation =
2422 2011160 : static_cast<GByte *>(pData) + nPixelSpace * iBufPixel +
2423 2011160 : static_cast<GPtrDiff_t>(nLineSpace) * iBufLine;
2424 :
2425 2011160 : if (eBufType == GDT_UInt8)
2426 2008660 : *pDstLocation = static_cast<GByte>(
2427 2008660 : std::min(255.0, std::max(0.0, dfOutputValue + 0.5)));
2428 : else
2429 2500 : GDALCopyWords(&dfOutputValue, GDT_Float32, 4, pDstLocation,
2430 : eBufType, 8, 1);
2431 : }
2432 : }
2433 :
2434 33 : VSIFree(pafSrc);
2435 :
2436 33 : if (psExtraArg->pfnProgress)
2437 0 : psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
2438 :
2439 33 : return CE_None;
2440 : }
2441 :
2442 : /************************************************************************/
2443 : /* GetMinimum() */
2444 : /************************************************************************/
2445 :
2446 0 : double VRTAveragedSource::GetMinimum(int /* nXSize */, int /* nYSize */,
2447 : int *pbSuccess)
2448 : {
2449 0 : *pbSuccess = FALSE;
2450 0 : return 0.0;
2451 : }
2452 :
2453 : /************************************************************************/
2454 : /* GetMaximum() */
2455 : /************************************************************************/
2456 :
2457 0 : double VRTAveragedSource::GetMaximum(int /* nXSize */, int /* nYSize */,
2458 : int *pbSuccess)
2459 : {
2460 0 : *pbSuccess = FALSE;
2461 0 : return 0.0;
2462 : }
2463 :
2464 : /************************************************************************/
2465 : /* GetHistogram() */
2466 : /************************************************************************/
2467 :
2468 0 : CPLErr VRTAveragedSource::GetHistogram(
2469 : int /* nXSize */, int /* nYSize */, double /* dfMin */, double /* dfMax */,
2470 : int /* nBuckets */, GUIntBig * /* panHistogram */,
2471 : int /* bIncludeOutOfRange */, int /* bApproxOK */,
2472 : GDALProgressFunc /* pfnProgress */, void * /* pProgressData */)
2473 : {
2474 0 : return CE_Failure;
2475 : }
2476 :
2477 : /************************************************************************/
2478 : /* ==================================================================== */
2479 : /* VRTNoDataFromMaskSource */
2480 : /* ==================================================================== */
2481 : /************************************************************************/
2482 :
2483 : /************************************************************************/
2484 : /* VRTNoDataFromMaskSource() */
2485 : /************************************************************************/
2486 :
2487 23 : VRTNoDataFromMaskSource::VRTNoDataFromMaskSource()
2488 : {
2489 23 : }
2490 :
2491 : /************************************************************************/
2492 : /* XMLInit() */
2493 : /************************************************************************/
2494 :
2495 : CPLErr
2496 8 : VRTNoDataFromMaskSource::XMLInit(const CPLXMLNode *psSrc,
2497 : const char *pszVRTPath,
2498 : VRTMapSharedResources &oMapSharedSources)
2499 :
2500 : {
2501 : /* -------------------------------------------------------------------- */
2502 : /* Do base initialization. */
2503 : /* -------------------------------------------------------------------- */
2504 : {
2505 : const CPLErr eErr =
2506 8 : VRTSimpleSource::XMLInit(psSrc, pszVRTPath, oMapSharedSources);
2507 8 : if (eErr != CE_None)
2508 0 : return eErr;
2509 : }
2510 :
2511 8 : if (const char *pszNODATA = CPLGetXMLValue(psSrc, "NODATA", nullptr))
2512 : {
2513 8 : m_bNoDataSet = true;
2514 8 : m_dfNoDataValue = CPLAtofM(pszNODATA);
2515 : }
2516 :
2517 8 : m_dfMaskValueThreshold =
2518 8 : CPLAtofM(CPLGetXMLValue(psSrc, "MaskValueThreshold", "0"));
2519 :
2520 8 : if (const char *pszRemappedValue =
2521 8 : CPLGetXMLValue(psSrc, "RemappedValue", nullptr))
2522 : {
2523 0 : m_bHasRemappedValue = true;
2524 0 : m_dfRemappedValue = CPLAtofM(pszRemappedValue);
2525 : }
2526 :
2527 8 : return CE_None;
2528 : }
2529 :
2530 : /************************************************************************/
2531 : /* GetTypeStatic() */
2532 : /************************************************************************/
2533 :
2534 27 : const char *VRTNoDataFromMaskSource::GetTypeStatic()
2535 : {
2536 : static const char *TYPE = "NoDataFromMaskSource";
2537 27 : return TYPE;
2538 : }
2539 :
2540 : /************************************************************************/
2541 : /* GetType() */
2542 : /************************************************************************/
2543 :
2544 11 : const char *VRTNoDataFromMaskSource::GetType() const
2545 : {
2546 11 : return GetTypeStatic();
2547 : }
2548 :
2549 : /************************************************************************/
2550 : /* SerializeToXML() */
2551 : /************************************************************************/
2552 :
2553 8 : CPLXMLNode *VRTNoDataFromMaskSource::SerializeToXML(const char *pszVRTPath)
2554 :
2555 : {
2556 8 : CPLXMLNode *const psSrc = VRTSimpleSource::SerializeToXML(pszVRTPath);
2557 :
2558 8 : if (psSrc == nullptr)
2559 0 : return nullptr;
2560 :
2561 8 : CPLFree(psSrc->pszValue);
2562 8 : psSrc->pszValue = CPLStrdup(GetTypeStatic());
2563 :
2564 8 : if (m_bNoDataSet)
2565 : {
2566 8 : CPLSetXMLValue(psSrc, "MaskValueThreshold",
2567 : CPLSPrintf("%.17g", m_dfMaskValueThreshold));
2568 :
2569 8 : GDALDataType eBandDT = GDT_Unknown;
2570 8 : double dfNoDataValue = m_dfNoDataValue;
2571 8 : const auto kMaxFloat = std::numeric_limits<float>::max();
2572 8 : if (std::fabs(std::fabs(m_dfNoDataValue) - kMaxFloat) <
2573 : 1e-10 * kMaxFloat)
2574 : {
2575 0 : auto l_band = GetRasterBand();
2576 0 : if (l_band)
2577 : {
2578 0 : eBandDT = l_band->GetRasterDataType();
2579 0 : if (eBandDT == GDT_Float32)
2580 : {
2581 : dfNoDataValue =
2582 0 : GDALAdjustNoDataCloseToFloatMax(m_dfNoDataValue);
2583 : }
2584 : }
2585 : }
2586 8 : CPLSetXMLValue(psSrc, "NODATA",
2587 16 : VRTSerializeNoData(dfNoDataValue, eBandDT, 18).c_str());
2588 : }
2589 :
2590 8 : if (m_bHasRemappedValue)
2591 : {
2592 0 : CPLSetXMLValue(psSrc, "RemappedValue",
2593 : CPLSPrintf("%.17g", m_dfRemappedValue));
2594 : }
2595 :
2596 8 : return psSrc;
2597 : }
2598 :
2599 : /************************************************************************/
2600 : /* SetParameters() */
2601 : /************************************************************************/
2602 :
2603 15 : void VRTNoDataFromMaskSource::SetParameters(double dfNoDataValue,
2604 : double dfMaskValueThreshold)
2605 : {
2606 15 : m_bNoDataSet = true;
2607 15 : m_dfNoDataValue = dfNoDataValue;
2608 15 : m_dfMaskValueThreshold = dfMaskValueThreshold;
2609 15 : if (!m_bHasRemappedValue)
2610 15 : m_dfRemappedValue = m_dfNoDataValue;
2611 15 : }
2612 :
2613 : /************************************************************************/
2614 : /* SetParameters() */
2615 : /************************************************************************/
2616 :
2617 0 : void VRTNoDataFromMaskSource::SetParameters(double dfNoDataValue,
2618 : double dfMaskValueThreshold,
2619 : double dfRemappedValue)
2620 : {
2621 0 : SetParameters(dfNoDataValue, dfMaskValueThreshold);
2622 0 : m_bHasRemappedValue = true;
2623 0 : m_dfRemappedValue = dfRemappedValue;
2624 0 : }
2625 :
2626 : /************************************************************************/
2627 : /* RasterIO() */
2628 : /************************************************************************/
2629 :
2630 13 : CPLErr VRTNoDataFromMaskSource::RasterIO(
2631 : GDALDataType eVRTBandDataType, int nXOff, int nYOff, int nXSize, int nYSize,
2632 : void *pData, int nBufXSize, int nBufYSize, GDALDataType eBufType,
2633 : GSpacing nPixelSpace, GSpacing nLineSpace,
2634 : GDALRasterIOExtraArg *psExtraArgIn, WorkingState &oWorkingState)
2635 :
2636 : {
2637 13 : if (!m_bNoDataSet)
2638 : {
2639 0 : return VRTSimpleSource::RasterIO(eVRTBandDataType, nXOff, nYOff, nXSize,
2640 : nYSize, pData, nBufXSize, nBufYSize,
2641 : eBufType, nPixelSpace, nLineSpace,
2642 0 : psExtraArgIn, oWorkingState);
2643 : }
2644 :
2645 : GDALRasterIOExtraArg sExtraArg;
2646 13 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
2647 13 : GDALRasterIOExtraArg *psExtraArg = &sExtraArg;
2648 :
2649 13 : double dfXOff = nXOff;
2650 13 : double dfYOff = nYOff;
2651 13 : double dfXSize = nXSize;
2652 13 : double dfYSize = nYSize;
2653 13 : if (psExtraArgIn != nullptr && psExtraArgIn->bFloatingPointWindowValidity)
2654 : {
2655 0 : dfXOff = psExtraArgIn->dfXOff;
2656 0 : dfYOff = psExtraArgIn->dfYOff;
2657 0 : dfXSize = psExtraArgIn->dfXSize;
2658 0 : dfYSize = psExtraArgIn->dfYSize;
2659 : }
2660 :
2661 : // The window we will actually request from the source raster band.
2662 13 : double dfReqXOff = 0.0;
2663 13 : double dfReqYOff = 0.0;
2664 13 : double dfReqXSize = 0.0;
2665 13 : double dfReqYSize = 0.0;
2666 13 : int nReqXOff = 0;
2667 13 : int nReqYOff = 0;
2668 13 : int nReqXSize = 0;
2669 13 : int nReqYSize = 0;
2670 :
2671 : // The window we will actual set _within_ the pData buffer.
2672 13 : int nOutXOff = 0;
2673 13 : int nOutYOff = 0;
2674 13 : int nOutXSize = 0;
2675 13 : int nOutYSize = 0;
2676 :
2677 13 : if (!m_osResampling.empty())
2678 : {
2679 0 : psExtraArg->eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
2680 : }
2681 13 : else if (psExtraArgIn != nullptr)
2682 : {
2683 13 : psExtraArg->eResampleAlg = psExtraArgIn->eResampleAlg;
2684 : }
2685 :
2686 13 : bool bError = false;
2687 13 : if (!GetSrcDstWindow(dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
2688 : psExtraArg->eResampleAlg, &dfReqXOff, &dfReqYOff,
2689 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
2690 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
2691 : &nOutXSize, &nOutYSize, bError))
2692 : {
2693 0 : return bError ? CE_Failure : CE_None;
2694 : }
2695 :
2696 13 : auto l_band = GetRasterBand();
2697 13 : if (!l_band)
2698 0 : return CE_Failure;
2699 :
2700 : /* -------------------------------------------------------------------- */
2701 : /* Allocate temporary buffer(s). */
2702 : /* -------------------------------------------------------------------- */
2703 13 : const auto eSrcBandDT = l_band->GetRasterDataType();
2704 13 : const int nSrcBandDTSize = GDALGetDataTypeSizeBytes(eSrcBandDT);
2705 13 : const auto eSrcMaskBandDT = l_band->GetMaskBand()->GetRasterDataType();
2706 13 : const int nSrcMaskBandDTSize = GDALGetDataTypeSizeBytes(eSrcMaskBandDT);
2707 13 : double dfRemappedValue = m_dfRemappedValue;
2708 13 : if (!m_bHasRemappedValue)
2709 : {
2710 19 : if (eSrcBandDT == GDT_UInt8 &&
2711 6 : m_dfNoDataValue >= std::numeric_limits<GByte>::min() &&
2712 25 : m_dfNoDataValue <= std::numeric_limits<GByte>::max() &&
2713 6 : static_cast<int>(m_dfNoDataValue) == m_dfNoDataValue)
2714 : {
2715 6 : if (m_dfNoDataValue == std::numeric_limits<GByte>::max())
2716 1 : dfRemappedValue = m_dfNoDataValue - 1;
2717 : else
2718 5 : dfRemappedValue = m_dfNoDataValue + 1;
2719 : }
2720 10 : else if (eSrcBandDT == GDT_UInt16 &&
2721 3 : m_dfNoDataValue >= std::numeric_limits<uint16_t>::min() &&
2722 13 : m_dfNoDataValue <= std::numeric_limits<uint16_t>::max() &&
2723 3 : static_cast<int>(m_dfNoDataValue) == m_dfNoDataValue)
2724 : {
2725 3 : if (m_dfNoDataValue == std::numeric_limits<uint16_t>::max())
2726 1 : dfRemappedValue = m_dfNoDataValue - 1;
2727 : else
2728 2 : dfRemappedValue = m_dfNoDataValue + 1;
2729 : }
2730 6 : else if (eSrcBandDT == GDT_Int16 &&
2731 2 : m_dfNoDataValue >= std::numeric_limits<int16_t>::min() &&
2732 8 : m_dfNoDataValue <= std::numeric_limits<int16_t>::max() &&
2733 2 : static_cast<int>(m_dfNoDataValue) == m_dfNoDataValue)
2734 : {
2735 2 : if (m_dfNoDataValue == std::numeric_limits<int16_t>::max())
2736 1 : dfRemappedValue = m_dfNoDataValue - 1;
2737 : else
2738 1 : dfRemappedValue = m_dfNoDataValue + 1;
2739 : }
2740 : else
2741 : {
2742 2 : constexpr double EPS = 1e-3;
2743 2 : if (m_dfNoDataValue == 0)
2744 1 : dfRemappedValue = EPS;
2745 : else
2746 1 : dfRemappedValue = m_dfNoDataValue * (1 + EPS);
2747 : }
2748 : }
2749 13 : const bool bByteOptim =
2750 6 : (eSrcBandDT == GDT_UInt8 && eBufType == GDT_UInt8 &&
2751 5 : eSrcMaskBandDT == GDT_UInt8 && m_dfMaskValueThreshold >= 0 &&
2752 5 : m_dfMaskValueThreshold <= 255 &&
2753 5 : static_cast<int>(m_dfMaskValueThreshold) == m_dfMaskValueThreshold &&
2754 4 : m_dfNoDataValue >= 0 && m_dfNoDataValue <= 255 &&
2755 4 : static_cast<int>(m_dfNoDataValue) == m_dfNoDataValue &&
2756 23 : dfRemappedValue >= 0 && dfRemappedValue <= 255 &&
2757 4 : static_cast<int>(dfRemappedValue) == dfRemappedValue);
2758 : GByte *pabyWrkBuffer;
2759 : try
2760 : {
2761 13 : if (bByteOptim && nOutXOff == 0 && nOutYOff == 0 &&
2762 4 : nOutXSize == nBufXSize && nOutYSize == nBufYSize &&
2763 4 : nPixelSpace == nSrcBandDTSize &&
2764 4 : nLineSpace == nPixelSpace * nBufXSize)
2765 : {
2766 4 : pabyWrkBuffer = static_cast<GByte *>(pData);
2767 : }
2768 : else
2769 : {
2770 9 : oWorkingState.m_abyWrkBuffer.resize(static_cast<size_t>(nOutXSize) *
2771 9 : nOutYSize * nSrcBandDTSize);
2772 : pabyWrkBuffer =
2773 9 : reinterpret_cast<GByte *>(oWorkingState.m_abyWrkBuffer.data());
2774 : }
2775 13 : oWorkingState.m_abyWrkBufferMask.resize(static_cast<size_t>(nOutXSize) *
2776 13 : nOutYSize * nSrcMaskBandDTSize);
2777 : }
2778 0 : catch (const std::exception &)
2779 : {
2780 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
2781 : "Out of memory when allocating buffers");
2782 0 : return CE_Failure;
2783 : }
2784 :
2785 : /* -------------------------------------------------------------------- */
2786 : /* Load data. */
2787 : /* -------------------------------------------------------------------- */
2788 13 : psExtraArg->bFloatingPointWindowValidity = TRUE;
2789 13 : psExtraArg->dfXOff = dfReqXOff;
2790 13 : psExtraArg->dfYOff = dfReqYOff;
2791 13 : psExtraArg->dfXSize = dfReqXSize;
2792 13 : psExtraArg->dfYSize = dfReqYSize;
2793 13 : if (psExtraArgIn)
2794 : {
2795 13 : psExtraArg->pfnProgress = psExtraArgIn->pfnProgress;
2796 13 : psExtraArg->pProgressData = psExtraArgIn->pProgressData;
2797 : }
2798 :
2799 13 : if (l_band->RasterIO(GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
2800 : pabyWrkBuffer, nOutXSize, nOutYSize, eSrcBandDT, 0, 0,
2801 13 : psExtraArg) != CE_None)
2802 : {
2803 0 : return CE_Failure;
2804 : }
2805 :
2806 26 : if (l_band->GetMaskBand()->RasterIO(
2807 : GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
2808 13 : oWorkingState.m_abyWrkBufferMask.data(), nOutXSize, nOutYSize,
2809 13 : eSrcMaskBandDT, 0, 0, psExtraArg) != CE_None)
2810 : {
2811 0 : return CE_Failure;
2812 : }
2813 :
2814 : /* -------------------------------------------------------------------- */
2815 : /* Do the processing. */
2816 : /* -------------------------------------------------------------------- */
2817 :
2818 13 : GByte *const pabyOut = static_cast<GByte *>(pData) +
2819 13 : nPixelSpace * nOutXOff +
2820 13 : static_cast<GPtrDiff_t>(nLineSpace) * nOutYOff;
2821 13 : if (bByteOptim)
2822 : {
2823 : // Special case when everything fits on Byte
2824 4 : const GByte nMaskValueThreshold =
2825 4 : static_cast<GByte>(m_dfMaskValueThreshold);
2826 4 : const GByte nNoDataValue = static_cast<GByte>(m_dfNoDataValue);
2827 4 : const GByte nRemappedValue = static_cast<GByte>(dfRemappedValue);
2828 4 : size_t nSrcIdx = 0;
2829 8 : for (int iY = 0; iY < nOutYSize; iY++)
2830 : {
2831 4 : GSpacing nDstOffset = iY * nLineSpace;
2832 12 : for (int iX = 0; iX < nOutXSize; iX++)
2833 : {
2834 : const GByte nMaskVal =
2835 8 : oWorkingState.m_abyWrkBufferMask[nSrcIdx];
2836 8 : if (nMaskVal <= nMaskValueThreshold)
2837 : {
2838 4 : pabyOut[static_cast<GPtrDiff_t>(nDstOffset)] = nNoDataValue;
2839 : }
2840 : else
2841 : {
2842 4 : if (pabyWrkBuffer[nSrcIdx] == nNoDataValue)
2843 : {
2844 2 : pabyOut[static_cast<GPtrDiff_t>(nDstOffset)] =
2845 : nRemappedValue;
2846 : }
2847 : else
2848 : {
2849 2 : pabyOut[static_cast<GPtrDiff_t>(nDstOffset)] =
2850 2 : pabyWrkBuffer[nSrcIdx];
2851 : }
2852 : }
2853 8 : nDstOffset += nPixelSpace;
2854 8 : nSrcIdx++;
2855 : }
2856 : }
2857 : }
2858 : else
2859 : {
2860 9 : size_t nSrcIdx = 0;
2861 9 : double dfMaskVal = 0;
2862 9 : const int nBufDTSize = GDALGetDataTypeSizeBytes(eBufType);
2863 18 : std::vector<GByte> abyDstNoData(nBufDTSize);
2864 9 : GDALCopyWords(&m_dfNoDataValue, GDT_Float64, 0, abyDstNoData.data(),
2865 : eBufType, 0, 1);
2866 18 : std::vector<GByte> abyRemappedValue(nBufDTSize);
2867 9 : GDALCopyWords(&dfRemappedValue, GDT_Float64, 0, abyRemappedValue.data(),
2868 : eBufType, 0, 1);
2869 18 : for (int iY = 0; iY < nOutYSize; iY++)
2870 : {
2871 9 : GSpacing nDstOffset = iY * nLineSpace;
2872 28 : for (int iX = 0; iX < nOutXSize; iX++)
2873 : {
2874 19 : if (eSrcMaskBandDT == GDT_UInt8)
2875 : {
2876 19 : dfMaskVal = oWorkingState.m_abyWrkBufferMask[nSrcIdx];
2877 : }
2878 : else
2879 : {
2880 0 : GDALCopyWords(oWorkingState.m_abyWrkBufferMask.data() +
2881 0 : nSrcIdx * nSrcMaskBandDTSize,
2882 : eSrcMaskBandDT, 0, &dfMaskVal, GDT_Float64, 0,
2883 : 1);
2884 : }
2885 19 : void *const pDst =
2886 19 : pabyOut + static_cast<GPtrDiff_t>(nDstOffset);
2887 19 : if (!(dfMaskVal > m_dfMaskValueThreshold))
2888 : {
2889 9 : memcpy(pDst, abyDstNoData.data(), nBufDTSize);
2890 : }
2891 : else
2892 : {
2893 10 : const void *const pSrc =
2894 10 : pabyWrkBuffer + nSrcIdx * nSrcBandDTSize;
2895 10 : if (eSrcBandDT == eBufType)
2896 : {
2897 : // coverity[overrun-buffer-arg]
2898 8 : memcpy(pDst, pSrc, nBufDTSize);
2899 : }
2900 : else
2901 : {
2902 2 : GDALCopyWords(pSrc, eSrcBandDT, 0, pDst, eBufType, 0,
2903 : 1);
2904 : }
2905 10 : if (memcmp(pDst, abyDstNoData.data(), nBufDTSize) == 0)
2906 9 : memcpy(pDst, abyRemappedValue.data(), nBufDTSize);
2907 : }
2908 19 : nDstOffset += nPixelSpace;
2909 19 : nSrcIdx++;
2910 : }
2911 : }
2912 : }
2913 :
2914 13 : if (psExtraArg->pfnProgress)
2915 0 : psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
2916 :
2917 13 : return CE_None;
2918 : }
2919 :
2920 : /************************************************************************/
2921 : /* GetMinimum() */
2922 : /************************************************************************/
2923 :
2924 0 : double VRTNoDataFromMaskSource::GetMinimum(int /* nXSize */, int /* nYSize */,
2925 : int *pbSuccess)
2926 : {
2927 0 : *pbSuccess = FALSE;
2928 0 : return 0.0;
2929 : }
2930 :
2931 : /************************************************************************/
2932 : /* GetMaximum() */
2933 : /************************************************************************/
2934 :
2935 0 : double VRTNoDataFromMaskSource::GetMaximum(int /* nXSize */, int /* nYSize */,
2936 : int *pbSuccess)
2937 : {
2938 0 : *pbSuccess = FALSE;
2939 0 : return 0.0;
2940 : }
2941 :
2942 : /************************************************************************/
2943 : /* GetHistogram() */
2944 : /************************************************************************/
2945 :
2946 0 : CPLErr VRTNoDataFromMaskSource::GetHistogram(
2947 : int /* nXSize */, int /* nYSize */, double /* dfMin */, double /* dfMax */,
2948 : int /* nBuckets */, GUIntBig * /* panHistogram */,
2949 : int /* bIncludeOutOfRange */, int /* bApproxOK */,
2950 : GDALProgressFunc /* pfnProgress */, void * /* pProgressData */)
2951 : {
2952 0 : return CE_Failure;
2953 : }
2954 :
2955 : /************************************************************************/
2956 : /* ==================================================================== */
2957 : /* VRTComplexSource */
2958 : /* ==================================================================== */
2959 : /************************************************************************/
2960 :
2961 : /************************************************************************/
2962 : /* VRTComplexSource() */
2963 : /************************************************************************/
2964 :
2965 5 : VRTComplexSource::VRTComplexSource(const VRTComplexSource *poSrcSource,
2966 5 : double dfXDstRatio, double dfYDstRatio)
2967 : : VRTSimpleSource(poSrcSource, dfXDstRatio, dfYDstRatio),
2968 5 : m_nProcessingFlags(poSrcSource->m_nProcessingFlags),
2969 5 : m_dfNoDataValue(poSrcSource->m_dfNoDataValue),
2970 5 : m_osNoDataValueOri(poSrcSource->m_osNoDataValueOri),
2971 5 : m_dfScaleOff(poSrcSource->m_dfScaleOff),
2972 5 : m_dfScaleRatio(poSrcSource->m_dfScaleRatio),
2973 5 : m_bSrcMinMaxDefined(poSrcSource->m_bSrcMinMaxDefined),
2974 5 : m_dfSrcMin(poSrcSource->m_dfSrcMin), m_dfSrcMax(poSrcSource->m_dfSrcMax),
2975 5 : m_dfDstMin(poSrcSource->m_dfDstMin), m_dfDstMax(poSrcSource->m_dfDstMax),
2976 5 : m_dfExponent(poSrcSource->m_dfExponent), m_bClip(poSrcSource->m_bClip),
2977 5 : m_nColorTableComponent(poSrcSource->m_nColorTableComponent),
2978 5 : m_adfLUTInputs(poSrcSource->m_adfLUTInputs),
2979 5 : m_adfLUTOutputs(poSrcSource->m_adfLUTOutputs)
2980 : {
2981 5 : }
2982 :
2983 : /************************************************************************/
2984 : /* GetTypeStatic() */
2985 : /************************************************************************/
2986 :
2987 14773 : const char *VRTComplexSource::GetTypeStatic()
2988 : {
2989 : static const char *TYPE = "ComplexSource";
2990 14773 : return TYPE;
2991 : }
2992 :
2993 : /************************************************************************/
2994 : /* GetType() */
2995 : /************************************************************************/
2996 :
2997 4152 : const char *VRTComplexSource::GetType() const
2998 : {
2999 4152 : return GetTypeStatic();
3000 : }
3001 :
3002 : /************************************************************************/
3003 : /* SetNoDataValue() */
3004 : /************************************************************************/
3005 :
3006 132 : void VRTComplexSource::SetNoDataValue(double dfNewNoDataValue)
3007 :
3008 : {
3009 132 : if (dfNewNoDataValue == VRT_NODATA_UNSET)
3010 : {
3011 0 : m_nProcessingFlags &= ~PROCESSING_FLAG_NODATA;
3012 0 : m_dfNoDataValue = VRT_NODATA_UNSET;
3013 0 : return;
3014 : }
3015 :
3016 132 : m_nProcessingFlags |= PROCESSING_FLAG_NODATA;
3017 132 : m_dfNoDataValue = dfNewNoDataValue;
3018 : }
3019 :
3020 : /************************************************************************/
3021 : /* GetAdjustedNoDataValue() */
3022 : /************************************************************************/
3023 :
3024 10816 : double VRTComplexSource::GetAdjustedNoDataValue() const
3025 : {
3026 10816 : if ((m_nProcessingFlags & PROCESSING_FLAG_NODATA) != 0)
3027 : {
3028 4931 : auto l_band = GetRasterBand();
3029 4931 : if (l_band && l_band->GetRasterDataType() == GDT_Float32)
3030 : {
3031 22 : return GDALAdjustNoDataCloseToFloatMax(m_dfNoDataValue);
3032 : }
3033 : }
3034 10794 : return m_dfNoDataValue;
3035 : }
3036 :
3037 : /************************************************************************/
3038 : /* SerializeToXML() */
3039 : /************************************************************************/
3040 :
3041 98 : CPLXMLNode *VRTComplexSource::SerializeToXML(const char *pszVRTPath)
3042 :
3043 : {
3044 98 : CPLXMLNode *psSrc = VRTSimpleSource::SerializeToXML(pszVRTPath);
3045 :
3046 98 : if (psSrc == nullptr)
3047 0 : return nullptr;
3048 :
3049 98 : CPLFree(psSrc->pszValue);
3050 98 : psSrc->pszValue = CPLStrdup(GetTypeStatic());
3051 :
3052 98 : if ((m_nProcessingFlags & PROCESSING_FLAG_USE_MASK_BAND) != 0)
3053 : {
3054 33 : CPLSetXMLValue(psSrc, "UseMaskBand", "true");
3055 : }
3056 :
3057 98 : if ((m_nProcessingFlags & PROCESSING_FLAG_NODATA) != 0)
3058 : {
3059 25 : if (!m_osNoDataValueOri.empty() && GetRasterBandNoOpen() == nullptr)
3060 : {
3061 1 : CPLSetXMLValue(psSrc, "NODATA", m_osNoDataValueOri.c_str());
3062 : }
3063 : else
3064 : {
3065 24 : GDALDataType eBandDT = GDT_Unknown;
3066 24 : double dfNoDataValue = m_dfNoDataValue;
3067 24 : const auto kMaxFloat = std::numeric_limits<float>::max();
3068 24 : if (std::fabs(std::fabs(m_dfNoDataValue) - kMaxFloat) <
3069 : 1e-10 * kMaxFloat)
3070 : {
3071 1 : auto l_band = GetRasterBand();
3072 1 : if (l_band)
3073 : {
3074 1 : dfNoDataValue = GetAdjustedNoDataValue();
3075 1 : eBandDT = l_band->GetRasterDataType();
3076 : }
3077 : }
3078 24 : CPLSetXMLValue(
3079 : psSrc, "NODATA",
3080 48 : VRTSerializeNoData(dfNoDataValue, eBandDT, 18).c_str());
3081 : }
3082 : }
3083 :
3084 98 : if ((m_nProcessingFlags & PROCESSING_FLAG_SCALING_LINEAR) != 0)
3085 : {
3086 1 : CPLSetXMLValue(psSrc, "ScaleOffset", CPLSPrintf("%g", m_dfScaleOff));
3087 1 : CPLSetXMLValue(psSrc, "ScaleRatio", CPLSPrintf("%g", m_dfScaleRatio));
3088 : }
3089 97 : else if ((m_nProcessingFlags & PROCESSING_FLAG_SCALING_EXPONENTIAL) != 0)
3090 : {
3091 0 : CPLSetXMLValue(psSrc, "Exponent", CPLSPrintf("%g", m_dfExponent));
3092 0 : if (m_bSrcMinMaxDefined)
3093 : {
3094 0 : CPLSetXMLValue(psSrc, "SrcMin", CPLSPrintf("%g", m_dfSrcMin));
3095 0 : CPLSetXMLValue(psSrc, "SrcMax", CPLSPrintf("%g", m_dfSrcMax));
3096 : }
3097 0 : CPLSetXMLValue(psSrc, "DstMin", CPLSPrintf("%g", m_dfDstMin));
3098 0 : CPLSetXMLValue(psSrc, "DstMax", CPLSPrintf("%g", m_dfDstMax));
3099 0 : CPLSetXMLValue(psSrc, "Clip", m_bClip ? "true" : "false");
3100 : }
3101 :
3102 98 : if (!m_adfLUTInputs.empty())
3103 : {
3104 : // Make sure we print with sufficient precision to address really close
3105 : // entries (#6422).
3106 60 : CPLString osLUT;
3107 60 : if (m_adfLUTInputs.size() >= 2 &&
3108 60 : CPLString().Printf("%g", m_adfLUTInputs[0]) ==
3109 60 : CPLString().Printf("%g", m_adfLUTInputs[1]))
3110 : {
3111 6 : osLUT = CPLString().Printf("%.17g:%g", m_adfLUTInputs[0],
3112 3 : m_adfLUTOutputs[0]);
3113 : }
3114 : else
3115 : {
3116 54 : osLUT = CPLString().Printf("%g:%g", m_adfLUTInputs[0],
3117 27 : m_adfLUTOutputs[0]);
3118 : }
3119 249 : for (size_t i = 1; i < m_adfLUTInputs.size(); i++)
3120 : {
3121 438 : if (CPLString().Printf("%g", m_adfLUTInputs[i]) ==
3122 900 : CPLString().Printf("%g", m_adfLUTInputs[i - 1]) ||
3123 243 : (i + 1 < m_adfLUTInputs.size() &&
3124 333 : CPLString().Printf("%g", m_adfLUTInputs[i]) ==
3125 333 : CPLString().Printf("%g", m_adfLUTInputs[i + 1])))
3126 : {
3127 : // TODO(schwehr): An explanation of the 18 would be helpful.
3128 : // Can someone distill the issue down to a quick comment?
3129 : // https://trac.osgeo.org/gdal/ticket/6422
3130 306 : osLUT += CPLString().Printf(",%.17g:%g", m_adfLUTInputs[i],
3131 153 : m_adfLUTOutputs[i]);
3132 : }
3133 : else
3134 : {
3135 132 : osLUT += CPLString().Printf(",%g:%g", m_adfLUTInputs[i],
3136 66 : m_adfLUTOutputs[i]);
3137 : }
3138 : }
3139 30 : CPLSetXMLValue(psSrc, "LUT", osLUT);
3140 : }
3141 :
3142 98 : if (m_nColorTableComponent)
3143 : {
3144 7 : CPLSetXMLValue(psSrc, "ColorTableComponent",
3145 : CPLSPrintf("%d", m_nColorTableComponent));
3146 : }
3147 :
3148 98 : return psSrc;
3149 : }
3150 :
3151 : /************************************************************************/
3152 : /* XMLInit() */
3153 : /************************************************************************/
3154 :
3155 247 : CPLErr VRTComplexSource::XMLInit(const CPLXMLNode *psSrc,
3156 : const char *pszVRTPath,
3157 : VRTMapSharedResources &oMapSharedSources)
3158 :
3159 : {
3160 : /* -------------------------------------------------------------------- */
3161 : /* Do base initialization. */
3162 : /* -------------------------------------------------------------------- */
3163 : {
3164 : const CPLErr eErr =
3165 247 : VRTSimpleSource::XMLInit(psSrc, pszVRTPath, oMapSharedSources);
3166 247 : if (eErr != CE_None)
3167 0 : return eErr;
3168 : }
3169 :
3170 : /* -------------------------------------------------------------------- */
3171 : /* Complex parameters. */
3172 : /* -------------------------------------------------------------------- */
3173 247 : const char *pszScaleOffset = CPLGetXMLValue(psSrc, "ScaleOffset", nullptr);
3174 247 : const char *pszScaleRatio = CPLGetXMLValue(psSrc, "ScaleRatio", nullptr);
3175 247 : if (pszScaleOffset || pszScaleRatio)
3176 : {
3177 32 : m_nProcessingFlags |= PROCESSING_FLAG_SCALING_LINEAR;
3178 32 : if (pszScaleOffset)
3179 32 : m_dfScaleOff = CPLAtof(pszScaleOffset);
3180 32 : if (pszScaleRatio)
3181 29 : m_dfScaleRatio = CPLAtof(pszScaleRatio);
3182 : }
3183 215 : else if (CPLGetXMLValue(psSrc, "Exponent", nullptr) != nullptr &&
3184 216 : CPLGetXMLValue(psSrc, "DstMin", nullptr) != nullptr &&
3185 1 : CPLGetXMLValue(psSrc, "DstMax", nullptr) != nullptr)
3186 : {
3187 1 : m_nProcessingFlags |= PROCESSING_FLAG_SCALING_EXPONENTIAL;
3188 1 : m_dfExponent = CPLAtof(CPLGetXMLValue(psSrc, "Exponent", "1.0"));
3189 :
3190 1 : const char *pszSrcMin = CPLGetXMLValue(psSrc, "SrcMin", nullptr);
3191 1 : const char *pszSrcMax = CPLGetXMLValue(psSrc, "SrcMax", nullptr);
3192 1 : if (pszSrcMin && pszSrcMax)
3193 : {
3194 0 : m_dfSrcMin = CPLAtof(pszSrcMin);
3195 0 : m_dfSrcMax = CPLAtof(pszSrcMax);
3196 0 : m_bSrcMinMaxDefined = true;
3197 : }
3198 :
3199 1 : m_dfDstMin = CPLAtof(CPLGetXMLValue(psSrc, "DstMin", "0.0"));
3200 1 : m_dfDstMax = CPLAtof(CPLGetXMLValue(psSrc, "DstMax", "0.0"));
3201 1 : m_bClip = CPLTestBool(CPLGetXMLValue(psSrc, "Clip", "true"));
3202 : }
3203 :
3204 247 : if (const char *pszNODATA = CPLGetXMLValue(psSrc, "NODATA", nullptr))
3205 : {
3206 60 : m_nProcessingFlags |= PROCESSING_FLAG_NODATA;
3207 60 : m_osNoDataValueOri = pszNODATA;
3208 60 : m_dfNoDataValue = CPLAtofM(m_osNoDataValueOri.c_str());
3209 : }
3210 :
3211 247 : const char *pszUseMaskBand = CPLGetXMLValue(psSrc, "UseMaskBand", nullptr);
3212 247 : if (pszUseMaskBand && CPLTestBool(pszUseMaskBand))
3213 : {
3214 41 : m_nProcessingFlags |= PROCESSING_FLAG_USE_MASK_BAND;
3215 : }
3216 :
3217 247 : const char *pszLUT = CPLGetXMLValue(psSrc, "LUT", nullptr);
3218 247 : if (pszLUT)
3219 : {
3220 : const CPLStringList aosValues(
3221 62 : CSLTokenizeString2(pszLUT, ",:", CSLT_ALLOWEMPTYTOKENS));
3222 :
3223 62 : const int nLUTItemCount = aosValues.size() / 2;
3224 : try
3225 : {
3226 62 : m_adfLUTInputs.resize(nLUTItemCount);
3227 62 : m_adfLUTOutputs.resize(nLUTItemCount);
3228 : }
3229 0 : catch (const std::bad_alloc &e)
3230 : {
3231 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
3232 0 : m_adfLUTInputs.clear();
3233 0 : m_adfLUTOutputs.clear();
3234 0 : return CE_Failure;
3235 : }
3236 :
3237 565 : for (int nIndex = 0; nIndex < nLUTItemCount; nIndex++)
3238 : {
3239 503 : m_adfLUTInputs[nIndex] = CPLAtof(aosValues[nIndex * 2]);
3240 503 : m_adfLUTOutputs[nIndex] = CPLAtof(aosValues[nIndex * 2 + 1]);
3241 :
3242 : // Enforce the requirement that the LUT input array is
3243 : // monotonically non-decreasing.
3244 503 : if (std::isnan(m_adfLUTInputs[nIndex]) && nIndex != 0)
3245 : {
3246 0 : CPLError(CE_Failure, CPLE_AppDefined,
3247 : "A Not-A-Number (NaN) source value should be the "
3248 : "first one of the LUT.");
3249 0 : m_adfLUTInputs.clear();
3250 0 : m_adfLUTOutputs.clear();
3251 0 : return CE_Failure;
3252 : }
3253 944 : else if (nIndex > 0 &&
3254 441 : m_adfLUTInputs[nIndex] < m_adfLUTInputs[nIndex - 1])
3255 : {
3256 0 : CPLError(CE_Failure, CPLE_AppDefined,
3257 : "Source values of the LUT are not listed in a "
3258 : "monotonically non-decreasing order");
3259 0 : m_adfLUTInputs.clear();
3260 0 : m_adfLUTOutputs.clear();
3261 0 : return CE_Failure;
3262 : }
3263 : }
3264 :
3265 62 : m_nProcessingFlags |= PROCESSING_FLAG_LUT;
3266 : }
3267 :
3268 : const char *pszColorTableComponent =
3269 247 : CPLGetXMLValue(psSrc, "ColorTableComponent", nullptr);
3270 247 : if (pszColorTableComponent)
3271 : {
3272 15 : m_nColorTableComponent = atoi(pszColorTableComponent);
3273 15 : m_nProcessingFlags |= PROCESSING_FLAG_COLOR_TABLE_EXPANSION;
3274 : }
3275 :
3276 247 : return CE_None;
3277 : }
3278 :
3279 : /************************************************************************/
3280 : /* SetLUT() */
3281 : /************************************************************************/
3282 :
3283 55 : void VRTComplexSource::SetLUT(const std::vector<double> &adfLUTInputs,
3284 : const std::vector<double> &adfLUTOutputs)
3285 : {
3286 55 : m_adfLUTInputs = adfLUTInputs;
3287 55 : m_adfLUTOutputs = adfLUTOutputs;
3288 55 : m_nProcessingFlags |= PROCESSING_FLAG_LUT;
3289 55 : }
3290 :
3291 : /************************************************************************/
3292 : /* LookupValue() */
3293 : /************************************************************************/
3294 :
3295 995684 : double VRTComplexSource::LookupValue(double dfInput)
3296 : {
3297 995684 : auto beginIter = m_adfLUTInputs.begin();
3298 995684 : auto endIter = m_adfLUTInputs.end();
3299 995684 : size_t offset = 0;
3300 995684 : if (std::isnan(m_adfLUTInputs[0]))
3301 : {
3302 6 : if (std::isnan(dfInput) || m_adfLUTInputs.size() == 1)
3303 1 : return m_adfLUTOutputs[0];
3304 5 : ++beginIter;
3305 5 : offset = 1;
3306 : }
3307 :
3308 : // Find the index of the first element in the LUT input array that
3309 : // is not smaller than the input value.
3310 : const size_t i =
3311 : offset +
3312 995683 : std::distance(beginIter, std::lower_bound(beginIter, endIter, dfInput));
3313 :
3314 995683 : if (i == offset)
3315 129039 : return m_adfLUTOutputs[offset];
3316 :
3317 : // If the index is beyond the end of the LUT input array, the input
3318 : // value is larger than all the values in the array.
3319 866644 : if (i == m_adfLUTInputs.size())
3320 8 : return m_adfLUTOutputs.back();
3321 :
3322 866636 : if (m_adfLUTInputs[i] == dfInput)
3323 296380 : return m_adfLUTOutputs[i];
3324 :
3325 : // Otherwise, interpolate.
3326 570256 : return m_adfLUTOutputs[i - 1] +
3327 570256 : (dfInput - m_adfLUTInputs[i - 1]) *
3328 570256 : ((m_adfLUTOutputs[i] - m_adfLUTOutputs[i - 1]) /
3329 570256 : (m_adfLUTInputs[i] - m_adfLUTInputs[i - 1]));
3330 : }
3331 :
3332 : /************************************************************************/
3333 : /* SetLinearScaling() */
3334 : /************************************************************************/
3335 :
3336 123 : void VRTComplexSource::SetLinearScaling(double dfOffset, double dfScale)
3337 : {
3338 123 : m_nProcessingFlags &= ~PROCESSING_FLAG_SCALING_EXPONENTIAL;
3339 123 : m_nProcessingFlags |= PROCESSING_FLAG_SCALING_LINEAR;
3340 123 : m_dfScaleOff = dfOffset;
3341 123 : m_dfScaleRatio = dfScale;
3342 123 : }
3343 :
3344 : /************************************************************************/
3345 : /* SetPowerScaling() */
3346 : /************************************************************************/
3347 :
3348 33 : void VRTComplexSource::SetPowerScaling(double dfExponentIn, double dfSrcMinIn,
3349 : double dfSrcMaxIn, double dfDstMinIn,
3350 : double dfDstMaxIn, bool bClip)
3351 : {
3352 33 : m_nProcessingFlags &= ~PROCESSING_FLAG_SCALING_LINEAR;
3353 33 : m_nProcessingFlags |= PROCESSING_FLAG_SCALING_EXPONENTIAL;
3354 33 : m_dfExponent = dfExponentIn;
3355 33 : m_dfSrcMin = dfSrcMinIn;
3356 33 : m_dfSrcMax = dfSrcMaxIn;
3357 33 : m_dfDstMin = dfDstMinIn;
3358 33 : m_dfDstMax = dfDstMaxIn;
3359 33 : m_bSrcMinMaxDefined = true;
3360 33 : m_bClip = bClip;
3361 33 : }
3362 :
3363 : /************************************************************************/
3364 : /* SetColorTableComponent() */
3365 : /************************************************************************/
3366 :
3367 317 : void VRTComplexSource::SetColorTableComponent(int nComponent)
3368 : {
3369 317 : m_nProcessingFlags |= PROCESSING_FLAG_COLOR_TABLE_EXPANSION;
3370 317 : m_nColorTableComponent = nComponent;
3371 317 : }
3372 :
3373 : /************************************************************************/
3374 : /* RasterIO() */
3375 : /************************************************************************/
3376 :
3377 23898 : CPLErr VRTComplexSource::RasterIO(GDALDataType eVRTBandDataType, int nXOff,
3378 : int nYOff, int nXSize, int nYSize,
3379 : void *pData, int nBufXSize, int nBufYSize,
3380 : GDALDataType eBufType, GSpacing nPixelSpace,
3381 : GSpacing nLineSpace,
3382 : GDALRasterIOExtraArg *psExtraArgIn,
3383 : WorkingState &oWorkingState)
3384 :
3385 : {
3386 : GDALRasterIOExtraArg sExtraArg;
3387 23898 : INIT_RASTERIO_EXTRA_ARG(sExtraArg);
3388 23898 : GDALRasterIOExtraArg *psExtraArg = &sExtraArg;
3389 :
3390 23898 : double dfXOff = nXOff;
3391 23898 : double dfYOff = nYOff;
3392 23898 : double dfXSize = nXSize;
3393 23898 : double dfYSize = nYSize;
3394 23898 : if (psExtraArgIn != nullptr && psExtraArgIn->bFloatingPointWindowValidity)
3395 : {
3396 88 : dfXOff = psExtraArgIn->dfXOff;
3397 88 : dfYOff = psExtraArgIn->dfYOff;
3398 88 : dfXSize = psExtraArgIn->dfXSize;
3399 88 : dfYSize = psExtraArgIn->dfYSize;
3400 : }
3401 :
3402 : // The window we will actually request from the source raster band.
3403 23898 : double dfReqXOff = 0.0;
3404 23898 : double dfReqYOff = 0.0;
3405 23898 : double dfReqXSize = 0.0;
3406 23898 : double dfReqYSize = 0.0;
3407 23898 : int nReqXOff = 0;
3408 23898 : int nReqYOff = 0;
3409 23898 : int nReqXSize = 0;
3410 23898 : int nReqYSize = 0;
3411 :
3412 : // The window we will actual set _within_ the pData buffer.
3413 23898 : int nOutXOff = 0;
3414 23898 : int nOutYOff = 0;
3415 23898 : int nOutXSize = 0;
3416 23898 : int nOutYSize = 0;
3417 :
3418 23898 : if (!m_osResampling.empty())
3419 : {
3420 28 : psExtraArg->eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
3421 : }
3422 23870 : else if (psExtraArgIn != nullptr)
3423 : {
3424 23870 : psExtraArg->eResampleAlg = psExtraArgIn->eResampleAlg;
3425 : }
3426 23898 : if (psExtraArgIn)
3427 23898 : psExtraArg->bOperateInBufType =
3428 23898 : GDAL_GET_OPERATE_IN_BUF_TYPE(*psExtraArgIn);
3429 :
3430 23898 : bool bError = false;
3431 23898 : if (!GetSrcDstWindow(dfXOff, dfYOff, dfXSize, dfYSize, nBufXSize, nBufYSize,
3432 : psExtraArg->eResampleAlg, &dfReqXOff, &dfReqYOff,
3433 : &dfReqXSize, &dfReqYSize, &nReqXOff, &nReqYOff,
3434 : &nReqXSize, &nReqYSize, &nOutXOff, &nOutYOff,
3435 : &nOutXSize, &nOutYSize, bError))
3436 : {
3437 13061 : return bError ? CE_Failure : CE_None;
3438 : }
3439 : #if DEBUG_VERBOSE
3440 : CPLDebug("VRT",
3441 : "nXOff=%d, nYOff=%d, nXSize=%d, nYSize=%d, nBufXSize=%d, "
3442 : "nBufYSize=%d,\n"
3443 : "dfReqXOff=%g, dfReqYOff=%g, dfReqXSize=%g, dfReqYSize=%g,\n"
3444 : "nReqXOff=%d, nReqYOff=%d, nReqXSize=%d, nReqYSize=%d,\n"
3445 : "nOutXOff=%d, nOutYOff=%d, nOutXSize=%d, nOutYSize=%d",
3446 : nXOff, nYOff, nXSize, nYSize, nBufXSize, nBufYSize, dfReqXOff,
3447 : dfReqYOff, dfReqXSize, dfReqYSize, nReqXOff, nReqYOff, nReqXSize,
3448 : nReqYSize, nOutXOff, nOutYOff, nOutXSize, nOutYSize);
3449 : #endif
3450 :
3451 10837 : auto poSourceBand = GetRasterBand();
3452 10837 : if (!poSourceBand)
3453 0 : return CE_Failure;
3454 :
3455 10837 : psExtraArg->bFloatingPointWindowValidity = TRUE;
3456 10837 : psExtraArg->dfXOff = dfReqXOff;
3457 10837 : psExtraArg->dfYOff = dfReqYOff;
3458 10837 : psExtraArg->dfXSize = dfReqXSize;
3459 10837 : psExtraArg->dfYSize = dfReqYSize;
3460 10837 : if (psExtraArgIn)
3461 : {
3462 10837 : psExtraArg->pfnProgress = psExtraArgIn->pfnProgress;
3463 10837 : psExtraArg->pProgressData = psExtraArgIn->pProgressData;
3464 10837 : if (psExtraArgIn->nVersion >= 2)
3465 : {
3466 10837 : psExtraArg->bUseOnlyThisScale = psExtraArgIn->bUseOnlyThisScale;
3467 : }
3468 10837 : psExtraArg->bOperateInBufType =
3469 10837 : GDAL_GET_OPERATE_IN_BUF_TYPE(*psExtraArgIn);
3470 : }
3471 :
3472 10837 : GByte *const pabyOut = static_cast<GByte *>(pData) +
3473 10837 : nPixelSpace * nOutXOff +
3474 10837 : static_cast<GPtrDiff_t>(nLineSpace) * nOutYOff;
3475 10837 : const auto eSourceType = poSourceBand->GetRasterDataType();
3476 10837 : if (m_nProcessingFlags == PROCESSING_FLAG_NODATA)
3477 : {
3478 : // Optimization if doing only nodata processing
3479 101 : if (eBufType != eSourceType && psExtraArg->bOperateInBufType)
3480 : {
3481 58 : if (eBufType == GDT_Float32 &&
3482 13 : GDALDataTypeUnion(eBufType, eSourceType) == eBufType)
3483 : {
3484 11 : return RasterIOProcessNoData<float, GDT_Float32>(
3485 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff,
3486 : nReqXSize, nReqYSize, pabyOut, nOutXSize, nOutYSize,
3487 : eBufType, nPixelSpace, nLineSpace, psExtraArg,
3488 11 : oWorkingState);
3489 : }
3490 52 : else if (eBufType == GDT_Float64 &&
3491 18 : GDALDataTypeUnion(eBufType, eSourceType) == eBufType)
3492 : {
3493 18 : return RasterIOProcessNoData<double, GDT_Float64>(
3494 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff,
3495 : nReqXSize, nReqYSize, pabyOut, nOutXSize, nOutYSize,
3496 : eBufType, nPixelSpace, nLineSpace, psExtraArg,
3497 18 : oWorkingState);
3498 : }
3499 : }
3500 56 : else if (eSourceType == GDT_UInt8)
3501 : {
3502 31 : if (!GDALIsValueInRange<GByte>(m_dfNoDataValue))
3503 : {
3504 1 : return VRTSimpleSource::RasterIO(
3505 : eVRTBandDataType, nXOff, nYOff, nXSize, nYSize, pData,
3506 : nBufXSize, nBufYSize, eBufType, nPixelSpace, nLineSpace,
3507 1 : psExtraArgIn, oWorkingState);
3508 : }
3509 30 : return RasterIOProcessNoData<GByte, GDT_UInt8>(
3510 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff, nReqXSize,
3511 : nReqYSize, pabyOut, nOutXSize, nOutYSize, eBufType, nPixelSpace,
3512 30 : nLineSpace, psExtraArg, oWorkingState);
3513 : }
3514 25 : else if (eSourceType == GDT_Int16)
3515 : {
3516 2 : if (!GDALIsValueInRange<int16_t>(m_dfNoDataValue))
3517 : {
3518 1 : return VRTSimpleSource::RasterIO(
3519 : eVRTBandDataType, nXOff, nYOff, nXSize, nYSize, pData,
3520 : nBufXSize, nBufYSize, eBufType, nPixelSpace, nLineSpace,
3521 1 : psExtraArgIn, oWorkingState);
3522 : }
3523 :
3524 1 : return RasterIOProcessNoData<int16_t, GDT_Int16>(
3525 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff, nReqXSize,
3526 : nReqYSize, pabyOut, nOutXSize, nOutYSize, eBufType, nPixelSpace,
3527 1 : nLineSpace, psExtraArg, oWorkingState);
3528 : }
3529 23 : else if (eSourceType == GDT_UInt16)
3530 : {
3531 5 : if (!GDALIsValueInRange<uint16_t>(m_dfNoDataValue))
3532 : {
3533 1 : return VRTSimpleSource::RasterIO(
3534 : eVRTBandDataType, nXOff, nYOff, nXSize, nYSize, pData,
3535 : nBufXSize, nBufYSize, eBufType, nPixelSpace, nLineSpace,
3536 1 : psExtraArgIn, oWorkingState);
3537 : }
3538 :
3539 4 : return RasterIOProcessNoData<uint16_t, GDT_UInt16>(
3540 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff, nReqXSize,
3541 : nReqYSize, pabyOut, nOutXSize, nOutYSize, eBufType, nPixelSpace,
3542 4 : nLineSpace, psExtraArg, oWorkingState);
3543 : }
3544 : }
3545 :
3546 10770 : const bool bIsComplex = CPL_TO_BOOL(GDALDataTypeIsComplex(eBufType));
3547 : CPLErr eErr;
3548 : // For Int32, float32 isn't sufficiently precise as working data type
3549 10770 : if (eVRTBandDataType == GDT_CInt32 || eVRTBandDataType == GDT_CFloat64 ||
3550 10744 : eVRTBandDataType == GDT_Int32 || eVRTBandDataType == GDT_UInt32 ||
3551 10740 : eVRTBandDataType == GDT_Int64 || eVRTBandDataType == GDT_UInt64 ||
3552 10715 : eVRTBandDataType == GDT_Float64 || eSourceType == GDT_Int32 ||
3553 10715 : eSourceType == GDT_UInt32 || eSourceType == GDT_Int64 ||
3554 : eSourceType == GDT_UInt64)
3555 : {
3556 56 : eErr = RasterIOInternal<double>(
3557 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff, nReqXSize,
3558 : nReqYSize, pabyOut, nOutXSize, nOutYSize, eBufType, nPixelSpace,
3559 : nLineSpace, psExtraArg, bIsComplex ? GDT_CFloat64 : GDT_Float64,
3560 : oWorkingState);
3561 : }
3562 : else
3563 : {
3564 10714 : eErr = RasterIOInternal<float>(
3565 : poSourceBand, eVRTBandDataType, nReqXOff, nReqYOff, nReqXSize,
3566 : nReqYSize, pabyOut, nOutXSize, nOutYSize, eBufType, nPixelSpace,
3567 : nLineSpace, psExtraArg, bIsComplex ? GDT_CFloat32 : GDT_Float32,
3568 : oWorkingState);
3569 : }
3570 :
3571 10770 : if (psExtraArg->pfnProgress)
3572 57 : psExtraArg->pfnProgress(1.0, "", psExtraArg->pProgressData);
3573 :
3574 10770 : return eErr;
3575 : }
3576 :
3577 : /************************************************************************/
3578 : /* hasZeroByte() */
3579 : /************************************************************************/
3580 :
3581 : CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
3582 4718160 : static inline bool hasZeroByte(uint32_t v)
3583 : {
3584 : // Cf https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
3585 4718160 : return (((v)-0x01010101U) & ~(v) & 0x80808080U) != 0;
3586 : }
3587 :
3588 : /************************************************************************/
3589 : /* RasterIOProcessNoData() */
3590 : /************************************************************************/
3591 :
3592 : // This method is an optimization of the generic RasterIOInternal()
3593 : // that deals with a VRTComplexSource with only a NODATA value in it, and
3594 : // no other processing flags.
3595 :
3596 : // nReqXOff, nReqYOff, nReqXSize, nReqYSize are expressed in source band
3597 : // referential.
3598 : template <class WorkingDT, GDALDataType eWorkingDT>
3599 64 : CPLErr VRTComplexSource::RasterIOProcessNoData(
3600 : GDALRasterBand *poSourceBand, GDALDataType eVRTBandDataType, int nReqXOff,
3601 : int nReqYOff, int nReqXSize, int nReqYSize, void *pData, int nOutXSize,
3602 : int nOutYSize, GDALDataType eBufType, GSpacing nPixelSpace,
3603 : GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg,
3604 : WorkingState &oWorkingState)
3605 : {
3606 64 : CPLAssert(m_nProcessingFlags == PROCESSING_FLAG_NODATA);
3607 64 : CPLAssert(GDALIsValueExactAs<WorkingDT>(m_dfNoDataValue));
3608 64 : const int nWorkDTSize = GDALGetDataTypeSizeBytes(eWorkingDT);
3609 64 : assert(nWorkDTSize != 0);
3610 :
3611 : /* -------------------------------------------------------------------- */
3612 : /* Read into a temporary buffer. */
3613 : /* -------------------------------------------------------------------- */
3614 : try
3615 : {
3616 : // Cannot overflow since pData should at least have that number of
3617 : // elements
3618 64 : const size_t nPixelCount = static_cast<size_t>(nOutXSize) * nOutYSize;
3619 64 : if (nPixelCount >
3620 64 : static_cast<size_t>(std::numeric_limits<ptrdiff_t>::max()) /
3621 64 : nWorkDTSize)
3622 : {
3623 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
3624 : "Too large temporary buffer");
3625 0 : return CE_Failure;
3626 : }
3627 64 : oWorkingState.m_abyWrkBuffer.resize(nWorkDTSize * nPixelCount);
3628 : }
3629 0 : catch (const std::bad_alloc &e)
3630 : {
3631 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
3632 0 : return CE_Failure;
3633 : }
3634 : auto paSrcData =
3635 64 : reinterpret_cast<WorkingDT *>(oWorkingState.m_abyWrkBuffer.data());
3636 :
3637 : GDALRasterIOExtraArg sExtraArg;
3638 64 : GDALCopyRasterIOExtraArg(&sExtraArg, psExtraArg);
3639 64 : if (!m_osResampling.empty())
3640 : {
3641 0 : sExtraArg.eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
3642 : }
3643 64 : sExtraArg.bOperateInBufType = true;
3644 :
3645 64 : const CPLErr eErr = poSourceBand->RasterIO(
3646 : GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
3647 64 : oWorkingState.m_abyWrkBuffer.data(), nOutXSize, nOutYSize, eWorkingDT,
3648 64 : nWorkDTSize, nWorkDTSize * static_cast<GSpacing>(nOutXSize),
3649 : &sExtraArg);
3650 :
3651 64 : if (eErr != CE_None)
3652 : {
3653 0 : return eErr;
3654 : }
3655 :
3656 64 : const auto nNoDataValue = static_cast<WorkingDT>(m_dfNoDataValue);
3657 64 : size_t idxBuffer = 0;
3658 128 : if (eWorkingDT == eBufType &&
3659 64 : !GDALDataTypeIsConversionLossy(eWorkingDT, eVRTBandDataType))
3660 : {
3661 : // Most optimized case: the output type is the same as the working type,
3662 : // and conversion from the working type to the VRT band data type is
3663 : // not lossy
3664 19766 : for (int iY = 0; iY < nOutYSize; iY++)
3665 : {
3666 19707 : GByte *pDstLocation = static_cast<GByte *>(pData) +
3667 19707 : static_cast<GPtrDiff_t>(nLineSpace) * iY;
3668 :
3669 19707 : int iX = 0;
3670 19625 : if (sizeof(WorkingDT) == 1 && nPixelSpace == 1)
3671 : {
3672 : // Optimization to detect more quickly if source pixels are
3673 : // at nodata.
3674 19625 : const GByte byNoDataValue = static_cast<GByte>(nNoDataValue);
3675 19625 : const uint32_t wordNoData =
3676 19625 : (static_cast<uint32_t>(byNoDataValue) << 24) |
3677 19625 : (byNoDataValue << 16) | (byNoDataValue << 8) |
3678 19625 : byNoDataValue;
3679 :
3680 : // Warning: hasZeroByte() assumes WORD_SIZE = 4
3681 19625 : constexpr int WORD_SIZE = 4;
3682 4737790 : for (; iX < nOutXSize - (WORD_SIZE - 1); iX += WORD_SIZE)
3683 : {
3684 : uint32_t v;
3685 : static_assert(sizeof(v) == WORD_SIZE,
3686 : "sizeof(v) == WORD_SIZE");
3687 4718160 : memcpy(&v, paSrcData + idxBuffer, sizeof(v));
3688 : // Cf https://graphics.stanford.edu/~seander/bithacks.html#ValueInWord
3689 4718160 : if (!hasZeroByte(v ^ wordNoData))
3690 : {
3691 : // No bytes are at nodata
3692 4587030 : memcpy(pDstLocation, &v, WORD_SIZE);
3693 4587030 : idxBuffer += WORD_SIZE;
3694 4587030 : pDstLocation += WORD_SIZE;
3695 : }
3696 131128 : else if (v == wordNoData)
3697 : {
3698 : // All bytes are at nodata
3699 131077 : idxBuffer += WORD_SIZE;
3700 131077 : pDstLocation += WORD_SIZE;
3701 : }
3702 : else
3703 : {
3704 : // There are both bytes at nodata and valid bytes
3705 255 : for (int k = 0; k < WORD_SIZE; ++k)
3706 : {
3707 204 : if (paSrcData[idxBuffer] != nNoDataValue)
3708 : {
3709 143 : memcpy(pDstLocation, &paSrcData[idxBuffer],
3710 : sizeof(WorkingDT));
3711 : }
3712 204 : idxBuffer++;
3713 204 : pDstLocation += nPixelSpace;
3714 : }
3715 : }
3716 : }
3717 : }
3718 :
3719 24022 : for (; iX < nOutXSize;
3720 4315 : iX++, pDstLocation += nPixelSpace, idxBuffer++)
3721 : {
3722 : if constexpr (eWorkingDT == GDT_Float32 ||
3723 : eWorkingDT == GDT_Float64)
3724 : {
3725 471 : if (std::isnan(paSrcData[idxBuffer]))
3726 : {
3727 0 : continue;
3728 : }
3729 : }
3730 4315 : if (paSrcData[idxBuffer] != nNoDataValue)
3731 : {
3732 4264 : memcpy(pDstLocation, &paSrcData[idxBuffer],
3733 : sizeof(WorkingDT));
3734 : }
3735 : }
3736 : }
3737 : }
3738 5 : else if (!GDALDataTypeIsConversionLossy(eWorkingDT, eVRTBandDataType))
3739 : {
3740 : // Conversion from the work type to the VRT band data type is
3741 : // not lossy, so we can directly convert from the work type to
3742 : // the output type.
3743 0 : for (int iY = 0; iY < nOutYSize; iY++)
3744 : {
3745 0 : GByte *pDstLocation = static_cast<GByte *>(pData) +
3746 0 : static_cast<GPtrDiff_t>(nLineSpace) * iY;
3747 :
3748 0 : for (int iX = 0; iX < nOutXSize;
3749 0 : iX++, pDstLocation += nPixelSpace, idxBuffer++)
3750 : {
3751 : if constexpr (eWorkingDT == GDT_Float32 ||
3752 : eWorkingDT == GDT_Float64)
3753 : {
3754 0 : if (std::isnan(paSrcData[idxBuffer]))
3755 : {
3756 0 : continue;
3757 : }
3758 : }
3759 0 : if (paSrcData[idxBuffer] != nNoDataValue)
3760 : {
3761 0 : CopyWordOut(&paSrcData[idxBuffer], eWorkingDT, pDstLocation,
3762 : eBufType);
3763 : }
3764 : }
3765 : }
3766 : }
3767 : else
3768 : {
3769 5 : const bool bClampToVRTBandType =
3770 5 : GDAL_GET_OPERATE_IN_BUF_TYPE(*psExtraArg);
3771 :
3772 : GByte abyTemp[2 * sizeof(double)];
3773 43 : for (int iY = 0; iY < nOutYSize; iY++)
3774 : {
3775 38 : GByte *pDstLocation = static_cast<GByte *>(pData) +
3776 38 : static_cast<GPtrDiff_t>(nLineSpace) * iY;
3777 :
3778 328 : for (int iX = 0; iX < nOutXSize;
3779 290 : iX++, pDstLocation += nPixelSpace, idxBuffer++)
3780 : {
3781 : if constexpr (eWorkingDT == GDT_Float32 ||
3782 : eWorkingDT == GDT_Float64)
3783 : {
3784 260 : if (std::isnan(paSrcData[idxBuffer]))
3785 : {
3786 20 : continue;
3787 : }
3788 : }
3789 270 : if (paSrcData[idxBuffer] != nNoDataValue)
3790 : {
3791 240 : if (bClampToVRTBandType)
3792 : {
3793 240 : GDALClampValueToType(&paSrcData[idxBuffer],
3794 : eVRTBandDataType);
3795 240 : CopyWordOut(&paSrcData[idxBuffer], eWorkingDT,
3796 : pDstLocation, eBufType);
3797 : }
3798 : else
3799 : {
3800 : // Convert first to the VRTRasterBand data type
3801 : // to get its clamping, before outputting to buffer data type
3802 0 : CopyWordOut(&paSrcData[idxBuffer], eWorkingDT, abyTemp,
3803 : eVRTBandDataType);
3804 0 : GDALCopyWords(abyTemp, eVRTBandDataType, 0,
3805 : pDstLocation, eBufType, 0, 1);
3806 : }
3807 : }
3808 : }
3809 : }
3810 : }
3811 :
3812 64 : return CE_None;
3813 : }
3814 :
3815 : /************************************************************************/
3816 : /* RasterIOInternal() */
3817 : /************************************************************************/
3818 :
3819 : // nReqXOff, nReqYOff, nReqXSize, nReqYSize are expressed in source band
3820 : // referential.
3821 : template <class WorkingDT>
3822 10815 : CPLErr VRTComplexSource::RasterIOInternal(
3823 : GDALRasterBand *poSourceBand, GDALDataType eVRTBandDataType, int nReqXOff,
3824 : int nReqYOff, int nReqXSize, int nReqYSize, void *pData, int nOutXSize,
3825 : int nOutYSize, GDALDataType eBufType, GSpacing nPixelSpace,
3826 : GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg,
3827 : GDALDataType eWrkDataType, WorkingState &oWorkingState)
3828 : {
3829 10815 : const GDALColorTable *poColorTable = nullptr;
3830 10815 : const bool bIsComplex = CPL_TO_BOOL(GDALDataTypeIsComplex(eBufType));
3831 10815 : const int nWorkDTSize = GDALGetDataTypeSizeBytes(eWrkDataType);
3832 10815 : assert(nWorkDTSize != 0);
3833 :
3834 : // If no explicit <NODATA> is set, but UseMaskBand is set, and the band
3835 : // has a nodata value, then use it as if it was set as <NODATA>
3836 10815 : int bNoDataSet = (m_nProcessingFlags & PROCESSING_FLAG_NODATA) != 0;
3837 10815 : double dfNoDataValue = GetAdjustedNoDataValue();
3838 :
3839 10922 : if ((m_nProcessingFlags & PROCESSING_FLAG_USE_MASK_BAND) != 0 &&
3840 107 : poSourceBand->GetMaskFlags() == GMF_NODATA)
3841 : {
3842 0 : dfNoDataValue = poSourceBand->GetNoDataValue(&bNoDataSet);
3843 : }
3844 :
3845 10815 : const bool bNoDataSetIsNan = bNoDataSet && std::isnan(dfNoDataValue);
3846 10815 : const bool bNoDataSetAndNotNan =
3847 15738 : bNoDataSet && !std::isnan(dfNoDataValue) &&
3848 4923 : GDALIsValueInRange<WorkingDT>(dfNoDataValue);
3849 10815 : const auto fWorkingDataTypeNoData = static_cast<WorkingDT>(dfNoDataValue);
3850 :
3851 10815 : const GByte *pabyMask = nullptr;
3852 10815 : const WorkingDT *pafData = nullptr;
3853 10815 : if ((m_nProcessingFlags & PROCESSING_FLAG_SCALING_LINEAR) != 0 &&
3854 4995 : m_dfScaleRatio == 0 && bNoDataSet == FALSE &&
3855 4575 : (m_nProcessingFlags & PROCESSING_FLAG_USE_MASK_BAND) == 0)
3856 : {
3857 : /* ------------------------------------------------------------------ */
3858 : /* Optimization when writing a constant value */
3859 : /* (used by the -addalpha option of gdalbuildvrt) */
3860 : /* ------------------------------------------------------------------ */
3861 : // Already set to NULL when defined.
3862 : // pafData = NULL;
3863 : }
3864 : else
3865 : {
3866 : /* ---------------------------------------------------------------- */
3867 : /* Read into a temporary buffer. */
3868 : /* ---------------------------------------------------------------- */
3869 6240 : const size_t nPixelCount = static_cast<size_t>(nOutXSize) * nOutYSize;
3870 : try
3871 : {
3872 : // Cannot overflow since pData should at least have that number of
3873 : // elements
3874 6240 : if (nPixelCount >
3875 6240 : static_cast<size_t>(std::numeric_limits<ptrdiff_t>::max()) /
3876 6240 : static_cast<size_t>(nWorkDTSize))
3877 : {
3878 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
3879 : "Too large temporary buffer");
3880 0 : return CE_Failure;
3881 : }
3882 6240 : oWorkingState.m_abyWrkBuffer.resize(nWorkDTSize * nPixelCount);
3883 : }
3884 0 : catch (const std::bad_alloc &e)
3885 : {
3886 0 : CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
3887 0 : return CE_Failure;
3888 : }
3889 : pafData = reinterpret_cast<const WorkingDT *>(
3890 6240 : oWorkingState.m_abyWrkBuffer.data());
3891 :
3892 : GDALRasterIOExtraArg sExtraArg;
3893 6240 : GDALCopyRasterIOExtraArg(&sExtraArg, psExtraArg);
3894 6240 : if (!m_osResampling.empty())
3895 : {
3896 28 : sExtraArg.eResampleAlg = GDALRasterIOGetResampleAlg(m_osResampling);
3897 : }
3898 6240 : sExtraArg.bOperateInBufType = true;
3899 :
3900 6240 : const CPLErr eErr = poSourceBand->RasterIO(
3901 : GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
3902 6240 : oWorkingState.m_abyWrkBuffer.data(), nOutXSize, nOutYSize,
3903 : eWrkDataType, nWorkDTSize,
3904 6240 : nWorkDTSize * static_cast<GSpacing>(nOutXSize), &sExtraArg);
3905 :
3906 6240 : if (eErr != CE_None)
3907 : {
3908 0 : return eErr;
3909 : }
3910 :
3911 : // Allocate and read mask band if needed
3912 13790 : if (!bNoDataSet &&
3913 6347 : (m_nProcessingFlags & PROCESSING_FLAG_USE_MASK_BAND) != 0 &&
3914 107 : (poSourceBand->GetMaskFlags() != GMF_ALL_VALID ||
3915 52 : poSourceBand->GetColorInterpretation() == GCI_AlphaBand ||
3916 32 : GetMaskBandMainBand() != nullptr))
3917 : {
3918 : try
3919 : {
3920 107 : oWorkingState.m_abyWrkBufferMask.resize(nPixelCount);
3921 : }
3922 0 : catch (const std::exception &)
3923 : {
3924 0 : CPLError(CE_Failure, CPLE_OutOfMemory,
3925 : "Out of memory when allocating mask buffer");
3926 0 : return CE_Failure;
3927 : }
3928 : pabyMask = reinterpret_cast<const GByte *>(
3929 107 : oWorkingState.m_abyWrkBufferMask.data());
3930 52 : auto poMaskBand =
3931 107 : (poSourceBand->GetColorInterpretation() == GCI_AlphaBand ||
3932 87 : GetMaskBandMainBand() != nullptr)
3933 : ? poSourceBand
3934 55 : : poSourceBand->GetMaskBand();
3935 107 : if (poMaskBand->RasterIO(
3936 : GF_Read, nReqXOff, nReqYOff, nReqXSize, nReqYSize,
3937 107 : oWorkingState.m_abyWrkBufferMask.data(), nOutXSize,
3938 : nOutYSize, GDT_UInt8, 1, static_cast<GSpacing>(nOutXSize),
3939 107 : psExtraArg) != CE_None)
3940 : {
3941 0 : return CE_Failure;
3942 : }
3943 : }
3944 :
3945 6240 : if (m_nColorTableComponent != 0)
3946 : {
3947 5254 : poColorTable = poSourceBand->GetColorTable();
3948 5254 : if (poColorTable == nullptr)
3949 : {
3950 0 : CPLError(CE_Failure, CPLE_AppDefined,
3951 : "Source band has no color table.");
3952 0 : return CE_Failure;
3953 : }
3954 : }
3955 : }
3956 :
3957 : /* -------------------------------------------------------------------- */
3958 : /* Selectively copy into output buffer with nodata masking, */
3959 : /* and/or scaling. */
3960 : /* -------------------------------------------------------------------- */
3961 :
3962 10815 : const bool bTwoStepDataTypeConversion =
3963 10815 : CPL_TO_BOOL(
3964 21499 : GDALDataTypeIsConversionLossy(eWrkDataType, eVRTBandDataType)) &&
3965 10684 : !CPL_TO_BOOL(GDALDataTypeIsConversionLossy(eVRTBandDataType, eBufType));
3966 :
3967 10815 : size_t idxBuffer = 0;
3968 163945 : for (int iY = 0; iY < nOutYSize; iY++)
3969 : {
3970 153130 : GByte *pDstLocation = static_cast<GByte *>(pData) +
3971 153130 : static_cast<GPtrDiff_t>(nLineSpace) * iY;
3972 :
3973 38320218 : for (int iX = 0; iX < nOutXSize;
3974 38167041 : iX++, pDstLocation += nPixelSpace, idxBuffer++)
3975 : {
3976 : WorkingDT afResult[2];
3977 38167041 : if (pafData && !bIsComplex)
3978 : {
3979 30370338 : WorkingDT fResult = pafData[idxBuffer];
3980 30370338 : if (bNoDataSetIsNan && std::isnan(fResult))
3981 20978582 : continue;
3982 52623278 : if (bNoDataSetAndNotNan &&
3983 22252846 : ARE_REAL_EQUAL(fResult, fWorkingDataTypeNoData))
3984 19790276 : continue;
3985 10580156 : if (pabyMask && pabyMask[idxBuffer] == 0)
3986 1188350 : continue;
3987 :
3988 9391796 : if (poColorTable)
3989 : {
3990 : const GDALColorEntry *poEntry =
3991 4071050 : poColorTable->GetColorEntry(static_cast<int>(fResult));
3992 4071050 : if (poEntry)
3993 : {
3994 4071050 : if (m_nColorTableComponent == 1)
3995 1398360 : fResult = poEntry->c1;
3996 2672700 : else if (m_nColorTableComponent == 2)
3997 1243780 : fResult = poEntry->c2;
3998 1428910 : else if (m_nColorTableComponent == 3)
3999 1243780 : fResult = poEntry->c3;
4000 185131 : else if (m_nColorTableComponent == 4)
4001 185131 : fResult = poEntry->c4;
4002 : }
4003 : else
4004 : {
4005 0 : CPLErrorOnce(CE_Failure, CPLE_AppDefined,
4006 : "No entry %d.", static_cast<int>(fResult));
4007 0 : continue;
4008 : }
4009 : }
4010 :
4011 9391796 : if ((m_nProcessingFlags & PROCESSING_FLAG_SCALING_LINEAR) != 0)
4012 : {
4013 2629752 : fResult = static_cast<WorkingDT>(fResult * m_dfScaleRatio +
4014 2629752 : m_dfScaleOff);
4015 : }
4016 6762044 : else if ((m_nProcessingFlags &
4017 : PROCESSING_FLAG_SCALING_EXPONENTIAL) != 0)
4018 : {
4019 6379 : if (!m_bSrcMinMaxDefined)
4020 : {
4021 1 : int bSuccessMin = FALSE;
4022 1 : int bSuccessMax = FALSE;
4023 2 : double adfMinMax[2] = {
4024 1 : poSourceBand->GetMinimum(&bSuccessMin),
4025 1 : poSourceBand->GetMaximum(&bSuccessMax)};
4026 2 : if ((bSuccessMin && bSuccessMax) ||
4027 1 : poSourceBand->ComputeRasterMinMax(
4028 : TRUE, adfMinMax) == CE_None)
4029 : {
4030 1 : m_dfSrcMin = adfMinMax[0];
4031 1 : m_dfSrcMax = adfMinMax[1];
4032 1 : m_bSrcMinMaxDefined = true;
4033 : }
4034 : else
4035 : {
4036 0 : CPLError(CE_Failure, CPLE_AppDefined,
4037 : "Cannot determine source min/max value");
4038 0 : return CE_Failure;
4039 : }
4040 : }
4041 :
4042 12758 : double dfPowVal = (m_dfSrcMin == m_dfSrcMax)
4043 6379 : ? 0
4044 6379 : : (fResult - m_dfSrcMin) /
4045 6379 : (m_dfSrcMax - m_dfSrcMin);
4046 6379 : if (m_bClip)
4047 : {
4048 6374 : if (dfPowVal < 0.0)
4049 1 : dfPowVal = 0.0;
4050 6373 : else if (dfPowVal > 1.0)
4051 700 : dfPowVal = 1.0;
4052 : }
4053 6379 : fResult =
4054 6379 : static_cast<WorkingDT>((m_dfDstMax - m_dfDstMin) *
4055 6379 : pow(dfPowVal, m_dfExponent) +
4056 6379 : m_dfDstMin);
4057 : }
4058 :
4059 9391796 : if (!m_adfLUTInputs.empty())
4060 995684 : fResult = static_cast<WorkingDT>(LookupValue(fResult));
4061 :
4062 9391796 : if (m_nMaxValue != 0 && fResult > m_nMaxValue)
4063 800 : fResult = static_cast<WorkingDT>(m_nMaxValue);
4064 :
4065 9391796 : afResult[0] = fResult;
4066 9391796 : afResult[1] = 0;
4067 : }
4068 7796703 : else if (pafData && bIsComplex)
4069 : {
4070 1018 : afResult[0] = pafData[2 * idxBuffer];
4071 1018 : afResult[1] = pafData[2 * idxBuffer + 1];
4072 :
4073 : // Do not use color table.
4074 1018 : if ((m_nProcessingFlags & PROCESSING_FLAG_SCALING_LINEAR) != 0)
4075 : {
4076 4 : afResult[0] = static_cast<WorkingDT>(
4077 4 : afResult[0] * m_dfScaleRatio + m_dfScaleOff);
4078 4 : afResult[1] = static_cast<WorkingDT>(
4079 4 : afResult[1] * m_dfScaleRatio + m_dfScaleOff);
4080 : }
4081 :
4082 : /* Do not use LUT */
4083 : }
4084 : else
4085 : {
4086 7795692 : afResult[0] = static_cast<WorkingDT>(m_dfScaleOff);
4087 7795692 : afResult[1] = 0;
4088 :
4089 7795692 : if (!m_adfLUTInputs.empty())
4090 0 : afResult[0] =
4091 0 : static_cast<WorkingDT>(LookupValue(afResult[0]));
4092 :
4093 7795692 : if (m_nMaxValue != 0 && afResult[0] > m_nMaxValue)
4094 0 : afResult[0] = static_cast<WorkingDT>(m_nMaxValue);
4095 : }
4096 :
4097 17188459 : if (eBufType == GDT_UInt8 && eVRTBandDataType == GDT_UInt8)
4098 : {
4099 6626760 : *pDstLocation = static_cast<GByte>(std::min(
4100 13253500 : 255.0f,
4101 6626760 : std::max(0.0f, static_cast<float>(afResult[0]) + 0.5f)));
4102 : }
4103 10561759 : else if (!bTwoStepDataTypeConversion)
4104 : {
4105 391358 : CopyWordOut(afResult, eWrkDataType, pDstLocation, eBufType);
4106 : }
4107 10170378 : else if (eVRTBandDataType == GDT_Float32 && eBufType == GDT_Float64)
4108 : {
4109 : // Particular case of the below 2-step conversion.
4110 : // Helps a bit for some geolocation based warping with Sentinel3
4111 : // data where the longitude/latitude arrays are Int32 bands,
4112 : // rescaled in VRT as Float32 and requested as Float64
4113 : float fVal;
4114 0 : GDALCopyWord(afResult[0], fVal);
4115 0 : *reinterpret_cast<double *>(pDstLocation) = fVal;
4116 : }
4117 : else
4118 : {
4119 : GByte abyTemp[2 * sizeof(double)];
4120 : // Convert first to the VRTRasterBand data type
4121 : // to get its clamping, before outputting to buffer data type
4122 10170378 : CopyWordOut(afResult, eWrkDataType, abyTemp, eVRTBandDataType);
4123 10170378 : GDALCopyWords(abyTemp, eVRTBandDataType, 0, pDstLocation,
4124 : eBufType, 0, 1);
4125 : }
4126 : }
4127 : }
4128 :
4129 10815 : return CE_None;
4130 : }
4131 :
4132 : // Explicitly instantiate template method, as it is used in another file.
4133 : template CPLErr VRTComplexSource::RasterIOInternal<float>(
4134 : GDALRasterBand *poSourceBand, GDALDataType eVRTBandDataType, int nReqXOff,
4135 : int nReqYOff, int nReqXSize, int nReqYSize, void *pData, int nOutXSize,
4136 : int nOutYSize, GDALDataType eBufType, GSpacing nPixelSpace,
4137 : GSpacing nLineSpace, GDALRasterIOExtraArg *psExtraArg,
4138 : GDALDataType eWrkDataType, WorkingState &oWorkingState);
4139 :
4140 : /************************************************************************/
4141 : /* AreValuesUnchanged() */
4142 : /************************************************************************/
4143 :
4144 11 : bool VRTComplexSource::AreValuesUnchanged() const
4145 : {
4146 14 : return m_dfScaleOff == 0.0 && m_dfScaleRatio == 1.0 &&
4147 32 : m_adfLUTInputs.empty() && m_nColorTableComponent == 0 &&
4148 18 : (m_nProcessingFlags & PROCESSING_FLAG_SCALING_EXPONENTIAL) == 0;
4149 : }
4150 :
4151 : /************************************************************************/
4152 : /* GetMinimum() */
4153 : /************************************************************************/
4154 :
4155 2 : double VRTComplexSource::GetMinimum(int nXSize, int nYSize, int *pbSuccess)
4156 : {
4157 2 : if (AreValuesUnchanged())
4158 : {
4159 1 : return VRTSimpleSource::GetMinimum(nXSize, nYSize, pbSuccess);
4160 : }
4161 :
4162 1 : *pbSuccess = FALSE;
4163 1 : return 0;
4164 : }
4165 :
4166 : /************************************************************************/
4167 : /* GetMaximum() */
4168 : /************************************************************************/
4169 :
4170 2 : double VRTComplexSource::GetMaximum(int nXSize, int nYSize, int *pbSuccess)
4171 : {
4172 2 : if (AreValuesUnchanged())
4173 : {
4174 1 : return VRTSimpleSource::GetMaximum(nXSize, nYSize, pbSuccess);
4175 : }
4176 :
4177 1 : *pbSuccess = FALSE;
4178 1 : return 0;
4179 : }
4180 :
4181 : /************************************************************************/
4182 : /* GetHistogram() */
4183 : /************************************************************************/
4184 :
4185 1 : CPLErr VRTComplexSource::GetHistogram(int nXSize, int nYSize, double dfMin,
4186 : double dfMax, int nBuckets,
4187 : GUIntBig *panHistogram,
4188 : int bIncludeOutOfRange, int bApproxOK,
4189 : GDALProgressFunc pfnProgress,
4190 : void *pProgressData)
4191 : {
4192 2 : if (AreValuesUnchanged() &&
4193 1 : (m_nProcessingFlags & PROCESSING_FLAG_NODATA) == 0)
4194 : {
4195 0 : return VRTSimpleSource::GetHistogram(
4196 : nXSize, nYSize, dfMin, dfMax, nBuckets, panHistogram,
4197 0 : bIncludeOutOfRange, bApproxOK, pfnProgress, pProgressData);
4198 : }
4199 :
4200 1 : return CE_Failure;
4201 : }
4202 :
4203 : /************************************************************************/
4204 : /* ==================================================================== */
4205 : /* VRTFuncSource */
4206 : /* ==================================================================== */
4207 : /************************************************************************/
4208 :
4209 : /************************************************************************/
4210 : /* VRTFuncSource() */
4211 : /************************************************************************/
4212 :
4213 18 : VRTFuncSource::VRTFuncSource()
4214 : : pfnReadFunc(nullptr), pCBData(nullptr), eType(GDT_UInt8),
4215 18 : fNoDataValue(static_cast<float>(VRT_NODATA_UNSET))
4216 : {
4217 18 : }
4218 :
4219 : /************************************************************************/
4220 : /* ~VRTFuncSource() */
4221 : /************************************************************************/
4222 :
4223 36 : VRTFuncSource::~VRTFuncSource()
4224 : {
4225 36 : }
4226 :
4227 : /************************************************************************/
4228 : /* GetType() */
4229 : /************************************************************************/
4230 :
4231 0 : const char *VRTFuncSource::GetType() const
4232 : {
4233 : static const char *TYPE = "FuncSource";
4234 0 : return TYPE;
4235 : }
4236 :
4237 : /************************************************************************/
4238 : /* SerializeToXML() */
4239 : /************************************************************************/
4240 :
4241 0 : CPLXMLNode *VRTFuncSource::SerializeToXML(CPL_UNUSED const char *pszVRTPath)
4242 : {
4243 0 : return nullptr;
4244 : }
4245 :
4246 : /************************************************************************/
4247 : /* RasterIO() */
4248 : /************************************************************************/
4249 :
4250 16 : CPLErr VRTFuncSource::RasterIO(GDALDataType /*eVRTBandDataType*/, int nXOff,
4251 : int nYOff, int nXSize, int nYSize, void *pData,
4252 : int nBufXSize, int nBufYSize,
4253 : GDALDataType eBufType, GSpacing nPixelSpace,
4254 : GSpacing nLineSpace,
4255 : GDALRasterIOExtraArg * /* psExtraArg */,
4256 : WorkingState & /* oWorkingState */)
4257 : {
4258 16 : if (nPixelSpace == GDALGetDataTypeSizeBytes(eBufType) &&
4259 16 : nLineSpace == nPixelSpace * nXSize && nBufXSize == nXSize &&
4260 32 : nBufYSize == nYSize && eBufType == eType)
4261 : {
4262 16 : return pfnReadFunc(pCBData, nXOff, nYOff, nXSize, nYSize, pData);
4263 : }
4264 : else
4265 : {
4266 0 : CPLError(CE_Failure, CPLE_AppDefined,
4267 : "VRTFuncSource::RasterIO() - Irregular request.");
4268 0 : CPLDebug("VRT", "Irregular request: %d,%d %d,%d, %d,%d %d,%d %d,%d",
4269 : static_cast<int>(nPixelSpace),
4270 : GDALGetDataTypeSizeBytes(eBufType),
4271 : static_cast<int>(nLineSpace),
4272 : static_cast<int>(nPixelSpace) * nXSize, nBufXSize, nXSize,
4273 : nBufYSize, nYSize, static_cast<int>(eBufType),
4274 0 : static_cast<int>(eType));
4275 :
4276 0 : return CE_Failure;
4277 : }
4278 : }
4279 :
4280 : /************************************************************************/
4281 : /* GetMinimum() */
4282 : /************************************************************************/
4283 :
4284 0 : double VRTFuncSource::GetMinimum(int /* nXSize */, int /* nYSize */,
4285 : int *pbSuccess)
4286 : {
4287 0 : *pbSuccess = FALSE;
4288 0 : return 0;
4289 : }
4290 :
4291 : /************************************************************************/
4292 : /* GetMaximum() */
4293 : /************************************************************************/
4294 :
4295 0 : double VRTFuncSource::GetMaximum(int /* nXSize */, int /* nYSize */,
4296 : int *pbSuccess)
4297 : {
4298 0 : *pbSuccess = FALSE;
4299 0 : return 0;
4300 : }
4301 :
4302 : /************************************************************************/
4303 : /* GetHistogram() */
4304 : /************************************************************************/
4305 :
4306 0 : CPLErr VRTFuncSource::GetHistogram(
4307 : int /* nXSize */, int /* nYSize */, double /* dfMin */, double /* dfMax */,
4308 : int /* nBuckets */, GUIntBig * /* panHistogram */,
4309 : int /* bIncludeOutOfRange */, int /* bApproxOK */,
4310 : GDALProgressFunc /* pfnProgress */, void * /* pProgressData */)
4311 : {
4312 0 : return CE_Failure;
4313 : }
4314 :
4315 : /************************************************************************/
4316 : /* VRTParseCoreSources() */
4317 : /************************************************************************/
4318 :
4319 103845 : VRTSource *VRTParseCoreSources(const CPLXMLNode *psChild,
4320 : const char *pszVRTPath,
4321 : VRTMapSharedResources &oMapSharedSources)
4322 :
4323 : {
4324 103845 : VRTSource *poSource = nullptr;
4325 :
4326 207683 : if (EQUAL(psChild->pszValue, VRTAveragedSource::GetTypeStatic()) ||
4327 103838 : (EQUAL(psChild->pszValue, VRTSimpleSource::GetTypeStatic()) &&
4328 103597 : STARTS_WITH_CI(CPLGetXMLValue(psChild, "Resampling", "Nearest"),
4329 : "Aver")))
4330 : {
4331 16 : poSource = new VRTAveragedSource();
4332 : }
4333 103829 : else if (EQUAL(psChild->pszValue, VRTSimpleSource::GetTypeStatic()))
4334 : {
4335 103588 : poSource = new VRTSimpleSource();
4336 : }
4337 241 : else if (EQUAL(psChild->pszValue, VRTComplexSource::GetTypeStatic()))
4338 : {
4339 233 : poSource = new VRTComplexSource();
4340 : }
4341 8 : else if (EQUAL(psChild->pszValue, VRTNoDataFromMaskSource::GetTypeStatic()))
4342 : {
4343 8 : poSource = new VRTNoDataFromMaskSource();
4344 : }
4345 : else
4346 : {
4347 0 : CPLError(CE_Failure, CPLE_AppDefined,
4348 : "VRTParseCoreSources() - Unknown source : %s",
4349 0 : psChild->pszValue);
4350 0 : return nullptr;
4351 : }
4352 :
4353 103845 : if (poSource->XMLInit(psChild, pszVRTPath, oMapSharedSources) == CE_None)
4354 103840 : return poSource;
4355 :
4356 5 : delete poSource;
4357 5 : return nullptr;
4358 : }
4359 :
4360 : /*! @endcond */
|