Isis 3 Programmer Reference
JP2Importer.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "JP2Importer.h"
8
9#include "FileName.h"
10#include "IException.h"
11#include "JP2Decoder.h"
12#include "ProcessImport.h"
13
14using namespace Isis;
15
16
17namespace Isis {
24 m_decoder = NULL;
25 m_buffer = NULL;
26
27 try {
28 // Determine if input file is a JPEG2000 file
29 m_decoder = new JP2Decoder(inputName.expanded());
31 setSamples(m_decoder->GetSampleDimension());
32 setLines(m_decoder->GetLineDimension());
33 setBands(m_decoder->GetBandDimension());
34
35 int pixelBytes = m_decoder->GetPixelBytes();
36 if (pixelBytes == 1) {
37 m_pixelType = Isis::UnsignedByte;
38 }
39 else if (pixelBytes == 2) {
40 bool signedData = m_decoder->GetSignedData();
41 m_pixelType = signedData ? Isis::SignedWord : Isis::UnsignedWord;
42 }
43 else {
45 "The file [" + filename().expanded() +
46 "] contains unsupported data type",
47 _FILEINFO_);
48 }
49
50 int pixelSize = Isis::SizeOf(m_pixelType);
51 int readBytes = pixelSize * samples() * bands();
52
53 m_buffer = new char* [bands()];
54 for (int i = 0; i < bands(); i++) m_buffer[i] = new char [readBytes];
55 }
56 catch (IException &e) {
58 "The file [" + inputName.expanded() +
59 "] cannot be opened as a JPEG 2000 file",
60 _FILEINFO_);
61 }
62 }
63
64
69 delete m_decoder;
70 m_decoder = NULL;
71
72 delete [] m_buffer;
73 m_buffer = NULL;
74 }
75
76
84 return m_decoder->GetBandDimension() == 1;
85 }
86
87
93 bool JP2Importer::isRgb() const {
94 return m_decoder->GetBandDimension() == 3;
95 }
96
97
103 bool JP2Importer::isArgb() const {
104 return m_decoder->GetBandDimension() == 4;
105 }
106
107
116 void JP2Importer::updateRawBuffer(int line, int band) const {
117 // Only read a new chunk of data when we move to a new line, since we read
118 // all the input bands for the current line at once
119 // NOTE m_buffer is changed in this method, making the const-ness a lie
120 // TODO make the buffer local to the operator() method and read all bands of
121 // a line at a time with a ProcessByBrick
122 if (band == 1) {
123 if (m_pixelType == Isis::UnsignedByte)
124 m_decoder->Read((unsigned char **) m_buffer);
125 else
126 m_decoder->Read((short int **) m_buffer);
127 }
128 }
129
130
144 int JP2Importer::getPixel(int s, int l) const {
145 return s;
146 }
147
148
158 int JP2Importer::getGray(int pixel) const {
159 return isGrayscale() ? getFromBuffer(pixel, 0) : convertRgbToGray(pixel);
160 }
161
162
171 int JP2Importer::getRed(int pixel) const {
172 return getFromBuffer(pixel, 0);
173 }
174
175
184 int JP2Importer::getGreen(int pixel) const {
185 return getFromBuffer(pixel, 1);
186 }
187
188
197 int JP2Importer::getBlue(int pixel) const {
198 return getFromBuffer(pixel, 2);
199 }
200
201
210 int JP2Importer::getAlpha(int pixel) const {
211 return getFromBuffer(pixel, 3);
212 }
213
214
225 int JP2Importer::getFromBuffer(int s, int b) const {
226 int value;
227
228 switch (m_pixelType) {
229 case Isis::UnsignedByte:
230 value = (int) ((unsigned char *) m_buffer[b])[s];
231 break;
232 case Isis::UnsignedWord:
233 value = (int) ((unsigned short int *) m_buffer[b])[s];
234 break;
235 case Isis::SignedWord:
236 value = (int) ((short int *) m_buffer[b])[s];
237 break;
238 default:
240 "Unknown pixel type [" + IString(m_pixelType) + "]",
241 _FILEINFO_);
242 }
243
244 return value;
245 }
246};
247
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Adds specific functionality to C++ strings.
Definition IString.h:165
Imports images with standard formats into Isis as cubes.
int bands() const
The band dimension (depth) of the output image.
void setLines(int l)
Set the line dimension (height) of the output image.
int samples() const
The sample dimension (width) of the output image.
void setSamples(int s)
Set the sample dimension (width) of the output image.
FileName filename() const
The filename of the input image this instance was constructed with.
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...
void setBands(int b)
Set the band dimension (depth) of the output image.
JPEG2000 decoder class.
Definition JP2Decoder.h:83
void OpenFile()
Open the JPEG2000 file.
void Read(unsigned char **inbuf)
Read data from JP2 file containing 8-bit data.
virtual int getGreen(int pixel) const
Retrieves the green component of the given pixel from the second band of the input buffer.
virtual bool isRgb() const
Tests to see if the input image is triple-banded, implying RGB (no alpha).
virtual void updateRawBuffer(int line, int band) const
Updates the buffer used to store chunks of the input data at a time.
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...
virtual ~JP2Importer()
Destruct the importer.
virtual int getGray(int pixel) const
Retrieves the gray component of the given pixel.
JP2Importer(FileName inputName)
Construct the importer.
virtual int getRed(int pixel) const
Retrieves the red component of the given pixel from the first band of the input buffer.
char ** m_buffer
Buffer that stores a line of JPEG 2000 data and all its color bands.
Definition JP2Importer.h:56
virtual bool isArgb() const
Tests to see if the input image is quadruple-banded, implying RGBA.
Isis::PixelType m_pixelType
Pixel type of the input image needed for reading data into the buffer.
Definition JP2Importer.h:59
virtual int getBlue(int pixel) const
Retrieves the blue component of the given pixel from the third band of the input buffer.
JP2Decoder * m_decoder
Takes a raw stream of JPEG 2000 data and reads it into a buffer.
Definition JP2Importer.h:53
int getFromBuffer(int s, int b) const
Retrieves the pixel value from the input buffer corresponding to the given sample and band (the buffe...
virtual int getAlpha(int pixel) const
Retrieves the alpha component of the given pixel from the fourth band of the input buffer.
virtual bool isGrayscale() const
Tests to see if the input image is single-banded, implying grayscale (no RGB/A).
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
int SizeOf(Isis::PixelType pixelType)
Returns the number of bytes of the specified PixelType.
Definition PixelType.h:46