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
+ : 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

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 --
* Parse argc/argv argument vector.
@ -151,100 +135,96 @@ again:
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;
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;
if (*optstring==':') return ':';
getopterror(1);
return ':';
}
++optind;
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;
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

View File

@ -361,7 +361,10 @@ int get_file_format(char *filename) {
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];
@ -378,12 +381,14 @@ char * get_file_name(char *name){
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));
@ -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);
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";
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;
}
}
@ -1134,7 +1139,7 @@ int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,i
/* ------------------------------------------------------ */
default:
fprintf(stderr, "ERROR -> this option is not valid \"-%c %s\"\n", c, optarg);
fprintf(stderr, "ERROR -> Command line not valid\n");
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){
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))){
@ -1299,11 +1304,13 @@ int main(int argc, char **argv) {
image = NULL;
fprintf(stderr,"\n");
process_file = 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) {
case PGX_DFMT:
@ -1315,11 +1322,9 @@ int main(int argc, char **argv) {
default:
fprintf(stderr,"skipping file...\n");
process_file = 0;
continue;
}
if(process_file == 1){
/* decode the source image */
/* ----------------------- */
@ -1441,7 +1446,6 @@ int main(int argc, char **argv) {
/* free image data */
opj_image_destroy(image);
}
}
/* free user parameters structure */

View File

@ -181,7 +181,10 @@ int get_file_format(char *filename) {
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) {
@ -193,12 +196,14 @@ int get_file_format(char *filename) {
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));
@ -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);
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))){
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;
}
}
@ -530,11 +535,12 @@ int main(int argc, char **argv) {
image = NULL;
fprintf(stderr,"\n");
process_file = 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:
fprintf(stderr, "False input image format skipping file..\n");
process_file = 0;
continue;
}
/* free the memory containing the code-stream */
@ -661,7 +667,6 @@ int main(int argc, char **argv) {
/* create output image */
/* ------------------- */
if(process_file == 1){
switch (parameters.cod_format) {
case PXM_DFMT: /* PNM PGM PPM */
imagetopnm(image, parameters.outfile);
@ -682,7 +687,7 @@ int main(int argc, char **argv) {
}
/* free image data structure */
opj_image_destroy(image);
}
}
return 0;
}