USGS

Isis 3.0 Object Programmers' Reference

Home

JP2Importer.cpp
00001 #include "JP2Importer.h"
00002 
00003 #include "FileName.h"
00004 #include "IException.h"
00005 #include "JP2Decoder.h"
00006 #include "ProcessImport.h"
00007 
00008 using namespace Isis;
00009 
00010 
00011 namespace Isis {
00017   JP2Importer::JP2Importer(FileName inputName) : ImageImporter(inputName) {
00018     m_decoder = NULL;
00019     m_buffer = NULL;
00020 
00021     try {
00022       // Determine if input file is a JPEG2000 file
00023       m_decoder = new JP2Decoder(inputName.expanded());
00024       m_decoder->OpenFile();
00025       setSamples(m_decoder->GetSampleDimension());
00026       setLines(m_decoder->GetLineDimension());
00027       setBands(m_decoder->GetBandDimension());
00028 
00029       int pixelBytes = m_decoder->GetPixelBytes();
00030       if (pixelBytes == 1) {
00031         m_pixelType = Isis::UnsignedByte;
00032       }
00033       else if (pixelBytes == 2) {
00034         bool signedData = m_decoder->GetSignedData();
00035         m_pixelType = signedData ? Isis::SignedWord : Isis::UnsignedWord;
00036       }
00037       else {
00038         throw IException(IException::User,
00039             "The file [" + filename().expanded() +
00040             "] contains unsupported data type",
00041             _FILEINFO_);
00042       }
00043 
00044       int pixelSize = Isis::SizeOf(m_pixelType);
00045       int readBytes = pixelSize * samples() * bands();
00046 
00047       m_buffer = new char* [bands()];
00048       for (int i = 0; i < bands(); i++) m_buffer[i] = new char [readBytes];
00049     }
00050     catch (IException &e) {
00051       throw IException(IException::Programmer,
00052           "The file [" + inputName.expanded() +
00053           "] cannot be opened as a JPEG 2000 file",
00054           _FILEINFO_);
00055     }
00056   }
00057 
00058 
00062   JP2Importer::~JP2Importer() {
00063     delete m_decoder;
00064     m_decoder = NULL;
00065 
00066     delete [] m_buffer;
00067     m_buffer = NULL;
00068   }
00069 
00070 
00077   bool JP2Importer::isGrayscale() const {
00078     return m_decoder->GetBandDimension() == 1;
00079   }
00080 
00081 
00087   bool JP2Importer::isRgb() const {
00088     return m_decoder->GetBandDimension() == 3;
00089   }
00090 
00091 
00097   bool JP2Importer::isArgb() const {
00098     return m_decoder->GetBandDimension() == 4;
00099   }
00100 
00101 
00110   void JP2Importer::updateRawBuffer(int line, int band) const {
00111     // Only read a new chunk of data when we move to a new line, since we read
00112     // all the input bands for the current line at once
00113     // NOTE m_buffer is changed in this method, making the const-ness a lie
00114     // TODO make the buffer local to the operator() method and read all bands of
00115     // a line at a time with a ProcessByBrick
00116     if (band == 1) {
00117       if (m_pixelType == Isis::UnsignedByte)
00118         m_decoder->Read((unsigned char **) m_buffer);
00119       else
00120         m_decoder->Read((short int **) m_buffer);
00121     }
00122   }
00123 
00124 
00138   int JP2Importer::getPixel(int s, int l) const {
00139     return s;
00140   }
00141 
00142 
00152   int JP2Importer::getGray(int pixel) const {
00153     return isGrayscale() ? getFromBuffer(pixel, 0) : convertRgbToGray(pixel);
00154   }
00155 
00156 
00165   int JP2Importer::getRed(int pixel) const {
00166     return getFromBuffer(pixel, 0);
00167   }
00168 
00169 
00178   int JP2Importer::getGreen(int pixel) const {
00179     return getFromBuffer(pixel, 1);
00180   }
00181 
00182 
00191   int JP2Importer::getBlue(int pixel) const {
00192     return getFromBuffer(pixel, 2);
00193   }
00194 
00195 
00204   int JP2Importer::getAlpha(int pixel) const {
00205     return getFromBuffer(pixel, 3);
00206   }
00207 
00208 
00219   int JP2Importer::getFromBuffer(int s, int b) const {
00220     int value;
00221 
00222     switch (m_pixelType) {
00223       case Isis::UnsignedByte:
00224         value = (int) ((unsigned char *) m_buffer[b])[s];
00225         break;
00226       case Isis::UnsignedWord:
00227         value = (int) ((unsigned short int *) m_buffer[b])[s];
00228         break;
00229       case Isis::SignedWord:
00230         value = (int) ((short int *) m_buffer[b])[s];
00231         break;
00232       default:
00233         throw IException(IException::Programmer,
00234             "Unknown pixel type [" + IString(m_pixelType) + "]",
00235             _FILEINFO_);
00236     }
00237 
00238     return value;
00239   }
00240 };
00241