| 157 | } |
| 158 | |
| 159 | void JPEG2000Codec::decode(unsigned char* buf, const unsigned int& inSize, const unsigned int& outSize) |
| 160 | { |
| 161 | opj_memory_stream decodeStream; |
| 162 | // OpenJPEG stuff. |
| 163 | opj_image_t *decompImage = NULL; |
| 164 | opj_stream_t *l_stream = NULL; |
| 165 | |
| 166 | //Set up the input buffer as a stream |
| 167 | decodeStream.data = (OPJ_UINT8 *)buf; |
| 168 | decodeStream.size = inSize; |
| 169 | decodeStream.offset = 0; |
| 170 | //Open the memory as a stream. |
| 171 | l_stream = opj_stream_create_default_memory_stream(&decodeStream, OPJ_TRUE); |
| 172 | |
| 173 | opj_dparameters_t decodeParameters; |
| 174 | opj_set_default_decoder_parameters(&decodeParameters); |
| 175 | opj_codec_t* decoder = opj_create_decompress(OPJ_CODEC_FORMAT::OPJ_CODEC_J2K);//Bad openjpeg, can't be reused! |
| 176 | //Catch events using our callbacks and give a local context. |
| 177 | opj_set_info_handler(decoder, info_callback, NULL); |
| 178 | opj_set_warning_handler(decoder, warning_callback, NULL); |
| 179 | opj_set_error_handler(decoder, error_callback, NULL); |
| 180 | |
| 181 | //Setup the decoder decoding parameters using user parameters. |
| 182 | opj_setup_decoder(decoder, &decodeParameters); |
| 183 | #if OPJ_VERSION_MAJOR >= 2 && OPJ_VERSION_MINOR >= 3 |
| 184 | opj_codec_set_threads(decoder, 4); |
| 185 | #endif |
| 186 | |
| 187 | // Read the main header of the codestream, if necessary the JP2 boxes, and create decompImage. |
| 188 | OPJ_BOOL headerSucces = opj_read_header(l_stream, decoder, &decompImage); |
| 189 | OPJ_BOOL setAreaSucces = opj_set_decode_area(decoder, decompImage, decodeParameters.DA_x0, |
| 190 | decodeParameters.DA_y0, decodeParameters.DA_x1, decodeParameters.DA_y1); |
| 191 | OPJ_BOOL decodeSucces = opj_decode(decoder, l_stream, decompImage); |
| 192 | OPJ_BOOL decompressSuccess = opj_end_decompress(decoder, l_stream); |
| 193 | |
| 194 | //Done with the input stream and the decoder. |
| 195 | opj_stream_destroy(l_stream); |
| 196 | opj_destroy_codec(decoder); |
| 197 | |
| 198 | //Get the total uncompressed length. |
| 199 | int prec_jpc = decompImage->comps[0].prec; //Need it at the end |
| 200 | int bytes_jpc= (prec_jpc + 7) / 8;// Bytes or words. |
| 201 | int stream_len = (decompImage->comps[0].w * decompImage->comps[0].h); |
| 202 | |
| 203 | OPJ_INT32 ** compPointers = new OPJ_INT32*[decompImage->numcomps]; |
| 204 | for (unsigned int cmp = 0; cmp < decompImage->numcomps; cmp++) { |
| 205 | compPointers[cmp] = decompImage->comps[cmp].data; |
| 206 | } |
| 207 | std::fill(buf, buf + outSize, 0); |
| 208 | for (int index = 0; index < stream_len; index++) { |
| 209 | for (unsigned int cmp = 0; cmp < decompImage->numcomps; cmp++) { |
| 210 | for (int byteCnt = 0; byteCnt < bytes_jpc; byteCnt++) |
| 211 | { |
| 212 | *(buf)++ |= (unsigned char)((*compPointers[cmp] >> (8 * byteCnt)) & 0xFF); |
| 213 | } |
| 214 | *compPointers[cmp]++; |
| 215 | } |
| 216 | } |
no test coverage detected