2 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
26 #define USE_OPJ_DEPRECATED
27 /* set this macro to enable profiling for the given test */
28 /* warning : in order to be effective, openjpeg must have been built with profiling enabled !! */
42 #define INPUT_FILE "test.j2k"
44 /* -------------------------------------------------------------------------- */
47 sample error callback expecting a FILE* client object
49 void error_callback_file(const char *msg, void *client_data) {
50 FILE *stream = (FILE*)client_data;
51 fprintf(stream, "[ERROR] %s", msg);
54 sample warning callback expecting a FILE* client object
56 void warning_callback_file(const char *msg, void *client_data) {
57 FILE *stream = (FILE*)client_data;
58 fprintf(stream, "[WARNING] %s", msg);
61 sample error debug callback expecting no client object
63 void error_callback(const char *msg, void *client_data) {
65 fprintf(stdout, "[ERROR] %s", msg);
68 sample warning debug callback expecting no client object
70 void warning_callback(const char *msg, void *client_data) {
72 fprintf(stdout, "[WARNING] %s", msg);
75 sample debug callback expecting no client object
77 void info_callback(const char *msg, void *client_data) {
79 fprintf(stdout, "[INFO] %s", msg);
82 /* -------------------------------------------------------------------------- */
86 opj_dparameters_t l_param;
87 opj_codec_t * l_codec;
88 opj_image_t * l_image;
90 opj_stream_t * l_stream;
91 OPJ_UINT32 l_data_size;
92 OPJ_UINT32 l_max_data_size = 1000;
93 OPJ_UINT32 l_tile_index;
94 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
96 OPJ_INT32 l_tile_x0,l_tile_y0;
97 OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
98 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
108 opj_set_default_decoder_parameters(&l_param);
110 /** you may here add custom decoding parameters */
111 /* do not use layer decoding limitations */
112 l_param.cp_layer = 0;
114 /* do not use resolutions reductions */
115 l_param.cp_reduce = 0;
117 /* to decode only a part of the image data */
118 //opj_restrict_decoding(&l_param,0,0,1000,1000);
120 l_codec = opj_create_decompress(CODEC_J2K);
128 /* catch events using our callbacks and give a local context */
129 opj_set_info_handler(l_codec, info_callback,00);
130 opj_set_warning_handler(l_codec, warning_callback,00);
131 opj_set_error_handler(l_codec, error_callback,00);
134 (! opj_setup_decoder(l_codec,&l_param))
137 opj_destroy_codec(l_codec);
141 l_file = fopen(INPUT_FILE,"rb");
145 fprintf(stdout, "Error opening input file\n");
147 opj_destroy_codec(l_codec);
151 l_stream = opj_stream_create_default_file_stream(l_file,true);
154 (! opj_read_header(l_codec,
165 opj_stream_destroy(l_stream);
167 opj_destroy_codec(l_codec);
170 printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
171 opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1);
176 (! opj_read_tile_header(
189 opj_stream_destroy(l_stream);
191 opj_destroy_codec(l_codec);
192 opj_image_destroy(l_image);
199 (l_data_size > l_max_data_size)
201 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
205 opj_stream_destroy(l_stream);
207 opj_destroy_codec(l_codec);
208 opj_image_destroy(l_image);
211 l_max_data_size = l_data_size;
215 (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
218 opj_stream_destroy(l_stream);
220 opj_destroy_codec(l_codec);
221 opj_image_destroy(l_image);
224 /** now should inspect image to know the reduction factor and then how to behave with data */
228 (! opj_end_decompress(l_codec,l_stream))
231 opj_stream_destroy(l_stream);
233 opj_destroy_codec(l_codec);
234 opj_image_destroy(l_image);
238 opj_stream_destroy(l_stream);
240 opj_destroy_codec(l_codec);
241 opj_image_destroy(l_image);