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 #include "opj_config.h"
50 #define INPUT_FILE "test.j2k"
52 /* -------------------------------------------------------------------------- */
55 sample error callback expecting a FILE* client object
57 void error_callback_file(const char *msg, void *client_data) {
58 FILE *stream = (FILE*)client_data;
59 fprintf(stream, "[ERROR] %s", msg);
62 sample warning callback expecting a FILE* client object
64 void warning_callback_file(const char *msg, void *client_data) {
65 FILE *stream = (FILE*)client_data;
66 fprintf(stream, "[WARNING] %s", msg);
69 sample error debug callback expecting no client object
71 void error_callback(const char *msg, void *client_data) {
73 fprintf(stdout, "[ERROR] %s", msg);
76 sample warning debug callback expecting no client object
78 void warning_callback(const char *msg, void *client_data) {
80 fprintf(stdout, "[WARNING] %s", msg);
83 sample debug callback expecting no client object
85 void info_callback(const char *msg, void *client_data) {
87 fprintf(stdout, "[INFO] %s", msg);
90 /* -------------------------------------------------------------------------- */
94 opj_dparameters_t l_param;
95 opj_codec_t * l_codec;
96 opj_image_t * l_image;
98 opj_stream_t * l_stream;
99 OPJ_UINT32 l_data_size;
100 OPJ_UINT32 l_max_data_size = 1000;
101 OPJ_UINT32 l_tile_index;
102 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
103 opj_bool l_go_on = OPJ_TRUE;
104 OPJ_INT32 l_tile_x0,l_tile_y0;
105 OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y,l_nb_comps;
106 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
116 opj_set_default_decoder_parameters(&l_param);
118 /** you may here add custom decoding parameters */
119 /* do not use layer decoding limitations */
120 l_param.cp_layer = 0;
122 /* do not use resolutions reductions */
123 l_param.cp_reduce = 0;
125 /* to decode only a part of the image data */
126 //opj_restrict_decoding(&l_param,0,0,1000,1000);
128 l_codec = opj_create_decompress(CODEC_J2K);
136 /* catch events using our callbacks and give a local context */
137 opj_set_info_handler(l_codec, info_callback,00);
138 opj_set_warning_handler(l_codec, warning_callback,00);
139 opj_set_error_handler(l_codec, error_callback,00);
140 opj_event_mgr_t l_event_mgr;
143 (! opj_setup_decoder_v2(l_codec,&l_param,&l_event_mgr))
146 opj_destroy_codec(l_codec);
150 l_file = fopen(INPUT_FILE,"rb");
154 fprintf(stdout, "Error opening input file\n");
156 opj_destroy_codec(l_codec);
160 l_stream = opj_stream_create_default_file_stream(l_file,OPJ_TRUE);
163 (! opj_read_header(l_codec,
174 opj_stream_destroy(l_stream);
176 opj_destroy_codec(l_codec);
179 printf("Setting decoding area to %d,%d,%d,%d\n", DA_X0, DA_Y0, DA_X1, DA_Y1);
180 opj_set_decode_area(l_codec, DA_X0, DA_Y0, DA_X1, DA_Y1);
185 (! opj_read_tile_header(
198 opj_stream_destroy(l_stream);
200 opj_destroy_codec(l_codec);
201 opj_image_destroy(l_image);
208 (l_data_size > l_max_data_size)
210 l_data = (OPJ_BYTE *) realloc(l_data,l_data_size);
214 opj_stream_destroy(l_stream);
216 opj_destroy_codec(l_codec);
217 opj_image_destroy(l_image);
220 l_max_data_size = l_data_size;
224 (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
227 opj_stream_destroy(l_stream);
229 opj_destroy_codec(l_codec);
230 opj_image_destroy(l_image);
233 /** now should inspect image to know the reduction factor and then how to behave with data */
237 (! opj_end_decompress(l_codec,l_stream))
240 opj_stream_destroy(l_stream);
242 opj_destroy_codec(l_codec);
243 opj_image_destroy(l_image);
247 opj_stream_destroy(l_stream);
249 opj_destroy_codec(l_codec);
250 opj_image_destroy(l_image);