Coverage Report

Created: 2026-07-14 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/gcore/gdal_proxy.h
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Core
4
 * Purpose:  GDAL Core C++/Private declarations
5
 * Author:   Even Rouault <even dot rouault at spatialys.com>
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
9
 *
10
 * SPDX-License-Identifier: MIT
11
 ****************************************************************************/
12
13
#ifndef GDAL_PROXY_H_INCLUDED
14
#define GDAL_PROXY_H_INCLUDED
15
16
#ifndef DOXYGEN_SKIP
17
18
#include "gdal.h"
19
20
#ifdef __cplusplus
21
22
#include "gdal_priv.h"
23
#include "cpl_hash_set.h"
24
25
/* ******************************************************************** */
26
/*                        GDALProxyDataset                              */
27
/* ******************************************************************** */
28
29
class CPL_DLL GDALProxyDataset : public GDALDataset
30
{
31
  protected:
32
    GDALProxyDataset()
33
0
    {
34
0
    }
35
36
    virtual GDALDataset *RefUnderlyingDataset() const = 0;
37
    virtual void UnrefUnderlyingDataset(GDALDataset *poUnderlyingDataset) const;
38
39
    CPLErr IBuildOverviews(const char *, int, const int *, int, const int *,
40
                           GDALProgressFunc, void *,
41
                           CSLConstList papszOptions) override;
42
    CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
43
                     GDALDataType, int, BANDMAP_TYPE, GSpacing, GSpacing,
44
                     GSpacing, GDALRasterIOExtraArg *psExtraArg) override;
45
    CPLErr BlockBasedRasterIO(GDALRWFlag eRWFlag, int nXOff, int nYOff,
46
                              int nXSize, int nYSize, void *pData,
47
                              int nBufXSize, int nBufYSize,
48
                              GDALDataType eBufType, int nBandCount,
49
                              const int *panBandMap, GSpacing nPixelSpace,
50
                              GSpacing nLineSpace, GSpacing nBandSpace,
51
                              GDALRasterIOExtraArg *psExtraArg) override;
52
53
  public:
54
    CPLErr Close(GDALProgressFunc, void *) override;
55
    bool GetCloseReportsProgress() const override;
56
57
    char **GetMetadataDomainList() override;
58
    CSLConstList GetMetadata(const char *pszDomain) override;
59
    CPLErr SetMetadata(CSLConstList papszMetadata,
60
                       const char *pszDomain) override;
61
    const char *GetMetadataItem(const char *pszName,
62
                                const char *pszDomain) override;
63
    CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
64
                           const char *pszDomain) override;
65
66
    CPLErr FlushCache(bool bAtClosing) override;
67
68
    const OGRSpatialReference *GetSpatialRef() const override;
69
    CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
70
71
    CPLErr GetGeoTransform(GDALGeoTransform &) const override;
72
    CPLErr SetGeoTransform(const GDALGeoTransform &) override;
73
74
    void *GetInternalHandle(const char *) override;
75
    GDALDriver *GetDriver() const override;
76
    char **GetFileList() override;
77
78
    int GetGCPCount() override;
79
    const OGRSpatialReference *GetGCPSpatialRef() const override;
80
    const GDAL_GCP *GetGCPs() override;
81
    CPLErr SetGCPs(int nGCPCount, const GDAL_GCP *pasGCPList,
82
                   const OGRSpatialReference *poGCP_SRS) override;
83
84
    CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
85
                      int nBufXSize, int nBufYSize, GDALDataType eDT,
86
                      int nBandCount, int *panBandList,
87
                      CSLConstList papszOptions) override;
88
89
    CPLErr CreateMaskBand(int nFlags) override;
90
91
    virtual CPLStringList
92
    GetCompressionFormats(int nXOff, int nYOff, int nXSize, int nYSize,
93
                          int nBandCount, const int *panBandList) override;
94
    virtual CPLErr ReadCompressedData(const char *pszFormat, int nXOff,
95
                                      int nYOff, int nXSize, int nYSize,
96
                                      int nBandCount, const int *panBandList,
97
                                      void **ppBuffer, size_t *pnBufferSize,
98
                                      char **ppszDetailedFormat) override;
99
100
  private:
101
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyDataset)
102
};
103
104
/* ******************************************************************** */
105
/*                         GDALProxyRasterBand                          */
106
/* ******************************************************************** */
107
108
class CPL_DLL GDALProxyRasterBand : public GDALRasterBand
109
{
110
  protected:
111
    GDALProxyRasterBand()
112
0
    {
113
0
    }
114
115
    virtual GDALRasterBand *
116
    RefUnderlyingRasterBand(bool bForceOpen = true) const = 0;
117
    virtual void
118
    UnrefUnderlyingRasterBand(GDALRasterBand *poUnderlyingRasterBand) const;
119
120
    CPLErr IReadBlock(int, int, void *) override;
121
    CPLErr IWriteBlock(int, int, void *) override;
122
    CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
123
                     GDALDataType, GSpacing, GSpacing,
124
                     GDALRasterIOExtraArg *psExtraArg) override;
125
126
    int IGetDataCoverageStatus(int nXOff, int nYOff, int nXSize, int nYSize,
127
                               int nMaskFlagStop, double *pdfDataPct) override;
128
129
  public:
130
    char **GetMetadataDomainList() override;
131
    CSLConstList GetMetadata(const char *pszDomain) override;
132
    CPLErr SetMetadata(CSLConstList papszMetadata,
133
                       const char *pszDomain) override;
134
    const char *GetMetadataItem(const char *pszName,
135
                                const char *pszDomain) override;
136
    CPLErr SetMetadataItem(const char *pszName, const char *pszValue,
137
                           const char *pszDomain) override;
138
139
    GDALRasterBlock *GetLockedBlockRef(int nXBlockOff, int nYBlockOff,
140
                                       int bJustInitialize) override;
141
142
    GDALRasterBlock *TryGetLockedBlockRef(int nXBlockOff,
143
                                          int nYBlockYOff) override;
144
145
    CPLErr FlushBlock(int nXBlockOff, int nYBlockOff,
146
                      int bWriteDirtyBlock) override;
147
148
    CPLErr FlushCache(bool bAtClosing) override;
149
    char **GetCategoryNames() override;
150
    double GetNoDataValue(int *pbSuccess = nullptr) override;
151
    double GetMinimum(int *pbSuccess = nullptr) override;
152
    double GetMaximum(int *pbSuccess = nullptr) override;
153
    double GetOffset(int *pbSuccess = nullptr) override;
154
    double GetScale(int *pbSuccess = nullptr) override;
155
    const char *GetUnitType() override;
156
    GDALColorInterp GetColorInterpretation() override;
157
    GDALColorTable *GetColorTable() override;
158
    CPLErr Fill(double dfRealValue, double dfImaginaryValue = 0) override;
159
160
    CPLErr SetCategoryNames(char **) override;
161
    CPLErr SetNoDataValue(double) override;
162
    CPLErr DeleteNoDataValue() override;
163
    CPLErr SetColorTable(GDALColorTable *) override;
164
    CPLErr SetColorInterpretation(GDALColorInterp) override;
165
    CPLErr SetOffset(double) override;
166
    CPLErr SetScale(double) override;
167
    CPLErr SetUnitType(const char *) override;
168
169
    CPLErr GetStatistics(int bApproxOK, int bForce, double *pdfMin,
170
                         double *pdfMax, double *pdfMean,
171
                         double *padfStdDev) override;
172
    CPLErr ComputeStatistics(int bApproxOK, double *pdfMin, double *pdfMax,
173
                             double *pdfMean, double *pdfStdDev,
174
                             GDALProgressFunc, void *pProgressData,
175
                             CSLConstList papszOptions) override;
176
    CPLErr SetStatistics(double dfMin, double dfMax, double dfMean,
177
                         double dfStdDev) override;
178
    CPLErr ComputeRasterMinMax(int, double *) override;
179
180
    int HasArbitraryOverviews() override;
181
    int GetOverviewCount() override;
182
    GDALRasterBand *GetOverview(int) override;
183
    GDALRasterBand *GetRasterSampleOverview(GUIntBig) override;
184
    CPLErr BuildOverviews(const char *, int, const int *, GDALProgressFunc,
185
                          void *, CSLConstList papszOptions) override;
186
187
    CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
188
                      int nBufXSize, int nBufYSize, GDALDataType eDT,
189
                      CSLConstList papszOptions) override;
190
191
    CPLErr GetHistogram(double dfMin, double dfMax, int nBuckets,
192
                        GUIntBig *panHistogram, int bIncludeOutOfRange,
193
                        int bApproxOK, GDALProgressFunc,
194
                        void *pProgressData) override;
195
196
    CPLErr GetDefaultHistogram(double *pdfMin, double *pdfMax, int *pnBuckets,
197
                               GUIntBig **ppanHistogram, int bForce,
198
                               GDALProgressFunc, void *pProgressData) override;
199
    CPLErr SetDefaultHistogram(double dfMin, double dfMax, int nBuckets,
200
                               GUIntBig *panHistogram) override;
201
202
    GDALRasterAttributeTable *GetDefaultRAT() override;
203
    CPLErr SetDefaultRAT(const GDALRasterAttributeTable *) override;
204
205
    GDALRasterBand *GetMaskBand() override;
206
    int GetMaskFlags() override;
207
    CPLErr CreateMaskBand(int nFlags) override;
208
    bool IsMaskBand() const override;
209
    GDALMaskValueRange GetMaskValueRange() const override;
210
211
    CPLVirtualMem *GetVirtualMemAuto(GDALRWFlag eRWFlag, int *pnPixelSpace,
212
                                     GIntBig *pnLineSpace,
213
                                     CSLConstList papszOptions) override;
214
215
    CPLErr InterpolateAtPoint(double dfPixel, double dfLine,
216
                              GDALRIOResampleAlg eInterpolation,
217
                              double *pdfRealValue,
218
                              double *pdfImagValue) const override;
219
220
    void EnablePixelTypeSignedByteWarning(bool b) override;
221
222
  private:
223
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyRasterBand)
224
};
225
226
/* ******************************************************************** */
227
/*                     GDALProxyPoolDataset                             */
228
/* ******************************************************************** */
229
230
typedef struct _GDALProxyPoolCacheEntry GDALProxyPoolCacheEntry;
231
class GDALProxyPoolRasterBand;
232
233
class CPL_DLL GDALProxyPoolDataset /* non final */ : public GDALProxyDataset
234
{
235
  private:
236
    GIntBig responsiblePID = -1;
237
    mutable char *pszProjectionRef = nullptr;
238
    const CPLStringList m_aosAllowedDrivers;
239
    mutable OGRSpatialReference *m_poSRS = nullptr;
240
    mutable OGRSpatialReference *m_poGCPSRS = nullptr;
241
    GDALGeoTransform m_gt{};
242
    bool m_bHasSrcSRS = false;
243
    bool m_bHasSrcGeoTransform = false;
244
    char *pszGCPProjection = nullptr;
245
    int nGCPCount = 0;
246
    GDAL_GCP *pasGCPList = nullptr;
247
    CPLHashSet *metadataSet = nullptr;
248
    CPLHashSet *metadataItemSet = nullptr;
249
250
    mutable GDALProxyPoolCacheEntry *cacheEntry = nullptr;
251
    char *m_pszOwner = nullptr;
252
253
    GDALDataset *RefUnderlyingDataset(bool bForceOpen) const;
254
255
    GDALProxyPoolDataset(const char *pszSourceDatasetDescription,
256
                         GDALAccess eAccess, int bShared, const char *pszOwner,
257
                         CSLConstList papszAllowedDrivers);
258
259
  protected:
260
    GDALDataset *RefUnderlyingDataset() const override;
261
    void
262
    UnrefUnderlyingDataset(GDALDataset *poUnderlyingDataset) const override;
263
264
    friend class GDALProxyPoolRasterBand;
265
266
  public:
267
    GDALProxyPoolDataset(const char *pszSourceDatasetDescription,
268
                         int nRasterXSize, int nRasterYSize,
269
                         GDALAccess eAccess = GA_ReadOnly, int bShared = FALSE,
270
                         const char *pszProjectionRef = nullptr,
271
                         const GDALGeoTransform *pGT = nullptr,
272
                         const char *pszOwner = nullptr,
273
                         CSLConstList papszAllowedDrivers = nullptr);
274
275
    static GDALProxyPoolDataset *
276
    Create(const char *pszSourceDatasetDescription,
277
           CSLConstList papszOpenOptions = nullptr,
278
           GDALAccess eAccess = GA_ReadOnly, int bShared = FALSE,
279
           const char *pszOwner = nullptr,
280
           CSLConstList papszAllowedDrivers = nullptr);
281
282
    ~GDALProxyPoolDataset() override;
283
284
    void SetOpenOptions(CSLConstList papszOpenOptions);
285
286
    // If size (nBlockXSize&nBlockYSize) parameters is zero
287
    // they will be loaded when RefUnderlyingRasterBand function is called.
288
    // But in this case we cannot use them in other non-virtual methods before
289
    // RefUnderlyingRasterBand fist call.
290
    void AddSrcBandDescription(GDALDataType eDataType, int nBlockXSize,
291
                               int nBlockYSize);
292
293
    // Used by VRT SimpleSource to add a single GDALProxyPoolRasterBand while
294
    // keeping all other bands initialized to a nullptr. This is under the
295
    // assumption, VRT SimpleSource will not have to access any other bands than
296
    // the one added.
297
    void AddSrcBand(int nBand, GDALDataType eDataType, int nBlockXSize,
298
                    int nBlockYSize);
299
    CPLErr FlushCache(bool bAtClosing) override;
300
301
    const OGRSpatialReference *GetSpatialRef() const override;
302
    CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
303
304
    CPLErr GetGeoTransform(GDALGeoTransform &) const override;
305
    CPLErr SetGeoTransform(const GDALGeoTransform &) override;
306
307
    // Special behavior for the following methods : they return a pointer
308
    // data type, that must be cached by the proxy, so it doesn't become invalid
309
    // when the underlying object get closed.
310
    CSLConstList GetMetadata(const char *pszDomain) override;
311
    const char *GetMetadataItem(const char *pszName,
312
                                const char *pszDomain) override;
313
314
    void *GetInternalHandle(const char *pszRequest) override;
315
316
    const OGRSpatialReference *GetGCPSpatialRef() const override;
317
    const GDAL_GCP *GetGCPs() override;
318
319
  private:
320
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolDataset)
321
};
322
323
/* ******************************************************************** */
324
/*                  GDALProxyPoolRasterBand                             */
325
/* ******************************************************************** */
326
327
class GDALProxyPoolOverviewRasterBand;
328
class GDALProxyPoolMaskBand;
329
330
class CPL_DLL
331
    GDALProxyPoolRasterBand /* non final */ : public GDALProxyRasterBand
332
{
333
  private:
334
    CPLHashSet *metadataSet = nullptr;
335
    CPLHashSet *metadataItemSet = nullptr;
336
    char *pszUnitType = nullptr;
337
    char **papszCategoryNames = nullptr;
338
    GDALColorTable *poColorTable = nullptr;
339
340
    int nSizeProxyOverviewRasterBand = 0;
341
    GDALProxyPoolOverviewRasterBand **papoProxyOverviewRasterBand = nullptr;
342
    GDALProxyPoolMaskBand *poProxyMaskBand = nullptr;
343
344
  protected:
345
    GDALRasterBand *
346
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
347
    void UnrefUnderlyingRasterBand(
348
        GDALRasterBand *poUnderlyingRasterBand) const override;
349
350
    friend class GDALProxyPoolOverviewRasterBand;
351
    friend class GDALProxyPoolMaskBand;
352
353
  public:
354
    GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDS, int nBand,
355
                            GDALDataType eDataType, int nBlockXSize,
356
                            int nBlockYSize);
357
    GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDS,
358
                            GDALRasterBand *poUnderlyingRasterBand);
359
    ~GDALProxyPoolRasterBand() override;
360
361
    void AddSrcMaskBandDescription(GDALDataType eDataType, int nBlockXSize,
362
                                   int nBlockYSize);
363
364
    void AddSrcMaskBandDescriptionFromUnderlying();
365
366
    // Special behavior for the following methods : they return a pointer
367
    // data type, that must be cached by the proxy, so it doesn't become invalid
368
    // when the underlying object get closed.
369
    CSLConstList GetMetadata(const char *pszDomain) override;
370
    const char *GetMetadataItem(const char *pszName,
371
                                const char *pszDomain) override;
372
    char **GetCategoryNames() override;
373
    const char *GetUnitType() override;
374
    GDALColorTable *GetColorTable() override;
375
    GDALRasterBand *GetOverview(int) override;
376
    GDALRasterBand *
377
    GetRasterSampleOverview(GUIntBig nDesiredSamples) override;  // TODO
378
    GDALRasterBand *GetMaskBand() override;
379
380
    CPLErr FlushCache(bool bAtClosing) override;
381
382
  private:
383
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolRasterBand)
384
};
385
386
/* ******************************************************************** */
387
/*                  GDALProxyPoolOverviewRasterBand                     */
388
/* ******************************************************************** */
389
390
class GDALProxyPoolOverviewRasterBand final : public GDALProxyPoolRasterBand
391
{
392
  private:
393
    GDALProxyPoolRasterBand *poMainBand = nullptr;
394
    int nOverviewBand = 0;
395
396
    mutable GDALRasterBand *poUnderlyingMainRasterBand = nullptr;
397
    mutable int nRefCountUnderlyingMainRasterBand = 0;
398
399
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolOverviewRasterBand)
400
401
  protected:
402
    GDALRasterBand *
403
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
404
    void UnrefUnderlyingRasterBand(
405
        GDALRasterBand *poUnderlyingRasterBand) const override;
406
407
  public:
408
    GDALProxyPoolOverviewRasterBand(GDALProxyPoolDataset *poDS,
409
                                    GDALRasterBand *poUnderlyingOverviewBand,
410
                                    GDALProxyPoolRasterBand *poMainBand,
411
                                    int nOverviewBand);
412
    ~GDALProxyPoolOverviewRasterBand() override;
413
};
414
415
/* ******************************************************************** */
416
/*                      GDALProxyPoolMaskBand                           */
417
/* ******************************************************************** */
418
419
class GDALProxyPoolMaskBand final : public GDALProxyPoolRasterBand
420
{
421
  private:
422
    GDALProxyPoolRasterBand *poMainBand = nullptr;
423
424
    mutable GDALRasterBand *poUnderlyingMainRasterBand = nullptr;
425
    mutable int nRefCountUnderlyingMainRasterBand = 0;
426
427
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolMaskBand)
428
429
  protected:
430
    GDALRasterBand *
431
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
432
    void UnrefUnderlyingRasterBand(
433
        GDALRasterBand *poUnderlyingRasterBand) const override;
434
435
  public:
436
    GDALProxyPoolMaskBand(GDALProxyPoolDataset *poDS,
437
                          GDALRasterBand *poUnderlyingMaskBand,
438
                          GDALProxyPoolRasterBand *poMainBand);
439
    GDALProxyPoolMaskBand(GDALProxyPoolDataset *poDS,
440
                          GDALProxyPoolRasterBand *poMainBand,
441
                          GDALDataType eDataType, int nBlockXSize,
442
                          int nBlockYSize);
443
    ~GDALProxyPoolMaskBand() override;
444
445
    bool IsMaskBand() const override
446
0
    {
447
0
        return true;
448
0
    }
449
};
450
451
#endif
452
453
/* ******************************************************************** */
454
/*            C types and methods declarations                          */
455
/* ******************************************************************** */
456
457
CPL_C_START
458
459
typedef struct GDALProxyPoolDatasetHS *GDALProxyPoolDatasetH;
460
461
GDALProxyPoolDatasetH CPL_DLL GDALProxyPoolDatasetCreate(
462
    const char *pszSourceDatasetDescription, int nRasterXSize, int nRasterYSize,
463
    GDALAccess eAccess, int bShared, const char *pszProjectionRef,
464
    const double *padfGeoTransform);
465
466
void CPL_DLL
467
GDALProxyPoolDatasetDelete(GDALProxyPoolDatasetH hProxyPoolDataset);
468
469
void CPL_DLL GDALProxyPoolDatasetAddSrcBandDescription(
470
    GDALProxyPoolDatasetH hProxyPoolDataset, GDALDataType eDataType,
471
    int nBlockXSize, int nBlockYSize);
472
473
int CPL_DLL GDALGetMaxDatasetPoolSize(void);
474
475
CPL_C_END
476
477
#endif /* #ifndef DOXYGEN_SKIP */
478
479
#endif /* GDAL_PROXY_H_INCLUDED */