Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: NITF Read/Write Translator
4 : * Purpose: GDALDataset/GDALRasterBand declarations.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2002, Frank Warmerdam
9 : * Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * Portions Copyright (c) Her majesty the Queen in right of Canada as
12 : * represented by the Minister of National Defence, 2006.
13 : *
14 : * SPDX-License-Identifier: MIT
15 : ****************************************************************************/
16 :
17 : #ifndef NITF_DATASET_H_INCLUDED
18 : #define NITF_DATASET_H_INCLUDED
19 :
20 : #include "gdal_pam.h"
21 : #include "nitflib.h"
22 : #include "ogr_spatialref.h"
23 : #include "gdal_proxy.h"
24 :
25 : #include <array>
26 : #include <map>
27 :
28 : CPLErr NITFSetColorInterpretation(NITFImage *psImage, int nBand,
29 : GDALColorInterp eInterp);
30 :
31 : /* Unused in normal builds. Caller code in nitfdataset.cpp is protected by
32 : * #ifdef ESRI_BUILD */
33 : #ifdef ESRI_BUILD
34 : /* -------------------------------------------------------------------- */
35 : /* Functions in nitf_gcprpc.cpp. */
36 : /* -------------------------------------------------------------------- */
37 :
38 : void NITFDensifyGCPs(GDAL_GCP **psGCPs, int *pnGCPCount);
39 : void NITFUpdateGCPsWithRPC(NITFRPC00BInfo *psRPCInfo, GDAL_GCP *psGCPs,
40 : int *pnGCPCount);
41 : #endif
42 :
43 : /************************************************************************/
44 : /* ==================================================================== */
45 : /* NITFDataset */
46 : /* ==================================================================== */
47 : /************************************************************************/
48 :
49 : class NITFRasterBand;
50 : class NITFWrapperRasterBand;
51 :
52 : class NITFDataset final : public GDALPamDataset
53 : {
54 : friend class NITFRasterBand;
55 : friend class NITFWrapperRasterBand;
56 : friend class NITFComplexRasterBand;
57 :
58 : NITFFile *psFile = nullptr;
59 : NITFImage *psImage = nullptr;
60 :
61 : std::unique_ptr<GDALDataset> poJ2KDataset{};
62 : int bJP2Writing = false;
63 : vsi_l_offset m_nImageOffset = 0;
64 : int m_nIMIndex = 0;
65 : int m_nImageCount = 0;
66 : vsi_l_offset m_nICOffset = 0;
67 : bool m_bHasComplexRasterBand = false;
68 :
69 : std::unique_ptr<GDALDataset> poJPEGDataset{};
70 :
71 : int bGotGeoTransform = false;
72 : GDALGeoTransform m_gt{};
73 :
74 : OGRSpatialReference m_oSRS{};
75 :
76 : int nGCPCount = 0;
77 : GDAL_GCP *pasGCPList = nullptr;
78 : OGRSpatialReference m_oGCPSRS{};
79 :
80 : GDALMultiDomainMetadata oSpecialMD{};
81 :
82 : #ifdef ESRI_BUILD
83 : void InitializeNITFDESMetadata();
84 : void InitializeNITFTREs();
85 : #endif
86 : bool InitializeNITFDESs(bool bValidate);
87 : void InitializeNITFMetadata();
88 : void InitializeCGMMetadata();
89 : void InitializeTextMetadata();
90 : bool InitializeTREMetadata(bool bValidate);
91 : void InitializeImageStructureMetadata();
92 :
93 : GIntBig *panJPEGBlockOffset = nullptr;
94 : GByte *pabyJPEGBlock = nullptr;
95 : int nQLevel = 0;
96 :
97 : int ScanJPEGQLevel(GUIntBig *pnDataStart, bool *pbError);
98 : CPLErr ScanJPEGBlocks();
99 : CPLErr ReadJPEGBlock(int, int);
100 : void CheckGeoSDEInfo();
101 : char **AddFile(char **papszFileList, const char *EXTENSION,
102 : const char *extension);
103 :
104 : int nIMIndex = 0;
105 : CPLString osNITFFilename{};
106 :
107 : CPLString osRSetVRT{};
108 : int CheckForRSets(const char *pszFilename, char **papszSiblingFiles);
109 :
110 : char **papszTextMDToWrite = nullptr;
111 : char **papszCgmMDToWrite = nullptr;
112 : CPLStringList aosCreationOptions{};
113 :
114 : int bInLoadXML = false;
115 :
116 : CPLString m_osRPCTXTFilename{};
117 :
118 : int bExposeUnderlyingJPEGDatasetOverviews = false;
119 :
120 3 : int ExposeUnderlyingJPEGDatasetOverviews() const
121 : {
122 3 : return bExposeUnderlyingJPEGDatasetOverviews;
123 : }
124 :
125 : bool Validate();
126 :
127 : CPLErr Close(int &bHasDroppedRef);
128 :
129 : CPL_DISALLOW_COPY_ASSIGN(NITFDataset)
130 :
131 : protected:
132 : int CloseDependentDatasets() override;
133 :
134 : public:
135 : NITFDataset();
136 : ~NITFDataset() override;
137 :
138 : CPLErr Close() override;
139 :
140 : CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
141 : int nBufXSize, int nBufYSize, GDALDataType eDT,
142 : int nBandCount, int *panBandList,
143 : char **papszOptions) override;
144 :
145 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
146 : GDALDataType, int, BANDMAP_TYPE, GSpacing nPixelSpace,
147 : GSpacing nLineSpace, GSpacing nBandSpace,
148 : GDALRasterIOExtraArg *psExtraArg) override;
149 :
150 : const OGRSpatialReference *GetSpatialRef() const override;
151 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
152 :
153 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
154 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
155 : CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
156 : const OGRSpatialReference *poSRS) override;
157 :
158 : int GetGCPCount() override;
159 : const OGRSpatialReference *GetGCPSpatialRef() const override;
160 : const GDAL_GCP *GetGCPs() override;
161 : char **GetFileList() override;
162 :
163 : char **GetMetadataDomainList() override;
164 : char **GetMetadata(const char *pszDomain = "") override;
165 : virtual const char *GetMetadataItem(const char *pszName,
166 : const char *pszDomain = "") override;
167 : CPLErr FlushCache(bool bAtClosing) override;
168 : CPLErr IBuildOverviews(const char *, int, const int *, int, const int *,
169 : GDALProgressFunc, void *,
170 : CSLConstList papszOptions) override;
171 :
172 : static NITFDataset *OpenInternal(GDALOpenInfo *,
173 : GDALDataset *poWritableJ2KDataset,
174 : bool bOpenForCreate, int nIMIndex);
175 : static GDALDataset *Open(GDALOpenInfo *);
176 : static GDALDataset *NITFCreateCopy(const char *pszFilename,
177 : GDALDataset *poSrcDS, int bStrict,
178 : char **papszOptions,
179 : GDALProgressFunc pfnProgress,
180 : void *pProgressData);
181 : static GDALDataset *NITFDatasetCreate(const char *pszFilename, int nXSize,
182 : int nYSize, int nBands,
183 : GDALDataType eType,
184 : char **papszOptions);
185 : };
186 :
187 : /************************************************************************/
188 : /* ==================================================================== */
189 : /* NITFRasterBand */
190 : /* ==================================================================== */
191 : /************************************************************************/
192 :
193 : class NITFRasterBand CPL_NON_FINAL : public GDALPamRasterBand
194 : {
195 : friend class NITFDataset;
196 :
197 : NITFImage *psImage = nullptr;
198 :
199 : GDALColorTable *poColorTable = nullptr;
200 :
201 : GByte *pUnpackData = nullptr;
202 :
203 : int bScanlineAccess = false;
204 :
205 : CPL_DISALLOW_COPY_ASSIGN(NITFRasterBand)
206 :
207 : public:
208 : NITFRasterBand(NITFDataset *, int);
209 : ~NITFRasterBand() override;
210 :
211 : CPLErr IReadBlock(int, int, void *) override;
212 : CPLErr IWriteBlock(int, int, void *) override;
213 :
214 : GDALColorInterp GetColorInterpretation() override;
215 : CPLErr SetColorInterpretation(GDALColorInterp) override;
216 : GDALColorTable *GetColorTable() override;
217 : CPLErr SetColorTable(GDALColorTable *) override;
218 : double GetNoDataValue(int *pbSuccess = nullptr) override;
219 :
220 : void Unpack(GByte *pData);
221 : };
222 :
223 : /************************************************************************/
224 : /* ==================================================================== */
225 : /* NITFProxyPamRasterBand */
226 : /* ==================================================================== */
227 : /************************************************************************/
228 :
229 : /* This class is potentially of general interest and could be moved to
230 : * gdal_proxy.h */
231 : /* We don't proxy all methods. Generally speaking, the getters go to PAM first
232 : * and */
233 : /* then to the underlying band if no value exist in PAM. The setters aren't */
234 : /* overridden, so they go to PAM */
235 :
236 : class NITFProxyPamRasterBand CPL_NON_FINAL : public GDALPamRasterBand
237 : {
238 : private:
239 : std::map<CPLString, char **> oMDMap{};
240 :
241 : protected:
242 : virtual GDALRasterBand *RefUnderlyingRasterBand() = 0;
243 : virtual void
244 : UnrefUnderlyingRasterBand(GDALRasterBand *poUnderlyingRasterBand);
245 :
246 : CPLErr IReadBlock(int, int, void *) override;
247 : CPLErr IWriteBlock(int, int, void *) override;
248 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
249 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
250 : GDALRasterIOExtraArg *psExtraArg) override;
251 :
252 : public:
253 : ~NITFProxyPamRasterBand() override;
254 :
255 : char **GetMetadata(const char *pszDomain = "") override;
256 : /*virtual CPLErr SetMetadata( char ** papszMetadata,
257 : const char * pszDomain = "" );*/
258 : virtual const char *GetMetadataItem(const char *pszName,
259 : const char *pszDomain = "") override;
260 : /*virtual CPLErr SetMetadataItem( const char * pszName,
261 : const char * pszValue,
262 : const char * pszDomain = "" );*/
263 : CPLErr FlushCache(bool bAtClosing) override;
264 : /*virtual char **GetCategoryNames();*/
265 : double GetNoDataValue(int *pbSuccess = nullptr) override;
266 : double GetMinimum(int *pbSuccess = nullptr) override;
267 : double GetMaximum(int *pbSuccess = nullptr) override;
268 : /*virtual double GetOffset( int *pbSuccess = NULL );
269 : virtual double GetScale( int *pbSuccess = NULL );*/
270 : /*virtual const char *GetUnitType();*/
271 : GDALColorInterp GetColorInterpretation() override;
272 : GDALColorTable *GetColorTable() override;
273 : virtual CPLErr Fill(double dfRealValue,
274 : double dfImaginaryValue = 0) override;
275 :
276 : /*
277 : virtual CPLErr SetCategoryNames( char ** );
278 : virtual CPLErr SetNoDataValue( double );
279 : virtual CPLErr SetColorTable( GDALColorTable * );
280 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
281 : virtual CPLErr SetOffset( double );
282 : virtual CPLErr SetScale( double );
283 : virtual CPLErr SetUnitType( const char * );
284 : */
285 :
286 : virtual CPLErr GetStatistics(int bApproxOK, int bForce, double *pdfMin,
287 : double *pdfMax, double *pdfMean,
288 : double *padfStdDev) override;
289 : virtual CPLErr ComputeStatistics(int bApproxOK, double *pdfMin,
290 : double *pdfMax, double *pdfMean,
291 : double *pdfStdDev, GDALProgressFunc,
292 : void *pProgressData) override;
293 : /*virtual CPLErr SetStatistics( double dfMin, double dfMax,
294 : double dfMean, double dfStdDev );*/
295 : CPLErr ComputeRasterMinMax(int, double *) override;
296 :
297 : int HasArbitraryOverviews() override;
298 : int GetOverviewCount() override;
299 : GDALRasterBand *GetOverview(int) override;
300 : GDALRasterBand *GetRasterSampleOverview(GUIntBig) override;
301 : virtual CPLErr BuildOverviews(const char *, int, const int *,
302 : GDALProgressFunc, void *,
303 : CSLConstList papszOptions) override;
304 :
305 : CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
306 : int nBufXSize, int nBufYSize, GDALDataType eDT,
307 : char **papszOptions) override;
308 :
309 : /*virtual CPLErr GetHistogram( double dfMin, double dfMax,
310 : int nBuckets, GUIntBig * panHistogram,
311 : int bIncludeOutOfRange, int bApproxOK,
312 : GDALProgressFunc, void *pProgressData );
313 :
314 : CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
315 : int *pnBuckets, GUIntBig **
316 : ppanHistogram, int bForce, GDALProgressFunc, void *pProgressData); virtual
317 : CPLErr SetDefaultHistogram( double dfMin, double dfMax, int nBuckets,
318 : GUIntBig *panHistogram );*/
319 :
320 : /*virtual const GDALRasterAttributeTable *GetDefaultRAT();
321 : virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * );*/
322 :
323 : GDALRasterBand *GetMaskBand() override;
324 : int GetMaskFlags() override;
325 : CPLErr CreateMaskBand(int nFlags) override;
326 : };
327 :
328 : /************************************************************************/
329 : /* ==================================================================== */
330 : /* NITFWrapperRasterBand */
331 : /* ==================================================================== */
332 : /************************************************************************/
333 :
334 : /* This class is used to wrap bands from JPEG or JPEG2000 datasets in */
335 : /* bands of the NITF dataset. Previously a trick was applied in the */
336 : /* relevant drivers to define a SetColorInterpretation() method and */
337 : /* to make sure they keep the proper pointer to their "natural" dataset */
338 : /* This trick is no longer necessary with the NITFWrapperRasterBand */
339 : /* We just override the few specific methods where we want that */
340 : /* the NITFWrapperRasterBand behavior differs from the JPEG/JPEG2000 one */
341 :
342 : class NITFWrapperRasterBand final : public NITFProxyPamRasterBand
343 : {
344 : GDALRasterBand *const poBaseBand;
345 : GDALColorTable *poColorTable = nullptr;
346 : GDALColorInterp eInterp = GCI_Undefined;
347 : const bool bIsJPEG;
348 :
349 : CPL_DISALLOW_COPY_ASSIGN(NITFWrapperRasterBand)
350 :
351 : protected:
352 : /* Pure virtual method of the NITFProxyPamRasterBand */
353 : GDALRasterBand *RefUnderlyingRasterBand() override;
354 :
355 : public:
356 : NITFWrapperRasterBand(NITFDataset *poDS, GDALRasterBand *poBaseBand,
357 : int nBand);
358 : ~NITFWrapperRasterBand() override;
359 :
360 : /* Methods from GDALRasterBand we want to override */
361 : GDALColorInterp GetColorInterpretation() override;
362 : CPLErr SetColorInterpretation(GDALColorInterp) override;
363 :
364 : GDALColorTable *GetColorTable() override;
365 :
366 : int GetOverviewCount() override;
367 : GDALRasterBand *GetOverview(int) override;
368 :
369 : /* Specific method */
370 : void SetColorTableFromNITFBandInfo();
371 : };
372 :
373 : /************************************************************************/
374 : /* ==================================================================== */
375 : /* NITFComplexRasterBand */
376 : /* ==================================================================== */
377 : /************************************************************************/
378 :
379 : /* This class is used to wrap 2 bands (I and Q) as a complex raster band */
380 : class NITFComplexRasterBand final : public NITFRasterBand
381 : {
382 : std::unique_ptr<NITFDataset> poIntermediateDS{};
383 : std::array<int, 2> anBandMap = {0, 0};
384 : GDALDataType underlyingDataType = GDT_Unknown;
385 : int complexDataTypeSize = 0;
386 : int underlyingDataTypeSize = 0;
387 :
388 : private:
389 : CPLErr IBlockIO(int nBlockXOff, int nBlockYOff, void *pImage,
390 : GDALRWFlag rwFlag);
391 :
392 : public:
393 : NITFComplexRasterBand(NITFDataset *poDSIn, GDALRasterBand *poBandI,
394 : GDALRasterBand *poBandQ, int nIBand, int nQBand);
395 :
396 : CPLErr IReadBlock(int, int, void *) override;
397 : CPLErr IWriteBlock(int, int, void *) override;
398 : };
399 :
400 : #endif /* NITF_DATASET_H_INCLUDED */
|