Coverage Report

Created: 2025-08-11 09:23

/src/gdal/frmts/hfa/hfadataset.h
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Name:     hfadataset.cpp
4
 * Project:  Erdas Imagine Driver
5
 * Purpose:  Main driver for Erdas Imagine format.
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 1999, Frank Warmerdam
10
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
11
 *
12
 * SPDX-License-Identifier: MIT
13
 ****************************************************************************/
14
15
#ifndef HFADATASET_H_INCLUDED
16
#define HFADATASET_H_INCLUDED
17
18
#include <cstddef>
19
#include <vector>
20
21
#include "gdal_frmts.h"
22
#include "gdal_pam.h"
23
#include "gdal_rat.h"
24
#include "hfa_p.h"
25
#include "ogr_spatialref.h"
26
#include "ogr_srs_api.h"
27
28
/************************************************************************/
29
/* ==================================================================== */
30
/*                              HFADataset                              */
31
/* ==================================================================== */
32
/************************************************************************/
33
34
class HFARasterBand;
35
36
class HFADataset final : public GDALPamDataset
37
{
38
    friend class HFARasterBand;
39
40
    HFAHandle hHFA = nullptr;
41
42
    bool bMetadataDirty = false;
43
44
    bool bGeoDirty = false;
45
    GDALGeoTransform m_gt{};
46
    OGRSpatialReference m_oSRS{};
47
48
    bool bIgnoreUTM = false;
49
50
    CPLErr ReadProjection();
51
    CPLErr WriteProjection();
52
    bool bForceToPEString = false;
53
    bool bDisablePEString = false;
54
55
    std::vector<gdal::GCP> m_aoGCPs{};
56
57
    void UseXFormStack(int nStepCount, Efga_Polynomial *pasPolyListForward,
58
                       Efga_Polynomial *pasPolyListReverse);
59
60
  protected:
61
    virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
62
                             GDALDataType, int, BANDMAP_TYPE,
63
                             GSpacing nPixelSpace, GSpacing nLineSpace,
64
                             GSpacing nBandSpace,
65
                             GDALRasterIOExtraArg *psExtraArg) override;
66
67
  public:
68
    HFADataset();
69
    virtual ~HFADataset();
70
71
    static int Identify(GDALOpenInfo *);
72
    static CPLErr Rename(const char *pszNewName, const char *pszOldName);
73
    static CPLErr CopyFiles(const char *pszNewName, const char *pszOldName);
74
    static GDALDataset *Open(GDALOpenInfo *);
75
    static GDALDataset *Create(const char *pszFilename, int nXSize, int nYSize,
76
                               int nBands, GDALDataType eType,
77
                               char **papszParamList);
78
    static GDALDataset *CreateCopy(const char *pszFilename,
79
                                   GDALDataset *poSrcDS, int bStrict,
80
                                   char **papszOptions,
81
                                   GDALProgressFunc pfnProgress,
82
                                   void *pProgressData);
83
    static CPLErr Delete(const char *pszFilename);
84
85
    virtual char **GetFileList() override;
86
87
    const OGRSpatialReference *GetSpatialRef() const override;
88
    CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
89
90
    virtual CPLErr GetGeoTransform(GDALGeoTransform &gt) const override;
91
    virtual CPLErr SetGeoTransform(const GDALGeoTransform &gt) override;
92
93
    virtual int GetGCPCount() override;
94
    const OGRSpatialReference *GetGCPSpatialRef() const override;
95
    virtual const GDAL_GCP *GetGCPs() override;
96
97
    virtual CPLErr SetMetadata(char **, const char * = "") override;
98
    virtual CPLErr SetMetadataItem(const char *, const char *,
99
                                   const char * = "") override;
100
101
    virtual CPLErr FlushCache(bool bAtClosing) override;
102
    virtual CPLErr IBuildOverviews(const char *pszResampling, int nOverviews,
103
                                   const int *panOverviewList, int nListBands,
104
                                   const int *panBandList,
105
                                   GDALProgressFunc pfnProgress,
106
                                   void *pProgressData,
107
                                   CSLConstList papszOptions) override;
108
};
109
110
/************************************************************************/
111
/* ==================================================================== */
112
/*                            HFARasterBand                             */
113
/* ==================================================================== */
114
/************************************************************************/
115
116
class HFARasterBand final : public GDALPamRasterBand
117
{
118
    friend class HFADataset;
119
    friend class HFARasterAttributeTable;
120
121
    GDALColorTable *poCT;
122
123
    EPTType eHFADataType;
124
125
    int nOverviews;
126
    int nThisOverview;
127
    HFARasterBand **papoOverviewBands;
128
129
    CPLErr CleanOverviews();
130
131
    HFAHandle hHFA;
132
133
    bool bMetadataDirty;
134
135
    GDALRasterAttributeTable *poDefaultRAT;
136
137
    void ReadAuxMetadata();
138
    void ReadHistogramMetadata();
139
    void EstablishOverviews();
140
    CPLErr WriteNamedRAT(const char *pszName,
141
                         const GDALRasterAttributeTable *poRAT);
142
143
  public:
144
    HFARasterBand(HFADataset *, int, int);
145
    virtual ~HFARasterBand();
146
147
    virtual CPLErr IReadBlock(int, int, void *) override;
148
    virtual CPLErr IWriteBlock(int, int, void *) override;
149
150
    virtual const char *GetDescription() const override;
151
    virtual void SetDescription(const char *) override;
152
153
    virtual GDALColorInterp GetColorInterpretation() override;
154
    virtual GDALColorTable *GetColorTable() override;
155
    virtual CPLErr SetColorTable(GDALColorTable *) override;
156
    virtual int GetOverviewCount() override;
157
    virtual GDALRasterBand *GetOverview(int) override;
158
159
    virtual double GetMinimum(int *pbSuccess = nullptr) override;
160
    virtual double GetMaximum(int *pbSuccess = nullptr) override;
161
    virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
162
    virtual CPLErr SetNoDataValue(double dfValue) override;
163
164
    virtual CPLErr SetMetadata(char **, const char * = "") override;
165
    virtual CPLErr SetMetadataItem(const char *, const char *,
166
                                   const char * = "") override;
167
    virtual CPLErr BuildOverviews(const char *, int, const int *,
168
                                  GDALProgressFunc, void *,
169
                                  CSLConstList papszOptions) override;
170
171
    virtual CPLErr GetDefaultHistogram(double *pdfMin, double *pdfMax,
172
                                       int *pnBuckets, GUIntBig **ppanHistogram,
173
                                       int bForce, GDALProgressFunc,
174
                                       void *pProgressData) override;
175
176
    virtual GDALRasterAttributeTable *GetDefaultRAT() override;
177
    virtual CPLErr SetDefaultRAT(const GDALRasterAttributeTable *) override;
178
};
179
180
class HFAAttributeField
181
{
182
  public:
183
    CPLString sName;
184
    GDALRATFieldType eType;
185
    GDALRATFieldUsage eUsage;
186
    int nDataOffset;
187
    int nElementSize;
188
    HFAEntry *poColumn;
189
    bool bIsBinValues;    // Handled differently.
190
    bool bConvertColors;  // Map 0-1 floats to 0-255 ints.
191
};
192
193
class HFARasterAttributeTable final : public GDALRasterAttributeTable
194
{
195
  private:
196
    HFAHandle hHFA;
197
    HFAEntry *poDT;
198
    CPLString osName;
199
    int nBand;
200
    GDALAccess eAccess;
201
202
    std::vector<HFAAttributeField> aoFields;
203
    int nRows;
204
205
    bool bLinearBinning;
206
    double dfRow0Min;
207
    double dfBinSize;
208
    GDALRATTableType eTableType;
209
210
    CPLString osWorkingResult;
211
212
    void AddColumn(const char *pszName, GDALRATFieldType eType,
213
                   GDALRATFieldUsage eUsage, int nDataOffset, int nElementSize,
214
                   HFAEntry *poColumn, bool bIsBinValues = false,
215
                   bool bConvertColors = false)
216
7.47k
    {
217
7.47k
        HFAAttributeField aField;
218
7.47k
        aField.sName = pszName;
219
7.47k
        aField.eType = eType;
220
7.47k
        aField.eUsage = eUsage;
221
7.47k
        aField.nDataOffset = nDataOffset;
222
7.47k
        aField.nElementSize = nElementSize;
223
7.47k
        aField.poColumn = poColumn;
224
7.47k
        aField.bIsBinValues = bIsBinValues;
225
7.47k
        aField.bConvertColors = bConvertColors;
226
227
7.47k
        aoFields.push_back(std::move(aField));
228
7.47k
    }
229
230
    void CreateDT()
231
0
    {
232
0
        poDT = HFAEntry::New(hHFA->papoBand[nBand - 1]->psInfo, osName,
233
0
                             "Edsc_Table", hHFA->papoBand[nBand - 1]->poNode);
234
0
        poDT->SetIntField("numrows", nRows);
235
0
    }
236
237
  public:
238
    HFARasterAttributeTable(HFARasterBand *poBand, const char *pszName);
239
    virtual ~HFARasterAttributeTable();
240
241
    GDALRasterAttributeTable *Clone() const override;
242
243
    virtual int GetColumnCount() const override;
244
245
    virtual const char *GetNameOfCol(int) const override;
246
    virtual GDALRATFieldUsage GetUsageOfCol(int) const override;
247
    virtual GDALRATFieldType GetTypeOfCol(int) const override;
248
249
    virtual int GetColOfUsage(GDALRATFieldUsage) const override;
250
251
    virtual int GetRowCount() const override;
252
253
    virtual const char *GetValueAsString(int iRow, int iField) const override;
254
    virtual int GetValueAsInt(int iRow, int iField) const override;
255
    virtual double GetValueAsDouble(int iRow, int iField) const override;
256
257
    virtual CPLErr SetValue(int iRow, int iField,
258
                            const char *pszValue) override;
259
    virtual CPLErr SetValue(int iRow, int iField, double dfValue) override;
260
    virtual CPLErr SetValue(int iRow, int iField, int nValue) override;
261
262
    virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
263
                            int iLength, double *pdfData) override;
264
    virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
265
                            int iLength, int *pnData) override;
266
    virtual CPLErr ValuesIO(GDALRWFlag eRWFlag, int iField, int iStartRow,
267
                            int iLength, char **papszStrList) override;
268
269
    virtual int ChangesAreWrittenToFile() override;
270
    virtual void SetRowCount(int iCount) override;
271
272
    virtual int GetRowOfValue(double dfValue) const override;
273
    virtual int GetRowOfValue(int nValue) const override;
274
275
    virtual CPLErr CreateColumn(const char *pszFieldName,
276
                                GDALRATFieldType eFieldType,
277
                                GDALRATFieldUsage eFieldUsage) override;
278
    virtual CPLErr SetLinearBinning(double dfRow0Min,
279
                                    double dfBinSize) override;
280
    virtual int GetLinearBinning(double *pdfRow0Min,
281
                                 double *pdfBinSize) const override;
282
283
    virtual CPLXMLNode *Serialize() const override;
284
285
    virtual CPLErr SetTableType(const GDALRATTableType eInTableType) override;
286
    virtual GDALRATTableType GetTableType() const override;
287
    virtual void RemoveStatistics() override;
288
289
  protected:
290
    CPLErr ColorsIO(GDALRWFlag eRWFlag, int iField, int iStartRow, int iLength,
291
                    int *pnData);
292
};
293
294
#endif  // HFADATASET_H_INCLUDED