OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJ2kInfo.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: David Burken
8 //
9 // Description: J2K Info object.
10 //
11 //----------------------------------------------------------------------------
12 // $Id$
13 
15 #include <ossim/base/ossimCommon.h>
16 #include <ossim/base/ossimEndian.h>
17 #include <ossim/base/ossimNotify.h>
18 #include <ossim/base/ossimTrace.h>
19 // #include <ossim/support_data/ossimJ2kCommon.h>
23 #include <fstream>
24 #include <iostream>
25 #include <iomanip>
26 #include <sstream>
27 
28 // Static trace for debugging
29 static ossimTrace traceDebug("ossimJ2kInfo:debug");
30 static ossimTrace traceDump("ossimJ2kInfo:dump"); // This will dump offsets.
31 
33  : ossimInfoBase(),
34  m_file(),
35  m_endian(0)
36 {
37  // See if we need to byte swap. J2k, JP2 boxes are big endian.
39  {
40  m_endian = new ossimEndian();
41  }
42 }
43 
45 {
46  if (m_endian)
47  {
48  delete m_endian;
49  m_endian = 0;
50  }
51 }
52 
54 {
55  bool result = false;
56 
57  //---
58  // Open the file.
59  //---
60  std::ifstream str(file.c_str(), std::ios_base::binary|std::ios_base::in);
61  if (str.good())
62  {
63  //---
64  // Check for the Start Of Codestream (SOC) and Size (SIZ) markers which
65  // are required as first and second fields in the main header.
66  //---
67  ossim_uint16 soc;
68  ossim_uint16 siz;
69  readUInt16(soc, str);
70  readUInt16(siz, str);
71 
72  const ossim_uint16 SOC_MARKER = 0xff4f; // start of codestream marker
73  const ossim_uint16 SIZ_MARKER = 0xff51; // size maker
74 
75  if ( (soc == SOC_MARKER) && (siz == SIZ_MARKER) )
76  {
77  result = true; // Is a j2k...
78  }
79  }
80 
81  if (result)
82  {
83 
84  m_file = file;
85  }
86  else
87  {
88  m_file.clear();
89  if (m_endian)
90  {
91  delete m_endian;
92  m_endian = 0;
93  }
94  }
95 
96  return result;
97 }
98 
100 {
101  static const char MODULE[] = "ossimJ2kInfo::print";
102 
103  if (traceDebug())
104  {
106  << MODULE << " DEBUG Entered...\n";
107  }
108 
109  if ( m_file.size() )
110  {
111  // Open the file.
112  std::ifstream str(m_file.c_str(), std::ios_base::binary|std::ios_base::in);
113  if (str.good())
114  {
115  ossim_uint16 marker;
116  readUInt16(marker, str); // SOC
117  readUInt16(marker, str); // SIZ
118 
119  std::string prefix = "j2k.";
120 
121  // SIZ marker required next.
122  printSizMarker(out, prefix, str);
123 
124  readUInt16(marker, str);
125 
126  const ossim_uint16 COD_MARKER = 0xff52; // cod maker
127  const ossim_uint16 EOC_MARKER = 0xffd9; // End of codestream marker.
128  const ossim_uint16 SOT_MARKER = 0xff90; // start of tile marker
129 
130  while ( str.good() && (marker != EOC_MARKER) )
131  {
132  switch(marker)
133  {
134  case COD_MARKER:
135  {
136  printCodMarker(out, prefix, str);
137  break;
138  }
139  case SOT_MARKER:
140  {
141  printSotMarker(out, prefix, str);
142  break;
143  }
144  default:
145  {
146  printUnknownMarker(out, prefix, str, marker);
147  }
148  }
149 
150  readUInt16(marker, str);
151 
152  }
153  }
154  }
155 
156  return out;
157 }
158 
160 {
161  str.read((char*)&s, 2);
162  if (m_endian)
163  {
164  m_endian->swap(s);
165  }
166 }
167 
169  const std::string& prefix,
170  std::ifstream& str) const
171 {
172  ossimJ2kCodRecord siz;
173  siz.parseStream(str);
174  siz.print(out, prefix);
175  return out;
176 }
177 
179  const std::string& prefix,
180  std::ifstream& str) const
181 {
182  ossimJ2kSizRecord siz;
183  siz.parseStream(str);
184  siz.print(out, prefix);
185  return out;
186 }
187 
189  const std::string& prefix,
190  std::ifstream& str) const
191 {
192  // Get the stream posistion.
193  std::streamoff pos = str.tellg();
194 
195  ossimJ2kSotRecord sot;
196  sot.parseStream(str);
197  pos += sot.thePsot - 2;
198 
199  // Seek past the tile to the next marker.
200  str.seekg(pos, std::ios_base::beg);
201 
202  sot.print(out,prefix);
203  return out;
204 }
205 
206 
208  const std::string& prefix,
209  std::ifstream& str,
210  ossim_uint16 marker) const
211 {
212  // Capture the original flags.
213  std::ios_base::fmtflags f = out.flags();
214 
215  ossim_uint16 segmentLength;
216  readUInt16(segmentLength, str);
217 
218  std::string pfx = prefix;
219  pfx += "unknown.";
220 
221  out.setf(std::ios_base::hex, std::ios_base::basefield);
222  out << pfx << "marker: 0x" << std::setfill('0') << std::setw(4)
223  << marker << "\n";
224  out.setf(std::ios_base::fmtflags(0), std::ios_base::basefield);
225 
226  out << pfx << "length: " << segmentLength
227  << std::endl;
228 
229  // Reset flags.
230  out.setf(f);
231 
232  // Seek to the next marker.
233  str.seekg( (segmentLength-2), std::ios_base::cur);
234 
235  return out;
236 }
237 
238 
void clear()
Erases the entire container.
Definition: ossimString.h:432
void readUInt16(ossim_uint16 &s, std::ifstream &str) const
Initializes s reference.
ossimFilename m_file
Definition: ossimJ2kInfo.h:120
virtual ~ossimJ2kInfo()
virtual destructor
void parseStream(ossim::istream &in)
Parse method.
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
void parseStream(std::istream &in)
Parse method.
OSSIM_DLL ossimByteOrder byteOrder()
Definition: ossimCommon.cpp:54
ossimEndian * m_endian
Definition: ossimJ2kInfo.h:121
unsigned short ossim_uint16
std::string::size_type size() const
Definition: ossimString.h:405
Info Base.
Definition: ossimInfoBase.h:32
std::ostream & printCodMarker(std::ostream &out, const std::string &prefix, std::ifstream &str) const
Prints 0xff52 COD_MARKER (0xff52).
ossim_uint32 thePsot
The length in bytes of this record including the SOT marker.
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 bool open(const ossimFilename &file)
open method.
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.
std::ostream & printUnknownMarker(std::ostream &out, const std::string &prefix, std::ifstream &str, ossim_uint16 marker) const
Prints unhandle segment.
std::ostream & printSizMarker(std::ostream &out, const std::string &prefix, std::ifstream &str) const
Prints 0xff51 SIZ_MARKER (0xff51).
ossimJ2kInfo()
default constructor
virtual std::ostream & print(std::ostream &out) const
Print method.
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.
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
void swap(ossim_sint8 &)
Definition: ossimEndian.h:26
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
std::ostream & printSotMarker(std::ostream &out, const std::string &prefix, std::ifstream &str) const
Prints 0xff90 SOT_MARKER (0xff90)
void parseStream(ossim::istream &in)
Parse method.