File: nitfdataset.h

package info (click to toggle)
gdal 3.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 89,664 kB
  • sloc: cpp: 1,136,033; ansic: 197,355; python: 35,910; java: 5,511; xml: 4,011; sh: 3,950; cs: 2,443; yacc: 1,047; makefile: 288
file content (370 lines) | stat: -rw-r--r-- 15,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/******************************************************************************
 * $Id$
 *
 * Project:  NITF Read/Write Translator
 * Purpose:  GDALDataset/GDALRasterBand declarations.
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2002, Frank Warmerdam
 * Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com>
 *
 * Portions Copyright (c) Her majesty the Queen in right of Canada as
 * represented by the Minister of National Defence, 2006.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ****************************************************************************/

#include "gdal_pam.h"
#include "nitflib.h"
#include "ogr_spatialref.h"
#include "gdal_proxy.h"
#include <map>

CPLErr NITFSetColorInterpretation(NITFImage *psImage, int nBand,
                                  GDALColorInterp eInterp);

/* Unused in normal builds. Caller code in nitfdataset.cpp is protected by
 * #ifdef ESRI_BUILD */
#ifdef ESRI_BUILD
/* -------------------------------------------------------------------- */
/*      Functions in nitf_gcprpc.cpp.                                   */
/* -------------------------------------------------------------------- */

void NITFDensifyGCPs(GDAL_GCP **psGCPs, int *pnGCPCount);
void NITFUpdateGCPsWithRPC(NITFRPC00BInfo *psRPCInfo, GDAL_GCP *psGCPs,
                           int *pnGCPCount);
#endif

/************************************************************************/
/* ==================================================================== */
/*                              NITFDataset                             */
/* ==================================================================== */
/************************************************************************/

class NITFRasterBand;
class NITFWrapperRasterBand;

class NITFDataset final : public GDALPamDataset
{
    friend class NITFRasterBand;
    friend class NITFWrapperRasterBand;

    NITFFile *psFile;
    NITFImage *psImage;

    GDALDataset *poJ2KDataset;
    int bJP2Writing;
    vsi_l_offset m_nImageOffset = 0;
    int m_nIMIndex = 0;
    int m_nImageCount = 0;
    vsi_l_offset m_nICOffset = 0;

    GDALDataset *poJPEGDataset;

    int bGotGeoTransform;
    double adfGeoTransform[6];

    OGRSpatialReference m_oSRS{};

    int nGCPCount;
    GDAL_GCP *pasGCPList;
    OGRSpatialReference m_oGCPSRS{};

    GDALMultiDomainMetadata oSpecialMD;

#ifdef ESRI_BUILD
    void InitializeNITFDESMetadata();
    void InitializeNITFTREs();
#endif
    void InitializeNITFDESs();
    void InitializeNITFMetadata();
    void InitializeCGMMetadata();
    void InitializeTextMetadata();
    void InitializeTREMetadata();
    void InitializeImageStructureMetadata();

    GIntBig *panJPEGBlockOffset;
    GByte *pabyJPEGBlock;
    int nQLevel;

    int ScanJPEGQLevel(GUIntBig *pnDataStart, bool *pbError);
    CPLErr ScanJPEGBlocks();
    CPLErr ReadJPEGBlock(int, int);
    void CheckGeoSDEInfo();
    char **AddFile(char **papszFileList, const char *EXTENSION,
                   const char *extension);

    int nIMIndex;
    CPLString osNITFFilename;

    CPLString osRSetVRT;
    int CheckForRSets(const char *pszFilename, char **papszSiblingFiles);

    char **papszTextMDToWrite;
    char **papszCgmMDToWrite;
    CPLStringList aosCreationOptions;

    int bInLoadXML;

    CPLString m_osRPCTXTFilename;

    int bExposeUnderlyingJPEGDatasetOverviews;
    int ExposeUnderlyingJPEGDatasetOverviews() const
    {
        return bExposeUnderlyingJPEGDatasetOverviews;
    }

  protected:
    virtual int CloseDependentDatasets() override;

  public:
    NITFDataset();
    virtual ~NITFDataset();

    virtual CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
                              int nBufXSize, int nBufYSize, GDALDataType eDT,
                              int nBandCount, int *panBandList,
                              char **papszOptions) override;

    virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
                             GDALDataType, int, int *, GSpacing nPixelSpace,
                             GSpacing nLineSpace, GSpacing nBandSpace,
                             GDALRasterIOExtraArg *psExtraArg) override;

    const OGRSpatialReference *GetSpatialRef() const override;
    CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;

    virtual CPLErr GetGeoTransform(double *) override;
    virtual CPLErr SetGeoTransform(double *) override;
    CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
                   const OGRSpatialReference *poSRS) override;

    virtual int GetGCPCount() override;
    const OGRSpatialReference *GetGCPSpatialRef() const override;
    virtual const GDAL_GCP *GetGCPs() override;
    virtual char **GetFileList() override;

    virtual char **GetMetadataDomainList() override;
    virtual char **GetMetadata(const char *pszDomain = "") override;
    virtual const char *GetMetadataItem(const char *pszName,
                                        const char *pszDomain = "") override;
    virtual void FlushCache(bool bAtClosing) override;
    virtual CPLErr IBuildOverviews(const char *, int, const int *, int,
                                   const int *, GDALProgressFunc, void *,
                                   CSLConstList papszOptions) override;

    static int Identify(GDALOpenInfo *);
    static NITFDataset *OpenInternal(GDALOpenInfo *,
                                     GDALDataset *poWritableJ2KDataset,
                                     bool bOpenForCreate, int nIMIndex);
    static GDALDataset *Open(GDALOpenInfo *);
    static GDALDataset *NITFCreateCopy(const char *pszFilename,
                                       GDALDataset *poSrcDS, int bStrict,
                                       char **papszOptions,
                                       GDALProgressFunc pfnProgress,
                                       void *pProgressData);
    static GDALDataset *NITFDatasetCreate(const char *pszFilename, int nXSize,
                                          int nYSize, int nBands,
                                          GDALDataType eType,
                                          char **papszOptions);
};

/************************************************************************/
/* ==================================================================== */
/*                            NITFRasterBand                             */
/* ==================================================================== */
/************************************************************************/

class NITFRasterBand final : public GDALPamRasterBand
{
    friend class NITFDataset;

    NITFImage *psImage;

    GDALColorTable *poColorTable;

    GByte *pUnpackData;

    int bScanlineAccess;

  public:
    NITFRasterBand(NITFDataset *, int);
    virtual ~NITFRasterBand();

    virtual CPLErr IReadBlock(int, int, void *) override;
    virtual CPLErr IWriteBlock(int, int, void *) override;

    virtual GDALColorInterp GetColorInterpretation() override;
    virtual CPLErr SetColorInterpretation(GDALColorInterp) override;
    virtual GDALColorTable *GetColorTable() override;
    virtual CPLErr SetColorTable(GDALColorTable *) override;
    virtual double GetNoDataValue(int *pbSuccess = nullptr) override;

    void Unpack(GByte *pData);
};

/************************************************************************/
/* ==================================================================== */
/*                        NITFProxyPamRasterBand                        */
/* ==================================================================== */
/************************************************************************/

/* This class is potentially of general interest and could be moved to
 * gdal_proxy.h */
/* We don't proxy all methods. Generally speaking, the getters go to PAM first
 * and */
/* then to the underlying band if no value exist in PAM. The setters aren't */
/* overridden, so they go to PAM */

class NITFProxyPamRasterBand CPL_NON_FINAL : public GDALPamRasterBand
{
  private:
    std::map<CPLString, char **> oMDMap;

  protected:
    virtual GDALRasterBand *RefUnderlyingRasterBand() = 0;
    virtual void
    UnrefUnderlyingRasterBand(GDALRasterBand *poUnderlyingRasterBand);

    virtual CPLErr IReadBlock(int, int, void *) override;
    virtual CPLErr IWriteBlock(int, int, void *) override;
    virtual CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
                             GDALDataType, GSpacing nPixelSpace,
                             GSpacing nLineSpace,
                             GDALRasterIOExtraArg *psExtraArg) override;

  public:
    virtual ~NITFProxyPamRasterBand();

    virtual char **GetMetadata(const char *pszDomain = "") override;
    /*virtual CPLErr      SetMetadata( char ** papszMetadata,
                                    const char * pszDomain = ""  );*/
    virtual const char *GetMetadataItem(const char *pszName,
                                        const char *pszDomain = "") override;
    /*virtual CPLErr      SetMetadataItem( const char * pszName,
                                        const char * pszValue,
                                        const char * pszDomain = "" );*/
    virtual CPLErr FlushCache(bool bAtClosing) override;
    /*virtual char **GetCategoryNames();*/
    virtual double GetNoDataValue(int *pbSuccess = nullptr) override;
    virtual double GetMinimum(int *pbSuccess = nullptr) override;
    virtual double GetMaximum(int *pbSuccess = nullptr) override;
    /*virtual double GetOffset( int *pbSuccess = NULL );
    virtual double GetScale( int *pbSuccess = NULL );*/
    /*virtual const char *GetUnitType();*/
    virtual GDALColorInterp GetColorInterpretation() override;
    virtual GDALColorTable *GetColorTable() override;
    virtual CPLErr Fill(double dfRealValue,
                        double dfImaginaryValue = 0) override;

    /*
    virtual CPLErr SetCategoryNames( char ** );
    virtual CPLErr SetNoDataValue( double );
    virtual CPLErr SetColorTable( GDALColorTable * );
    virtual CPLErr SetColorInterpretation( GDALColorInterp );
    virtual CPLErr SetOffset( double );
    virtual CPLErr SetScale( double );
    virtual CPLErr SetUnitType( const char * );
    */

    virtual CPLErr GetStatistics(int bApproxOK, int bForce, double *pdfMin,
                                 double *pdfMax, double *pdfMean,
                                 double *padfStdDev) override;
    virtual CPLErr ComputeStatistics(int bApproxOK, double *pdfMin,
                                     double *pdfMax, double *pdfMean,
                                     double *pdfStdDev, GDALProgressFunc,
                                     void *pProgressData) override;
    /*virtual CPLErr SetStatistics( double dfMin, double dfMax,
                                double dfMean, double dfStdDev );*/
    virtual CPLErr ComputeRasterMinMax(int, double *) override;

    virtual int HasArbitraryOverviews() override;
    virtual int GetOverviewCount() override;
    virtual GDALRasterBand *GetOverview(int) override;
    virtual GDALRasterBand *GetRasterSampleOverview(GUIntBig) override;
    virtual CPLErr BuildOverviews(const char *, int, const int *,
                                  GDALProgressFunc, void *,
                                  CSLConstList papszOptions) override;

    virtual CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
                              int nBufXSize, int nBufYSize, GDALDataType eDT,
                              char **papszOptions) override;

    /*virtual CPLErr  GetHistogram( double dfMin, double dfMax,
                        int nBuckets, GUIntBig * panHistogram,
                        int bIncludeOutOfRange, int bApproxOK,
                        GDALProgressFunc, void *pProgressData );

    virtual CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
                                        int *pnBuckets, GUIntBig **
    ppanHistogram, int bForce, GDALProgressFunc, void *pProgressData); virtual
    CPLErr SetDefaultHistogram( double dfMin, double dfMax, int nBuckets,
    GUIntBig *panHistogram );*/

    /*virtual const GDALRasterAttributeTable *GetDefaultRAT();
    virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * );*/

    virtual GDALRasterBand *GetMaskBand() override;
    virtual int GetMaskFlags() override;
    virtual CPLErr CreateMaskBand(int nFlags) override;
};

/************************************************************************/
/* ==================================================================== */
/*                       NITFWrapperRasterBand                          */
/* ==================================================================== */
/************************************************************************/

/* This class is used to wrap bands from JPEG or JPEG2000 datasets in */
/* bands of the NITF dataset. Previously a trick was applied in the */
/* relevant drivers to define a SetColorInterpretation() method and */
/* to make sure they keep the proper pointer to their "natural" dataset */
/* This trick is no longer necessary with the NITFWrapperRasterBand */
/* We just override the few specific methods where we want that */
/* the NITFWrapperRasterBand behavior differs from the JPEG/JPEG2000 one */

class NITFWrapperRasterBand final : public NITFProxyPamRasterBand
{
    GDALRasterBand *poBaseBand;
    GDALColorTable *poColorTable;
    GDALColorInterp eInterp;
    int bIsJPEG;

  protected:
    /* Pure virtual method of the NITFProxyPamRasterBand */
    virtual GDALRasterBand *RefUnderlyingRasterBand() override;

  public:
    NITFWrapperRasterBand(NITFDataset *poDS, GDALRasterBand *poBaseBand,
                          int nBand);
    virtual ~NITFWrapperRasterBand();

    /* Methods from GDALRasterBand we want to override */
    virtual GDALColorInterp GetColorInterpretation() override;
    virtual CPLErr SetColorInterpretation(GDALColorInterp) override;

    virtual GDALColorTable *GetColorTable() override;

    virtual int GetOverviewCount() override;
    virtual GDALRasterBand *GetOverview(int) override;

    /* Specific method */
    void SetColorTableFromNITFBandInfo();
};