Made get_file_format function more robust. Modifications in image_to_j2k.c, j2k_to_image.c, getopt.c, getopt.h

This commit is contained in:
Parvatha Elangovan 2007-02-27 14:19:09 +00:00
parent 313c772f4a
commit fd680a1cf0
4 changed files with 106 additions and 114 deletions

View File

@ -5,6 +5,9 @@ What's New for OpenJPEG
! : changed ! : changed
+ : added + : 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 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] 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 + [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

View File

@ -61,22 +61,6 @@ typedef struct option
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 -- * getopt --
* Parse argc/argv argument vector. * Parse argc/argv argument vector.
@ -151,100 +135,96 @@ again:
if (argv[optind][0]=='-' && argv[optind][1]==0) { if (argv[optind][0]=='-' && argv[optind][1]==0) {
++optind; ++optind;
return -1; return (BADCH);
} }
if (argv[optind][0]=='-') { /* long option */ if (argv[optind][0]=='-') { /* long option */
char* arg=argv[optind]+1; char* arg=argv[optind]+1;
char* max=strchr(arg,'=');
const struct option* o; const struct option* o;
if (!max) max=arg+strlen(arg);
o=longopts; o=longopts;
len=sizeof(longopts[0]); len=sizeof(longopts[0]);
for (i=0;i<totlen;i=i+len,o++) { if(strlen(arg)>1){
if (!strncmp(o->name,arg,(size_t)(max-arg))) { /* match */ for (i=0;i<totlen;i=i+len,o++) {
if (longindex) *longindex=o-longopts; if (!strcmp(o->name,arg)) { /* match */
if (o->has_arg>0) { if (longindex) *longindex=o-longopts;
if (*max=='=') if (o->has_arg>0) {
optarg=max+1; optarg=argv[optind+1];
else { if(optarg){
optarg=argv[optind+1]; if (strchr(optarg,'-')){ /* Has read next input parameter: No arg for current parameter */
if(optarg){ if (opterr) {
if (strchr(optarg,'-')){ /* No argument */ fprintf(stderr,"%s: option requires an argument\n",arg);
if (*optstring==':') return ':'; return (BADCH);
}
}
}
if (!optarg && o->has_arg==1) { /* no argument there */
if (opterr) { if (opterr) {
fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt); fprintf(stderr,"%s: option requires an argument %c\n",arg, optopt);
return (BADCH); 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;
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;
}
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 */
++optind; ++optind;
if (*optstring==':') return ':'; }
getopterror(1); ++optind;
return ':'; if (o->flag)
*(o->flag)=o->val;
else
return o->val;
return 0;
} }
++optind; }//(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;
} }
else { 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;
goto again;
}
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; ++lastofs;
return optopt; return optopt;
} }
found: found:
++optind; ++optind;
return optopt; return optopt;
} } else { /* not found */
else { /* not found */ fprintf(stderr,"Invalid option %s\n",arg);
getopterror(0);
++optind; ++optind;
return '?'; return (BADCH);
} }//end of not found
fprintf(stderr,"Invalid option %s\n",arg); }// end of single character
++optind; }//end '-'
return '?'; fprintf(stderr,"Invalid option %s\n");
}// end of long option ++optind;
return (BADCH); return (BADCH);;
} }//end function

View File

@ -361,7 +361,10 @@ int get_file_format(char *filename) {
static const int format[] = { static const int format[] = {
PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, J2K_CFMT, JP2_CFMT 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++) { for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
if(strnicmp(ext, extension[i], 3) == 0) { if(strnicmp(ext, extension[i], 3) == 0) {
return format[i]; return format[i];
@ -378,12 +381,14 @@ char * get_file_name(char *name){
return fname; 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]; 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]); 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); parameters->decod_format = get_file_format(image_filename);
if (parameters->decod_format == -1)
return 1;
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename); sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename)); strncpy(parameters->infile, infilename, sizeof(infilename));
@ -393,7 +398,7 @@ get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_cparameters_t
sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
strncpy(parameters->outfile, outfilename, sizeof(outfilename)); strncpy(parameters->outfile, outfilename, sizeof(outfilename));
} }
return 0;
} }
@ -484,7 +489,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i
img_fol->out_format = "jp2"; img_fol->out_format = "jp2";
break; break;
default: default:
fprintf(stderr, "Unknown output format image %s [only j2k, jp2]!! \n"); fprintf(stderr, "Unknown output format image [only j2k, jp2]!! \n");
return 1; return 1;
} }
} }
@ -1134,7 +1139,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i
/* ------------------------------------------------------ */ /* ------------------------------------------------------ */
default: default:
fprintf(stderr, "ERROR -> this option is not valid \"-%c %s\"\n", c, optarg); fprintf(stderr, "ERROR -> Command line not valid\n");
return 1; return 1;
} }
} }
@ -1147,7 +1152,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i
} }
if(img_fol->set_out_format == 0){ if(img_fol->set_out_format == 0){
fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n"); 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; return 1;
} }
if(!((parameters->outfile[0] == 0))){ if(!((parameters->outfile[0] == 0))){
@ -1299,11 +1304,13 @@ int main(int argc, char **argv) {
image = NULL; image = NULL;
fprintf(stderr,"\n"); fprintf(stderr,"\n");
process_file = 1;
if(img_fol.set_imgdir==1){ if(img_fol.set_imgdir==1){
get_next_file(imageno, dirptr,&img_fol, &parameters ); if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
fprintf(stderr,"skipping file...\n");
continue;
}
} }
switch(parameters.decod_format) { switch(parameters.decod_format) {
case PGX_DFMT: case PGX_DFMT:
@ -1315,11 +1322,9 @@ int main(int argc, char **argv) {
default: default:
fprintf(stderr,"skipping file...\n"); fprintf(stderr,"skipping file...\n");
process_file = 0; continue;
} }
if(process_file == 1){
/* decode the source image */ /* decode the source image */
/* ----------------------- */ /* ----------------------- */
@ -1441,7 +1446,6 @@ int main(int argc, char **argv) {
/* free image data */ /* free image data */
opj_image_destroy(image); opj_image_destroy(image);
}
} }
/* free user parameters structure */ /* free user parameters structure */

View File

@ -181,7 +181,10 @@ int get_file_format(char *filename) {
unsigned int i; unsigned int i;
static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp", "j2k", "jp2", "jpt" }; 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 }; 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) { if(ext) {
for(i = 0; i < sizeof(format)/sizeof(*format); i++) { for(i = 0; i < sizeof(format)/sizeof(*format); i++) {
if(strnicmp(ext, extension[i], 3) == 0) { if(strnicmp(ext, extension[i], 3) == 0) {
@ -193,12 +196,14 @@ int get_file_format(char *filename) {
return -1; 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]; 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]); strcpy(image_filename,dirptr->filename[imageno]);
fprintf(stderr,"Imageno=%d %s\n",imageno,image_filename); fprintf(stderr,"Imageno=%d %s\n",imageno,image_filename);
parameters->decod_format = get_file_format(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); sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
strncpy(parameters->infile, infilename, sizeof(infilename)); strncpy(parameters->infile, infilename, sizeof(infilename));
@ -208,6 +213,7 @@ get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_dparameters_t
sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format); sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
strncpy(parameters->outfile, outfilename, sizeof(outfilename)); strncpy(parameters->outfile, outfilename, sizeof(outfilename));
} }
return 0;
} }
@ -427,14 +433,13 @@ int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,i
} }
if(!((parameters->outfile[0] == 0))){ if(!((parameters->outfile[0] == 0))){
fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n"); fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
fprintf(stderr, "Specify OutputFormat using -OutFor<FORMAT> !!\n");
return 1; return 1;
} }
}else{ }else{
if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { 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: One of option; -i or -ImgDir must be specified\n");
fprintf(stderr, "Error: When using -i; -o must be used\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; return 1;
} }
} }
@ -530,11 +535,12 @@ int main(int argc, char **argv) {
image = NULL; image = NULL;
fprintf(stderr,"\n"); fprintf(stderr,"\n");
process_file = 1;
if(img_fol.set_imgdir==1){ if(img_fol.set_imgdir==1){
get_next_file(imageno, dirptr,&img_fol, &parameters ); if (get_next_file(imageno, dirptr,&img_fol, &parameters)) {
fprintf(stderr,"skipping file...\n");
continue;
}
} }
@ -652,7 +658,7 @@ int main(int argc, char **argv) {
default: default:
fprintf(stderr, "False input image format skipping file..\n"); fprintf(stderr, "False input image format skipping file..\n");
process_file = 0; continue;
} }
/* free the memory containing the code-stream */ /* free the memory containing the code-stream */
@ -661,7 +667,6 @@ int main(int argc, char **argv) {
/* create output image */ /* create output image */
/* ------------------- */ /* ------------------- */
if(process_file == 1){
switch (parameters.cod_format) { switch (parameters.cod_format) {
case PXM_DFMT: /* PNM PGM PPM */ case PXM_DFMT: /* PNM PGM PPM */
imagetopnm(image, parameters.outfile); imagetopnm(image, parameters.outfile);
@ -682,7 +687,7 @@ int main(int argc, char **argv) {
} }
/* free image data structure */ /* free image data structure */
opj_image_destroy(image); opj_image_destroy(image);
}
} }
return 0; return 0;
} }