2 * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
3 * Copyright (c) 2012, CS Systemes d'Information, France
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
27 #include "opj_config.h"
38 #define _stricmp strcasecmp
39 #define _strnicmp strncasecmp
43 #include "format_defs.h"
45 /* -------------------------------------------------------------------------- */
46 static int get_file_format(const char *filename) {
48 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
49 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
50 char * ext = strrchr(filename, '.');
55 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
56 if(_strnicmp(ext, extension[i], 3) == 0) {
65 /* -------------------------------------------------------------------------- */
68 sample error callback expecting a FILE* client object
70 static void error_callback(const char *msg, void *client_data) {
72 fprintf(stdout, "[ERROR] %s", msg);
75 sample warning callback expecting a FILE* client object
77 static void warning_callback(const char *msg, void *client_data) {
79 fprintf(stdout, "[WARNING] %s", msg);
82 sample debug callback expecting no client object
84 static void info_callback(const char *msg, void *client_data) {
86 fprintf(stdout, "[INFO] %s", msg);
90 /* -------------------------------------------------------------------------- */
91 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
92 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
93 /* position 45: "\xff\x52" */
94 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
96 static int infile_format(const char *fname)
99 const char *s, *magic_s;
100 int ext_format, magic_format;
101 unsigned char buf[12];
102 OPJ_SIZE_T l_nb_read;
104 reader = fopen(fname, "rb");
110 l_nb_read = fread(buf, 1, 12, reader);
117 ext_format = get_file_format(fname);
119 if (ext_format == JPT_CFMT)
122 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
123 magic_format = JP2_CFMT;
126 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
127 magic_format = J2K_CFMT;
128 magic_s = ".j2k or .jpc or .j2c";
133 if (magic_format == ext_format)
136 s = fname + strlen(fname) - 4;
138 fputs("\n===========================================\n", stderr);
139 fprintf(stderr, "The extension of this file is incorrect.\n"
140 "FOUND %s. SHOULD BE %s\n", s, magic_s);
141 fputs("===========================================\n", stderr);
146 /* -------------------------------------------------------------------------- */
148 * J2K_RANDOM_TILE_ACCESS MAIN
150 /* -------------------------------------------------------------------------- */
151 int main(int argc, char **argv)
156 opj_dparameters_t parameters; /* decompression parameters */
157 opj_image_t* image = NULL;
158 opj_stream_t *l_stream = NULL; /* Stream */
159 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
160 opj_codestream_info_v2_t* cstr_info = NULL;
162 /* Index of corner tiles */
163 OPJ_UINT32 tile_ul = 0;
164 OPJ_UINT32 tile_ur = 0;
165 OPJ_UINT32 tile_lr = 0;
166 OPJ_UINT32 tile_ll = 0;
169 fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
173 /* Set decoding parameters to default values */
174 opj_set_default_decoder_parameters(¶meters);
176 strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);
178 /* read the input file */
179 /* ------------------- */
180 fsrc = fopen(parameters.infile, "rb");
182 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
186 /* decode the JPEG2000 stream */
187 /* -------------------------- */
188 parameters.decod_format = infile_format(parameters.infile);
190 switch(parameters.decod_format) {
191 case J2K_CFMT: /* JPEG-2000 codestream */
193 /* Get a decoder handle */
194 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
197 case JP2_CFMT: /* JPEG 2000 compressed image data */
199 /* Get a decoder handle */
200 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
203 case JPT_CFMT: /* JPEG 2000, JPIP */
205 /* Get a decoder handle */
206 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
211 "Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
216 /* catch events using our callbacks and give a local context */
217 opj_set_info_handler(l_codec, info_callback,00);
218 opj_set_warning_handler(l_codec, warning_callback,00);
219 opj_set_error_handler(l_codec, error_callback,00);
221 l_stream = opj_stream_create_default_file_stream(fsrc,1);
224 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
228 /* Setup the decoder decoding parameters using user parameters */
229 if ( !opj_setup_decoder(l_codec, ¶meters) ){
230 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
231 opj_stream_destroy(l_stream);
233 opj_destroy_codec(l_codec);
237 /* Read the main header of the codestream and if necessary the JP2 boxes*/
238 if(! opj_read_header(l_stream, l_codec, &image)){
239 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
240 opj_stream_destroy(l_stream);
242 opj_destroy_codec(l_codec);
243 opj_image_destroy(image);
247 /* Extract some info from the code stream */
248 cstr_info = opj_get_cstr_info(l_codec);
250 fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);
253 tile_ur = cstr_info->tw - 1;
254 tile_lr = cstr_info->tw * cstr_info->th - 1;
255 tile_ll = tile_lr - cstr_info->tw;
257 #define TEST_TILE( tile_index ) \
258 fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
259 if(!opj_get_decoded_tile(l_codec, l_stream, image, tile_index )){ \
260 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
261 opj_stream_destroy(l_stream); \
262 opj_destroy_cstr_info(&cstr_info); \
263 opj_destroy_codec(l_codec); \
264 opj_image_destroy(image); \
266 return EXIT_FAILURE; \
268 for(index = 0; index < image->numcomps; ++index) { \
269 if( image->comps[index].data == NULL ){ \
270 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
271 opj_stream_destroy(l_stream); \
272 opj_destroy_cstr_info(&cstr_info); \
273 opj_destroy_codec(l_codec); \
274 opj_image_destroy(image); \
276 return EXIT_FAILURE; \
279 fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);
288 /* Close the byte stream */
289 opj_stream_destroy(l_stream);
291 /* Destroy code stream info */
292 opj_destroy_cstr_info(&cstr_info);
294 /* Free remaining structures */
295 opj_destroy_codec(l_codec);
297 /* Free image data structure */
298 opj_image_destroy(image);
300 /* Close the input file */