/* Display information on the various key commands during a game. */ static void drawhelpscreen(void) { static char const *keys[] = { "h j k l\0move the selection blockwise", "arrows\0move the selection cursorwise", "H J K L\0move the selected block", "g\0display the puzzle's goal", "x\0undo move", "X\0undo step", "z\0redo undone move", "Z\0redo undone step", "R\0return to starting position", "s\0save current position", "r\0restore saved position", "S\0save current position to disk", "P\0previous level", "N\0next level", "Ctrl-L\0redraw screen", "q\0quit" }; displayhelp(keys, sizeof keys / sizeof *keys); input(); }
/* Main function */ int main(int argc, char *argv[]) { /* Start */ printf("A review of classic edge detection algorithms\n"); printf("IPOL 2013 - Haldo Spontón & Juan Cardelino\n"); /* Options and parameters handling */ int n; // loop counter int argc_sobel = 0; // int argc_prewitt = 0; // int argc_roberts = 0; // indexes int argc_haralick = 0; // in argv[] int argc_mh = 0; // int argc_mhl = 0; // int padding_method = 1; // Reflection of image boundary. Hard-coded. Can be changed to 0 (zero-padding) for ( n = 1; n < argc; n++ ) { // scan through args switch ( (int)argv[n][0] ) { // check for option character case '-': switch ( (int)argv[n][1] ) { case 'r': argc_roberts = n; break; case 'p': argc_prewitt = n; break; case 's': argc_sobel = n; break; case 'h': argc_haralick = n; break; case 'm': argc_mh = n; break; case 'l': argc_mhl = n; break; case 'H': /* Display help! */ displayhelp(); exit(1); break; default: printf("Error: Invalid option -> %s. Valid options are -r, -p, -s, -h, -m and -l (-H for help).\n", argv[n]); exit(1); break; } /* Error message if an option is the last input parameter or if the next parameter after an option is another option */ if ( n==argc-1 ) { printf("Error: Missing parameter(s) for %s option.\n",argv[n]); exit(1); break; } else if ( argv[n+1][0]=='-' ) { printf("Error: Missing parameter(s) for %s option.\n",argv[n]); exit(1); break; } /* In the case of -m and -l, three parameters are needed */ if ( (argv[n][1]=='m') || (argv[n][1]=='l') ) { if ( n+3>argc-1 ) { printf("Error: Missing parameter(s) for %s option.\n",argv[n]); exit(1); break; } else if ( (argv[n+2][0]=='-') || (argv[n+3][0]=='-') ) { printf("Error: Missing parameter(s) for %s option.\n",argv[n]); exit(1); break; } } default: break; } } /* Check for total number of parameters */ int nparam = 2; if (argc_roberts!=0) nparam+=2; if (argc_prewitt!=0) nparam+=2; if (argc_sobel!=0) nparam+=2; if (argc_haralick!=0) nparam+=2; if (argc_mh!=0) nparam+=4; if (argc_mhl!=0) nparam+=4; if (nparam!=argc) { printf("Error: Wrong number of arguments (%d instead of %d).\nUsage: %s [options] input.png\nFor help type %s -H.\n",argc-1,nparam-1,argv[0],argv[0]); exit(1); } /* Read parameters once checked */ float th_roberts, // threshold in Roberts algorithm th_prewitt, // threshold in Prewitt algorithm th_sobel, // threshold in Sobel algorithm rhozero, // threshold for the Haralick edge condition sigma_m, // standard deviation of Gaussian kernel in Marr-Hildreth algorithm tzc_m, // threshold for zero-crossing in Marr-Hildreth algorithm sigma_l, // standard deviation of LoG kernel in Marr-Hildreth-LoG algorithm tzc_l; // threshold for zero-crossing in Marr-Hildreth-LoG algorithm int n_m, // kernel size in Marr-Hildreth algorithm n_l; // kernel size in Marr-Hildreth-LoG algorithm if (argc_roberts!=0) th_roberts = atof(argv[argc_roberts+1]); if (argc_roberts!=0) th_prewitt = atof(argv[argc_prewitt+1]); if (argc_roberts!=0) th_sobel = atof(argv[argc_sobel+1]); if (argc_haralick!=0) rhozero = atof(argv[argc_haralick+1]); if (argc_mh!=0) { sigma_m = atof(argv[argc_mh+1]); n_m = atoi(argv[argc_mh+2]); tzc_m = atof(argv[argc_mh+3]); } if (argc_mhl!=0) { sigma_l = atof(argv[argc_mhl+1]); n_l = atoi(argv[argc_mhl+2]); tzc_l = atof(argv[argc_mhl+3]); } /* Display parameters */ printf("\nPARAMETERS:\n"); if (argc_roberts!=0) printf("\n\t--> Roberts selected.\n\tThreshold = %.2f.\n",th_roberts); else printf("\n\t--> Roberts not selected.\n"); if (argc_prewitt!=0) printf("\n\t--> Prewitt selected.\n\tThreshold = %.2f.\n",th_prewitt); else printf("\n\t--> Prewitt not selected.\n"); if (argc_sobel!=0) printf("\n\t--> Sobel selected.\n\tThreshold = %.2f.\n",th_sobel); else printf("\n\t--> Sobel not selected.\n"); if (argc_haralick!=0) printf("\n\t--> Haralick selected.\n\tRhozero = %.2f.\n",rhozero); else printf("\n\t--> Haralick not selected.\n"); if (argc_mh!=0) printf("\n\t--> Marr-Hildreth (Gaussian) selected.\n\tSigma = %.2f.\n\tN = %d.\n\tTZC = %.2f.\n",sigma_m,n_m,tzc_m); else printf("\n\t--> Marr-Hildreth (Gaussian) not selected.\n"); if (argc_mhl!=0) printf("\n\t--> Marr-Hildreth (LoG) selected.\n\tSigma = %.2f.\n\tN = %d.\n\tTZC = %.2f.\n",sigma_l,n_l,tzc_l); else printf("\n\t--> Marr-Hildreth (LoG) not selected.\n"); /* Load input image */ int w, h, pixeldim; float *im = iio_read_image_float_vec(argv[argc-1], &w, &h, &pixeldim); double *imdouble = malloc(w*h*sizeof(double)); printf("\nINPUT IMAGE:\n\n\t%s\n\tDimensions: %d x %d pixeles. Channels: %d.\n",argv[argc-1],w,h,pixeldim); for ( int i=0; i<w*h; i++ ) { imdouble[i] = (double)im[i]; } /* Run the selected edge detection algorithms */ printf("\nRUNNING SELECTED EDGE DETECTION ALGORITHMS...\n"); /* Roberts edge detection algorithm */ if (argc_roberts!=0) { printf("\n\tRunning Roberts edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_roberts = edges_roberts(imdouble, w, h, th_roberts, padding_method); iio_save_image_float_vec("im_roberts.png", im_roberts, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_roberts.png.\n"); } /* Prewitt edge detection algorithm */ if (argc_prewitt!=0) { printf("\n\tRunning Prewitt edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_prewitt = edges_prewitt(imdouble, w, h, th_prewitt, padding_method); iio_save_image_float_vec("im_prewitt.png", im_prewitt, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_prewitt.png.\n"); } /* Sobel edge detection algorithm */ if (argc_sobel!=0) { printf("\n\tRunning Sobel edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_sobel = edges_sobel(imdouble, w, h, th_sobel, padding_method); iio_save_image_float_vec("im_sobel.png", im_sobel, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_sobel.png.\n"); } /* Haralick edge detection algorithm */ if (argc_haralick!=0) { printf("\n\tRunning Haralick edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_haralick = edges_haralick(imdouble, w, h, rhozero, padding_method); iio_save_image_float_vec("im_haralick.png", im_haralick, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_haralick.png.\n"); } /* Marr-Hildreth (Gaussian) edge detection algorithm */ if (argc_mh!=0) { printf("\n\tRunning Marr-Hildreth (Gaussian) edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_mh = edges_mh(imdouble, w, h, sigma_m, n_m, tzc_m, padding_method); iio_save_image_float_vec("im_mh.png", im_mh, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_mh.png.\n"); } /* Marr-Hildreth (LoG) edge detection algorithm */ if (argc_mhl!=0) { printf("\n\tRunning Marr-Hildreth (LoG) edge detection algorithm...\n"); double start = (double)clock(); /* Processing here */ float *im_mhl = edges_mh_log(imdouble, w, h, sigma_l, n_l, tzc_l, padding_method); iio_save_image_float_vec("im_mhl.png", im_mhl, w, h, 1); /* End of processing */ double finish = (double)clock(); double exectime = (finish - start)/CLOCKS_PER_SEC; printf("\tDone! Execution time: %1.3f s.\n", exectime); printf("\tOutput image saved as im_mhl.png.\n"); } /* Memory free */ free(im); free(imdouble); }
int main(int argc, char *argv[]) { /* Check command line arguments */ // Must have at least 3 arguments (one ./indexer, two command line) if (argc < 3) { fprintf(stderr, "Indexer.c program requires at least two input arguments\n"); if (DISPLAY_HELP) {displayhelp();} return 1; } // Check validity of arguments else { // Check argv[1] - webPageDirectory webPageDirectory = strdup(argv[1]); // Check if webPageDirectory is an existing directory if (!IsDir(webPageDirectory)) { fprintf(stderr, "Please provide an existing directory for the file database\n"); if (DISPLAY_HELP) {displayhelp();} free(webPageDirectory); return 1; } // Check if webPageDirectory contains regular files num_files = GetFilenamesInDir(webPageDirectory, &filenames); if(num_files < 0) { fprintf(stderr, "Please provide a directory containing regular files for the file database\n"); if (DISPLAY_HELP) {displayhelp();} free(webPageDirectory); return 1; } // Check if webPageDirectory contains valid docId's for(int index = 0; index < num_files; index++) { char posschar; int possint; // Checks to see if any characters can be picked up if (sscanf(filenames[index], "%d%c", &possint, &posschar) != 1) { fprintf(stderr, "Please provide int's for filenames in file database\n \ The file %s is not a valid filename\n", filenames[index]); if (DISPLAY_HELP) {displayhelp();} // Free all filenames up to this point for (int index2 = 0; index2 < num_files; index2++) { free(filenames[index2]); } free(filenames); free(webPageDirectory); return 1; } } // Check argv[2] - outputFile outputFile = strdup(argv[2]); // Check if outputFile contains .dat extension char *point = strrchr(outputFile,'.'); if (point != NULL ) { if(strcmp(point,".dat") != 0) { fprintf(stderr, "Please provide a .dat file for the outputFile to be written into\n"); // Free all filenames up to this point for (int index = 0; index < num_files; index++) { free(filenames[index]); } free(filenames); free(webPageDirectory); free(outputFile); return 1; } } }
int main (int argc, char *argv[]) { int count; unsigned long maxitems=0; int c; int index; FILE *fp; unsigned long items; char line[MAX_LINE_SIZE]; char pline[MAX_LINE_SIZE]; char unhex[MAX_LINE_SIZE]; char *toprocess; int size; int found=0; /* safe defaults */ opt_errorrate=0.01; opt_bloomfile=NULL; /* load config */ loadconfig(); while ((c = getopt (argc, argv, "huicp:svde:b:f:")) != -1) switch (c) { case 'h': displayhelp(); exit(0); break; case 'u': opt_unhex = 1; break; case 'i': opt_ignorecase = 1; break; case 'c': opt_init = 1; break; case 'p': opt_progressitems = atoi(optarg); break; case 'e': opt_errorrate = atof(optarg); break; case 'b': opt_bloomfile = optarg; break; case 'f': opt_readfromfile = optarg; break; case 's': opt_search = 1; break; case 'v': opt_verbose++; break; case 'd': opt_debug++; break; case '?': if (optopt == 'b') fprintf (stderr, "Option -%c requires an argument.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: abort (); } if (opt_debug) { printf ("opt_init = %d, opt_search = %d, opt_bloomfile = %s\n", opt_init, opt_search, opt_bloomfile); for (count = 1; count < argc; count++) { printf("argv[%d] = %s\n", count, argv[count]); } for (index = optind; index < argc; index++) printf ("Non-option argument %s\n", argv[index]); } if (opt_init) { for (index = optind; index < argc; index++) { if (opt_verbose) fprintf(stderr,"[i] Counting lines for %s\n", argv[index]); fp=fopen(argv[index],"r"); if (fp==NULL) { fprintf(stderr,"Error opening %s\n",argv[index]); break; } items=getlinecount(fp); if (opt_verbose) fprintf(stderr,"[i] %s have %lu lines/items\n",argv[index],items); maxitems=maxitems+items; fclose(fp); } if (opt_verbose) fprintf(stderr,"[i] Maximum number of items: %lu\n",maxitems); bloom_init(&bloom, maxitems, opt_errorrate); items=0; for (index = optind; index < argc; index++) { if (opt_verbose) fprintf(stderr,"[i] Processing %s\n", argv[index]); fp=fopen(argv[index],"r"); if (fp==NULL) { fprintf(stderr,"Error opening %s\n",argv[index]); break; } /* read line by line */ while (fgets (line, sizeof(line), fp)) { toprocess=line; size=strlen(line); if (line[size-1]=='\n') line[--size]='\0'; if (line[size-1]=='\r') line[--size]='\0'; if (opt_debug) fprintf(stderr,"Line (%d): %s \n",size,line); if (opt_verbose && (items++ % opt_progressitems==0)) fprintf(stderr,"\r[i] Line %lu of %lu", items, maxitems); if (opt_ignorecase) { toprocess=str2upper(toprocess,pline); } if (opt_unhex) { size=hexstr2char(toprocess,unhex,MAX_LINE_SIZE); toprocess=unhex; } bloom_add(&bloom, toprocess, size); } if (opt_verbose) fprintf(stderr,"\n[i] Done for %s!\n",argv[index]); fclose(fp); } if (opt_bloomfile==NULL) { fprintf(stderr,"No bloom file specified for init. Not saving.\n"); } else { if (opt_verbose) fprintf(stderr,"[i] Saving to %s\n",opt_bloomfile); bloom_save(&bloom,opt_bloomfile); /* if (opt_verbose) bloom_print(&bloom); */ } } if (opt_search || (!opt_init)) { if (opt_bloomfile==NULL) { fprintf(stderr,"No bloom file specified.\n"); } else { if (opt_verbose) fprintf(stderr,"[i] Opening bloom file: %s\n", opt_bloomfile); if (bloom_load(&bloom, opt_bloomfile)) { fprintf(stderr,"[i] Error loading bloom file: %s\n", opt_bloomfile); return (1); } } if (opt_verbose) fprintf(stderr,"[i] Searching patterns\n"); for (index = optind; index < argc; index++) { toprocess=argv[index]; if (opt_verbose) fprintf(stderr,"[i] Processing %s\n", toprocess); if (searchpattern(toprocess)) { fprintf(stdout,"%s found\n", argv[index]); } else { fprintf(stdout,"%s not found\n", argv[index]); } } if (opt_readfromfile!=NULL) { if (opt_verbose) fprintf(stderr,"[v] Reading from file %s\n",opt_readfromfile); if (strcmp(opt_readfromfile,"-")==0) { fprintf (stderr,"[i] Reading from standard input. Specify pattern separated by new line.\n"); fp=stdin; } else { fp=fopen(opt_readfromfile,"r"); } if (fp==NULL) { fprintf(stderr,"[!] Error opening file: %s\n",opt_readfromfile); exit(1); } while (fgets (line, sizeof(line), fp)) { toprocess=line; size=strlen(line); if (line[size-1]=='\n') line[--size]='\0'; if (line[size-1]=='\r') line[--size]='\0'; if (opt_debug) fprintf(stderr,"[d] Line in pattern (%d): %s \n",size,line); if (opt_verbose) fprintf(stderr,"[v] Processing from file %s\n", toprocess); if (searchpattern(toprocess)) { fprintf(stdout,"%s found\n", toprocess); } else { fprintf(stdout,"%s not found\n", toprocess); } } if (fp!=stdin) fclose (fp); } } }
int main(int argc, char *argv[]) { /*Handle Arguments*/ if( argc == 1 ) { displayhelp(); exit(EXIT_FAILURE); } for (counter=1; counter < argc; counter++) { if ( !strcmp(argv[counter], "-e") || !strcmp(argv[counter], "--eyes") ) { nextarg=(counter + 1); if ( strlen(argv[nextarg]) == 1 ) { strcpy(&eyes, argv[nextarg]); } else { displayhelp(); exit(EXIT_FAILURE); } } else if ( !strcmp(argv[counter], "-t") || !strcmp(argv[counter], "--thought") ) { thought=1; strcpy(&upperbubble, "O"); strcpy(&lowerbubble, "o"); } else if ( !strcmp(argv[counter], "-h") || !strcmp(argv[counter], "--help") ) { displayhelp(); exit(EXIT_SUCCESS); } else if ( !strcmp(argv[counter], "-l") || !strcmp(argv[counter], "--list") ) { displaycowlist(); exit(EXIT_SUCCESS); } } /* Count characters in non-flag arguments */ for(counter=1; counter < argc; counter++) { skiparg=0; if ( !strcmp(argv[counter], "-c") || !strcmp(argv[counter], "--cow") || !strcmp(argv[counter], "-e") || !strcmp(argv[counter], "--eyes") ) { skiparg=1; counter++; } else if ( !strcmp(argv[counter], "-t") || !strcmp(argv[counter], "--thought") ) { skiparg=1; } else if (counter < argc && skiparg == 0) { argscharcount=(argscharcount + 1 + (strlen(argv[counter]))); } } if (argscharcount == 0) { displayhelp(); exit(EXIT_FAILURE); } argscharcount=argscharcount + 1; /* Display speech bubble */ printf(" "); for(counter=1; counter <= argscharcount; counter++) { printf("_"); } if ( thought == 0 ) { printf("\n< "); } else if ( thought == 1 ) { printf("\n( "); } for(counter=1; counter < argc; counter++) { skiparg=0; if ( !strcmp(argv[counter], "-c") || !strcmp(argv[counter], "--cow") || !strcmp(argv[counter], "-e") || !strcmp(argv[counter], "--eyes") ) { skiparg=1; counter++; } else if ( !strcmp(argv[counter], "-t") || !strcmp(argv[counter], "--thought") ) { skiparg=1; } else if ( skiparg == 0 ) { printf("%s ", argv[counter]); } } if ( thought == 0 ) { printf(">\n "); } else if ( thought == 1 ) { printf(")\n "); } for(counter=1; counter <= argscharcount; counter++) { printf("-"); } printf("\n"); printcow(); return 0; }
int main(int argc, char *argv[]) { //File Creation //add edit already existing file funcionality -- Done FILE *fptr; char *string=(char *)calloc(sizeof(char),500); char *rawin=(char *)calloc(sizeof(char),500); //create buffer and build first line HANDLE_CALLOC(buffer,sizeof(line),1); lastline=buffer; lastline->thisline=1; if (argc==1) //no filename given by the user printf("\nUsage: ./sit filename\n\n"),exit(1); if(argc>2) //Too many arguments enetred by the user printf("\nToo many arguments\n\n"); else if(argc==2) //filename received { strcpy(filename,argv[1]); if(fptr= (FILE *)fopen(filename,"r")) //check if file with same name exists readfileintobuffer(fptr); else { if(! (fptr = (FILE *)fopen(filename,"w")))//open file and check if successful printf("Memory error"), exit(1); } } displayhelp(); //Ready to receive commands while(1) { printf("\n?\n"); fflush(stdin); gets(rawin); processraw(rawin,string); switch (command) { case QUIT: if(savebeforequit) { char c; printf("The buffer has been changed since the last save, save now? \n[y or n]? :\t"); scanf("%c",&c); if(c=='y' || c=='Y') write(&fptr); } exit (0); break; case REPLACE: replace(linenum,string); break; case APPEND: append(linenum,string); break; case WRITE: write(&fptr); break; case PRINT: print(); break; case DELETE: if(delete(linenum)==LINE_ERROR) printf("Invalid line number"); break; case COMMAND_ERROR: printf("Command Error, type 'help' for help"); break; case LINE_ERROR: printf("Improper line number"); break; case HELP: displayhelp(); break; default: printf("Command Error"); } command = 0; linenum =0; memset(rawin,'\0',500); memset(string,'\0',500); } }