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)
154 opj_dparameters_t parameters; /* decompression parameters */
155 opj_image_t* image = NULL;
156 opj_stream_t *l_stream = NULL; /* Stream */
157 opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
158 opj_codestream_info_v2_t* cstr_info = NULL;
160 /* Index of corner tiles */
161 OPJ_UINT32 tile_ul = 0;
162 OPJ_UINT32 tile_ur = 0;
163 OPJ_UINT32 tile_lr = 0;
164 OPJ_UINT32 tile_ll = 0;
167 fprintf(stderr, "Usage: %s <input_file>\n", argv[0]);
171 /* Set decoding parameters to default values */
172 opj_set_default_decoder_parameters(¶meters);
174 strncpy(parameters.infile, argv[1], OPJ_PATH_LEN - 1);
177 /* decode the JPEG2000 stream */
178 /* -------------------------- */
179 parameters.decod_format = infile_format(parameters.infile);
181 switch(parameters.decod_format) {
182 case J2K_CFMT: /* JPEG-2000 codestream */
184 /* Get a decoder handle */
185 l_codec = opj_create_decompress(OPJ_CODEC_J2K);
188 case JP2_CFMT: /* JPEG 2000 compressed image data */
190 /* Get a decoder handle */
191 l_codec = opj_create_decompress(OPJ_CODEC_JP2);
194 case JPT_CFMT: /* JPEG 2000, JPIP */
196 /* Get a decoder handle */
197 l_codec = opj_create_decompress(OPJ_CODEC_JPT);
202 "Unrecognized format for input %s [accept only *.j2k, *.jp2, *.jpc or *.jpt]\n\n",
207 /* catch events using our callbacks and give a local context */
208 opj_set_info_handler(l_codec, info_callback,00);
209 opj_set_warning_handler(l_codec, warning_callback,00);
210 opj_set_error_handler(l_codec, error_callback,00);
212 l_stream = opj_stream_create_default_file_stream_v3(parameters.infile,1);
214 fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile);
218 /* Setup the decoder decoding parameters using user parameters */
219 if ( !opj_setup_decoder(l_codec, ¶meters) ){
220 fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
221 opj_stream_destroy_v3(l_stream);
222 opj_destroy_codec(l_codec);
226 /* Read the main header of the codestream and if necessary the JP2 boxes*/
227 if(! opj_read_header(l_stream, l_codec, &image)){
228 fprintf(stderr, "ERROR -> j2k_to_image: failed to read the header\n");
229 opj_stream_destroy_v3(l_stream);
230 opj_destroy_codec(l_codec);
231 opj_image_destroy(image);
235 /* Extract some info from the code stream */
236 cstr_info = opj_get_cstr_info(l_codec);
238 fprintf(stdout, "The file contains %dx%d tiles\n", cstr_info->tw, cstr_info->th);
241 tile_ur = cstr_info->tw - 1;
242 tile_lr = cstr_info->tw * cstr_info->th - 1;
243 tile_ll = tile_lr - cstr_info->tw;
245 #define TEST_TILE( tile_index ) \
246 fprintf(stdout, "Decoding tile %d ...\n", tile_index); \
247 if(!opj_get_decoded_tile(l_codec, l_stream, image, tile_index )){ \
248 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
249 opj_stream_destroy_v3(l_stream); \
250 opj_destroy_cstr_info(&cstr_info); \
251 opj_destroy_codec(l_codec); \
252 opj_image_destroy(image); \
253 return EXIT_FAILURE; \
255 for(index = 0; index < image->numcomps; ++index) { \
256 if( image->comps[index].data == NULL ){ \
257 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode tile %d\n", tile_index); \
258 opj_stream_destroy_v3(l_stream); \
259 opj_destroy_cstr_info(&cstr_info); \
260 opj_destroy_codec(l_codec); \
261 opj_image_destroy(image); \
262 return EXIT_FAILURE; \
265 fprintf(stdout, "Tile %d is decoded successfully\n", tile_index);
274 /* Close the byte stream */
275 opj_stream_destroy_v3(l_stream);
277 /* Destroy code stream info */
278 opj_destroy_cstr_info(&cstr_info);
280 /* Free remaining structures */
281 opj_destroy_codec(l_codec);
283 /* Free image data structure */
284 opj_image_destroy(image);