2 * Copyright (c) 20010, Mathieu Malaterre, GDCM
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
35 #include "compat/getopt.h"
42 #define _stricmp strcasecmp
43 #define _strnicmp strncasecmp
46 /* ----------------------------------------------------------------------- */
60 /* ----------------------------------------------------------------------- */
62 typedef struct dircnt{
63 /** Buffer for holding images read from Directory*/
65 /** Pointer to the buffer*/
70 typedef struct img_folder{
71 /** The directory path of the folder containing input images*/
74 const char *out_format;
77 /** Enable Cod Format for output*/
82 void decode_help_display() {
83 fprintf(stdout,"HELP\n----\n\n");
84 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
87 fprintf(stdout,"List of parameters for the JPEG 2000 "
95 fprintf(stdout," -ImgDir \n");
96 fprintf(stdout," Image file Directory path \n");
97 fprintf(stdout," -i <compressed file>\n");
98 fprintf(stdout," REQUIRED only if an Input image directory not specified\n");
99 fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
100 fprintf(stdout," is identified based on its suffix.\n");
101 fprintf(stdout,"\n");
104 /* -------------------------------------------------------------------------- */
106 int get_num_images(char *imgdirpath){
108 struct dirent* content;
111 /*Reading the input images from given input directory*/
113 dir= opendir(imgdirpath);
115 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
119 while((content=readdir(dir))!=NULL){
120 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
127 int load_images(dircnt_t *dirptr, char *imgdirpath){
129 struct dirent* content;
132 /*Reading the input images from given input directory*/
134 dir= opendir(imgdirpath);
136 fprintf(stderr,"Could not open Folder %s\n",imgdirpath);
139 fprintf(stderr,"Folder opened successfully\n");
142 while((content=readdir(dir))!=NULL){
143 if(strcmp(".",content->d_name)==0 || strcmp("..",content->d_name)==0 )
146 strcpy(dirptr->filename[i],content->d_name);
152 int get_file_format(char *filename) {
154 static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc" };
155 static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
156 char * ext = strrchr(filename, '.');
161 for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
162 if(_strnicmp(ext, extension[i], 3) == 0) {
171 char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
172 char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
173 char *temp_p, temp1[OPJ_PATH_LEN]="";
175 strcpy(image_filename,dirptr->filename[imageno]);
176 fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
177 parameters->decod_format = get_file_format(image_filename);
178 if (parameters->decod_format == -1)
180 sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
181 strncpy(parameters->infile, infilename, sizeof(infilename));
184 strcpy(temp_ofname,strtok(image_filename,"."));
185 while((temp_p = strtok(NULL,".")) != NULL){
186 strcat(temp_ofname,temp1);
187 sprintf(temp1,".%s",temp_p);
189 if(img_fol->set_out_format==1){
190 sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
191 strncpy(parameters->outfile, outfilename, sizeof(outfilename));
196 /* -------------------------------------------------------------------------- */
197 int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol, char *indexfilename) {
198 /* parse the command line */
200 option_t long_option[]={
201 {"ImgDir",REQ_ARG, NULL ,'y'},
204 const char optlist[] = "i:h";
205 totlen=sizeof(long_option);
206 img_fol->set_out_format = 0;
208 int c = getopt_long(argc, argv,optlist,long_option,totlen);
212 case 'i': /* input file */
214 char *infile = optarg;
215 parameters->decod_format = get_file_format(infile);
216 switch(parameters->decod_format) {
223 "!! Unrecognized format for infile : %s [accept only *.j2k, *.jp2, *.jpc or *.jpt] !!\n\n",
227 strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
231 /* ----------------------------------------------------- */
233 case 'h': /* display an help description */
234 decode_help_display();
237 /* ------------------------------------------------------ */
239 case 'y': /* Image Directory path */
241 img_fol->imgdirpath = (char*)malloc(strlen(optarg) + 1);
242 strcpy(img_fol->imgdirpath,optarg);
243 img_fol->set_imgdir=1;
247 /* ----------------------------------------------------- */
250 fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, optarg);
255 /* check for possible errors */
256 if(img_fol->set_imgdir==1){
257 if(!(parameters->infile[0]==0)){
258 fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
261 if(img_fol->set_out_format == 0){
262 fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
263 fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
266 if(!((parameters->outfile[0] == 0))){
267 fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
271 if((parameters->infile[0] == 0) ) {
272 fprintf(stderr, "Error: One of the options -i or -ImgDir must be specified\n");
273 fprintf(stderr, "usage: image_to_j2k -i *.j2k/jp2/j2c (+ options)\n");
281 /* -------------------------------------------------------------------------- */
284 sample error callback expecting a FILE* client object
286 void error_callback(const char *msg, void *client_data) {
287 FILE *stream = (FILE*)client_data;
288 fprintf(stream, "[ERROR] %s", msg);
291 sample warning callback expecting a FILE* client object
293 void warning_callback(const char *msg, void *client_data) {
294 FILE *stream = (FILE*)client_data;
295 fprintf(stream, "[WARNING] %s", msg);
298 sample debug callback expecting no client object
300 void info_callback(const char *msg, void *client_data) {
302 fprintf(stdout, "[INFO] %s", msg);
305 /* -------------------------------------------------------------------------- */
307 int main(int argc, char *argv[])
309 opj_dparameters_t parameters; /* decompression parameters */
311 opj_event_mgr_t event_mgr; /* event manager */
312 opj_image_t *image = NULL;
314 unsigned char *src = NULL;
318 dircnt_t *dirptr = NULL;
319 opj_dinfo_t* dinfo = NULL; /* handle to a decompressor */
320 opj_cio_t *cio = NULL;
321 opj_codestream_info_t cstr_info; /* Codestream information structure */
322 char indexfilename[OPJ_PATH_LEN]; /* index file name */
324 /* configure the event callbacks (not required) */
325 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
326 event_mgr.error_handler = error_callback;
327 event_mgr.warning_handler = warning_callback;
328 event_mgr.info_handler = info_callback;
330 /* set decoding parameters to default values */
331 opj_set_default_decoder_parameters(¶meters);
333 /* Initialize indexfilename and img_fol */
335 memset(&img_fol,0,sizeof(img_fol_t));
337 /* parse input and get user encoding parameters */
338 if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) {
342 /* Initialize reading of directory */
343 if(img_fol.set_imgdir==1){
344 num_images=get_num_images(img_fol.imgdirpath);
346 dirptr=(dircnt_t*)malloc(sizeof(dircnt_t));
348 dirptr->filename_buf = (char*)malloc(num_images*OPJ_PATH_LEN*sizeof(char)); // Stores at max 10 image file names
349 dirptr->filename = (char**) malloc(num_images*sizeof(char*));
351 if(!dirptr->filename_buf){
354 for(i=0;i<num_images;i++){
355 dirptr->filename[i] = dirptr->filename_buf + i*OPJ_PATH_LEN;
358 if(load_images(dirptr,img_fol.imgdirpath)==1){
362 fprintf(stdout,"Folder is empty\n");
369 /*Encoding image one by one*/
370 for(imageno = 0; imageno < num_images ; imageno++)
373 fprintf(stderr,"\n");
375 if(img_fol.set_imgdir==1){
376 if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
377 fprintf(stderr,"skipping file...\n");
382 /* read the input file and put it in memory */
383 /* ---------------------------------------- */
384 fsrc = fopen(parameters.infile, "rb");
386 fprintf(stderr, "ERROR -> failed to open %s for reading\n", parameters.infile);
389 fseek(fsrc, 0, SEEK_END);
390 file_length = ftell(fsrc);
391 fseek(fsrc, 0, SEEK_SET);
392 src = (unsigned char *) malloc(file_length);
393 fread(src, 1, file_length, fsrc);
396 /* decode the code-stream */
397 /* ---------------------- */
399 switch(parameters.decod_format) {
402 /* JPEG-2000 codestream */
404 /* get a decoder handle */
405 dinfo = opj_create_decompress(CODEC_J2K);
407 /* catch events using our callbacks and give a local context */
408 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
410 /* setup the decoder decoding parameters using user parameters */
411 opj_setup_decoder(dinfo, ¶meters);
413 /* open a byte stream */
414 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
416 /* decode the stream and fill the image structure */
417 if (*indexfilename) // If need to extract codestream information
418 image = opj_decode_with_info(dinfo, cio, &cstr_info);
420 image = opj_decode(dinfo, cio);
422 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
423 opj_destroy_decompress(dinfo);
428 j2k_dump_image(stdout, image);
431 j2k_dump_cp(stdout, image, ((opj_j2k_t*)dinfo->j2k_handle)->cp);
433 /* close the byte stream */
436 /* Write the index to disk */
437 if (*indexfilename) {
439 bSuccess = write_index_file(&cstr_info, indexfilename);
441 fprintf(stderr, "Failed to output index file\n");
449 /* JPEG 2000 compressed image data */
451 /* get a decoder handle */
452 dinfo = opj_create_decompress(CODEC_JP2);
454 /* catch events using our callbacks and give a local context */
455 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
457 /* setup the decoder decoding parameters using the current image and user parameters */
458 opj_setup_decoder(dinfo, ¶meters);
460 /* open a byte stream */
461 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
463 /* decode the stream and fill the image structure */
464 if (*indexfilename) // If need to extract codestream information
465 image = opj_decode_with_info(dinfo, cio, &cstr_info);
467 image = opj_decode(dinfo, cio);
469 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
470 opj_destroy_decompress(dinfo);
475 j2k_dump_image(stdout, image);
478 j2k_dump_cp(stdout, image, ((opj_jp2_t*)dinfo->jp2_handle)->j2k->cp);
480 /* close the byte stream */
483 /* Write the index to disk */
484 if (*indexfilename) {
486 bSuccess = write_index_file(&cstr_info, indexfilename);
488 fprintf(stderr, "Failed to output index file\n");
496 /* JPEG 2000, JPIP */
498 /* get a decoder handle */
499 dinfo = opj_create_decompress(CODEC_JPT);
501 /* catch events using our callbacks and give a local context */
502 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
504 /* setup the decoder decoding parameters using user parameters */
505 opj_setup_decoder(dinfo, ¶meters);
507 /* open a byte stream */
508 cio = opj_cio_open((opj_common_ptr)dinfo, src, file_length);
510 /* decode the stream and fill the image structure */
511 if (*indexfilename) // If need to extract codestream information
512 image = opj_decode_with_info(dinfo, cio, &cstr_info);
514 image = opj_decode(dinfo, cio);
516 fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
517 opj_destroy_decompress(dinfo);
522 /* close the byte stream */
525 /* Write the index to disk */
526 if (*indexfilename) {
528 bSuccess = write_index_file(&cstr_info, indexfilename);
530 fprintf(stderr, "Failed to output index file\n");
537 fprintf(stderr, "skipping file..\n");
541 /* free the memory containing the code-stream */
545 /* free remaining structures */
547 opj_destroy_decompress(dinfo);
549 /* free codestream information structure */
551 opj_destroy_cstr_info(&cstr_info);
552 /* free image data structure */
553 opj_image_destroy(image);