2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2006-2007, Parvatha Elangovan
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 #include "opj_config.h"
43 #define _stricmp strcasecmp
44 #define _strnicmp strncasecmp
48 #include "format_defs.h"
50 /* -------------------------------------------------------------------------- */
51 int get_file_format(const char *filename) {
53 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
54 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 };
55 char * ext = strrchr(filename, '.');
60 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
61 if(_strnicmp(ext, extension[i], 3) == 0) {
70 /* -------------------------------------------------------------------------- */
73 sample error callback expecting a FILE* client object
75 void error_callback(const char *msg, void *client_data) {
76 FILE *stream = (FILE*)client_data;
77 fprintf(stream, "[ERROR] %s", msg);
80 sample warning callback expecting a FILE* client object
82 void warning_callback(const char *msg, void *client_data) {
83 FILE *stream = (FILE*)client_data;
84 fprintf(stream, "[WARNING] %s", msg);
87 sample debug callback expecting no client object
89 void info_callback(const char *msg, void *client_data) {
91 fprintf(stdout, "[INFO] %s", msg);
95 /* -------------------------------------------------------------------------- */
96 #define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
97 #define JP2_MAGIC "\x0d\x0a\x87\x0a"
98 /* position 45: "\xff\x52" */
99 #define J2K_CODESTREAM_MAGIC "\xff\x4f\xff\x51"
101 static int infile_format(const char *fname)
104 const char *s, *magic_s;
105 int ext_format, magic_format;
106 unsigned char buf[12];
107 unsigned int l_nb_read;
109 reader = fopen(fname, "rb");
115 l_nb_read = fread(buf, 1, 12, reader);
122 ext_format = get_file_format(fname);
124 if (ext_format == JPT_CFMT)
127 if (memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0 || memcmp(buf, JP2_MAGIC, 4) == 0) {
128 magic_format = JP2_CFMT;
131 else if (memcmp(buf, J2K_CODESTREAM_MAGIC, 4) == 0) {
132 magic_format = J2K_CFMT;
133 magic_s = ".j2k or .jpc or .j2c";
138 if (magic_format == ext_format)
141 s = fname + strlen(fname) - 4;
143 fputs("\n===========================================\n", stderr);
144 fprintf(stderr, "The extension of this file is incorrect.\n"
145 "FOUND %s. SHOULD BE %s\n", s, magic_s);
146 fputs("===========================================\n", stderr);
151 /* -------------------------------------------------------------------------- */
153 * J2K_RANDOM_TILE_ACCESS MAIN
155 /* -------------------------------------------------------------------------- */
156 int main(int argc, char **argv)
160 opj_dparameters_t parameters; /* decompression parameters */
161 opj_event_mgr_t event_mgr; /* event manager */
162 opj_image_t* image = NULL;
163 opj_stream_t *cio = NULL; /* Stream */
164 opj_codec_t* dinfo = NULL; /* Handle to a decompressor */
165 opj_codestream_info_v2_t* cstr_info = NULL;
167 /* Index of corner tiles */
168 OPJ_UINT32 tile_ul = 0;
169 OPJ_UINT32 tile_ur = 0;
170 OPJ_UINT32 tile_lr = 0;
171 OPJ_UINT32 tile_ll = 0;
174 fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
179 event_mgr.error_handler = error_callback;
180 event_mgr.warning_handler = warning_callback;
181 event_mgr.info_handler = info_callback;
182 opj_initialize_default_event_handler(&event_mgr, 1);
184 /* Set decoding parameters to default values */
185 opj_set_default_decoder_parameters(¶meters);
187 strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);
189 /* read the input file */
190 /* ------------------- */
191 fsrc = fopen(parameters.infile, "rb");
193 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
197 /* decode the JPEG2000 stream */
198 /* -------------------------- */
199 parameters.decod_format = infile_format(parameters.infile);
201 switch(parameters.decod_format) {
202 case J2K_CFMT: /* JPEG-2000 codestream */
204 /* Get a decoder handle */
205 dinfo = opj_create_decompress_v2(CODEC_J2K);
208 case JP2_CFMT: /* JPEG 2000 compressed image data */
210 /* Get a decoder handle */
211 dinfo = opj_create_decompress_v2(CODEC_JP2);
214 case JPT_CFMT: /* JPEG 2000, JPIP */
216 /* Get a decoder handle */
217 dinfo = opj_create_decompress_v2(CODEC_JPT);
222 "Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
227 cio = opj_stream_create_default_file_stream(fsrc,1);
230 fprintf(stderr, "ERROR -> failed to create the stream from the file\n");
234 /* Setup the decoder decoding parameters using user parameters */
235 if ( !opj_setup_decoder_v2(dinfo, ¶meters, &event_mgr) ){
236 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
237 opj_stream_destroy(cio);
239 opj_destroy_codec(dinfo);
243 /* Read the main header of the codestream and if necessary the JP2 boxes*/
244 if(! opj_read_header(cio, dinfo, &image)){
245 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
246 opj_stream_destroy(cio);
248 opj_destroy_codec(dinfo);
249 opj_image_destroy(image);
253 /* Extract some info from the code stream */
254 cstr_info = opj_get_cstr_info(dinfo);
256 fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);
259 tile_ur = cstr_info->tw - 1;
260 tile_lr = cstr_info->tw * cstr_info->th - 1;
261 tile_ll = tile_lr - cstr_info->tw;
263 #define TEST_TILE( tile_index ) \
264 fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
265 if(!opj_get_decoded_tile(dinfo, cio, image, tile_index )){ \
266 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
267 opj_stream_destroy(cio); \
268 opj_destroy_cstr_info_v2(&cstr_info); \
269 opj_destroy_codec(dinfo); \
270 opj_image_destroy(image); \
272 return EXIT_FAILURE; \
274 fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);
283 /* Close the byte stream */
284 opj_stream_destroy(cio);
286 /* Destroy code stream info */
287 opj_destroy_cstr_info_v2(&cstr_info);
289 /* Free remaining structures */
290 opj_destroy_codec(dinfo);
292 /* Free image data structure */
293 opj_image_destroy(image);
295 /* Close the input file */