1 : /******************************************************************************
2 : * $Id: nitfdataset.h 23997 2012-02-20 19:19:31Z 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 : protected:
120 : virtual int CloseDependentDatasets();
121 :
122 : public:
123 : NITFDataset();
124 : ~NITFDataset();
125 :
126 : virtual CPLErr AdviseRead( int nXOff, int nYOff, int nXSize, int nYSize,
127 : int nBufXSize, int nBufYSize,
128 : GDALDataType eDT,
129 : int nBandCount, int *panBandList,
130 : char **papszOptions );
131 :
132 : virtual CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
133 : void *, int, int, GDALDataType,
134 : int, int *, int, int, int );
135 :
136 : virtual const char *GetProjectionRef(void);
137 : virtual CPLErr SetProjection( const char * );
138 : virtual CPLErr GetGeoTransform( double * );
139 : virtual CPLErr SetGeoTransform( double * );
140 : virtual CPLErr SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
141 : const char *pszGCPProjection );
142 :
143 : virtual int GetGCPCount();
144 : virtual const char *GetGCPProjection();
145 : virtual const GDAL_GCP *GetGCPs();
146 : virtual char **GetFileList(void);
147 :
148 : virtual char **GetMetadata( const char * pszDomain = "" );
149 : virtual const char *GetMetadataItem( const char * pszName,
150 : const char * pszDomain = "" );
151 : virtual void FlushCache();
152 : virtual CPLErr IBuildOverviews( const char *, int, int *,
153 : int, int *, GDALProgressFunc, void * );
154 :
155 : static int Identify( GDALOpenInfo * );
156 : static GDALDataset *OpenInternal( GDALOpenInfo *, GDALDataset *poWritableJ2KDataset,
157 : int bOpenForCreate);
158 : static GDALDataset *Open( GDALOpenInfo * );
159 : static GDALDataset *
160 : NITFCreateCopy( const char *pszFilename, GDALDataset *poSrcDS,
161 : int bStrict, char **papszOptions,
162 : GDALProgressFunc pfnProgress, void * pProgressData );
163 : static GDALDataset *
164 : NITFDatasetCreate( const char *pszFilename,
165 : int nXSize, int nYSize, int nBands,
166 : GDALDataType eType, char **papszOptions );
167 :
168 : };
169 :
170 : /************************************************************************/
171 : /* ==================================================================== */
172 : /* NITFRasterBand */
173 : /* ==================================================================== */
174 : /************************************************************************/
175 :
176 : class NITFRasterBand : public GDALPamRasterBand
177 : {
178 : friend class NITFDataset;
179 :
180 : NITFImage *psImage;
181 :
182 : GDALColorTable *poColorTable;
183 :
184 : GByte *pUnpackData;
185 :
186 : int bScanlineAccess;
187 :
188 : public:
189 : NITFRasterBand( NITFDataset *, int );
190 : ~NITFRasterBand();
191 :
192 : virtual CPLErr IReadBlock( int, int, void * );
193 : virtual CPLErr IWriteBlock( int, int, void * );
194 :
195 : virtual GDALColorInterp GetColorInterpretation();
196 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
197 : virtual GDALColorTable *GetColorTable();
198 : virtual CPLErr SetColorTable( GDALColorTable * );
199 : virtual double GetNoDataValue( int *pbSuccess = NULL );
200 :
201 : void Unpack(GByte* pData);
202 : };
203 :
204 : /************************************************************************/
205 : /* ==================================================================== */
206 : /* NITFProxyPamRasterBand */
207 : /* ==================================================================== */
208 : /************************************************************************/
209 :
210 : /* This class is potentially of general interest and could be moved to gdal_proxy.h */
211 : /* We don't proxy all methods. Generally speaking, the getters go to PAM first and */
212 : /* then to the underlying band if no value exist in PAM. The setters aren't */
213 : /* overriden, so they go to PAM */
214 :
215 : class NITFProxyPamRasterBand : public GDALPamRasterBand
216 112 : {
217 : private:
218 : std::map<CPLString, char**> oMDMap;
219 :
220 : protected:
221 : virtual GDALRasterBand* RefUnderlyingRasterBand() = 0;
222 : virtual void UnrefUnderlyingRasterBand(GDALRasterBand* poUnderlyingRasterBand);
223 :
224 : virtual CPLErr IReadBlock( int, int, void * );
225 : virtual CPLErr IWriteBlock( int, int, void * );
226 : virtual CPLErr IRasterIO( GDALRWFlag, int, int, int, int,
227 : void *, int, int, GDALDataType,
228 : int, int );
229 :
230 : public:
231 : ~NITFProxyPamRasterBand();
232 :
233 : virtual char **GetMetadata( const char * pszDomain = "" );
234 : /*virtual CPLErr SetMetadata( char ** papszMetadata,
235 : const char * pszDomain = "" );*/
236 : virtual const char *GetMetadataItem( const char * pszName,
237 : const char * pszDomain = "" );
238 : /*virtual CPLErr SetMetadataItem( const char * pszName,
239 : const char * pszValue,
240 : const char * pszDomain = "" );*/
241 : virtual CPLErr FlushCache();
242 : /*virtual char **GetCategoryNames();*/
243 : virtual double GetNoDataValue( int *pbSuccess = NULL );
244 : virtual double GetMinimum( int *pbSuccess = NULL );
245 : virtual double GetMaximum(int *pbSuccess = NULL );
246 : /*virtual double GetOffset( int *pbSuccess = NULL );
247 : virtual double GetScale( int *pbSuccess = NULL );*/
248 : /*virtual const char *GetUnitType();*/
249 : virtual GDALColorInterp GetColorInterpretation();
250 : virtual GDALColorTable *GetColorTable();
251 : virtual CPLErr Fill(double dfRealValue, double dfImaginaryValue = 0);
252 :
253 : /*
254 : virtual CPLErr SetCategoryNames( char ** );
255 : virtual CPLErr SetNoDataValue( double );
256 : virtual CPLErr SetColorTable( GDALColorTable * );
257 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
258 : virtual CPLErr SetOffset( double );
259 : virtual CPLErr SetScale( double );
260 : virtual CPLErr SetUnitType( const char * );
261 : */
262 :
263 : virtual CPLErr GetStatistics( int bApproxOK, int bForce,
264 : double *pdfMin, double *pdfMax,
265 : double *pdfMean, double *padfStdDev );
266 : virtual CPLErr ComputeStatistics( int bApproxOK,
267 : double *pdfMin, double *pdfMax,
268 : double *pdfMean, double *pdfStdDev,
269 : GDALProgressFunc, void *pProgressData );
270 : /*virtual CPLErr SetStatistics( double dfMin, double dfMax,
271 : double dfMean, double dfStdDev );*/
272 : virtual CPLErr ComputeRasterMinMax( int, double* );
273 :
274 : virtual int HasArbitraryOverviews();
275 : virtual int GetOverviewCount();
276 : virtual GDALRasterBand *GetOverview(int);
277 : virtual GDALRasterBand *GetRasterSampleOverview( int );
278 : virtual CPLErr BuildOverviews( const char *, int, int *,
279 : GDALProgressFunc, void * );
280 :
281 : virtual CPLErr AdviseRead( int nXOff, int nYOff, int nXSize, int nYSize,
282 : int nBufXSize, int nBufYSize,
283 : GDALDataType eDT, char **papszOptions );
284 :
285 : /*virtual CPLErr GetHistogram( double dfMin, double dfMax,
286 : int nBuckets, int * panHistogram,
287 : int bIncludeOutOfRange, int bApproxOK,
288 : GDALProgressFunc, void *pProgressData );
289 :
290 : virtual CPLErr GetDefaultHistogram( double *pdfMin, double *pdfMax,
291 : int *pnBuckets, int ** ppanHistogram,
292 : int bForce,
293 : GDALProgressFunc, void *pProgressData);
294 : virtual CPLErr SetDefaultHistogram( double dfMin, double dfMax,
295 : int nBuckets, int *panHistogram );*/
296 :
297 : /*virtual const GDALRasterAttributeTable *GetDefaultRAT();
298 : virtual CPLErr SetDefaultRAT( const GDALRasterAttributeTable * );*/
299 :
300 : virtual GDALRasterBand *GetMaskBand();
301 : virtual int GetMaskFlags();
302 : virtual CPLErr CreateMaskBand( int nFlags );
303 : };
304 :
305 : /************************************************************************/
306 : /* ==================================================================== */
307 : /* NITFWrapperRasterBand */
308 : /* ==================================================================== */
309 : /************************************************************************/
310 :
311 : /* This class is used to wrap bands from JPEG or JPEG2000 datasets in */
312 : /* bands of the NITF dataset. Previously a trick was applied in the */
313 : /* relevant drivers to define a SetColorInterpretation() method and */
314 : /* to make sure they keep the proper pointer to their "natural" dataset */
315 : /* This trick is no longer necessary with the NITFWrapperRasterBand */
316 : /* We just override the few specific methods where we want that */
317 : /* the NITFWrapperRasterBand behaviour differs from the JPEG/JPEG2000 one */
318 :
319 : class NITFWrapperRasterBand : public NITFProxyPamRasterBand
320 : {
321 : GDALRasterBand* poBaseBand;
322 : GDALColorTable* poColorTable;
323 : GDALColorInterp eInterp;
324 :
325 : protected:
326 : /* Pure virtual method of the NITFProxyPamRasterBand */
327 : virtual GDALRasterBand* RefUnderlyingRasterBand();
328 :
329 : public:
330 : NITFWrapperRasterBand( NITFDataset * poDS,
331 : GDALRasterBand* poBaseBand,
332 : int nBand);
333 : ~NITFWrapperRasterBand();
334 :
335 : /* Methods from GDALRasterBand we want to override */
336 : virtual GDALColorInterp GetColorInterpretation();
337 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
338 :
339 : virtual GDALColorTable *GetColorTable();
340 :
341 : /* Specific method */
342 : void SetColorTableFromNITFBandInfo();
343 : };
344 :
|