# include <math.h>
#include <stdio.h>

#include <CVIPtoolkit.h>
#include <CVIPdef.h>
#include <CVIPimage.h>
#include <CVIPdrawimg.h>
#include <CVIPspfltr.h>
#include <CVIPview.h>
#include <CVIPobject.h>
//#include <CVIPconvert.h>
#include <CVIPfeatures.h>
#include <histogram.h>
#include <ObjectContour.h>
#include "CVIPgeometry.h"
#include "bilinear.h"
#include "threshold.h"
#include "CVIParithlogic.h"
#include "histogram.h"
#include "CVIPcolor.h"
#include "CVIPhisto.h"
#include <CVIPspfltr.h>
#include <float.h>
#include "CVIPfs.h"
#include <limits.h>
#include "CVIPnoise.h"
#include "CVIPenhance.h"






#define VIEWER "picture"

int main()
{
 Image *origImage, *cvipImage, *cvipHistogramImage, *tempI, *tempI1;
 IMAGE_FORMAT format;
 char *inputfile, *outputfile;
 unsigned iRows, iCols;
 Matrix *mat;
 float pepperF = 0.03;
 float saltF = 0.03;
 float varr = 100.0;
 float meann = 0.03;

 

 //initialize
 setDisplay_Image(VIEWER, "JPG");
 inputfile = "beifang";
 format = getFormat_CVIP(inputfile);
 origImage = read_Image(inputfile, 1); 
 free(inputfile);
 
 //get image size
 iCols = getNoOfCols_Image(origImage);
 iRows = getNoOfRows_Image(origImage);


 /* (a) (i):
  * translation:do-wrap-up, to left: 30, down 15, whole
                 whole image move,
 */
 tempI = duplicate_Image(origImage);
 tempI = translate(tempI,CVIP_YES,0,0,iRows,iCols,15,30,0);
 write_Image(tempI,"beifangTr.jpg",CVIP_NO,CVIP_NO,format,1);


 /* (a) (ii):
  *rotation:  
             30 degrees
  */
 tempI = duplicate_Image(origImage);
 tempI = rotate(tempI, 30);
 write_Image(tempI,"beifangRot30.jpg",CVIP_NO,CVIP_NO,format,1);

 /*  (a) (ii):
  *scaling:
            shrink to 60% of original size.
	    (can use enlarge/shrink to a definite size)---
 */
 tempI = duplicate_Image(origImage);
 tempI = bilinear_interp(tempI, 0.6);
 write_Image(tempI,"beifang06.jpg",CVIP_NO,CVIP_NO,format,1);


 /* (b) (i):
  *thresholding:
            thresholding value: 128
 */
 tempI = duplicate_Image(origImage);
 tempI = threshold_segment(tempI, 128, CVIP_NO);
 write_Image(tempI,"beifangThreshold128.jpg",CVIP_NO,CVIP_NO,format,1);


 /* (b) (ii):
  * negative:  
            not operation
 */
 tempI = duplicate_Image(origImage);
 tempI = not_Image(tempI);
 write_Image(tempI,"beifangNot.jpg",CVIP_NO,CVIP_NO,format,1);


 /* (C) (i):
    histogram:             
 */
 tempI = duplicate_Image(origImage);
 tempI = get_histogram_Image(tempI);
 write_Image(tempI,"beifangHist.jpg",CVIP_NO,CVIP_NO,format,1);


 /* (C) (ii):
    histogram-equalized:  
            
 */
 tempI = duplicate_Image(origImage);

 tempI = histeq(tempI, 0);
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 tempI1 = duplicate_Image(tempI);
 write_Image(tempI,"beifangHeq.jpg",CVIP_NO,CVIP_NO,format,1);

 
 /* (C) (ii)
    histogram for this new image:
 */
 tempI = get_histogram_Image(tempI1);
 write_Image(tempI,"beifangHist1.jpg",CVIP_NO,CVIP_NO,format,1);

 

 /* (d) (i)
    smoothing via averaging: 3x3
 */
 tempI = duplicate_Image(origImage);
 tempI = mean_filter(tempI, 3);
 write_Image(tempI,"beifangMean3.jpg",CVIP_NO,CVIP_NO,format,1);
 


 /* (d) (ii)
    smoothing via Gaussian(non-linear): 3x3
 */
 tempI = duplicate_Image(origImage);
 tempI = geometric_filter(tempI, 3);
 write_Image(tempI,"beifangGaussBlur3.jpg",CVIP_NO,CVIP_NO,format,1);
 


 /* (d) (ii)
    smoothing via Gaussian(non-linear): 7x7
 */
 tempI = duplicate_Image(origImage);
 tempI = geometric_filter(tempI, 7);
 write_Image(tempI,"beifangGaussBlur7.jpg",CVIP_NO,CVIP_NO,format,1);
 



 /* (e) (i)
    sharpen via Laplacian: algorithm 1
 */
 tempI = duplicate_Image(origImage);
 mat = get_default_filter(HIGHPASS_SPATIAL, 3, 0);
 tempI = (Image *)convolve_filter(tempI,mat);
 
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 write_Image(tempI,"beifangSharpen1.jpg",CVIP_NO,CVIP_NO,format,1);
 
 tempI = duplicate_Image(origImage);
 mat = get_default_filter(HIGHPASS_SPATIAL, 7, 0);
 tempI = (Image *)convolve_filter(tempI,mat);
 
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 write_Image(tempI,"beifangSharpen2.jpg",CVIP_NO,CVIP_NO,format,1);
 


 /* (f) 
    add salt-pepper noise:

 */
 tempI = duplicate_Image(origImage);
 //cast_Image(tempI, CVIP_BYTE);
 tempI = speckle_noise(tempI, &saltF, &pepperF);
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 write_Image(tempI,"beifangSpeckl.jpg",CVIP_YES,CVIP_NO,format,1);


 /* (f) (i)
    clean the above image using smoothing

 */
 tempI1 = duplicate_Image(tempI);
 tempI = smooth_filter(tempI, 3);
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 write_Image(tempI,"beifangSpecklMean3.jpg",CVIP_YES,CVIP_NO,format,1);


 /* (f) (ii)
    clean the above image using median filter

 */
 tempI1 = median_filter(tempI1, 3);
 tempI1 = remap_Image(tempI1, CVIP_BYTE, 0, 255);
 write_Image(tempI1,"beifangSpecklMedian3.jpg",CVIP_YES,CVIP_NO,format,1);



 /* (g) 
    add gaussian noise:

 */
 tempI = duplicate_Image(origImage);
 //cast_Image(tempI, CVIP_BYTE);
 tempI = gaussian_noise(tempI, &varr, &meann);
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 cast_Image(tempI, CVIP_BYTE);
 //view_Image(tempI,"xyzz");
 write_Image(tempI,"beifangGaussian.jpg",CVIP_YES,CVIP_NO,format,CVIP_YES);
 //view_Image(tempI, "xyz");



 /* (g) (i)
    clean the above image using smoothing

 */
 tempI1 = duplicate_Image(tempI);
 tempI = smooth_filter(tempI, 3);
 tempI = remap_Image(tempI, CVIP_BYTE, 0, 255);
 write_Image(tempI,"beifangGaussianMean3.jpg",CVIP_YES,CVIP_NO,format,1);


 /* (g) (ii)
    clean the above image using median filter

 */
 tempI1 = median_filter(tempI1, 3);
 tempI1 = remap_Image(tempI1, CVIP_BYTE, 0, 255);
 write_Image(tempI1,"beifangGaussianMedian3.jpg",CVIP_YES,CVIP_NO,format,1);


 return 1;
} 
