test to add root directory to modules
[openjpeg.git] / codec / j2k_to_image.c
1 /*
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "openjpeg.h"
35 #include "compat/getopt.h"
36 #include "convert.h"
37
38 #define J2K_CFMT 0
39 #define JP2_CFMT 1
40 #define JPT_CFMT 2
41 #define MJ2_CFMT 3
42 #define PXM_DFMT 0
43 #define PGX_DFMT 1
44 #define BMP_DFMT 2
45 #define YUV_DFMT 3
46
47 /* ----------------------------------------------------------------------- */
48
49 void decode_help_display() {
50   fprintf(stdout,"HELP\n----\n\n");
51   fprintf(stdout,"- the -h option displays this help information on screen\n\n");
52
53   fprintf(stdout,"List of parameters for the JPEG 2000 encoder:\n");
54   fprintf(stdout,"\n");
55   fprintf(stdout,"  -i <compressed file>\n");
56   fprintf(stdout,"    REQUIRED\n");
57   fprintf(stdout,"    Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
58   fprintf(stdout,"    is identified based on its suffix.\n");
59   fprintf(stdout,"  -o <decompressed file>\n");
60   fprintf(stdout,"    REQUIRED\n");
61   fprintf(stdout,"    Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
62   fprintf(stdout,"    BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
63   fprintf(stdout,"    filename is given, there will be as many output files as there are\n");
64   fprintf(stdout,"    components: an indice starting from 0 will then be appended to the\n");
65   fprintf(stdout,"    output filename, just before the \"pgx\" extension. If a PGM filename\n");
66   fprintf(stdout,"    is given and there are more than one component, only the first component\n");
67   fprintf(stdout,"    will be written to the file.\n");
68   fprintf(stdout,"  -r <reduce factor>\n");
69   fprintf(stdout,"    Set the number of highest resolution levels to be discarded. The\n");
70   fprintf(stdout,"    image resolution is effectively divided by 2 to the power of the\n");
71   fprintf(stdout,"    number of discarded levels. The reduce factor is limited by the\n");
72   fprintf(stdout,"    smallest total number of decomposition levels among tiles.\n");
73   fprintf(stdout,"  -l <number of quality layers to decode>\n");
74   fprintf(stdout,"    Set the maximum number of quality layers to decode. If there are\n");
75   fprintf(stdout,"    less quality layers than the specified number, all the quality layers\n");
76   fprintf(stdout,"    are decoded.\n");
77   fprintf(stdout,"\n");
78 }
79
80 /* -------------------------------------------------------------------------- */
81
82 int get_file_format(char *filename) {
83   int i;
84   static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2", "jpt" };
85   static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT };
86   char * ext = strrchr(filename, '.') + 1;
87   if(ext) {
88     for(i = 0; i < sizeof(format); i++) {
89       if(strnicmp(ext, extension[i], 3) == 0) {
90         return format[i];
91       }
92     }
93   }
94
95   return -1;
96 }
97
98 /* -------------------------------------------------------------------------- */
99
100 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) {
101   /* parse the command line */
102
103   while (1) {
104     int c = getopt(argc, argv, "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
105     if (c == -1)
106       break;
107     switch (c) {
108       case 'i':     /* input file */
109       {
110         char *infile = optarg;
111         parameters->decod_format = get_file_format(infile);
112         switch(parameters->decod_format) {
113           case J2K_CFMT:
114           case JP2_CFMT:
115           case JPT_CFMT:
116             break;
117           default:
118             fprintf(stderr, 
119               "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
120               infile);
121             return 1;
122             break;
123         }
124         strncpy(parameters->infile, infile, MAX_PATH);
125       }
126       break;
127         
128         /* ----------------------------------------------------- */
129
130       case 'o':     /* output file */
131       {
132         char *outfile = optarg;
133         parameters->cod_format = get_file_format(outfile);
134         switch(parameters->cod_format) {
135           case PGX_DFMT:
136           case PXM_DFMT:
137           case BMP_DFMT:
138             break;
139           default:
140             fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
141             return 1;
142             break;
143         }
144         strncpy(parameters->outfile, outfile, MAX_PATH);
145       }
146       break;
147       
148         /* ----------------------------------------------------- */
149       
150     
151       case 'r':   /* reduce option */
152       {
153         sscanf(optarg, "%d", &parameters->cp_reduce);
154       }
155       break;
156       
157         /* ----------------------------------------------------- */
158       
159
160       case 'l':   /* layering option */
161       {
162         sscanf(optarg, "%d", &parameters->cp_layer);
163       }
164       break;
165       
166         /* ----------------------------------------------------- */
167       
168       case 'h':       /* display an help description */
169       {
170         decode_help_display();
171         return 1;
172       }
173       break;
174             
175         /* ----------------------------------------------------- */
176       
177       default:
178         fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
179         break;
180     }
181   }
182
183   /* check for possible errors */
184
185   if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
186     fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -h for usage information\n");
187     return 1;
188   }
189
190   return 0;
191 }
192
193 /* -------------------------------------------------------------------------- */
194
195 /**
196 sample error callback expecting a FILE* client object
197 */
198 void error_callback(const char *msg, void *client_data) {
199   FILE *stream = (FILE*)client_data;
200   fprintf(stream, "[ERROR] %s", msg);
201 }
202 /**
203 sample warning callback expecting a FILE* client object
204 */
205 void warning_callback(const char *msg, void *client_data) {
206   FILE *stream = (FILE*)client_data;
207   fprintf(stream, "[WARNING] %s", msg);
208 }
209 /**
210 sample debug callback expecting no client object
211 */
212 void info_callback(const char *msg, void *client_data) {
213   fprintf(stdout, "[INFO] %s", msg);
214 }
215
216 /* -------------------------------------------------------------------------- */
217
218 int main(int argc, char **argv) {
219   opj_dparameters_t parameters; /* decompression parameters */
220   opj_event_mgr_t event_mgr;    /* event manager */
221   opj_image_t *image = NULL;
222   FILE *fsrc = NULL;
223   unsigned char *src = NULL; 
224   int file_length;
225
226   opj_dinfo_t* dinfo = NULL;  /* handle to a decompressor */
227   opj_cio_t *cio = NULL;
228
229   /* configure the event callbacks (not required) */
230   memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
231   event_mgr.error_handler = error_callback;
232   event_mgr.warning_handler = warning_callback;
233   event_mgr.info_handler = info_callback;
234
235   /* set decoding parameters to default values */
236   opj_set_default_decoder_parameters(&parameters);
237
238   /* parse input and get user decoding parameters */
239   if(parse_cmdline_decoder(argc, argv, &parameters) == 1) {
240     return 0;
241   }
242   
243   /* read the input file and put it in memory */
244   /* ---------------------------------------- */
245   fsrc = fopen(parameters.infile, "rb");
246   if (!fsrc) {
247     fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
248     return 1;
249   }  
250   fseek(fsrc, 0, SEEK_END);
251   file_length = ftell(fsrc);
252   fseek(fsrc, 0, SEEK_SET);
253   src = (unsigned char *) malloc(file_length);
254   fread(src, 1, file_length, fsrc);
255   fclose(fsrc);
256   
257   /* decode the code-stream */
258   /* ---------------------- */
259
260     switch(parameters.decod_format) {
261     case J2K_CFMT:
262     {
263       /* JPEG-2000 codestream */
264
265       /* get a decoder handle */
266       dinfo = opj_create_decompress(CODEC_J2K);
267       
268       /* catch events using our callbacks and give a local context */
269       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);     
270
271       /* setup the decoder decoding parameters using user parameters */
272       opj_setup_decoder(dinfo, &parameters);
273
274       /* open a byte stream */
275       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
276
277       /* decode the stream and fill the image structure */
278       image = opj_decode(dinfo, cio);
279       if(!image) {
280         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
281         opj_destroy_decompress(dinfo);
282         opj_cio_close(cio);
283         return 1;
284       }
285       
286       /* close the byte stream */
287       opj_cio_close(cio);
288     }
289     break;
290
291     case JP2_CFMT:
292     {
293       /* JPEG 2000 compressed image data */
294
295       /* get a decoder handle */
296       dinfo = opj_create_decompress(CODEC_JP2);
297       
298       /* catch events using our callbacks and give a local context */
299       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);     
300
301       /* setup the decoder decoding parameters using the current image and using user parameters */
302       opj_setup_decoder(dinfo, &parameters);
303
304       /* open a byte stream */
305       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
306
307       /* decode the stream and fill the image structure */
308       image = opj_decode(dinfo, cio);
309       if(!image) {
310         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
311         opj_destroy_decompress(dinfo);
312         opj_cio_close(cio);
313         return 1;
314       }
315
316       /* close the byte stream */
317       opj_cio_close(cio);
318
319     }
320     break;
321
322     case JPT_CFMT:
323     {
324       /* JPEG 2000, JPIP */
325
326       /* get a decoder handle */
327       dinfo = opj_create_decompress(CODEC_JPT);
328       
329       /* catch events using our callbacks and give a local context */
330       opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);     
331
332       /* setup the decoder decoding parameters using user parameters */
333       opj_setup_decoder(dinfo, &parameters);
334
335       /* open a byte stream */
336       cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
337
338       /* decode the stream and fill the image structure */
339       image = opj_decode(dinfo, cio);
340       if(!image) {
341         fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");        
342         opj_destroy_decompress(dinfo);
343         opj_cio_close(cio);
344         return 1;
345       } 
346
347       /* close the byte stream */
348       opj_cio_close(cio);
349     }
350     break;
351
352     default:
353       fprintf(stderr, "ERROR -> j2k_to_image : Unknown input image format\n");
354       return 1;
355       break;
356   }
357   
358   /* free the memory containing the code-stream */
359   free(src);
360   src = NULL;
361
362   /* create output image */
363   /* ------------------- */
364
365   switch (parameters.cod_format) {
366     case PXM_DFMT:      /* PNM PGM PPM */
367       imagetopnm(image, parameters.outfile);
368       break;
369             
370     case PGX_DFMT:      /* PGX */
371       imagetopgx(image, parameters.outfile);
372       break;
373     
374     case BMP_DFMT:      /* BMP */
375       imagetobmp(image, parameters.outfile);
376       break;
377   }
378
379   /* free remaining structures */
380   if(dinfo) {
381     opj_destroy_decompress(dinfo);
382   }
383
384   /* free image data structure */
385   opj_image_destroy(image);
386    
387   return 0;
388 }
389