|
From: Herton R. K. <he...@us...> - 2005-06-23 23:27:25
|
Update of /cvsroot/kimageprocess/kimageprocess/res2pgm In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3557/res2pgm Added Files: lex.yy.c res.l res2pgm.c Log Message: - Added first files of res2pgm new version. --- NEW FILE: lex.yy.c --- #line 3 "lex.yy.c" #define YY_INT_ALIGNED short int /* A lexical scanner generated by flex */ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 #define YY_FLEX_SUBMINOR_VERSION 31 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif /* First, we deal with platform-specific or compiler-specific issues. */ /* begin standard C headers. */ #include <stdio.h> [...1866 lines suppressed...] #undef yy_new_buffer #undef yy_set_interactive #undef yytext_ptr #undef YY_DO_BEFORE_ACTION #ifdef YY_DECL_IS_OURS #undef YY_DECL_IS_OURS #undef YY_DECL #endif #line 119 "res.l" void yyerror(char *s) { fprintf(stderr, " %s at line: %d, column: %d.\n", s, row, col); } /* * vim:noet */ --- NEW FILE: res.l --- %{ #include <stdio.h> #include <math.h> #define DEBUG 0 #define INTNUMBER 1 #define FLOATNUMBER 2 #define RESHEADER 3 #define RESDATE 4 #define NUMPAT 5 #define NUMIN 6 #define NUMOUT 7 #define END 8 int row=1, col=1; /* function prototypes */ void yyerror(char *s); %} INTNUMBER [0-9]+ FLOATNUMBER [0-9]+"."[0-9]* WHITESPACE [ \t\r] COMMENT [#]+.*[\n] RESHEADER "SNNS result file V1.4-3D" RESDATE "generated at".*[\n] NUMPAT "No. of patterns"[ ]*":"[ ]* NUMIN "No. of input units"[ ]*":"[ ]* NUMOUT "No. of output units"[ ]*":"[ ]* STARTPATTERN "startpattern"[ ]*":"[ ]* ENDPATTERN "endpattern"[ ]*":"[ ]* %% \n ++row; col=1; {INTNUMBER} { #if DEBUG printf("INTNUMBER: %d\n", atoi(yytext)); #endif col += yyleng; return INTNUMBER; } {FLOATNUMBER} { #if DEBUG printf("FLOATNUMBER: %f\n", atof(yytext)); #endif col += yyleng; return FLOATNUMBER; } {RESHEADER} { #if DEBUG printf("RESHEADER\n"); #endif col += yyleng; return RESHEADER; } {RESDATE} { #if DEBUG printf("RESDATE\n"); #endif col = 1; row++; return RESDATE; } {NUMPAT} { #if DEBUG printf("NUMPAT\n"); #endif col += yyleng; return NUMPAT; } {NUMIN} { #if DEBUG printf("NUMIN\n"); #endif col += yyleng; return NUMIN; } {NUMOUT} { #if DEBUG printf("NUMOUT\n"); #endif col += yyleng; return NUMOUT; } {STARTPATTERN} { #if DEBUG printf("STARTPATTERN\n"); #endif col += yyleng; return STARTPATTERN; } {ENDPATTERN} { #if DEBUG printf("ENDPATTERN\n"); #endif col += yyleng; return ENDPATTERN; } {WHITESPACE}+ { #if DEBUG printf("WHITESPACE\n"); #endif col += yyleng; } {COMMENT} { #if DEBUG printf("COMMENT\n"); #endif col = 1; row++; } <<EOF>> { #if DEBUG printf("END\n"); #endif return END; } . { ++col; } %% void yyerror(char *s) { fprintf(stderr, " %s at line: %d, column: %d.\n", s, row, col); } /* * vim:noet */ --- NEW FILE: res2pgm.c --- #include <errno.h> #include <getopt.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <lex.yy.c> #define RES2PGM_VERSION "2" #define PARSER_ERROR 1 #define CHECK_ERROR 2 #define BUF_LEN 32767 typedef struct { float **resultValues; int numInputUnits; int numOutputUnits; int numPatterns; int startPattern; int endPattern; } res; static void usage(char *progname) { fprintf(stderr, "res2pgm version %s\n", RES2PGM_VERSION); fprintf(stderr, "Copyright (C) 2005 by\n"); fprintf(stderr, " Gustavo Pichorim Boiko <gus...@kd...>\n"); fprintf(stderr, " Herton Ronaldo Krzesinski <he...@my...>\n\n"); fprintf(stderr, "Usage: %s [options]\n", progname); fprintf(stderr, "Options: -r,--res-file resfile\n"); fprintf(stderr, " -p,--pgm-file pgm image\n"); fprintf(stderr, " -w,--width image width\n"); fprintf(stderr, " -h,--height image height\n"); exit(1); } static void fatal(int err) { fprintf(stderr, "%s\n", strerror(err)); exit(err); } static void resLoadFail(int code, char *fileName) { switch(code) { case PARSER_ERROR: fprintf(stderr, "Error: Invalid results file format.\n"); yyerror(fileName); break; case CHECK_ERROR: fprintf(stderr, "Error: Consistency check error on %s,\n", fileName); fprintf(stderr, " number of patterns or units informed is different\n"); fprintf(stderr, " from what's expected.\n"); break; } exit(1); } static void loadResults(res *resInf, FILE *resFile, char *fileName) { int tok, curRes = 0, i; float *tempp; yyin = resFile; tok = yylex(); if (tok != RESHEADER) resLoadFail(PARSER_ERROR, fileName); tok = yylex(); if (tok != RESDATE) resLoadFail(PARSER_ERROR, fileName); resInf->numInputUnits = -1; resInf->numOutputUnits = -1; resInf->numPatterns = -1; resInf->startPattern = -1; resInf->endPattern = -1; resInf->inputValues = NULL; resInf->outputValues = NULL; tok = yylex(); while (tok != END) { switch(tok) { case NUMPAT: tok = yylex(); if (tok != INTNUMBER) resLoadFail(PARSER_ERROR, fileName); resInf->numPatterns = atoi(yytext); break; case NUMIN: tok = yylex(); if (tok != INTNUMBER) resLoadFail(PARSER_ERROR, fileName); resInf->numInputUnits = atoi(yytext); break; case NUMOUT: tok = yylex(); if (tok != INTNUMBER) resLoadFail(PARSER_ERROR, fileName); resInf->numOutputUnits = atoi(yytext); break; case STARTPATTERN: tok = yylex(); if (tok != INTNUMBER) resLoadFail(PARSER_ERROR, fileName); resInf->startPattern = atoi(yytext); break; case ENDPATTERN: tok = yylex(); if (tok != INTNUMBER) resLoadFail(PARSER_ERROR, fileName); resInf->endPattern = atoi(yytext); break; case FLOATNUMBER: case INTNUMBER: if (resInf->numPatterns <= 0 || curPat == resInf->numPatterns) resLoadFail(CHECK_ERROR, fileName); if (resInf->resultValues == NULL && resInf->numOutputUnits > 0) { tempp = malloc(sizeof(float) * resInf->numPatterns * resInf->numOutputUnits); if (tempp == NULL) fatal(errno); resInf->resultValues = malloc(sizeof(float *) * resInf->numPatterns); if (resInf->resultValues == NULL) fatal(errno); for (i = 0; i < resInf->numPatterns; i++) resInf->resultValues[i] = tempp + (i * resInf->numOutputUnits); } i = 0; while (i < resInf->numOutputUnits) { tok = yylex(); if (tok != FLOATNUMBER && tok != INTNUMBER) resLoadFail(CHECK_ERROR, fileName); resInf->resultValues[curPat][i] = atof(yytext); ++i; } curPat++; break; default: resLoadFail(PARSER_ERROR, fileName); } tok = yylex(); } } int main(int argc, char **argv) { int c; int option_index = 0; static struct option long_options[] = { {"res-file", 1, NULL, 'r'}, {"pgm-file", 1, NULL, 'p'}, {"width", 1, NULL, 'w'}, {"height", 1, NULL, 'h'}, {0, 0, 0, 0} }; opterr = 0; char *r_file, *p_file; FILE *pr_file, *pp_file; res resData; int cols = -1, rows = -1; int npat, t; char p_buf[BUF_LEN]; if (argc == 1) usage(argv[0]); r_file = NULL; p_file = NULL; while (1) { c = getopt_long (argc, argv, "+r:p:w:h:", long_options, &option_index); if (c == -1 && optind != argc) usage(argv[0]); if (c == -1) break; switch (c) { case 'r': r_file = optarg; break; case 'p': p_file = optarg; break; case 'w': cols = atoi(optarg); break; case 'h': rows = atoi(optarg); break; case ':': case '?': usage(argv[0]); break; default: printf ("?? getopt returned character code 0%o ??\n", c); exit(1); } } if (!r_file || !p_file) usage(argv[0]); if (cols < 0 || rows < 0) usage(argv[0]); pr_file = fopen(r_file, "r"); if (pr_file == NULL) fatal(errno); loadResults(&resData, pr_file, r_file); pp_file = fopen(p_file, "w"); if (pp_file == NULL) fatal(errno); if (fprintf(pp_file, "P5\n%d %d\n%d\n", cols, rows, resData.numOutputUnits) < 0) fatal(errno); npat = 0; while (npat < resData.numPatterns) { t = (npat + NUM_BUF) > resData.numPatterns ? resData.numPatters - npat : NUM_BUF; while (npat < t) { ++npat; } ++npat; } for (npat = 0; npat < resData.numPatterns; npat++) { if (fprintf(pr_file, "#%d.1\n", npat + 1) < 0) fatal(errno); result = fann_run(tfann, c_patData.inputValues[npat]); for (c = 0; c < t_patData.numOutputUnits; c++) { if (fprintf(pr_file, "%.5f ", result[c]) < 0) fatal(errno); } fseek(pr_file, -1, SEEK_CUR); /* remove extra space */ fprintf(pr_file, "\n"); } fann_destroy(tfann); if (t_patData.inputValues != NULL) { /* free only first element because is a contiguous allocated memory space (see loadNetwork) */ if (t_patData.inputValues[0] != NULL) free(t_patData.inputValues[0]); free(t_patData.inputValues); } if (t_patData.outputValues != NULL) { /* free only first element because is a contiguous allocated memory space (see loadNetwork) */ if (t_patData.outputValues[0] != NULL) free(t_patData.outputValues[0]); free(t_patData.outputValues); } if (c_patData.inputValues != NULL) { /* free only first element because is a contiguous allocated memory space (see loadNetwork) */ if (c_patData.inputValues[0] != NULL) free(c_patData.inputValues[0]); free(c_patData.inputValues); } if (c_patData.outputValues != NULL) { /* free only first element because is a contiguous allocated memory space (see loadNetwork) */ if (c_patData.outputValues[0] != NULL) free(c_patData.outputValues[0]); free(c_patData.outputValues); } if (fclose(pt_file) != 0) fatal(errno); if (fclose(pc_file) != 0) fatal(errno); if (fclose(pr_file) != 0) fatal(errno); return 0; } /* * vim:noet */ |