Coverage Report

Created: 2025-06-13 06:29

/src/gdal/gcore/gdal_proxy.h
Line
Count
Source (jump to first uncovered line)
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(double *) override;
68
    CPLErr SetGeoTransform(double *) 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 : 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
    double adfGeoTransform[6]{0, 1, 0, 0, 0, 1};
236
    bool m_bHasSrcSRS = false;
237
    bool 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
                         double *padfGeoTransform = 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(double *) override;
296
    CPLErr SetGeoTransform(double *) 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 GDALProxyPoolRasterBand : public GDALProxyRasterBand
322
{
323
  private:
324
    CPLHashSet *metadataSet = nullptr;
325
    CPLHashSet *metadataItemSet = nullptr;
326
    char *pszUnitType = nullptr;
327
    char **papszCategoryNames = nullptr;
328
    GDALColorTable *poColorTable = nullptr;
329
330
    int nSizeProxyOverviewRasterBand = 0;
331
    GDALProxyPoolOverviewRasterBand **papoProxyOverviewRasterBand = nullptr;
332
    GDALProxyPoolMaskBand *poProxyMaskBand = nullptr;
333
334
  protected:
335
    GDALRasterBand *
336
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
337
    void UnrefUnderlyingRasterBand(
338
        GDALRasterBand *poUnderlyingRasterBand) const override;
339
340
    friend class GDALProxyPoolOverviewRasterBand;
341
    friend class GDALProxyPoolMaskBand;
342
343
  public:
344
    GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDS, int nBand,
345
                            GDALDataType eDataType, int nBlockXSize,
346
                            int nBlockYSize);
347
    GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDS,
348
                            GDALRasterBand *poUnderlyingRasterBand);
349
    ~GDALProxyPoolRasterBand() override;
350
351
    void AddSrcMaskBandDescription(GDALDataType eDataType, int nBlockXSize,
352
                                   int nBlockYSize);
353
354
    void AddSrcMaskBandDescriptionFromUnderlying();
355
356
    // Special behavior for the following methods : they return a pointer
357
    // data type, that must be cached by the proxy, so it doesn't become invalid
358
    // when the underlying object get closed.
359
    char **GetMetadata(const char *pszDomain) override;
360
    const char *GetMetadataItem(const char *pszName,
361
                                const char *pszDomain) override;
362
    char **GetCategoryNames() override;
363
    const char *GetUnitType() override;
364
    GDALColorTable *GetColorTable() override;
365
    GDALRasterBand *GetOverview(int) override;
366
    GDALRasterBand *
367
    GetRasterSampleOverview(GUIntBig nDesiredSamples) override;  // TODO
368
    GDALRasterBand *GetMaskBand() override;
369
370
    CPLErr FlushCache(bool bAtClosing) override;
371
372
  private:
373
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolRasterBand)
374
};
375
376
/* ******************************************************************** */
377
/*                  GDALProxyPoolOverviewRasterBand                     */
378
/* ******************************************************************** */
379
380
class GDALProxyPoolOverviewRasterBand : public GDALProxyPoolRasterBand
381
{
382
  private:
383
    GDALProxyPoolRasterBand *poMainBand = nullptr;
384
    int nOverviewBand = 0;
385
386
    mutable GDALRasterBand *poUnderlyingMainRasterBand = nullptr;
387
    mutable int nRefCountUnderlyingMainRasterBand = 0;
388
389
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolOverviewRasterBand)
390
391
  protected:
392
    GDALRasterBand *
393
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
394
    void UnrefUnderlyingRasterBand(
395
        GDALRasterBand *poUnderlyingRasterBand) const override;
396
397
  public:
398
    GDALProxyPoolOverviewRasterBand(GDALProxyPoolDataset *poDS,
399
                                    GDALRasterBand *poUnderlyingOverviewBand,
400
                                    GDALProxyPoolRasterBand *poMainBand,
401
                                    int nOverviewBand);
402
    ~GDALProxyPoolOverviewRasterBand() override;
403
};
404
405
/* ******************************************************************** */
406
/*                      GDALProxyPoolMaskBand                           */
407
/* ******************************************************************** */
408
409
class GDALProxyPoolMaskBand : public GDALProxyPoolRasterBand
410
{
411
  private:
412
    GDALProxyPoolRasterBand *poMainBand = nullptr;
413
414
    mutable GDALRasterBand *poUnderlyingMainRasterBand = nullptr;
415
    mutable int nRefCountUnderlyingMainRasterBand = 0;
416
417
    CPL_DISALLOW_COPY_ASSIGN(GDALProxyPoolMaskBand)
418
419
  protected:
420
    GDALRasterBand *
421
    RefUnderlyingRasterBand(bool bForceOpen = true) const override;
422
    void UnrefUnderlyingRasterBand(
423
        GDALRasterBand *poUnderlyingRasterBand) const override;
424
425
  public:
426
    GDALProxyPoolMaskBand(GDALProxyPoolDataset *poDS,
427
                          GDALRasterBand *poUnderlyingMaskBand,
428
                          GDALProxyPoolRasterBand *poMainBand);
429
    GDALProxyPoolMaskBand(GDALProxyPoolDataset *poDS,
430
                          GDALProxyPoolRasterBand *poMainBand,
431
                          GDALDataType eDataType, int nBlockXSize,
432
                          int nBlockYSize);
433
    ~GDALProxyPoolMaskBand() override;
434
435
    bool IsMaskBand() const override
436
0
    {
437
0
        return true;
438
0
    }
439
};
440
441
#endif
442
443
/* ******************************************************************** */
444
/*            C types and methods declarations                          */
445
/* ******************************************************************** */
446
447
CPL_C_START
448
449
typedef struct GDALProxyPoolDatasetHS *GDALProxyPoolDatasetH;
450
451
GDALProxyPoolDatasetH CPL_DLL GDALProxyPoolDatasetCreate(
452
    const char *pszSourceDatasetDescription, int nRasterXSize, int nRasterYSize,
453
    GDALAccess eAccess, int bShared, const char *pszProjectionRef,
454
    double *padfGeoTransform);
455
456
void CPL_DLL
457
GDALProxyPoolDatasetDelete(GDALProxyPoolDatasetH hProxyPoolDataset);
458
459
void CPL_DLL GDALProxyPoolDatasetAddSrcBandDescription(
460
    GDALProxyPoolDatasetH hProxyPoolDataset, GDALDataType eDataType,
461
    int nBlockXSize, int nBlockYSize);
462
463
int CPL_DLL GDALGetMaxDatasetPoolSize(void);
464
465
CPL_C_END
466
467
#endif /* #ifndef DOXYGEN_SKIP */
468
469
#endif /* GDAL_PROXY_H_INCLUDED */