Coverage Report

Created: 2026-04-01 06:20

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