4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include "ext_libopenjpeg/ext_openjpeg.h"
33 #include "j2k_to_idxjp2.h"
34 #include "event_mgr_handler.h"
36 opj_image_t * decode_j2k( unsigned char *j2kstream, int j2klen, opj_codestream_info_t *cstr_info)
39 opj_dparameters_t parameters; /* decompression parameters */
40 opj_dinfo_t *dinfo; /* handle to a decompressor */
42 opj_event_mgr_t event_mgr; /* event manager */
44 /* set decoding parameters to default values */
45 opj_set_default_decoder_parameters(¶meters);
47 /* decode the code-stream */
48 /* ---------------------- */
50 /* JPEG-2000 codestream */
51 /* get a decoder handle */
52 dinfo = opj_create_decompress( CODEC_J2K);
54 event_mgr = set_default_event_mgr();
56 /* catch events using our callbacks and give a local context */
57 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
59 /* setup the decoder decoding parameters using user parameters */
60 opj_setup_decoder(dinfo, ¶meters);
62 /* open a byte stream */
63 cio = opj_cio_open((opj_common_ptr)dinfo, j2kstream, j2klen);
65 /* decode the stream and fill the image structure */
66 image = opj_decode_with_info(dinfo, cio, cstr_info);
68 fprintf(stderr, "ERROR -> jp2_to_image: failed to decode image!\n");
69 opj_destroy_decompress(dinfo);
74 if( cstr_info->marknum == 0)
75 add_mainheader_marker_info( cio, cstr_info);
77 /* close the byte stream */
80 /* free remaining structures */
82 opj_destroy_decompress(dinfo);