[trunk] fix name
[openjpeg.git] / applications / jpip / tools / indexer / j2k_decoder.c
1 /*
2  * $Id$
3  *
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include <stdio.h>
32 #include "ext_libopenjpeg/ext_openjpeg.h"
33 #include "j2k_to_idxjp2.h"
34 #include "event_mgr_handler.h"
35
36 opj_image_t * decode_j2k( unsigned char *j2kstream, int j2klen, opj_codestream_info_t *cstr_info)
37 {
38   opj_image_t *image;
39   opj_dparameters_t parameters; /* decompression parameters */
40   opj_dinfo_t *dinfo;   /* handle to a decompressor */
41   opj_cio_t *cio;
42   opj_event_mgr_t event_mgr;            /* event manager */
43
44   /* set decoding parameters to default values */
45   opj_set_default_decoder_parameters(&parameters);
46
47   /* decode the code-stream */
48   /* ---------------------- */
49
50   /* JPEG-2000 codestream */
51   /* get a decoder handle */
52   dinfo = opj_create_decompress( CODEC_J2K);
53
54   event_mgr = set_default_event_mgr();
55
56   /* catch events using our callbacks and give a local context */
57   opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
58
59   /* setup the decoder decoding parameters using user parameters */
60   opj_setup_decoder(dinfo, &parameters);
61
62   /* open a byte stream */
63   cio = opj_cio_open((opj_common_ptr)dinfo, j2kstream, j2klen);
64   
65   /* decode the stream and fill the image structure */
66   image = opj_decode_with_info(dinfo, cio, cstr_info);
67   if(!image) {
68     fprintf(stderr, "ERROR -> jp2_to_image: failed to decode image!\n");
69     opj_destroy_decompress(dinfo);
70     opj_cio_close(cio);
71     return NULL;
72   }
73
74   if( cstr_info->marknum == 0)
75     add_mainheader_marker_info( cio, cstr_info);
76
77   /* close the byte stream */
78   opj_cio_close(cio);
79  
80   /* free remaining structures */
81   if(dinfo) {
82     opj_destroy_decompress(dinfo);
83   }
84
85   return image;
86 }