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
8 * Copyright (c) 2006-2007, Parvatha Elangovan
9 * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #define USE_OPJ_DEPRECATED
41 #include "compat/getopt.h"
48 #define _stricmp strcasecmp
49 #define _strnicmp strncasecmp
52 /* ----------------------------------------------------------------------- */
66 /* ----------------------------------------------------------------------- */
68 typedef struct dircnt{
69 /** Buffer for holding images read from Directory*/
71 /** Pointer to the buffer*/
76 typedef struct img_folder{
77 /** The directory path of the folder containing input images*/
80 const char *out_format;
83 /** Enable Cod Format for output*/
88 void decode_help_display() {
89 fprintf(stdout,"HELP\n----\n\n");
90 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
93 fprintf(stdout,"List of parameters for the JPEG 2000 "
100 fprintf(stdout,"\n");
101 fprintf(stdout," -ImgDir \n");
102 fprintf(stdout," Image file Directory path \n");
103 fprintf(stdout," -i <compressed file>\n");
104 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
105 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
106 fprintf(stdout," is identified based on its suffix.\n");
107 fprintf(stdout,"\n");
110 /* -------------------------------------------------------------------------- */
112 int get_num_images(char *imgdirpath){
114 struct dirent* content;
117 /*Reading the input images from given input directory*/
119 dir= opendir(imgdirpath);
121 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
125 while((content=readdir(dir))!=NULL){
126 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
133 int load_images(dircnt_t *dirptr, char *imgdirpath){
135 struct dirent* content;
138 /*Reading the input images from given input directory*/
140 dir= opendir(imgdirpath);
142 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
145 fprintf(stderr,"Folder opened successfully\n");
148 while((content=readdir(dir))!=NULL){
149 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
152 strcpy(dirptr->filename[i],content->d_name);
158 int get_file_format(char *filename) {
160 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "j2k", "jp2", "jpt", "j2c", "jpc" };
161 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
162 char * ext = strrchr(filename, '.');
167 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
168 if(_strnicmp(ext, extension[i], 3) == 0) {
177 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
178 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
179 char *temp_p, temp1[OPJ_PATH_LEN]="";
181 strcpy(image_filename,dirptr->filename[imageno]);
182 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
183 parameters->decod_format = get_file_format(image_filename);
184 if (parameters->decod_format == -1)
186 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
187 strncpy(parameters->infile, infilename, sizeof(infilename));
190 strcpy(temp_ofname,strtok(image_filename,"."));
191 while((temp_p = strtok(NULL,".")) != NULL){
192 strcat(temp_ofname,temp1);
193 sprintf(temp1,".%s",temp_p);
195 if(img_fol->set_out_format==1){
196 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
197 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
202 /* -------------------------------------------------------------------------- */
203 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
204 /* parse the command line */
206 option_t long_option[]={
207 {"ImgDir",REQ_ARG, NULL ,'y'},
210 const char optlist[] = "i:h";
211 totlen=sizeof(long_option);
212 img_fol->set_out_format = 0;
214 int c = getopt_long(argc, argv,optlist,long_option,totlen);
218 case 'i': /* input file */
220 char *infile = optarg;
221 parameters->decod_format = get_file_format(infile);
222 switch(parameters->decod_format) {
229 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
233 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
237 /* ----------------------------------------------------- */
239 case 'h': /* display an help description */
240 decode_help_display();
243 /* ------------------------------------------------------ */
245 case 'y': /* Image Directory path */
247 img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
248 strcpy(img_fol->imgdirpath,optarg);
249 img_fol->set_imgdir=1;
253 /* ----------------------------------------------------- */
256 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
261 /* check for possible errors */
262 if(img_fol->set_imgdir==1){
263 if(!(parameters->infile[0]==0)){
264 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
267 if(img_fol->set_out_format == 0){
268 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
269 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
272 if(!((parameters->outfile[0] == 0))){
273 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
277 if((parameters->infile[0] == 0) ) {
278 fprintf(stderr, "Error: One of the options -i or -ImgDir must be specified\n");
279 fprintf(stderr, "usage: image_to_j2k -i *.j2k/jp2/j2c (+ options)\n");
287 /* -------------------------------------------------------------------------- */
290 sample error callback expecting a FILE* client object
292 void error_callback(const char *msg, void *client_data) {
293 FILE *stream = (FILE*)client_data;
294 fprintf(stream, "[ERROR] %s", msg);
297 sample warning callback expecting a FILE* client object
299 void warning_callback(const char *msg, void *client_data) {
300 FILE *stream = (FILE*)client_data;
301 fprintf(stream, "[WARNING] %s", msg);
304 sample debug callback expecting no client object
306 void info_callback(const char *msg, void *client_data) {
308 fprintf(stdout, "[INFO] %s", msg);
311 /* -------------------------------------------------------------------------- */
313 int main(int argc, char *argv[])
316 opj_dparameters_t parameters; /* decompression parameters */
318 opj_image_t *image = NULL;
324 opj_codec_t* dinfo = NULL; /* handle to a decompressor */
325 opj_stream_t *cio = NULL;
326 opj_codestream_info_t cstr_info; /* Codestream information structure */
327 char indexfilename[OPJ_PATH_LEN]; /* index file name */
328 OPJ_INT32 l_tile_x0,l_tile_y0;
329 OPJ_UINT32 l_tile_width,l_tile_height,l_nb_tiles_x,l_nb_tiles_y;
331 /* configure the event callbacks (not required) */
333 /* set decoding parameters to default values */
334 opj_set_default_decoder_parameters(¶meters);
336 /* Initialize indexfilename and img_fol */
338 memset(&img_fol,0,sizeof(img_fol_t));
340 /* parse input and get user encoding parameters */
341 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) {
345 /* Initialize reading of directory */
346 if(img_fol.set_imgdir==1){
347 num_images=get_num_images(img_fol.imgdirpath);
349 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
351 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names
352 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
354 if(!dirptr->filename_buf){
357 for(i=0;i<num_images;i++){
358 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
361 if(load_images(dirptr,img_fol.imgdirpath)==1){
365 fprintf(stdout,"Folder is empty\n");
372 /*Encoding image one by one*/
373 for(imageno = 0; imageno < num_images ; imageno++)
376 fprintf(stderr,"\n");
378 if(img_fol.set_imgdir==1){
379 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
380 fprintf(stderr,"skipping file...\n");
385 /* read the input file and put it in memory */
386 /* ---------------------------------------- */
387 fsrc = fopen(parameters.infile, "rb");
389 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
392 cio = opj_stream_create_default_file_stream(fsrc,true);
393 /* decode the code-stream */
394 /* ---------------------- */
396 switch (parameters.decod_format)
400 /* JPEG-2000 codestream */
402 /* get a decoder handle */
403 dinfo = opj_create_decompress(CODEC_J2K);
408 /* JPEG 2000 compressed image data */
409 /* get a decoder handle */
410 dinfo = opj_create_decompress(CODEC_JP2);
415 /* JPEG 2000, JPIP */
416 /* get a decoder handle */
417 dinfo = opj_create_decompress(CODEC_JPT);
421 fprintf(stderr, "skipping file..\n");
422 opj_stream_destroy(cio);
425 /* catch events using our callbacks and give a local context */
427 /* setup the decoder decoding parameters using user parameters */
428 opj_setup_decoder(dinfo, ¶meters);
430 /* decode the stream and fill the image structure */
431 /* if (*indexfilename) // If need to extract codestream information
432 image = opj_decode_with_info(dinfo, cio, &cstr_info);
435 bResult = opj_read_header(
445 //image = opj_decode(dinfo, cio);
446 //bResult = bResult && (image != 00);
447 //bResult = bResult && opj_end_decompress(dinfo,cio);
451 // fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
452 // opj_destroy_codec(dinfo);
453 // opj_stream_destroy(cio);
455 // return EXIT_FAILURE;
460 fprintf(stderr, "ERROR -> j2k_to_image: failed to read header\n");
463 j2k_dump_image(stdout, image);
466 //j2k_dump_cp(stdout, image, dinfo->m_codec);
468 /* close the byte stream */
469 opj_stream_destroy(cio);
471 /* Write the index to disk */
472 if (*indexfilename) {
474 bSuccess = write_index_file(&cstr_info, indexfilename);
476 fprintf(stderr, "Failed to output index file\n");
481 /* free remaining structures */
483 opj_destroy_codec(dinfo);
485 /* free codestream information structure */
487 opj_destroy_cstr_info(&cstr_info);
488 /* free image data structure */
489 opj_image_destroy(image);