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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
36 #include "compat/getopt.h"
40 #define stricmp strcasecmp
41 #define strnicmp strncasecmp
44 /* ----------------------------------------------------------------------- */
55 /* ----------------------------------------------------------------------- */
57 void decode_help_display() {
58 fprintf(stdout,"HELP\n----\n\n");
59 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
62 fprintf(stdout,"List of parameters for the JPEG 2000 "
69 fprintf(stdout," -i <compressed file>\n");
70 fprintf(stdout," REQUIRED\n");
71 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
72 fprintf(stdout," is identified based on its suffix.\n");
73 fprintf(stdout," -o <decompressed file>\n");
74 fprintf(stdout," REQUIRED\n");
75 fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
76 fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
77 fprintf(stdout," filename is given, there will be as many output files as there are\n");
78 fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
79 fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
80 fprintf(stdout," is given and there are more than one component, only the first component\n");
81 fprintf(stdout," will be written to the file.\n");
82 fprintf(stdout," -r <reduce factor>\n");
83 fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
84 fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
85 fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
86 fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
87 fprintf(stdout," -l <number of quality layers to decode>\n");
88 fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
89 fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
90 fprintf(stdout," are decoded.\n");
93 fprintf(stdout," -W <options>\n");
94 fprintf(stdout," Activates the JPWL correction capability, if the codestream complies.\n");
95 fprintf(stdout," Options can be a comma separated list of <param=val> tokens:\n");
96 fprintf(stdout," c, c=numcomps\n");
97 fprintf(stdout," numcomps is the number of expected components in the codestream\n");
98 fprintf(stdout," (search of first EPB rely upon this, default is %d)\n", JPWL_EXPECTED_COMPONENTS);
101 fprintf(stdout,"\n");
104 /* -------------------------------------------------------------------------- */
106 int get_file_format(char *filename) {
108 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2", "jpt" };
109 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT };
110 char * ext = strrchr(filename, '.') + 1;
112 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
113 if(strnicmp(ext, extension[i], 3) == 0) {
122 /* -------------------------------------------------------------------------- */
124 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) {
125 /* parse the command line */
128 const char optlist[] = "i:o:r:l:h"
132 #endif /* USE_JPWL */
137 int c = getopt(argc, argv, optlist); /* >>JPWL<< */
141 case 'i': /* input file */
143 char *infile = optarg;
144 parameters->decod_format = get_file_format(infile);
145 switch(parameters->decod_format) {
152 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
156 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
160 /* ----------------------------------------------------- */
162 case 'o': /* output file */
164 char *outfile = optarg;
165 parameters->cod_format = get_file_format(outfile);
166 switch(parameters->cod_format) {
172 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
175 strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
179 /* ----------------------------------------------------- */
182 case 'r': /* reduce option */
184 sscanf(optarg, "%d", ¶meters->cp_reduce);
188 /* ----------------------------------------------------- */
191 case 'l': /* layering option */
193 sscanf(optarg, "%d", ¶meters->cp_layer);
197 /* ----------------------------------------------------- */
199 case 'h': /* display an help description */
200 decode_help_display();
205 /* ----------------------------------------------------- */
207 case 'W': /* activate JPWL correction */
211 token = strtok(optarg, ",");
212 while(token != NULL) {
214 /* search expected number of components */
219 compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */
221 if(sscanf(token, "c=%d", &compno) == 1) {
223 if ((compno < 1) || (compno > 256)) {
224 fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno);
227 parameters->jpwl_exp_comps = compno;
229 } else if (!strcmp(token, "c")) {
231 parameters->jpwl_exp_comps = compno; /* auto for default size */
234 fprintf(stderr, "ERROR -> invalid components specified = %s\n", token);
239 /* search maximum number of tiles */
244 tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */
246 if(sscanf(token, "t=%d", &tileno) == 1) {
248 if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) {
249 fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno);
252 parameters->jpwl_max_tiles = tileno;
254 } else if (!strcmp(token, "t")) {
256 parameters->jpwl_max_tiles = tileno; /* auto for default size */
259 fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token);
264 /* next token or bust */
265 token = strtok(NULL, ",");
267 parameters->jpwl_correct = true;
268 fprintf(stdout, "JPWL correction capability activated\n");
269 fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps);
272 #endif /* USE_JPWL */
275 /* ----------------------------------------------------- */
278 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
283 /* check for possible errors */
285 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
286 fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -h for usage information\n");
293 /* -------------------------------------------------------------------------- */
296 sample error callback expecting a FILE* client object
298 void error_callback(const char *msg, void *client_data) {
299 FILE *stream = (FILE*)client_data;
300 fprintf(stream, "[ERROR] %s", msg);
303 sample warning callback expecting a FILE* client object
305 void warning_callback(const char *msg, void *client_data) {
306 FILE *stream = (FILE*)client_data;
307 fprintf(stream, "[WARNING] %s", msg);
310 sample debug callback expecting no client object
312 void info_callback(const char *msg, void *client_data) {
314 fprintf(stdout, "[INFO] %s", msg);
317 /* -------------------------------------------------------------------------- */
319 int main(int argc, char **argv) {
320 opj_dparameters_t parameters; /* decompression parameters */
321 opj_event_mgr_t event_mgr; /* event manager */
322 opj_image_t *image = NULL;
324 unsigned char *src = NULL;
327 opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
328 opj_cio_t *cio = NULL;
330 /* configure the event callbacks (not required) */
331 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
332 event_mgr.error_handler = error_callback;
333 event_mgr.warning_handler = warning_callback;
334 event_mgr.info_handler = info_callback;
336 /* set decoding parameters to default values */
337 opj_set_default_decoder_parameters(¶meters);
339 /* parse input and get user decoding parameters */
340 if(parse_cmdline_decoder(argc, argv, ¶meters) == 1) {
344 /* read the input file and put it in memory */
345 /* ---------------------------------------- */
346 fsrc = fopen(parameters.infile, "rb");
348 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
351 fseek(fsrc, 0, SEEK_END);
352 file_length = ftell(fsrc);
353 fseek(fsrc, 0, SEEK_SET);
354 src = (unsigned char *) malloc(file_length);
355 fread(src, 1, file_length, fsrc);
358 /* decode the code-stream */
359 /* ---------------------- */
361 switch(parameters.decod_format) {
364 /* JPEG-2000 codestream */
366 /* get a decoder handle */
367 dinfo = opj_create_decompress(CODEC_J2K);
369 /* catch events using our callbacks and give a local context */
370 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
372 /* setup the decoder decoding parameters using user parameters */
373 opj_setup_decoder(dinfo, ¶meters);
375 /* open a byte stream */
376 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
378 /* decode the stream and fill the image structure */
379 image = opj_decode(dinfo, cio);
381 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
382 opj_destroy_decompress(dinfo);
387 /* close the byte stream */
394 /* JPEG 2000 compressed image data */
396 /* get a decoder handle */
397 dinfo = opj_create_decompress(CODEC_JP2);
399 /* catch events using our callbacks and give a local context */
400 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
402 /* setup the decoder decoding parameters using the current image and user parameters */
403 opj_setup_decoder(dinfo, ¶meters);
405 /* open a byte stream */
406 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
408 /* decode the stream and fill the image structure */
409 image = opj_decode(dinfo, cio);
411 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
412 opj_destroy_decompress(dinfo);
417 /* close the byte stream */
425 /* JPEG 2000, JPIP */
427 /* get a decoder handle */
428 dinfo = opj_create_decompress(CODEC_JPT);
430 /* catch events using our callbacks and give a local context */
431 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
433 /* setup the decoder decoding parameters using user parameters */
434 opj_setup_decoder(dinfo, ¶meters);
436 /* open a byte stream */
437 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
439 /* decode the stream and fill the image structure */
440 image = opj_decode(dinfo, cio);
442 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
443 opj_destroy_decompress(dinfo);
448 /* close the byte stream */
454 fprintf(stderr, "ERROR -> j2k_to_image : Unknown input image format\n");
458 /* free the memory containing the code-stream */
462 /* create output image */
463 /* ------------------- */
465 switch (parameters.cod_format) {
466 case PXM_DFMT: /* PNM PGM PPM */
467 imagetopnm(image, parameters.outfile);
470 case PGX_DFMT: /* PGX */
471 imagetopgx(image, parameters.outfile);
474 case BMP_DFMT: /* BMP */
475 imagetobmp(image, parameters.outfile);
479 /* free remaining structures */
481 opj_destroy_decompress(dinfo);
484 /* free image data structure */
485 opj_image_destroy(image);