int runFilter(int argc, char *argv[]) { char * filenameInput=argv[1]; char * filenameOutput=argv[2]; unsigned int inputType = guessFilenameTypeStupid(filenameInput); struct Image * inputImage = readImage(filenameInput,inputType,0); struct Image * outputImage = 0; //This will get allocated when and if needed if (inputImage!=0) { unsigned int outputType = guessFilenameTypeStupid(filenameOutput); unsigned int i=0; for (i=0; i<argc; i++) { if ( strcmp(argv[i],"--learn")==0 ) { destroyImage(inputImage); learnImage(filenameInput,atoi(argv[i+1]),atoi(argv[i+2])); exit(0); } else if ( strcmp(argv[i],"--rgbcube")==0 ) { unsigned int dim=32; unsigned char R = (char) atoi(argv[i]+1); unsigned char G = (char) atoi(argv[i]+2); unsigned char B = (char) atoi(argv[i]+3); outputImage = createImage( dim , dim , 3 , 8 ); bitbltColorRGB(outputImage->pixels , 0 , 0 , dim , dim , R , G , B , dim-1 , dim-1); writeImageFile(outputImage,PPM_CODEC ,"new_mX.pnm"); writeImageFile(outputImage,PPM_CODEC ,"new_pX.pnm"); writeImageFile(outputImage,PPM_CODEC ,"new_mY.pnm"); writeImageFile(outputImage,PPM_CODEC ,"new_pY.pnm"); writeImageFile(outputImage,PPM_CODEC ,"new_mZ.pnm"); writeImageFile(outputImage,PPM_CODEC ,"new_pZ.pnm"); destroyImage(outputImage); } else if ( strcmp(argv[i],"--envcube")==0 ) { fprintf(stdout,"Converting Environment Cube \n"); unsigned int outputType = guessFilenameTypeStupid(filenameOutput); //outputImage = createSameDimensionsImage(inputImage); unsigned int outputWidth = inputImage->width; unsigned int outputHeight = (unsigned int ) (3*inputImage->width)/4; outputImage = createImage( outputWidth , outputHeight , 3 , 8 ); createCubeMapFace( outputImage->pixels , outputImage->width , outputImage->height , outputImage->channels , outputImage->bitsperpixel , inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels , inputImage->bitsperpixel ); struct Image * partImg=0; unsigned int outX=0 , outY=0 , outWidth=0 , outHeight=0; getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ -1 , /*y*/ 0 , /*z*/ 0 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_mX.pnm"); destroyImage(partImg); getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ 1 , /*y*/ 0 , /*z*/ 0 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_pX.pnm"); destroyImage(partImg); getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ 0 , /*y*/ -1 , /*z*/ 0 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_mY.pnm"); destroyImage(partImg); getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ 0 , /*y*/ 1 , /*z*/ 0 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_pY.pnm"); destroyImage(partImg); getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ 0 , /*y*/ 0 , /*z*/ -1 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_mZ.pnm"); destroyImage(partImg); getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ 0 , /*y*/ 0 , /*z*/ 1 , &outX , &outY , &outWidth , &outHeight ); partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight ); writeImageFile(partImg,PPM_CODEC ,"new_pZ.pnm"); destroyImage(partImg); } else if ( strcmp(argv[i],"--compare")==0 ) { unsigned int outputType = guessFilenameTypeStupid(filenameOutput); outputImage = readImage(filenameOutput,outputType ,0); float noise = calculatePSNR( outputImage->pixels , outputImage->width , outputImage->height , outputImage->channels , inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels ); fprintf(stdout,"Compared Detected Noise is %0.4f dB \n",noise); exit(0); } else if ( strcmp(argv[i],"--gaussian")==0 ) { monochrome(inputImage); outputImage = createSameDimensionsImage(inputImage); unsigned int normalizeGaussianKernel=1; unsigned int kernelWidth=5; unsigned int kernelHeight=5; float * convolutionMatrix=allocateGaussianKernel(kernelWidth,kernelHeight,normalizeGaussianKernel); float divisor=1.0; float * inF = copyUCharImage2Float(inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels ); float * outF = (float*) malloc(sizeof(float) * outputImage->width * outputImage->height * outputImage->channels ); convolutionFilter1ChF( outF , outputImage->width , outputImage->height , inF, inputImage->width , inputImage->height , convolutionMatrix , kernelWidth , kernelHeight , &divisor ); free(convolutionMatrix); castFloatImage2UChar(outputImage->pixels, outF, outputImage->width , outputImage->height , outputImage->channels ); free(inF); free(outF); } else if ( strcmp(argv[i],"--ctbilateral")==0 ) { monochrome(inputImage); outputImage = createSameDimensionsImage(inputImage); float sigma = atof(argv[i+1]); constantTimeBilateralFilter( inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels , outputImage->pixels , outputImage->width , outputImage->height ,&sigma //sigma ,atoi(argv[i+2]) //bins ,atoi(argv[i+3]) //useDeriche ); } else if ( strcmp(argv[i],"--deriche")==0 ) { monochrome(inputImage); outputImage = createSameDimensionsImage(inputImage); float sigma = atof(argv[i+1]); dericheRecursiveGaussianGray( outputImage->pixels , outputImage->width , outputImage->height , inputImage->channels , inputImage->pixels , inputImage->width , inputImage->height , &sigma , atoi(argv[i+2]) ); } else if ( strcmp(argv[i],"--dericheF")==0 ) { fprintf(stderr,"This is a test call for casting code , this shouldnt be normally used..\n"); monochrome(inputImage); outputImage = createSameDimensionsImage(inputImage); float sigma = atof(argv[i+1]); //outputImage = copyImage(inputImage); float * inF = copyUCharImage2Float(inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels ); float * outF = (float*) malloc(sizeof(float) * outputImage->width * outputImage->height * outputImage->channels ); dericheRecursiveGaussianGrayF( outF , outputImage->width , outputImage->height , inputImage->channels , inF , inputImage->width , inputImage->height , &sigma , atoi(argv[i+2]) ); castFloatImage2UChar(outputImage->pixels, outF, outputImage->width , outputImage->height , outputImage->channels ); free(inF); free(outF); } else if ( strcmp(argv[i],"--median")==0 ) { outputImage = copyImage(inputImage); medianFilter3ch( outputImage->pixels , outputImage->width , outputImage->height , inputImage->pixels , inputImage->width , inputImage->height , atoi(argv[i+1]) , atoi(argv[i+2]) ); } else if ( strcmp(argv[i],"--meansat")==0 ) { outputImage = copyImage(inputImage); meanFilterSAT( outputImage->pixels , outputImage->width , outputImage->height , outputImage->channels , inputImage->pixels , inputImage->width , inputImage->height , inputImage->channels , atoi(argv[i+1]) , atoi(argv[i+2]) ); } else if ( strcmp(argv[i],"--monochrome")==0 ) { outputImage = copyImage(inputImage); monochrome(outputImage); } else if ( strcmp(argv[i],"--bilateral")==0 ) { outputImage = copyImage(inputImage); bilateralFilter( outputImage->pixels , outputImage->width , outputImage->height , inputImage->pixels , inputImage->width , inputImage->height , atof(argv[i+1]) , atof(argv[i+2]) , atoi(argv[i+3]) ); } else if ( strcmp(argv[i],"--contrast")==0 ) { outputImage = copyImage(inputImage); contrast(outputImage,atof(argv[i+1])); } else if ( strcmp(argv[i],"--sattest")==0 ) { float * tmp = allocateGaussianKernel(3,5.0,1); if (tmp!=0) { free(tmp); } tmp = allocateGaussianKernel(9,5.0,1); if (tmp!=0) { free(tmp); } tmp = allocateGaussianKernel(15,5.0,1); if (tmp!=0) { free(tmp); } summedAreaTableTest(); unsigned int * integralImageOutput = 0; integralImageOutput = generateSummedAreaTableRGB(inputImage->pixels , inputImage->width , inputImage->height); if (integralImageOutput!=0) { free(integralImageOutput); fprintf(stderr,"integralImage test was successful\n"); } } } writeImageFile(outputImage,outputType ,filenameOutput); destroyImage(outputImage); destroyImage(inputImage); return 1; } return 0; }
int main(int argc, char *argv[]) { double time_spent; clock_t begin, end; begin = clock(); install_signal_handler(); struct gengetopt_args_info args_info; cod_t cod_struct; dict_t dict_struct; int parser_ret; parser_ret = cmdline_parser(argc, argv, &args_info); /** parse the user given parameters with gengetopt */ if (parser_ret != 0) { fprintf(stderr, "[ERROR] while calling cmdline_parser\n"); exit(1); } /**Missing Arguments or incorrect arguments ate least one argument must be provided*/ if (argc < 2) { printf("===================== HELP: Some Arguments Avaiable: =====================\n\n"); printf("--encode --> use this to encode an image file, don't forget to supply the file to encode\n"); printf("--parallel-encode --> use this to encode an image file with threads, don't forget to supply the file to encode and the number of threads\n"); printf("--decode --> use this to decode an image file, don't forget to supply the file to decode\n"); printf("--decode-dir --> use this to decode images in a given directory, suply the directory path\n"); printf("--PSNR --> use this to calculate .... between the original and decoded file, must supply both files as arguments\n"); printf("--dict --> use this to suply the dictionary file while encoding or decoding images\n"); printf("--about --> use this to know about the this app programmers\n"); printf("--help --> to know all the arguments avaiable\n\n"); printf("================================================================================\n"); exit(1); } /** About the authors */ if (args_info.about_given){ about(); } /** Decode the given file*/ if (args_info.decode_given){ cod_struct = read_cod_file(args_info.decode_arg); dict_struct = read_dictionary (args_info.dict_arg); decode_pgm(cod_struct, dict_struct, args_info.decode_arg); } /** Calculate PSNR */ if(args_info.PSNR_given){ char *token; char delim[2] = ","; char *original_filename; char *decoded_filename; token = strtok (args_info.PSNR_arg,delim); //point to 1 filename (before delimiter) original_filename = token; token = strtok (NULL, delim); //point to 2 filename (after delimiter) decoded_filename = token; DEBUG("%s", original_filename); DEBUG("%s", decoded_filename); calculatePSNR (original_filename, decoded_filename); } /** ------------------- Project Delivery II ------------------------- */ /** --encode argument given*/ if(args_info.encode_given){ printf("\n[TO BE DONE] option not implemented yet!!!\n"); } /** --parallel-encode argument given*/ if(args_info.parallel_encode_given){ /** --threads argument not given*/ if(!args_info.threads_given) { fprintf(stderr, "[ERROR] --threads <nthreads> parameter is mandatory with --parallel-enconde\n"); exit(1); }else { if(args_info.threads_arg > 1) { printf("\n[TO BE DONE] option not implemented yet!!!\n"); }else { fprintf(stderr, "[ERROR] The threads number must be more than 1\n"); exit(1); } } } if (args_info.decode_dir_given) { printf("\nOption not full implemented yet!!!\n"); } if (args_info.dict_given == 1) { validate_extension(args_info.dict_arg, ".dic"); } /** free the memory allocated by gengetop */ cmdline_parser_free (&args_info); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf ("Excution Time: %.3f s\n", time_spent); return 0; }