OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimNitfFile.cpp
Go to the documentation of this file.
1 //---
2 //
3 // License: MIT
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: Garrett Potts
8 //
9 // Description: Nitf support class
10 //
11 //---
12 // $Id$
13 
15 #include <ossim/base/ossimTrace.h>
17 #include <ossim/base/ossimNotify.h>
29 
30 #include <iostream>
31 #include <iomanip>
32 #include <memory>
33 
34 // Static trace for debugging
35 static ossimTrace traceDebug("ossimNitfFile:debug");
36 
38 {
39  return data.print(out);
40 }
41 
43  const std::string& prefix,
44  bool printOverviews) const
45 {
47  {
48  std::string pfx = prefix;
49  pfx += "nitf.";
50  theNitfFileHeader->print(out, pfx);
51 
53  for(ossim_int32 idx = 0; idx < n; ++idx)
54  {
56  if(ih)
57  {
58  bool printIt = true;
59 
60  if ( !printOverviews )
61  {
62  // Check the IMAG field.
63  ossim_float64 imag;
64  ih->getDecimationFactor(imag);
65  if ( !ossim::isnan(imag) )
66  {
67  if ( imag < 1.0)
68  {
69  printIt = false;
70  }
71  }
72 
73 #if 0 /* Not sure why this was in there for a print. (drb 09 Nov. 2016) */
74  //---
75  // Now see if it's a cloud mask image. Do not print
76  // cloud mask images if the printOverviews is false.
77  //---
78  if ( printIt )
79  {
80  if ( (ih->getCategory().trim(ossimString(" ")) ==
81  "CLOUD") &&
82  (ih->getRepresentation().trim(ossimString(" ")) ==
83  "NODISPLY") )
84  {
85  printIt = false;
86  }
87  }
88 #endif
89  }
90 
91  if (printIt)
92  {
93  // Add our prefix onto prefix.
94  std::string s = pfx;
95  s += "image";
96  s += ossimString::toString(idx).string();
97  s += ".";
98 
99  ih->print(out, s);
100  }
101 
102  delete ih;
103  ih = 0;
104  }
105  }
106 
107  //---
108  // Check for RPF stuff:
109  //---
111  theNitfFileHeader->getTag(info, "RPFHDR");
112  if(info.getTagName() == "RPFHDR")
113  {
114  // Open of the a.toc.
116  if ( toc->parseFile(getFilename()) ==
118  {
119  pfx += "rpf.";
120  toc->print(out, pfx, printOverviews);
121  }
122  }
123 
124  } // matches: if(theNitfFileHeader.valid())
125 
126  return out;
127 
128 }
129 
131  ossim_uint32 entryIndex,
132  const std::string& prefix,
133  bool printOverviews) const
134 {
136  {
137  std::string pfx = prefix;
138  pfx += "nitf.";
139  theNitfFileHeader->print(out, pfx);
140 
141  if ( entryIndex < (ossim_uint32)theNitfFileHeader->getNumberOfImages() )
142  {
144  if ( ih.valid() == true )
145  {
146  bool printIt = true;
147 
148  if ( !printOverviews )
149  {
150  // Check the IMAG field.
151  ossim_float64 imag;
152  ih->getDecimationFactor(imag);
153  if ( !ossim::isnan(imag) )
154  {
155  if ( imag < 1.0)
156  {
157  printIt = false;
158  }
159  }
160 
161  //---
162  // Now see if it's a cloud mask image. Do not print
163  // cloud mask images if the printOverviews is false.
164  //---
165  if ( printIt )
166  {
167  if ( (ih->getCategory().trim(ossimString(" ")) ==
168  "CLOUD") &&
169  (ih->getRepresentation().trim(ossimString(" ")) ==
170  "NODISPLY") )
171  {
172  printIt = false;
173  }
174  }
175  }
176 
177  if (printIt)
178  {
179  // Add our prefix onto prefix.
180  std::string s = pfx;
181  s += "image";
182  s += ossimString::toString(entryIndex).string();
183  s += ".";
184 
185  ih->print(out, s);
186  }
187  ih = 0;
188  }
189  }
190 
191  //---
192  // Check for RPF stuff:
193  //---
195  theNitfFileHeader->getTag(info, "RPFHDR");
196  if(info.getTagName() == "RPFHDR")
197  {
198  // Open of the a.toc.
200  if ( toc->parseFile(getFilename()) ==
202  {
203  pfx += "rpf.";
204  toc->printHeader(out, pfx);
205  toc->printTocEntry( out, entryIndex, pfx, printOverviews );
206  }
207  }
208 
209  } // matches: if(theNitfFileHeader.valid())
210 
211  return out;
212 
213 }
214 
216 {
217  bool result = theNitfFileHeader.valid();
218 
220  {
221  theNitfFileHeader->saveState(kwl, prefix);
222  }
224  for(ossim_int32 idx = 0; idx < n; ++idx)
225  {
227  ossimString newPrefix = prefix + "image" + ossimString::toString(idx) + ".";
228 #if 1
229  ih->saveState(kwl, newPrefix);
230 #else
231  if ( (ih->getCategory().trim(ossimString(" ")) !=
232  "CLOUD") ||
233  (ih->getRepresentation().trim(ossimString(" ")) !=
234  "NODISPLY") )
235  {
236  ih->saveState(kwl, newPrefix);
237  }
238 #endif
239  }
240  return result;
241 }
242 
244  : theFilename(""),
245  theNitfFileHeader(0)
246 {
247 }
248 
250 {
251  theNitfFileHeader = 0;
252 }
253 
255 {
256  bool result = false;
257 
258  if (traceDebug())
259  {
261  << "DEBUG ossimNitfFile::parseFile: entered...\n";
262  }
263 
264  //if( file.exists() )
265  {
266  // Open up a stream to the file.
267  std::shared_ptr<ossim::istream> str = ossim::StreamFactoryRegistry::instance()->
268  createIstream( file, std::ios_base::in | std::ios_base::binary );
269 
270  if ( str )
271  {
272  result = parseStream( file, *str );
273  }
274  else
275  {
276  if (traceDebug())
277  {
279  << "DEBUG ossimNitfFile::parseFile: "
280  << "Could not open file: " << file.c_str() << "\n";
281  }
282  }
283  }
284 
285  if (traceDebug())
286  {
288  << "DEBUG ossimNitfFile::parseFile: "
289  << "exit status: " << (result?"true":"false") << "\n";
290  }
291 
292  return result;
293 }
294 
296  ossim::istream& in )
297 {
298  bool result = false;
299 
300  if (traceDebug())
301  {
303  << "ossimNitfFile::parseStream: DEBUG entered...\n";
304  }
305 
307  {
308  theNitfFileHeader = 0;
309  }
310 
311  char temp[10];
312  in.read(temp, 9);
313  in.seekg(0, std::ios::beg);
314  temp[9] ='\0';
315 
316  theFilename = file;
317 
318  ossimString s(temp);
319  if(s == "NITF02.00")
320  {
321  if(traceDebug())
322  {
324  << "DEBUG: NITF Version 2.0"
325  << std::endl;
326  }
328  }
329  else if ( (s == "NITF02.10") || (s == "NSIF01.00") )
330  {
331  if(traceDebug())
332  {
334  << "DEBUG: NITF Version 2.1"
335  << std::endl;
336  }
338  }
339  else
340  {
341  if (traceDebug())
342  {
344  << "DEBUG ossimNitfFile::parseFile: "
345  << "Not an NITF file!\n";
346  }
347  }
348 
349  if( theNitfFileHeader.valid() )
350  {
351  try
352  {
354  result = true;
355  }
356  catch( const ossimException& e )
357  {
358  if (traceDebug())
359  {
361  << "ossimNitfFile::parseStream caught exception:\n"
362  << e.what()
363  << std::endl;
364  }
365  }
366  }
367 
368  if (traceDebug())
369  {
371  << "ossimNitfFile::parseStream: DEBUG\n"
372  << "exit status: " << (result?"true":"false") << "\n";
373  }
374  return result;
375 }
376 
378 {
379  return theNitfFileHeader.get();
380 }
381 
383 {
384  return theNitfFileHeader.get();
385 }
386 
388 {
390  {
392  }
393 
394  return ossimIrect(ossimIpt(0,0), ossimIpt(0,0));
395 }
396 
398  ossim_uint32 imageNumber)const
399 {
400  ossimNitfImageHeader* result = 0;
401 
403  {
404  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
405  createIstream(theFilename, std::ios::in|std::ios::binary);
406 
407  if ( in )
408  {
409  result = getNewImageHeader( *in, imageNumber );
410  }
411  }
412 
413  return result;
414 }
415 
417  ossim::istream& in, ossim_uint32 imageNumber)const
418 {
419  ossimNitfImageHeader* result = 0;
420 
422  {
423  try // getNewImageHeader can throw exception on parse.
424  {
425  result = theNitfFileHeader->getNewImageHeader(imageNumber, in);
426  }
427  catch( const ossimException& e )
428  {
429  if (traceDebug())
430  {
432  << "ossimNitfFile::getNewImageHeader caught exception:\n"
433  << e.what()
434  << std::endl;
435  }
436  result = 0;
437  }
438  }
439 
440  return result;
441 }
442 
444  ossim_uint32 symbolNumber)const
445 {
446  ossimNitfSymbolHeader* result = 0;
447 
449  {
450  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
451  createIstream(theFilename, std::ios::in|std::ios::binary);
452 
453  if ( in )
454  {
455  result = getNewSymbolHeader( *in, symbolNumber );
456  }
457  }
458 
459  return result;
460 }
461 
463  ossim::istream& in, ossim_uint32 symbolNumber)const
464 {
465  ossimNitfSymbolHeader* result = 0;
467  {
468  result = theNitfFileHeader->getNewSymbolHeader(symbolNumber, in);
469  }
470  return result;
471 }
472 
474  ossim_uint32 labelNumber)const
475 {
476  ossimNitfLabelHeader* result = 0;
478  {
479  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
480  createIstream(theFilename, std::ios::in|std::ios::binary);
481 
482  if ( in )
483  {
484  result = getNewLabelHeader( *in, labelNumber );
485  }
486  }
487 
488  return result;
489 }
490 
492  ossim::istream& in, ossim_uint32 labelNumber)const
493 {
494  ossimNitfLabelHeader* result = 0;
496  {
497  result = theNitfFileHeader->getNewLabelHeader(labelNumber, in);
498  }
499  return result;
500 }
501 
503  ossim_uint32 textNumber)const
504 {
505  ossimNitfTextHeader* result = 0;
507  {
508  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
509  createIstream(theFilename, std::ios::in|std::ios::binary);
510 
511  if ( in )
512  {
513  result = getNewTextHeader( *in, textNumber );
514  }
515  }
516  return result;
517 }
518 
520  ossim::istream& in, ossim_uint32 textNumber)const
521 {
522  ossimNitfTextHeader* result = 0;
524  {
525  result = theNitfFileHeader->getNewTextHeader(textNumber, in);
526  }
527  return result;
528 }
529 
531  ossim_uint32 dataExtNumber)const
532 {
533  ossimNitfDataExtensionSegment* result = 0;
535  {
536  std::shared_ptr<ossim::istream> in = ossim::StreamFactoryRegistry::instance()->
537  createIstream(theFilename, std::ios::in|std::ios::binary);
538 
539  if ( in )
540  {
541  result = getNewDataExtensionSegment( *in, dataExtNumber );
542  }
543  }
544  return result;
545 }
546 
548  ossim::istream& in, ossim_uint32 dataExtNumber)const
549 {
550  ossimNitfDataExtensionSegment* result = 0;
552  {
553  result = theNitfFileHeader->getNewDataExtensionSegment(dataExtNumber, in);
554  }
555  return result;
556 }
557 
559 {
561  {
563  }
564 
565  return ossimString("");
566 }
567 
569 {
570  return theFilename;
571 }
bool parseStream(const ossimFilename &file, ossim::istream &in)
Parse stream method.
ossimNitfSymbolHeader * getNewSymbolHeader(ossim_uint32 symbolNumber) const
virtual ~ossimNitfFile()
virtual std::ostream & print(std::ostream &out, const std::string &prefix=std::string()) const
Print method that outputs a key/value type format adding prefix to keys.
virtual ossimNitfLabelHeader * getNewLabelHeader(ossim_uint32 labelNumber, ossim::istream &in) const =0
virtual const char * getVersion() const =0
virtual ossimString getRepresentation() const =0
ossimNitfImageHeader * getNewImageHeader(ossim_uint32 imageNumber) const
std::ostream & operator<<(std::ostream &out, const ossimNitfFile &data)
virtual ossimNitfImageHeader * getNewImageHeader(ossim_uint32 imageNumber, ossim::istream &in) const =0
virtual void getDecimationFactor(ossim_float64 &result) const
Convenience method to get the decimation factor as a double from the string returned be the getImageM...
Represents serializable keyword/value map.
static const ossimErrorCode OSSIM_OK
bool valid() const
Definition: ossimRefPtr.h:75
std::ostream & print(std::ostream &out, const std::string &prefix=std::string(), bool printOverviews=false) const
print method that outputs a key/value type format adding prefix to keys.
virtual bool saveState(ossimKeywordlist &kwl, const ossimString &prefix="") const
static ossimString toString(bool aValue)
Numeric to string methods.
virtual std::ostream & print(std::ostream &out, const std::string &prefix) const =0
pure virtual print method that outputs a key/value type format adding prefix to keys.
static StreamFactoryRegistry * instance()
virtual ossimNitfSymbolHeader * getNewSymbolHeader(ossim_uint32 symbolNumber, ossim::istream &in) const =0
virtual bool saveState(ossimKeywordlist &kwl, const ossimString &prefix="") const
double ossim_float64
virtual bool getTag(ossimNitfTagInformation &tagInfo, const ossimString &tagName) const
ossimNitfLabelHeader * getNewLabelHeader(ossim_uint32 labelNumber) const
virtual ossimNitfTextHeader * getNewTextHeader(ossim_uint32 textNumber, ossim::istream &in) const =0
ossimNitfTextHeader * getNewTextHeader(ossim_uint32 textNumber) const
virtual ossimString getCategory() const =0
virtual const char * what() const
Returns the error message.
os2<< "> n<< " > nendobj n
const ossimNitfFileHeader * getHeader() const
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
virtual void parseStream(ossim::istream &in)=0
std::ostream & print(std::ostream &out, const std::string &prefix=std::string(), bool printOverviews=true) const
print method that outputs a key/value type format adding prefix to keys.
ossimErrorCode parseFile(const ossimFilename &fileName, bool keepFileHeader=false)
Parses a.toc file.
Definition: ossimRpfToc.cpp:55
virtual ossimDrect getImageRect() const =0
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
ossimFilename theFilename
ossimFilename getFilename() const
ossimNitfDataExtensionSegment * getNewDataExtensionSegment(ossim_uint32 dataExtNumber) const
ossimIrect getImageRect() const
virtual ossimNitfDataExtensionSegment * getNewDataExtensionSegment(ossim_int32 dataExtNumber, ossim::istream &in) const =0
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
virtual bool saveState(ossimKeywordlist &kwl, const ossimString &prefix) const
ossimRefPtr< ossimNitfFileHeader > theNitfFileHeader
bool parseFile(const ossimFilename &file)
std::ostream & printHeader(std::ostream &out, const std::string &prefix=std::string()) const
print method that outputs a key/value type format adding prefix to keys.
ossimString getVersion() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32
const std::string & string() const
Definition: ossimString.h:414
bool isnan(const float &v)
isnan Test for floating point Not A Number (NAN) value.
Definition: ossimCommon.h:91
std::ostream & printTocEntry(std::ostream &out, ossim_uint32 entryIndex, const std::string &prefix=std::string(), bool printOverviews=false) const
print method that outputs a key/value type format adding prefix to keys.
virtual ossim_int32 getNumberOfImages() const =0