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"
47 #define strcasecmp _stricmp
48 #define strncasecmp _strnicmp
54 #include "format_defs.h"
57 /* -------------------------------------------------------------------------- */
59 int get_file_format(const char *filename);
60 static int infile_format(const char *fname);
62 /* -------------------------------------------------------------------------- */
63 int get_file_format(const char *filename) {
65 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
66 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
67 char * ext = strrchr(filename, '.');
72 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
73 if(strcasecmp(ext, extension[i]) == 0) {
82 /* -------------------------------------------------------------------------- */
83 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
84 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
85 /* position 45: "\xff\x52" */
86 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
88 static int infile_format(const char *fname)
91 const char *s, *magic_s;
92 int ext_format, magic_format;
93 unsigned char buf[12];
94 unsigned int l_nb_read;
96 reader = fopen(fname, "rb");
102 l_nb_read = (unsigned int)fread(buf, 1, 12, reader);
107 ext_format = get_file_format(fname);
109 if (ext_format == JPT_CFMT)
112 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
113 magic_format = JP2_CFMT;
116 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
117 magic_format = J2K_CFMT;
118 magic_s = ".j2k or .jpc or .j2c";
123 if (magic_format == ext_format)
126 s = fname + strlen(fname) - 4;
128 fputs("\n===========================================\n", stderr);
129 fprintf(stderr, "The extension of this file is incorrect.\n"
130 "FOUND %s. SHOULD BE %s\n", s, magic_s);
131 fputs("===========================================\n", stderr);
137 /* -------------------------------------------------------------------------- */
140 sample error debug callback expecting no client object
142 static void error_callback(const char *msg, void *client_data) {
144 fprintf(stdout, "[ERROR] %s", msg);
147 sample warning debug callback expecting no client object
149 static void warning_callback(const char *msg, void *client_data) {
151 fprintf(stdout, "[WARNING] %s", msg);
154 sample debug callback expecting no client object
156 static void info_callback(const char *msg, void *client_data) {
158 fprintf(stdout, "[INFO] %s", msg);
161 /* -------------------------------------------------------------------------- */
163 int main (int argc, char *argv[])
165 opj_dparameters_t l_param;
166 opj_codec_t * l_codec;
167 opj_image_t * l_image;
168 opj_stream_t * l_stream;
169 OPJ_UINT32 l_data_size;
170 OPJ_UINT32 l_max_data_size = 1000;
171 OPJ_UINT32 l_tile_index;
172 OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
173 OPJ_BOOL l_go_on = OPJ_TRUE;
174 OPJ_UINT32 l_nb_comps=0 ;
175 OPJ_INT32 l_current_tile_x0,l_current_tile_y0,l_current_tile_x1,l_current_tile_y1;
181 const char *input_file;
183 /* should be test_tile_decoder 0 0 1000 1000 tte1.j2k */
190 input_file = argv[5];
199 input_file = "test.j2k";
206 l_stream = opj_stream_create_default_file_stream(input_file,OPJ_TRUE);
209 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
213 /* Set the default decoding parameters */
214 opj_set_default_decoder_parameters(&l_param);
217 l_param.decod_format = infile_format(input_file);
219 /** you may here add custom decoding parameters */
220 /* do not use layer decoding limitations */
221 l_param.cp_layer = 0;
223 /* do not use resolutions reductions */
224 l_param.cp_reduce = 0;
226 /* to decode only a part of the image data */
227 /*opj_restrict_decoding(&l_param,0,0,1000,1000);*/
230 switch(l_param.decod_format) {
231 case J2K_CFMT: /* JPEG-2000 codestream */
233 /* Get a decoder handle */
234 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
237 case JP2_CFMT: /* JPEG 2000 compressed image data */
239 /* Get a decoder handle */
240 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
245 fprintf(stderr, "ERROR -> Not a valid JPEG2000 file!\n");
247 opj_stream_destroy(l_stream);
252 /* catch events using our callbacks and give a local context */
253 opj_set_info_handler(l_codec, info_callback,00);
254 opj_set_warning_handler(l_codec, warning_callback,00);
255 opj_set_error_handler(l_codec, error_callback,00);
257 /* Setup the decoder decoding parameters using user parameters */
258 if (! opj_setup_decoder(l_codec, &l_param))
260 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
262 opj_stream_destroy(l_stream);
263 opj_destroy_codec(l_codec);
267 /* Read the main header of the codestream and if necessary the JP2 boxes*/
268 if (! opj_read_header(l_stream, l_codec, &l_image))
270 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
272 opj_stream_destroy(l_stream);
273 opj_destroy_codec(l_codec);
277 if (!opj_set_decode_area(l_codec, l_image, da_x0, da_y0,da_x1, da_y1)){
278 fprintf(stderr, "ERROR -> j2k_to_image: failed to set the decoded area\n");
280 opj_stream_destroy(l_stream);
281 opj_destroy_codec(l_codec);
282 opj_image_destroy(l_image);
289 if (! opj_read_tile_header( l_codec,
301 opj_stream_destroy(l_stream);
302 opj_destroy_codec(l_codec);
303 opj_image_destroy(l_image);
309 if (l_data_size > l_max_data_size)
311 OPJ_BYTE *l_new_data = (OPJ_BYTE *) realloc(l_data, l_data_size);
315 opj_stream_destroy(l_stream);
316 opj_destroy_codec(l_codec);
317 opj_image_destroy(l_image);
321 l_max_data_size = l_data_size;
324 if (! opj_decode_tile_data(l_codec,l_tile_index,l_data,l_data_size,l_stream))
327 opj_stream_destroy(l_stream);
328 opj_destroy_codec(l_codec);
329 opj_image_destroy(l_image);
332 /** now should inspect image to know the reduction factor and then how to behave with data */
336 if (! opj_end_decompress(l_codec,l_stream))
339 opj_stream_destroy(l_stream);
340 opj_destroy_codec(l_codec);
341 opj_image_destroy(l_image);
347 opj_stream_destroy(l_stream);
348 opj_destroy_codec(l_codec);
349 opj_image_destroy(l_image);