1 : /******************************************************************************
2 : * $Id: nitfdataset.h 25194 2012-10-30 22:33:21Z rouault $
3 : *
4 : * Project: NITF Read/Write Translator
5 : * Purpose: GDALDataset/GDALRasterBand declarations.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2002, Frank Warmerdam
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 : * Permission is hereby granted, free of charge, to any person obtaining a
15 : * copy of this software and associated documentation files (the "Software"),
16 : * to deal in the Software without restriction, including without limitation
17 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 : * and/or sell copies of the Software, and to permit persons to whom the
19 : * Software is furnished to do so, subject to the following conditions:
20 : *
21 : * The above copyright notice and this permission notice shall be included
22 : * in all copies or substantial portions of the Software.
23 : *
24 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 : * DEALINGS IN THE SOFTWARE.
31 : ****************************************************************************/
32 :
33 : #include "gdal_pam.h"
34 : #include "nitflib.h"
35 : #include "ogr_spatialref.h"
36 : #include "gdal_proxy.h"
37 : #include <map>
38 :
39 : CPLErr NITFSetColorInterpretation( NITFImage *psImage,
40 : int nBand,
41 : GDALColorInterp eInterp );
42 :
43 : /* Unused in normal builds. Caller code in nitfdataset.cpp is protected by #ifdef ESRI_BUILD */
44 : #ifdef ESRI_BUILD
45 : /* -------------------------------------------------------------------- */
46 : /* Functions in nitf_gcprpc.cpp. */
47 : /* -------------------------------------------------------------------- */
48 :
49 : void NITFDensifyGCPs( GDAL_GCP **psGCPs, int *pnGCPCount );
50 : void NITFUpdateGCPsWithRPC( NITFRPC00BInfo *psRPCInfo,
51 : GDAL_GCP *psGCPs,
52 : int *pnGCPCount );
53 : #endif
54 :
55 : /************************************************************************/
56 : /* ==================================================================== */
57 : /* NITFDataset */
58 : /* ==================================================================== */
59 : /************************************************************************/
60 :
61 : class NITFRasterBand;
62 : class NITFWrapperRasterBand;
63 :
64 : class NITFDataset : public GDALPamDataset
65 : {
66 : friend class NITFRasterBand;
67 : friend class NITFWrapperRasterBand;
68 :
69 : NITFFile *psFile;
70 : NITFImage *psImage;
71 :
72 : GDALPamDataset *poJ2KDataset;
73 : int bJP2Writing;
74 :
75 : GDALPamDataset *poJPEGDataset;
76 :
77 : int bGotGeoTransform;
78 : double adfGeoTransform[6];
79 :
80 : char *pszProjection;
81 :
82 : int nGCPCount;
83 : GDAL_GCP *pasGCPList;
84 : char *pszGCPProjection;
85 :
86 : GDALMultiDomainMetadata oSpecialMD;
87 :
88 : #ifdef ESRI_BUILD
89 : void InitializeNITFDESMetadata();
90 : void InitializeNITFDESs();
91 : void InitializeNITFTREs();
92 : #endif
93 : void InitializeNITFMetadata();
94 : void InitializeCGMMetadata();
95 : void InitializeTextMetadata();
96 : void InitializeTREMetadata();
97 :
98 : GIntBig *panJPEGBlockOffset;
99 : GByte *pabyJPEGBlock;
100 : int nQLevel;
101 :
102 : int ScanJPEGQLevel( GUIntBig *pnDataStart );
103 : CPLErr ScanJPEGBlocks( void );
104 : CPLErr ReadJPEGBlock( int, int );
105 : void CheckGeoSDEInfo();
106 : char** AddFile(char **papszFileList, const char* EXTENSION, const char* extension);
107 :
108 : int nIMIndex;
109 : CPLString osNITFFilename;
110 :
111 : CPLString osRSetVRT;
112 : int CheckForRSets( const char *pszFilename );
113 :
114 : char **papszTextMDToWrite;
115 : char **papszCgmMDToWrite;
116 :
117 : int bInLoadXML;
118 :
119 : int bExposeUnderlyingJPEGDatasetOverviews;
120 5 : int ExposeUnderlyingJPEGDatasetOverviews() const { return bExposeUnderlyingJPEGDatasetOverviews; }
121 :
122 : protected:
123 : virtual int CloseDependentDatasets();
124 :
125 : public:
126 : NITFDataset();
127 : ~NITFDataset();
128 :
129 : virtual CPLErr AdviseRead( int nXOff, int nYOff, int nXSize, int nYSize,
130 : int nBufXSize, int nBufYSize,
131 : GDALDataType eDT,
132 : int nBandCount, int *panBandList,
133 : char **papszOptions );
134 :
135 : virtual CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
136 : void *, int, int, GDALDataType,
137 : int, int *, int, int, int );
138 :
139 : virtual const char *GetProjectionRef(void);
140 : virtual CPLErr SetProjection( const char * );
141 : virtual CPLErr GetGeoTransform( double * );
142 : virtual CPLErr SetGeoTransform( double * );
143 : virtual CPLErr SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
144 : const char *pszGCPProjection );
145 :
146 : virtual int GetGCPCount();
147 : virtual const char *GetGCPProjection();
148 : virtual const GDAL_GCP *GetGCPs();
149 : virtual char **GetFileList(void);
150 :
151 : virtual char **GetMetadata( const char * pszDomain = "" );
152 : virtual const char *GetMetadataItem( const char * pszName,
153 : const char * pszDomain = "" );
154 : virtual void FlushCache();
155 : virtual CPLErr IBuildOverviews( const char *, int, int *,
156 : int, int *, GDALProgressFunc, void * );
157 :
158 : static int Identify( GDALOpenInfo * );
159 : static GDALDataset *OpenInternal( GDALOpenInfo *, GDALDataset *poWritableJ2KDataset,
160 : int bOpenForCreate);
161 : static GDALDataset *Open( GDALOpenInfo * );
162 : static GDALDataset *
163 : NITFCreateCopy( const char *pszFilename, GDALDataset *poSrcDS,
164 : int bStrict, char **papszOptions,
165 : GDALProgressFunc pfnProgress, void * pProgressData );
166 : static GDALDataset *
167 : NITFDatasetCreate( const char *pszFilename,
168 : int nXSize, int nYSize, int nBands,
169 : GDALDataType eType, char **papszOptions );
170 :
171 : };
172 :
173 : /************************************************************************/
174 : /* ==================================================================== */
175 : /* NITFRasterBand */
176 : /* ==================================================================== */
177 : /************************************************************************/
178 :
179 : class NITFRasterBand : public GDALPamRasterBand
180 : {
181 : friend class NITFDataset;
182 :
183 : NITFImage *psImage;
184 :
185 : GDALColorTable *poColorTable;
186 :
187 : GByte *pUnpackData;
188 :
189 : int bScanlineAccess;
190 :
191 : public:
192 : NITFRasterBand( NITFDataset *, int );
193 : ~NITFRasterBand();
194 :
195 : virtual CPLErr IReadBlock( int, int, void * );
196 : virtual CPLErr IWriteBlock( int, int, void * );
197 :
198 : virtual GDALColorInterp GetColorInterpretation();
199 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
200 : virtual GDALColorTable *GetColorTable();
201 : virtual CPLErr SetColorTable( GDALColorTable * );
202 : virtual double GetNoDataValue( int *pbSuccess = NULL );
203 :
204 : void Unpack(GByte* pData);
205 : };
206 :
207 : /************************************************************************/
208 : /* ==================================================================== */
209 : /* NITFProxyPamRasterBand */
210 : /* ==================================================================== */
211 : /************************************************************************/
212 :
213 : /* This class is potentially of general interest and could be moved to gdal_proxy.h */
214 : /* We don't proxy all methods. Generally speaking, the getters go to PAM first and */
215 : /* then to the underlying band if no value exist in PAM. The setters aren't */
216 : /* overriden, so they go to PAM */
217 :
218 : class NITFProxyPamRasterBand : public GDALPamRasterBand
219 56 : {
220 : private:
221 : std::map<CPLString, char**> oMDMap;
222 :
223 : protected:
224 : virtual GDALRasterBand* RefUnderlyingRasterBand() = 0;
225 : virtual void UnrefUnderlyingRasterBand(GDALRasterBand* poUnderlyingRasterBand);
226 :
227 : virtual CPLErr IReadBlock( int, int, void * );
228 : virtual CPLErr IWriteBlock( int, int, void * );
229 : virtual CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
230 : void *, int, int, GDALDataType,
231 : int, int );
232 :
233 : public:
234 : ~NITFProxyPamRasterBand();
235 :
236 : virtual char **GetMetadata( const char * pszDomain = "" );
237 : /*virtual CPLErr SetMetadata( char ** papszMetadata,
238 : const char * pszDomain = "" );*/
239 : virtual const char *GetMetadataItem( const char * pszName,
240 : const char * pszDomain = "" );
241 : /*virtual CPLErr SetMetadataItem( const char * pszName,
242 : const char * pszValue,
243 : const char * pszDomain = "" );*/
244 : virtual CPLErr FlushCache();
245 : /*virtual char **GetCategoryNames();*/
246 : virtual double GetNoDataValue( int *pbSuccess = NULL );
247 : virtual double GetMinimum( int *pbSuccess = NULL );
248 : virtual double GetMaximum(int *pbSuccess = NULL );
249 : /*virtual double GetOffset( int *pbSuccess = NULL );
250 : virtual double GetScale( int *pbSuccess = NULL );*/
251 : /*virtual const char *GetUnitType();*/
252 : virtual GDALColorInterp GetColorInterpretation();
253 : virtual GDALColorTable *GetColorTable();
254 : virtual CPLErr Fill(double dfRealValue, double dfImaginaryValue = 0);
255 :
256 : /*
257 : virtual CPLErr SetCategoryNames( char ** );
258 : virtual CPLErr SetNoDataValue( double );
259 : virtual CPLErr SetColorTable( GDALColorTable * );
260 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
261 : virtual CPLErr SetOffset( double );
262 : virtual CPLErr SetScale( double );
263 : virtual CPLErr SetUnitType( const char * );
264 : */
265 :
266 : virtual CPLErr GetStatistics( int bApproxOK, int bForce,
267 : double *pdfMin, double *pdfMax,
268 : double *pdfMean, double *padfStdDev );
269 : virtual CPLErr ComputeStatistics( int bApproxOK,
270 : double *pdfMin, double *pdfMax,
271 : double *pdfMean, double *pdfStdDev,
272 : GDALProgressFunc, void *pProgressData );
273 : /*virtual CPLErr SetStatistics( double dfMin, double dfMax,
274 : double dfMean, double dfStdDev );*/
275 : virtual CPLErr ComputeRasterMinMax( int, double* );
276 :
277 : virtual int HasArbitraryOverviews();
278 : virtual int GetOverviewCount();
279 : virtual GDALRasterBand *GetOverview(int);
280 : virtual GDALRasterBand *GetRasterSampleOverview( int );
281 : virtual CPLErr BuildOverviews( const char *, int, int *,
282 : GDALProgressFunc, void * );
283 :
284 : virtual CPLErr AdviseRead( int nXOff, int nYOff, int nXSize, int nYSize,
285 : int nBufXSize, int nBufYSize,
286 : GDALDataType eDT, char **papszOptions );
287 :
288 : /*virtual CPLErr GetHistogram( double dfMin, double dfMax,
289 : int nBuckets, int * panHistogram,
290 : int bIncludeOutOfRange, int bApproxOK,
291 : GDALProgressFunc, void *pProgressData );
292 :
293 : virtual CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
294 : int *pnBuckets, int ** ppanHistogram,
295 : int bForce,
296 : GDALProgressFunc, void *pProgressData);
297 : virtual CPLErr SetDefaultHistogram( double dfMin, double dfMax,
298 : int nBuckets, int *panHistogram );*/
299 :
300 : /*virtual const GDALRasterAttributeTable *GetDefaultRAT();
301 : virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * );*/
302 :
303 : virtual GDALRasterBand *GetMaskBand();
304 : virtual int GetMaskFlags();
305 : virtual CPLErr CreateMaskBand( int nFlags );
306 : };
307 :
308 : /************************************************************************/
309 : /* ==================================================================== */
310 : /* NITFWrapperRasterBand */
311 : /* ==================================================================== */
312 : /************************************************************************/
313 :
314 : /* This class is used to wrap bands from JPEG or JPEG2000 datasets in */
315 : /* bands of the NITF dataset. Previously a trick was applied in the */
316 : /* relevant drivers to define a SetColorInterpretation() method and */
317 : /* to make sure they keep the proper pointer to their "natural" dataset */
318 : /* This trick is no longer necessary with the NITFWrapperRasterBand */
319 : /* We just override the few specific methods where we want that */
320 : /* the NITFWrapperRasterBand behaviour differs from the JPEG/JPEG2000 one */
321 :
322 : class NITFWrapperRasterBand : public NITFProxyPamRasterBand
323 : {
324 : GDALRasterBand* poBaseBand;
325 : GDALColorTable* poColorTable;
326 : GDALColorInterp eInterp;
327 : int bIsJPEG;
328 :
329 : protected:
330 : /* Pure virtual method of the NITFProxyPamRasterBand */
331 : virtual GDALRasterBand* RefUnderlyingRasterBand();
332 :
333 : public:
334 : NITFWrapperRasterBand( NITFDataset * poDS,
335 : GDALRasterBand* poBaseBand,
336 : int nBand);
337 : ~NITFWrapperRasterBand();
338 :
339 : /* Methods from GDALRasterBand we want to override */
340 : virtual GDALColorInterp GetColorInterpretation();
341 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
342 :
343 : virtual GDALColorTable *GetColorTable();
344 :
345 : virtual int GetOverviewCount();
346 : virtual GDALRasterBand *GetOverview(int);
347 :
348 : /* Specific method */
349 : void SetColorTableFromNITFBandInfo();
350 : };
351 :
|