! : changed
+ : added
+February 27, 2007
+* [Parvatha] Made get_file_format function more robust. Modifications in image_to_j2k.c, j2k_to_image.c, getopt.c, getopt.h
+
February 26, 2007
+ [Parvatha] Option to read images from a Folder whose path is specified in the Input parameters by "-ImgDir" along with output decod format specified by "-OutFor" . Modifications in image_to_j2k.c, j2k_to_image.c, getopt.c, getopt.h
+ [Parvatha] Enabling use of multi character input parameters in the codec. Modifications in image_to_j2k.c, j2k_to_image.c, getopt.c, getopt.h
-static void getopterror(int which) {
- static char error1[]="Unknown option `-x'.\n";
- static char error2[]="Missing argument for `-x'.\n";
- if (opterr) {
- if (which) {
- error2[23]=optopt;
- fprintf(stderr,"%s\n",error2);
-
- } else {
- error1[17]=optopt;
- fprintf(stderr,"%s\n",error1);
- }
- }
-}
-
-
/*
* getopt --
* Parse argc/argv argument vector.
if (argv[optind][0]=='-' && argv[optind][1]==0) {
++optind;
- return -1;
+ return (BADCH);
}
if (argv[optind][0]=='-') { /* long option */
char* arg=argv[optind]+1;
- char* max=strchr(arg,'=');
const struct option* o;
- if (!max) max=arg+strlen(arg);
o=longopts;
len=sizeof(longopts[0]);
- for (i=0;i<totlen;i=i+len,o++) {
- if (!strncmp(o->name,arg,(size_t)(max-arg))) { /* match */
- if (longindex) *longindex=o-longopts;
- if (o->has_arg>0) {
- if (*max=='=')
- optarg=max+1;
- else {
- optarg=argv[optind+1];
- if(optarg){
- if (strchr(optarg,'-')){ /* No argument */
- if (*optstring==':') return ':';
+ if(strlen(arg)>1){
+ for (i=0;i<totlen;i=i+len,o++) {
+ if (!strcmp(o->name,arg)) { /* match */
+ if (longindex) *longindex=o-longopts;
+ if (o->has_arg>0) {
+ optarg=argv[optind+1];
+ if(optarg){
+ if (strchr(optarg,'-')){ /* Has read next input parameter: No arg for current parameter */
+ if (opterr) {
+ fprintf(stderr,"%s: option requires an argument\n",arg);
+ return (BADCH);
+ }
+ }
+ }
+ if (!optarg && o->has_arg==1) { /* no argument there */
if (opterr) {
fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
return (BADCH);
}
- ++optind;
- }
- }
- if (!optarg && o->has_arg==1) { /* no argument there */
- if (*optstring==':') return ':';
- if (opterr) {
- fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
- return (BADCH);
}
- ++optind;
- }
- ++optind;
+ ++optind;
}
+ ++optind;
+ if (o->flag)
+ *(o->flag)=o->val;
+ else
+ return o->val;
+ return 0;
}
- ++optind;
- if (o->flag)
- *(o->flag)=o->val;
- else
- return o->val;
- return 0;
- }
- }//(end for)
-
- if (*optstring==':') return ':';
-
- if (lastidx!=optind) {
- lastidx=optind; lastofs=0;
- }
- optopt=argv[optind][lastofs+1];
-
- if ((tmp=strchr(optstring,optopt))) {
- if (*tmp==0) { /* apparently, we looked for \0, i.e. end of argument */
- ++optind;
- goto again;
+ }//(end for)String not found in the list
+ fprintf(stderr,"Invalid option %s\n",arg);
+ ++optind;
+ return (BADCH);
+ }else{ /*Single character input parameter*/
+ if (*optstring==':') return ':';
+ if (lastidx!=optind) {
+ lastidx=optind; lastofs=0;
}
-
- if (tmp[1]==':') { /* argument expected */
- if (tmp[2]==':' || argv[optind][lastofs+2]) { /* "-foo", return "oo" as optarg */
- if (!*(optarg=argv[optind]+lastofs+2)) optarg=0;
- goto found;
- }
-
- optarg=argv[optind+1];
- if (!optarg) { /* missing argument */
+ optopt=argv[optind][lastofs+1];
+ if ((tmp=strchr(optstring,optopt))) {/*Found input parameter in list*/
+ if (*tmp==0) { /* apparently, we looked for \0, i.e. end of argument */
++optind;
- if (*optstring==':') return ':';
- getopterror(1);
- return ':';
+ goto again;
}
- ++optind;
- }
- else {
+ if (tmp[1]==':') { /* argument expected */
+ if (tmp[2]==':' || argv[optind][lastofs+2]) { /* "-foo", return "oo" as optarg */
+ if (!*(optarg=argv[optind]+lastofs+2)) optarg=0;
+ goto found;
+ }
+ optarg=argv[optind+1];
+ if(optarg){
+ if (strchr(optarg,'-')){ /* Has read next input parameter: No arg for current parameter */
+ if (opterr) {
+ fprintf(stderr,"%s: option requires an argument\n",arg);
+ return (BADCH);
+ }
+ }
+ }
+ if (!optarg) { /* missing argument */
+ if (opterr) {
+ fprintf(stderr,"%s: option requires an argument\n",arg);
+ return (BADCH);
+ }
+ }
+ ++optind;
+ }else {/*Argument not expected*/
++lastofs;
return optopt;
- }
+ }
found:
++optind;
return optopt;
- }
- else { /* not found */
- getopterror(0);
+ } else { /* not found */
+ fprintf(stderr,"Invalid option %s\n",arg);
++optind;
- return '?';
- }
+ return (BADCH);
+ }//end of not found
- fprintf(stderr,"Invalid option %s\n",arg);
- ++optind;
- return '?';
- }// end of long option
- return (BADCH);
-}
+ }// end of single character
+ }//end '-'
+ fprintf(stderr,"Invalid option %s\n");
+ ++optind;
+ return (BADCH);;
+ }//end function
static const int format[] = {
PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT
};
- char * ext = strrchr(filename, '.') + 1;
+ char * ext = strrchr(filename, '.');
+ if (ext == NULL)
+ return -1;
+ ext++;
for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
if(strnicmp(ext, extension[i], 3) == 0) {
return format[i];
return fname;
}
-get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){
+char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t *parameters){
char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
strcpy(image_filename,dirptr->filename[imageno]);
- fprintf(stderr,"Image Number %d \"%s\"\n",imageno,image_filename);
+ fprintf(stderr,"File Number %d \"%s\"\n",imageno,image_filename);
parameters->decod_format = get_file_format(image_filename);
+ if (parameters->decod_format == -1)
+ return 1;
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename));
sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
strncpy(parameters->outfile, outfilename, sizeof(outfilename));
}
-
+ return 0;
}
img_fol->out_format = "jp2";
break;
default:
- fprintf(stderr, "Unknown output format image %s [only j2k, jp2]!! \n");
+ fprintf(stderr, "Unknown output format image [only j2k, jp2]!! \n");
return 1;
}
}
/* ------------------------------------------------------ */
default:
- fprintf(stderr, "ERROR -> this option is not valid \"-%c %s\"\n", c, optarg);
+ fprintf(stderr, "ERROR -> Command line not valid\n");
return 1;
}
}
}
if(img_fol->set_out_format == 0){
fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
- fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX,BMP!!\n");
+ fprintf(stderr, "Only one format allowed! Valid formats are j2k and jp2!!\n");
return 1;
}
if(!((parameters->outfile[0] == 0))){
image = NULL;
fprintf(stderr,"\n");
- process_file = 1;
+
if(img_fol.set_imgdir==1){
- get_next_file(imageno, dirptr,&img_fol, ¶meters );
-
+ if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
+ fprintf(stderr,"skipping file...\n");
+ continue;
+ }
}
switch(parameters.decod_format) {
case PGX_DFMT:
default:
fprintf(stderr,"skipping file...\n");
- process_file = 0;
+ continue;
}
- if(process_file == 1){
-
/* decode the source image */
/* ----------------------- */
/* free image data */
opj_image_destroy(image);
- }
}
/* free user parameters structure */
unsigned int i;
static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2", "jpt" };
static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT };
- char * ext = strrchr(filename, '.') + 1;
+ char * ext = strrchr(filename, '.');
+ if (ext == NULL)
+ return -1;
+ ext++;
if(ext) {
for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
if(strnicmp(ext, extension[i], 3) == 0) {
return -1;
}
-get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
+char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t *parameters){
char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],outfilename[OPJ_PATH_LEN],temp_ofname[OPJ_PATH_LEN];
strcpy(image_filename,dirptr->filename[imageno]);
fprintf(stderr,"Imageno=%d %s\n",imageno,image_filename);
parameters->decod_format = get_file_format(image_filename);
+ if (parameters->decod_format == -1)
+ return 1;
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename));
sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
strncpy(parameters->outfile, outfilename, sizeof(outfilename));
}
+ return 0;
}
}
if(!((parameters->outfile[0] == 0))){
fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
- fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
return 1;
}
}else{
if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) {
fprintf(stderr, "Error: One of option; -i or -ImgDir must be specified\n");
fprintf(stderr, "Error: When using -i; -o must be used\n");
- fprintf(stderr, "usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
+ fprintf(stderr, "usage: image_to_j2k -i *.j2k/jp2 -o *.pgm/ppm/pnm/pgx/bmp(+ options)\n");
return 1;
}
}
image = NULL;
fprintf(stderr,"\n");
- process_file = 1;
-
if(img_fol.set_imgdir==1){
- get_next_file(imageno, dirptr,&img_fol, ¶meters );
+ if (get_next_file(imageno, dirptr,&img_fol, ¶meters)) {
+ fprintf(stderr,"skipping file...\n");
+ continue;
+ }
}
default:
fprintf(stderr, "False input image format skipping file..\n");
- process_file = 0;
+ continue;
}
/* free the memory containing the code-stream */
/* create output image */
/* ------------------- */
- if(process_file == 1){
switch (parameters.cod_format) {
case PXM_DFMT: /* PNM PGM PPM */
imagetopnm(image, parameters.outfile);
}
/* free image data structure */
opj_image_destroy(image);
- }
+
}
return 0;
}