Isis 3 Programmer Reference
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 
JPEG2000 decoder class.
Definition: JP2Decoder.h:98
virtual bool isRgb() const
Tests to see if the input image is triple-banded, implying RGB (no alpha).
Definition: JP2Importer.cpp:87
virtual int convertRgbToGray(int pixel) const
Convert the current pixel, taken from an RGB/A image, and blend its RGB components into a single gray...
virtual int getGreen(int pixel) const
Retrieves the green component of the given pixel from the second band of the input buffer...
File name manipulation and expansion.
Definition: FileName.h:116
int samples() const
The sample dimension (width) of the output image.
FileName filename() const
The filename of the input image this instance was constructed with.
virtual bool isArgb() const
Tests to see if the input image is quadruple-banded, implying RGBA.
Definition: JP2Importer.cpp:97
virtual int getAlpha(int pixel) const
Retrieves the alpha component of the given pixel from the fourth band of the input buffer...
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition: PixelType.h:62
JP2Importer(FileName inputName)
Construct the importer.
Definition: JP2Importer.cpp:17
char ** m_buffer
Buffer that stores a line of JPEG 2000 data and all its color bands.
Definition: JP2Importer.h:72
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
virtual int getRed(int pixel) const
Retrieves the red component of the given pixel from the first band of the input buffer.
void Read(unsigned char **inbuf)
Read data from JP2 file containing 8-bit data.
Definition: JP2Decoder.cpp:184
JP2Decoder * m_decoder
Takes a raw stream of JPEG 2000 data and reads it into a buffer.
Definition: JP2Importer.h:69
virtual int getPixel(int s, int l) const
Returns a representation of a pixel for the input format that can then be broken down into specific g...
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
void setSamples(int s)
Set the sample dimension (width) of the output image.
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
void setLines(int l)
Set the line dimension (height) of the output image.
void setBands(int b)
Set the band dimension (depth) of the output image.
virtual int getBlue(int pixel) const
Retrieves the blue component of the given pixel from the third band of the input buffer.
virtual int getGray(int pixel) const
Retrieves the gray component of the given pixel.
Isis::PixelType m_pixelType
Pixel type of the input image needed for reading data into the buffer.
Definition: JP2Importer.h:75
virtual void updateRawBuffer(int line, int band) const
Updates the buffer used to store chunks of the input data at a time.
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void OpenFile()
Open the JPEG2000 file.
Definition: JP2Decoder.cpp:68
virtual ~JP2Importer()
Destruct the importer.
Definition: JP2Importer.cpp:62
virtual bool isGrayscale() const
Tests to see if the input image is single-banded, implying grayscale (no RGB/A).
Definition: JP2Importer.cpp:77
Imports images with standard formats into Isis as cubes.
Definition: ImageImporter.h:55
int getFromBuffer(int s, int b) const
Retrieves the pixel value from the input buffer corresponding to the given sample and band (the buffe...
int bands() const
The band dimension (depth) of the output image.