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