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 : CPL_DISALLOW_COPY_ASSIGN(NITFDataset)
128 :
129 : protected:
130 : int CloseDependentDatasets() override;
131 :
132 : public:
133 : NITFDataset();
134 : ~NITFDataset() override;
135 :
136 : CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
137 : int nBufXSize, int nBufYSize, GDALDataType eDT,
138 : int nBandCount, int *panBandList,
139 : char **papszOptions) override;
140 :
141 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
142 : GDALDataType, int, BANDMAP_TYPE, GSpacing nPixelSpace,
143 : GSpacing nLineSpace, GSpacing nBandSpace,
144 : GDALRasterIOExtraArg *psExtraArg) override;
145 :
146 : const OGRSpatialReference *GetSpatialRef() const override;
147 : CPLErr SetSpatialRef(const OGRSpatialReference *poSRS) override;
148 :
149 : CPLErr GetGeoTransform(GDALGeoTransform >) const override;
150 : CPLErr SetGeoTransform(const GDALGeoTransform >) override;
151 : CPLErr SetGCPs(int nGCPCountIn, const GDAL_GCP *pasGCPListIn,
152 : const OGRSpatialReference *poSRS) override;
153 :
154 : int GetGCPCount() override;
155 : const OGRSpatialReference *GetGCPSpatialRef() const override;
156 : const GDAL_GCP *GetGCPs() override;
157 : char **GetFileList() override;
158 :
159 : char **GetMetadataDomainList() override;
160 : char **GetMetadata(const char *pszDomain = "") override;
161 : virtual const char *GetMetadataItem(const char *pszName,
162 : const char *pszDomain = "") override;
163 : CPLErr FlushCache(bool bAtClosing) override;
164 : CPLErr IBuildOverviews(const char *, int, const int *, int, const int *,
165 : GDALProgressFunc, void *,
166 : CSLConstList papszOptions) override;
167 :
168 : static NITFDataset *OpenInternal(GDALOpenInfo *,
169 : GDALDataset *poWritableJ2KDataset,
170 : bool bOpenForCreate, int nIMIndex);
171 : static GDALDataset *Open(GDALOpenInfo *);
172 : static GDALDataset *NITFCreateCopy(const char *pszFilename,
173 : GDALDataset *poSrcDS, int bStrict,
174 : char **papszOptions,
175 : GDALProgressFunc pfnProgress,
176 : void *pProgressData);
177 : static GDALDataset *NITFDatasetCreate(const char *pszFilename, int nXSize,
178 : int nYSize, int nBands,
179 : GDALDataType eType,
180 : char **papszOptions);
181 : };
182 :
183 : /************************************************************************/
184 : /* ==================================================================== */
185 : /* NITFRasterBand */
186 : /* ==================================================================== */
187 : /************************************************************************/
188 :
189 : class NITFRasterBand CPL_NON_FINAL : public GDALPamRasterBand
190 : {
191 : friend class NITFDataset;
192 :
193 : NITFImage *psImage = nullptr;
194 :
195 : GDALColorTable *poColorTable = nullptr;
196 :
197 : GByte *pUnpackData = nullptr;
198 :
199 : int bScanlineAccess = false;
200 :
201 : CPL_DISALLOW_COPY_ASSIGN(NITFRasterBand)
202 :
203 : public:
204 : NITFRasterBand(NITFDataset *, int);
205 : ~NITFRasterBand() override;
206 :
207 : CPLErr IReadBlock(int, int, void *) override;
208 : CPLErr IWriteBlock(int, int, void *) override;
209 :
210 : GDALColorInterp GetColorInterpretation() override;
211 : CPLErr SetColorInterpretation(GDALColorInterp) override;
212 : GDALColorTable *GetColorTable() override;
213 : CPLErr SetColorTable(GDALColorTable *) override;
214 : double GetNoDataValue(int *pbSuccess = nullptr) override;
215 :
216 : void Unpack(GByte *pData);
217 : };
218 :
219 : /************************************************************************/
220 : /* ==================================================================== */
221 : /* NITFProxyPamRasterBand */
222 : /* ==================================================================== */
223 : /************************************************************************/
224 :
225 : /* This class is potentially of general interest and could be moved to
226 : * gdal_proxy.h */
227 : /* We don't proxy all methods. Generally speaking, the getters go to PAM first
228 : * and */
229 : /* then to the underlying band if no value exist in PAM. The setters aren't */
230 : /* overridden, so they go to PAM */
231 :
232 : class NITFProxyPamRasterBand CPL_NON_FINAL : public GDALPamRasterBand
233 : {
234 : private:
235 : std::map<CPLString, char **> oMDMap{};
236 :
237 : protected:
238 : virtual GDALRasterBand *RefUnderlyingRasterBand() = 0;
239 : virtual void
240 : UnrefUnderlyingRasterBand(GDALRasterBand *poUnderlyingRasterBand);
241 :
242 : CPLErr IReadBlock(int, int, void *) override;
243 : CPLErr IWriteBlock(int, int, void *) override;
244 : CPLErr IRasterIO(GDALRWFlag, int, int, int, int, void *, int, int,
245 : GDALDataType, GSpacing nPixelSpace, GSpacing nLineSpace,
246 : GDALRasterIOExtraArg *psExtraArg) override;
247 :
248 : public:
249 : ~NITFProxyPamRasterBand() override;
250 :
251 : char **GetMetadata(const char *pszDomain = "") override;
252 : /*virtual CPLErr SetMetadata( char ** papszMetadata,
253 : const char * pszDomain = "" );*/
254 : virtual const char *GetMetadataItem(const char *pszName,
255 : const char *pszDomain = "") override;
256 : /*virtual CPLErr SetMetadataItem( const char * pszName,
257 : const char * pszValue,
258 : const char * pszDomain = "" );*/
259 : CPLErr FlushCache(bool bAtClosing) override;
260 : /*virtual char **GetCategoryNames();*/
261 : double GetNoDataValue(int *pbSuccess = nullptr) override;
262 : double GetMinimum(int *pbSuccess = nullptr) override;
263 : double GetMaximum(int *pbSuccess = nullptr) override;
264 : /*virtual double GetOffset( int *pbSuccess = NULL );
265 : virtual double GetScale( int *pbSuccess = NULL );*/
266 : /*virtual const char *GetUnitType();*/
267 : GDALColorInterp GetColorInterpretation() override;
268 : GDALColorTable *GetColorTable() override;
269 : virtual CPLErr Fill(double dfRealValue,
270 : double dfImaginaryValue = 0) override;
271 :
272 : /*
273 : virtual CPLErr SetCategoryNames( char ** );
274 : virtual CPLErr SetNoDataValue( double );
275 : virtual CPLErr SetColorTable( GDALColorTable * );
276 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
277 : virtual CPLErr SetOffset( double );
278 : virtual CPLErr SetScale( double );
279 : virtual CPLErr SetUnitType( const char * );
280 : */
281 :
282 : virtual CPLErr GetStatistics(int bApproxOK, int bForce, double *pdfMin,
283 : double *pdfMax, double *pdfMean,
284 : double *padfStdDev) override;
285 : virtual CPLErr ComputeStatistics(int bApproxOK, double *pdfMin,
286 : double *pdfMax, double *pdfMean,
287 : double *pdfStdDev, GDALProgressFunc,
288 : void *pProgressData) override;
289 : /*virtual CPLErr SetStatistics( double dfMin, double dfMax,
290 : double dfMean, double dfStdDev );*/
291 : CPLErr ComputeRasterMinMax(int, double *) override;
292 :
293 : int HasArbitraryOverviews() override;
294 : int GetOverviewCount() override;
295 : GDALRasterBand *GetOverview(int) override;
296 : GDALRasterBand *GetRasterSampleOverview(GUIntBig) override;
297 : virtual CPLErr BuildOverviews(const char *, int, const int *,
298 : GDALProgressFunc, void *,
299 : CSLConstList papszOptions) override;
300 :
301 : CPLErr AdviseRead(int nXOff, int nYOff, int nXSize, int nYSize,
302 : int nBufXSize, int nBufYSize, GDALDataType eDT,
303 : char **papszOptions) override;
304 :
305 : /*virtual CPLErr GetHistogram( double dfMin, double dfMax,
306 : int nBuckets, GUIntBig * panHistogram,
307 : int bIncludeOutOfRange, int bApproxOK,
308 : GDALProgressFunc, void *pProgressData );
309 :
310 : CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
311 : int *pnBuckets, GUIntBig **
312 : ppanHistogram, int bForce, GDALProgressFunc, void *pProgressData); virtual
313 : CPLErr SetDefaultHistogram( double dfMin, double dfMax, int nBuckets,
314 : GUIntBig *panHistogram );*/
315 :
316 : /*virtual const GDALRasterAttributeTable *GetDefaultRAT();
317 : virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * );*/
318 :
319 : GDALRasterBand *GetMaskBand() override;
320 : int GetMaskFlags() override;
321 : CPLErr CreateMaskBand(int nFlags) override;
322 : };
323 :
324 : /************************************************************************/
325 : /* ==================================================================== */
326 : /* NITFWrapperRasterBand */
327 : /* ==================================================================== */
328 : /************************************************************************/
329 :
330 : /* This class is used to wrap bands from JPEG or JPEG2000 datasets in */
331 : /* bands of the NITF dataset. Previously a trick was applied in the */
332 : /* relevant drivers to define a SetColorInterpretation() method and */
333 : /* to make sure they keep the proper pointer to their "natural" dataset */
334 : /* This trick is no longer necessary with the NITFWrapperRasterBand */
335 : /* We just override the few specific methods where we want that */
336 : /* the NITFWrapperRasterBand behavior differs from the JPEG/JPEG2000 one */
337 :
338 : class NITFWrapperRasterBand final : public NITFProxyPamRasterBand
339 : {
340 : GDALRasterBand *const poBaseBand;
341 : GDALColorTable *poColorTable = nullptr;
342 : GDALColorInterp eInterp = GCI_Undefined;
343 : const bool bIsJPEG;
344 :
345 : CPL_DISALLOW_COPY_ASSIGN(NITFWrapperRasterBand)
346 :
347 : protected:
348 : /* Pure virtual method of the NITFProxyPamRasterBand */
349 : GDALRasterBand *RefUnderlyingRasterBand() override;
350 :
351 : public:
352 : NITFWrapperRasterBand(NITFDataset *poDS, GDALRasterBand *poBaseBand,
353 : int nBand);
354 : ~NITFWrapperRasterBand() override;
355 :
356 : /* Methods from GDALRasterBand we want to override */
357 : GDALColorInterp GetColorInterpretation() override;
358 : CPLErr SetColorInterpretation(GDALColorInterp) override;
359 :
360 : GDALColorTable *GetColorTable() override;
361 :
362 : int GetOverviewCount() override;
363 : GDALRasterBand *GetOverview(int) override;
364 :
365 : /* Specific method */
366 : void SetColorTableFromNITFBandInfo();
367 : };
368 :
369 : /************************************************************************/
370 : /* ==================================================================== */
371 : /* NITFComplexRasterBand */
372 : /* ==================================================================== */
373 : /************************************************************************/
374 :
375 : /* This class is used to wrap 2 bands (I and Q) as a complex raster band */
376 : class NITFComplexRasterBand final : public NITFRasterBand
377 : {
378 : std::unique_ptr<NITFDataset> poIntermediateDS{};
379 : std::array<int, 2> anBandMap = {0, 0};
380 : GDALDataType underlyingDataType = GDT_Unknown;
381 : int complexDataTypeSize = 0;
382 : int underlyingDataTypeSize = 0;
383 :
384 : private:
385 : CPLErr IBlockIO(int nBlockXOff, int nBlockYOff, void *pImage,
386 : GDALRWFlag rwFlag);
387 :
388 : public:
389 : NITFComplexRasterBand(NITFDataset *poDSIn, GDALRasterBand *poBandI,
390 : GDALRasterBand *poBandQ, int nIBand, int nQBand);
391 :
392 : CPLErr IReadBlock(int, int, void *) override;
393 : CPLErr IWriteBlock(int, int, void *) override;
394 : };
395 :
396 : #endif /* NITF_DATASET_H_INCLUDED */
|