Coverage Report

Created: 2025-11-16 06:25

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