removed old readme and Makefile
[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 #ifndef WIN32
39 #define stricmp strcasecmp
40 #define strnicmp strncasecmp
41 #endif
42
43 /* ----------------------------------------------------------------------- */
44
45 #define J2K_CFMT 0
46 #define JP2_CFMT 1
47 #define JPT_CFMT 2
48 #define MJ2_CFMT 3
49 #define PXM_DFMT 0
50 #define PGX_DFMT 1
51 #define BMP_DFMT 2
52 #define YUV_DFMT 3
53
54 /* ----------------------------------------------------------------------- */
55
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");
59
60         fprintf(stdout,"List of parameters for the JPEG 2000 encoder:\n");
61         fprintf(stdout,"\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");
84         fprintf(stdout,"\n");
85 }
86
87 /* -------------------------------------------------------------------------- */
88
89 int get_file_format(char *filename) {
90         int i;
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;
94         if(ext) {
95                 for(i = 0; i < sizeof(format); i++) {
96                         if(strnicmp(ext, extension[i], 3) == 0) {
97                                 return format[i];
98                         }
99                 }
100         }
101
102         return -1;
103 }
104
105 /* -------------------------------------------------------------------------- */
106
107 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters) {
108         /* parse the command line */
109
110         while (1) {
111                 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");
112                 if (c == -1)
113                         break;
114                 switch (c) {
115                         case 'i':                       /* input file */
116                         {
117                                 char *infile = optarg;
118                                 parameters->decod_format = get_file_format(infile);
119                                 switch(parameters->decod_format) {
120                                         case J2K_CFMT:
121                                         case JP2_CFMT:
122                                         case JPT_CFMT:
123                                                 break;
124                                         default:
125                                                 fprintf(stderr, 
126                                                         "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n", 
127                                                         infile);
128                                                 return 1;
129                                                 break;
130                                 }
131                                 strncpy(parameters->infile, infile, MAX_PATH);
132                         }
133                         break;
134                                 
135                                 /* ----------------------------------------------------- */
136
137                         case 'o':                       /* output file */
138                         {
139                                 char *outfile = optarg;
140                                 parameters->cod_format = get_file_format(outfile);
141                                 switch(parameters->cod_format) {
142                                         case PGX_DFMT:
143                                         case PXM_DFMT:
144                                         case BMP_DFMT:
145                                                 break;
146                                         default:
147                                                 fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp]!! \n", outfile);
148                                                 return 1;
149                                                 break;
150                                 }
151                                 strncpy(parameters->outfile, outfile, MAX_PATH);
152                         }
153                         break;
154                         
155                                 /* ----------------------------------------------------- */
156                         
157     
158                         case 'r':               /* reduce option */
159                         {
160                                 sscanf(optarg, "%d", &parameters->cp_reduce);
161                         }
162                         break;
163                         
164                                 /* ----------------------------------------------------- */
165       
166
167                         case 'l':               /* layering option */
168                         {
169                                 sscanf(optarg, "%d", &parameters->cp_layer);
170                         }
171                         break;
172                         
173                                 /* ----------------------------------------------------- */
174                         
175                         case 'h':                       /* display an help description */
176                         {
177                                 decode_help_display();
178                                 return 1;
179                         }
180                         break;
181             
182                                 /* ----------------------------------------------------- */
183                         
184                         default:
185                                 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
186                                 break;
187                 }
188         }
189
190         /* check for possible errors */
191
192         if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
193                 fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -h for usage information\n");
194                 return 1;
195         }
196
197         return 0;
198 }
199
200 /* -------------------------------------------------------------------------- */
201
202 /**
203 sample error callback expecting a FILE* client object
204 */
205 void error_callback(const char *msg, void *client_data) {
206         FILE *stream = (FILE*)client_data;
207         fprintf(stream, "[ERROR] %s", msg);
208 }
209 /**
210 sample warning callback expecting a FILE* client object
211 */
212 void warning_callback(const char *msg, void *client_data) {
213         FILE *stream = (FILE*)client_data;
214         fprintf(stream, "[WARNING] %s", msg);
215 }
216 /**
217 sample debug callback expecting no client object
218 */
219 void info_callback(const char *msg, void *client_data) {
220         fprintf(stdout, "[INFO] %s", msg);
221 }
222
223 /* -------------------------------------------------------------------------- */
224
225 int main(int argc, char **argv) {
226         opj_dparameters_t parameters;   /* decompression parameters */
227         opj_event_mgr_t event_mgr;              /* event manager */
228         opj_image_t *image = NULL;
229         FILE *fsrc = NULL;
230         unsigned char *src = NULL; 
231         int file_length;
232
233         opj_dinfo_t* dinfo = NULL;      /* handle to a decompressor */
234         opj_cio_t *cio = NULL;
235
236         /* configure the event callbacks (not required) */
237         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
238         event_mgr.error_handler = error_callback;
239         event_mgr.warning_handler = warning_callback;
240         event_mgr.info_handler = info_callback;
241
242         /* set decoding parameters to default values */
243         opj_set_default_decoder_parameters(&parameters);
244
245         /* parse input and get user decoding parameters */
246         if(parse_cmdline_decoder(argc, argv, &parameters) == 1) {
247                 return 0;
248         }
249         
250         /* read the input file and put it in memory */
251         /* ---------------------------------------- */
252         fsrc = fopen(parameters.infile, "rb");
253         if (!fsrc) {
254                 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
255                 return 1;
256         }  
257         fseek(fsrc, 0, SEEK_END);
258         file_length = ftell(fsrc);
259         fseek(fsrc, 0, SEEK_SET);
260         src = (unsigned char *) malloc(file_length);
261         fread(src, 1, file_length, fsrc);
262         fclose(fsrc);
263         
264         /* decode the code-stream */
265         /* ---------------------- */
266
267     switch(parameters.decod_format) {
268                 case J2K_CFMT:
269                 {
270                         /* JPEG-2000 codestream */
271
272                         /* get a decoder handle */
273                         dinfo = opj_create_decompress(CODEC_J2K);
274                         
275                         /* catch events using our callbacks and give a local context */
276                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);                   
277
278                         /* setup the decoder decoding parameters using user parameters */
279                         opj_setup_decoder(dinfo, &parameters);
280
281                         /* open a byte stream */
282                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
283
284                         /* decode the stream and fill the image structure */
285                         image = opj_decode(dinfo, cio);
286                         if(!image) {
287                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
288                                 opj_destroy_decompress(dinfo);
289                                 opj_cio_close(cio);
290                                 return 1;
291                         }
292                         
293                         /* close the byte stream */
294                         opj_cio_close(cio);
295                 }
296                 break;
297
298                 case JP2_CFMT:
299                 {
300                         /* JPEG 2000 compressed image data */
301
302                         /* get a decoder handle */
303                         dinfo = opj_create_decompress(CODEC_JP2);
304                         
305                         /* catch events using our callbacks and give a local context */
306                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);                   
307
308                         /* setup the decoder decoding parameters using the current image and using user parameters */
309                         opj_setup_decoder(dinfo, &parameters);
310
311                         /* open a byte stream */
312                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
313
314                         /* decode the stream and fill the image structure */
315                         image = opj_decode(dinfo, cio);
316                         if(!image) {
317                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
318                                 opj_destroy_decompress(dinfo);
319                                 opj_cio_close(cio);
320                                 return 1;
321                         }
322
323                         /* close the byte stream */
324                         opj_cio_close(cio);
325
326                 }
327                 break;
328
329                 case JPT_CFMT:
330                 {
331                         /* JPEG 2000, JPIP */
332
333                         /* get a decoder handle */
334                         dinfo = opj_create_decompress(CODEC_JPT);
335                         
336                         /* catch events using our callbacks and give a local context */
337                         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);                   
338
339                         /* setup the decoder decoding parameters using user parameters */
340                         opj_setup_decoder(dinfo, &parameters);
341
342                         /* open a byte stream */
343                         cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
344
345                         /* decode the stream and fill the image structure */
346                         image = opj_decode(dinfo, cio);
347                         if(!image) {
348                                 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");                            
349                                 opj_destroy_decompress(dinfo);
350                                 opj_cio_close(cio);
351                                 return 1;
352                         }       
353
354                         /* close the byte stream */
355                         opj_cio_close(cio);
356                 }
357                 break;
358
359                 default:
360                         fprintf(stderr, "ERROR -> j2k_to_image : Unknown input image format\n");
361                         return 1;
362                         break;
363         }
364   
365         /* free the memory containing the code-stream */
366         free(src);
367         src = NULL;
368
369         /* create output image */
370         /* ------------------- */
371
372         switch (parameters.cod_format) {
373                 case PXM_DFMT:                  /* PNM PGM PPM */
374                         imagetopnm(image, parameters.outfile);
375                         break;
376             
377                 case PGX_DFMT:                  /* PGX */
378                         imagetopgx(image, parameters.outfile);
379                         break;
380                 
381                 case BMP_DFMT:                  /* BMP */
382                         imagetobmp(image, parameters.outfile);
383                         break;
384         }
385
386         /* free remaining structures */
387         if(dinfo) {
388                 opj_destroy_decompress(dinfo);
389         }
390
391         /* free image data structure */
392         opj_image_destroy(image);
393    
394         return 0;
395 }
396