12 #include "IException.h"
14 #include "JP2Decoder.h"
20 using namespace kdu_core;
21 using namespace kdu_supp;
32 JP2Decoder::JP2Decoder(
const QString &jp2file) {
36 p_resolutionLevel = 1;
41 kdu_customize_errors(Kakadu_Error);
43 std::string msg =
"JPEG2000 has not been enabled with this build of ISIS3";
44 throw IException(IException::Programmer, msg, _FILEINFO_);
52 void JP2Decoder::OpenFile() {
55 if(JP2_Source == NULL) {
58 JP2_Stream =
new jp2_family_src();
59 JP2_Stream->open(p_jp2File.toLatin1().data());
62 JP2_Source =
new jp2_source();
63 if(!JP2_Source->open(JP2_Stream)) {
64 QString msg =
"Unable to open the decoder because the source file ";
65 msg +=
"does not have valid JP2 format content [" + p_jp2File +
"]";
66 throw IException(IException::User, msg, _FILEINFO_);
70 JP2_Source->read_header();
73 JPEG2000_Codestream =
new kdu_codestream();
74 JPEG2000_Codestream->create(JP2_Source);
78 p_numBands = JPEG2000_Codestream->get_num_components(
true);
82 JPEG2000_Codestream->get_dims(0, p_imageDims,
true);
85 p_pixelBits = JPEG2000_Codestream->get_bit_depth(0,
true);
86 p_pixelBytes = (p_pixelBits >> 3) + ((p_pixelBits % 8) ? 1 : 0);
87 if(p_pixelBytes == 3) p_pixelBytes = 4;
88 if(p_pixelBits > 16 || p_pixelBytes > 2) {
89 QString msg =
"The source file has unsupported pixel type ";
90 msg +=
"[" + p_jp2File +
"]";
91 throw IException(IException::User, msg, _FILEINFO_);
93 p_signedData = JPEG2000_Codestream->get_signed(0,
true);
98 unsigned int pixel_bits;
100 for(
unsigned int band = 1; band < p_numBands; ++band) {
101 JPEG2000_Codestream->get_dims(band, dims,
true);
102 pixel_bits = JPEG2000_Codestream->get_bit_depth(band,
true);
103 signed_data = JPEG2000_Codestream->get_signed(band,
true);
104 if(dims.size.x != p_imageDims.size.x || dims.size.y != p_imageDims.size.y ||
105 dims.pos.x != p_imageDims.pos.x || dims.pos.y != p_imageDims.pos.y ||
106 pixel_bits != p_pixelBits || signed_data != p_signedData) {
107 std::string msg =
"The source file does not have bands with matching ";
108 msg +=
"characteristics";
109 throw IException(IException::User, msg, _FILEINFO_);
115 p_highestResLevel = JPEG2000_Codestream->get_min_dwt_levels() + 1;
116 SetResolutionAndRegion();
120 p_decompressor.start(*JPEG2000_Codestream);
125 p_stripeHeights =
new int[p_numBands];
126 p_maxStripeHeights =
new int[p_numBands];
127 p_precisions =
new int[p_numBands];
128 p_isSigned =
new bool[p_numBands];
129 p_decompressor.get_recommended_stripe_heights(MIN_STRIPE_HEIGHT,
130 MAX_STRIPE_HEIGHT, p_stripeHeights, p_maxStripeHeights);
131 for(
unsigned int i = 0; i < p_numBands; i++) {
132 p_precisions[i] = p_pixelBits;
133 p_stripeHeights[i] = 1;
134 p_isSigned[i] = p_signedData;
145 void JP2Decoder::SetResolutionAndRegion() {
149 JPEG2000_Codestream->apply_input_restrictions(0, 0, p_resolutionLevel - 1, 0, NULL,
150 KDU_WANT_OUTPUT_COMPONENTS);
152 JPEG2000_Codestream->get_dims(0, p_imageDims,
true);
153 p_numSamples = p_imageDims.size.x;
154 p_numLines = p_imageDims.size.y;
168 void JP2Decoder::Read(
unsigned char **inbuf) {
170 p_readStripes = p_decompressor.pull_stripe(inbuf, p_stripeHeights, NULL, NULL,
185 void JP2Decoder::Read(
short int **inbuf) {
187 p_readStripes = p_decompressor.pull_stripe(inbuf, p_stripeHeights, NULL, NULL,
188 p_precisions, p_isSigned);
196 JP2Decoder::~JP2Decoder() {
202 p_decompressor.finish();
203 if(JPEG2000_Codestream) {
204 JPEG2000_Codestream->destroy();
206 JPEG2000_Codestream = NULL;
220 delete [] p_stripeHeights;
221 delete [] p_maxStripeHeights;
222 delete [] p_precisions;
223 delete [] p_isSigned;
228 bool JP2Decoder::IsJP2(QString filename) {
230 jp2_family_src *stream =
new jp2_family_src();
231 stream->open(filename.toLatin1().data());
232 jp2_source *source =
new jp2_source();
234 bool result = source->open(stream);