2 * Copyright (c) 2001-2003, David Janssens
3 * Copyright (c) 2002-2003, Yannick Verschueren
4 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
5 * Copyright (c) 2005, Hervé Drolon, FreeImage Team
6 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
35 #include "compat/getopt.h"
39 #define stricmp strcasecmp
40 #define strnicmp strncasecmp
43 /* ----------------------------------------------------------------------- */
54 /* ----------------------------------------------------------------------- */
56 void decode_help_display() {
57 fprintf(stdout,"HELP\n----\n\n");
58 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
60 fprintf(stdout,"List of parameters for the JPEG 2000 encoder:\n");
62 fprintf(stdout," -i <compressed file>\n");
63 fprintf(stdout," REQUIRED\n");
64 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
65 fprintf(stdout," is identified based on its suffix.\n");
66 fprintf(stdout," -o <decompressed file>\n");
67 fprintf(stdout," REQUIRED\n");
68 fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
69 fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
70 fprintf(stdout," filename is given, there will be as many output files as there are\n");
71 fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
72 fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
73 fprintf(stdout," is given and there are more than one component, only the first component\n");
74 fprintf(stdout," will be written to the file.\n");
75 fprintf(stdout," -r <reduce factor>\n");
76 fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
77 fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
78 fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
79 fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
80 fprintf(stdout," -l <number of quality layers to decode>\n");
81 fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
82 fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
83 fprintf(stdout," are decoded.\n");
87 /* -------------------------------------------------------------------------- */
89 int get_file_format(char *filename) {
91 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2", "jpt" };
92 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT };
93 char * ext = strrchr(filename, '.') + 1;
95 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
96 if(strnicmp(ext, extension[i], 3) == 0) {
105 /* -------------------------------------------------------------------------- */
107 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) {
108 /* parse the command line */
111 int c = getopt(argc, argv, "i:o:r:q:f:t:n:c:b:x:p:s:d:hP:S:E:M:R:T:C:I");
115 case 'i': /* input file */
117 char *infile = optarg;
118 parameters->decod_format = get_file_format(infile);
119 switch(parameters->decod_format) {
126 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
130 strncpy(parameters->infile, infile, MAX_PATH);
134 /* ----------------------------------------------------- */
136 case 'o': /* output file */
138 char *outfile = optarg;
139 parameters->cod_format = get_file_format(outfile);
140 switch(parameters->cod_format) {
146 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
149 strncpy(parameters->outfile, outfile, MAX_PATH);
153 /* ----------------------------------------------------- */
156 case 'r': /* reduce option */
158 sscanf(optarg, "%d", ¶meters->cp_reduce);
162 /* ----------------------------------------------------- */
165 case 'l': /* layering option */
167 sscanf(optarg, "%d", ¶meters->cp_layer);
171 /* ----------------------------------------------------- */
173 case 'h': /* display an help description */
174 decode_help_display();
177 /* ----------------------------------------------------- */
180 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
185 /* check for possible errors */
187 if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
188 fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -h for usage information\n");
195 /* -------------------------------------------------------------------------- */
198 sample error callback expecting a FILE* client object
200 void error_callback(const char *msg, void *client_data) {
201 FILE *stream = (FILE*)client_data;
202 fprintf(stream, "[ERROR] %s", msg);
205 sample warning callback expecting a FILE* client object
207 void warning_callback(const char *msg, void *client_data) {
208 FILE *stream = (FILE*)client_data;
209 fprintf(stream, "[WARNING] %s", msg);
212 sample debug callback expecting no client object
214 void info_callback(const char *msg, void *client_data) {
216 fprintf(stdout, "[INFO] %s", msg);
219 /* -------------------------------------------------------------------------- */
221 int main(int argc, char **argv) {
222 opj_dparameters_t parameters; /* decompression parameters */
223 opj_event_mgr_t event_mgr; /* event manager */
224 opj_image_t *image = NULL;
226 unsigned char *src = NULL;
229 opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
230 opj_cio_t *cio = NULL;
232 /* configure the event callbacks (not required) */
233 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
234 event_mgr.error_handler = error_callback;
235 event_mgr.warning_handler = warning_callback;
236 event_mgr.info_handler = info_callback;
238 /* set decoding parameters to default values */
239 opj_set_default_decoder_parameters(¶meters);
241 /* parse input and get user decoding parameters */
242 if(parse_cmdline_decoder(argc, argv, ¶meters) == 1) {
246 /* read the input file and put it in memory */
247 /* ---------------------------------------- */
248 fsrc = fopen(parameters.infile, "rb");
250 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
253 fseek(fsrc, 0, SEEK_END);
254 file_length = ftell(fsrc);
255 fseek(fsrc, 0, SEEK_SET);
256 src = (unsigned char *) malloc(file_length);
257 fread(src, 1, file_length, fsrc);
260 /* decode the code-stream */
261 /* ---------------------- */
263 switch(parameters.decod_format) {
266 /* JPEG-2000 codestream */
268 /* get a decoder handle */
269 dinfo = opj_create_decompress(CODEC_J2K);
271 /* catch events using our callbacks and give a local context */
272 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
274 /* setup the decoder decoding parameters using user parameters */
275 opj_setup_decoder(dinfo, ¶meters);
277 /* open a byte stream */
278 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
280 /* decode the stream and fill the image structure */
281 image = opj_decode(dinfo, cio);
283 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
284 opj_destroy_decompress(dinfo);
289 /* close the byte stream */
296 /* JPEG 2000 compressed image data */
298 /* get a decoder handle */
299 dinfo = opj_create_decompress(CODEC_JP2);
301 /* catch events using our callbacks and give a local context */
302 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
304 /* setup the decoder decoding parameters using the current image and using user parameters */
305 opj_setup_decoder(dinfo, ¶meters);
307 /* open a byte stream */
308 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
310 /* decode the stream and fill the image structure */
311 image = opj_decode(dinfo, cio);
313 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
314 opj_destroy_decompress(dinfo);
319 /* close the byte stream */
327 /* JPEG 2000, JPIP */
329 /* get a decoder handle */
330 dinfo = opj_create_decompress(CODEC_JPT);
332 /* catch events using our callbacks and give a local context */
333 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
335 /* setup the decoder decoding parameters using user parameters */
336 opj_setup_decoder(dinfo, ¶meters);
338 /* open a byte stream */
339 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
341 /* decode the stream and fill the image structure */
342 image = opj_decode(dinfo, cio);
344 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
345 opj_destroy_decompress(dinfo);
350 /* close the byte stream */
356 fprintf(stderr, "ERROR -> j2k_to_image : Unknown input image format\n");
360 /* free the memory containing the code-stream */
364 /* create output image */
365 /* ------------------- */
367 switch (parameters.cod_format) {
368 case PXM_DFMT: /* PNM PGM PPM */
369 imagetopnm(image, parameters.outfile);
372 case PGX_DFMT: /* PGX */
373 imagetopgx(image, parameters.outfile);
376 case BMP_DFMT: /* BMP */
377 imagetobmp(image, parameters.outfile);
381 /* free remaining structures */
383 opj_destroy_decompress(dinfo);
386 /* free image data structure */
387 opj_image_destroy(image);