42 JP2Decoder::JP2Decoder(
const QString &jp2file) {
46 p_resolutionLevel = 1;
51 kdu_customize_errors(Kakadu_Error);
53 std::string msg =
"JPEG2000 has not been enabled with this build of ISIS3";
62 void JP2Decoder::OpenFile() {
65 if(JP2_Source == NULL) {
68 JP2_Stream =
new jp2_family_src();
69 JP2_Stream->open(p_jp2File.toLatin1().data());
72 JP2_Source =
new jp2_source();
73 if(!JP2_Source->open(JP2_Stream)) {
74 QString msg =
"Unable to open the decoder because the source file ";
75 msg +=
"does not have valid JP2 format content [" + p_jp2File +
"]";
80 JP2_Source->read_header();
83 JPEG2000_Codestream =
new kdu_codestream();
84 JPEG2000_Codestream->create(JP2_Source);
88 p_numBands = JPEG2000_Codestream->get_num_components(
true);
92 JPEG2000_Codestream->get_dims(0, p_imageDims,
true);
95 p_pixelBits = JPEG2000_Codestream->get_bit_depth(0,
true);
96 p_pixelBytes = (p_pixelBits >> 3) + ((p_pixelBits % 8) ? 1 : 0);
97 if(p_pixelBytes == 3) p_pixelBytes = 4;
98 if(p_pixelBits > 16 || p_pixelBytes > 2) {
99 QString msg =
"The source file has unsupported pixel type ";
100 msg +=
"[" + p_jp2File +
"]";
103 p_signedData = JPEG2000_Codestream->get_signed(0,
true);
108 unsigned int pixel_bits;
110 for(
unsigned int band = 1; band < p_numBands; ++band) {
111 JPEG2000_Codestream->get_dims(band, dims,
true);
112 pixel_bits = JPEG2000_Codestream->get_bit_depth(band,
true);
113 signed_data = JPEG2000_Codestream->get_signed(band,
true);
114 if(dims.size.x != p_imageDims.size.x || dims.size.y != p_imageDims.size.y ||
115 dims.pos.x != p_imageDims.pos.x || dims.pos.y != p_imageDims.pos.y ||
116 pixel_bits != p_pixelBits || signed_data != p_signedData) {
117 std::string msg =
"The source file does not have bands with matching ";
118 msg +=
"characteristics";
125 p_highestResLevel = JPEG2000_Codestream->get_min_dwt_levels() + 1;
126 SetResolutionAndRegion();
130 p_decompressor.start(*JPEG2000_Codestream);
135 p_stripeHeights =
new int[p_numBands];
136 p_maxStripeHeights =
new int[p_numBands];
137 p_precisions =
new int[p_numBands];
138 p_isSigned =
new bool[p_numBands];
139 p_decompressor.get_recommended_stripe_heights(MIN_STRIPE_HEIGHT,
140 MAX_STRIPE_HEIGHT, p_stripeHeights, p_maxStripeHeights);
141 for(
unsigned int i = 0; i < p_numBands; i++) {
142 p_precisions[i] = p_pixelBits;
143 p_stripeHeights[i] = 1;
144 p_isSigned[i] = p_signedData;
155 void JP2Decoder::SetResolutionAndRegion() {
159 JPEG2000_Codestream->apply_input_restrictions(0, 0, p_resolutionLevel - 1, 0, NULL,
160 KDU_WANT_OUTPUT_COMPONENTS);
162 JPEG2000_Codestream->get_dims(0, p_imageDims,
true);
163 p_numSamples = p_imageDims.size.x;
164 p_numLines = p_imageDims.size.y;
178 void JP2Decoder::Read(
unsigned char **inbuf) {
180 p_readStripes = p_decompressor.pull_stripe(inbuf, p_stripeHeights, NULL, NULL,
195 void JP2Decoder::Read(
short int **inbuf) {
197 p_readStripes = p_decompressor.pull_stripe(inbuf, p_stripeHeights, NULL, NULL,
198 p_precisions, p_isSigned);
206 JP2Decoder::~JP2Decoder() {
208 if(JPEG2000_Codestream) JPEG2000_Codestream->destroy();
209 JPEG2000_Codestream = NULL;
220 p_decompressor.finish();
224 delete [] p_stripeHeights;
225 delete [] p_maxStripeHeights;
226 delete [] p_precisions;
227 delete [] p_isSigned;
232 bool JP2Decoder::IsJP2(QString filename) {
234 jp2_family_src *stream =
new jp2_family_src();
235 stream->open(filename.toLatin1().data());
236 jp2_source *source =
new jp2_source();
238 bool result = source->open(stream);
Kakadu error messaging class.
#define _FILEINFO_
Macro for the filename and line number.