USGS

Isis 3.0 Object Programmers' Reference

Home

JP2Importer.cpp
1 #include "JP2Importer.h"
2 
3 #include "FileName.h"
4 #include "IException.h"
5 #include "JP2Decoder.h"
6 #include "ProcessImport.h"
7 
8 using namespace Isis;
9 
10 
11 namespace Isis {
18  m_decoder = NULL;
19  m_buffer = NULL;
20 
21  try {
22  // Determine if input file is a JPEG2000 file
23  m_decoder = new JP2Decoder(inputName.expanded());
25  setSamples(m_decoder->GetSampleDimension());
26  setLines(m_decoder->GetLineDimension());
27  setBands(m_decoder->GetBandDimension());
28 
29  int pixelBytes = m_decoder->GetPixelBytes();
30  if (pixelBytes == 1) {
31  m_pixelType = Isis::UnsignedByte;
32  }
33  else if (pixelBytes == 2) {
34  bool signedData = m_decoder->GetSignedData();
35  m_pixelType = signedData ? Isis::SignedWord : Isis::UnsignedWord;
36  }
37  else {
39  "The file [" + filename().expanded() +
40  "] contains unsupported data type",
41  _FILEINFO_);
42  }
43 
44  int pixelSize = Isis::SizeOf(m_pixelType);
45  int readBytes = pixelSize * samples() * bands();
46 
47  m_buffer = new char* [bands()];
48  for (int i = 0; i < bands(); i++) m_buffer[i] = new char [readBytes];
49  }
50  catch (IException &e) {
52  "The file [" + inputName.expanded() +
53  "] cannot be opened as a JPEG 2000 file",
54  _FILEINFO_);
55  }
56  }
57 
58 
63  delete m_decoder;
64  m_decoder = NULL;
65 
66  delete [] m_buffer;
67  m_buffer = NULL;
68  }
69 
70 
77  bool JP2Importer::isGrayscale() const {
78  return m_decoder->GetBandDimension() == 1;
79  }
80 
81 
87  bool JP2Importer::isRgb() const {
88  return m_decoder->GetBandDimension() == 3;
89  }
90 
91 
97  bool JP2Importer::isArgb() const {
98  return m_decoder->GetBandDimension() == 4;
99  }
100 
101 
110  void JP2Importer::updateRawBuffer(int line, int band) const {
111  // Only read a new chunk of data when we move to a new line, since we read
112  // all the input bands for the current line at once
113  // NOTE m_buffer is changed in this method, making the const-ness a lie
114  // TODO make the buffer local to the operator() method and read all bands of
115  // a line at a time with a ProcessByBrick
116  if (band == 1) {
117  if (m_pixelType == Isis::UnsignedByte)
118  m_decoder->Read((unsigned char **) m_buffer);
119  else
120  m_decoder->Read((short int **) m_buffer);
121  }
122  }
123 
124 
138  int JP2Importer::getPixel(int s, int l) const {
139  return s;
140  }
141 
142 
152  int JP2Importer::getGray(int pixel) const {
153  return isGrayscale() ? getFromBuffer(pixel, 0) : convertRgbToGray(pixel);
154  }
155 
156 
165  int JP2Importer::getRed(int pixel) const {
166  return getFromBuffer(pixel, 0);
167  }
168 
169 
178  int JP2Importer::getGreen(int pixel) const {
179  return getFromBuffer(pixel, 1);
180  }
181 
182 
191  int JP2Importer::getBlue(int pixel) const {
192  return getFromBuffer(pixel, 2);
193  }
194 
195 
204  int JP2Importer::getAlpha(int pixel) const {
205  return getFromBuffer(pixel, 3);
206  }
207 
208 
219  int JP2Importer::getFromBuffer(int s, int b) const {
220  int value;
221 
222  switch (m_pixelType) {
223  case Isis::UnsignedByte:
224  value = (int) ((unsigned char *) m_buffer[b])[s];
225  break;
226  case Isis::UnsignedWord:
227  value = (int) ((unsigned short int *) m_buffer[b])[s];
228  break;
229  case Isis::SignedWord:
230  value = (int) ((short int *) m_buffer[b])[s];
231  break;
232  default:
234  "Unknown pixel type [" + IString(m_pixelType) + "]",
235  _FILEINFO_);
236  }
237 
238  return value;
239  }
240 };
241