/**
    This program requires the PPM library (part of the netpbm package).
    Try "man libppm" for documentation.
    The image size is 640 * 480 pixels.
    The template size is 11 * 11 pixels.
*/

#include <stdio.h>
#include <ppm.h>
#include<stdlib.h>

#define imageWidth 640;
#define imageHeight 480;
#define templateWidth 11;
#define templateHeight 11;

void isTemplateMatch (
int image[480][640][3],     //array of image
int template[11][11][3],  //array of templates color samples 
double thatValue     // the value that will determine whether is it a matched.
);
double matchTemplate(
int xCoordination,
int yCoordination,
int template[11][11][3],
int image[480][640][3]
);
int D (int R1, int R2, int G1, int G2, int B1, int B2 );

int
main(
    int argc,                   /**< Number of input arguments */
    char *argv[]                /**< Input arguments */
)
{
    pixel **ppPixelIn;  
    pixel **templatePixelIn;   
    int cCols, cRows;
    pixval pixvalMax;
    FILE *pFile;
    int image[480][640][3];
    int template1[11][11][3];

    

    /* Initialize the PPM library */

    ppm_init( &argc, argv );

    /* Open the PPM file */

    pFile = fopen( "field.ppm", "r" );
    if ( pFile == NULL ) {
        fprintf( stderr, "%s %d: could not open field.ppm\n", __FILE__,
                 __LINE__ );
        exit( -1 );
    }

    /* Read the image file in its entirety into memory */

    ppPixelIn =  ppm_readppm( pFile, &cCols, &cRows, &pixvalMax );
    if ( ppPixelIn == NULL ) {
        fprintf( stderr, "%s %d: could not read pixel data from field.ppm\n",
                 __FILE__, __LINE__ );
        exit( -1 );
    }
    fclose( pFile );
 


    /* Open the template file */
    pFile = fopen( "template.ppm", "r" );
    if ( pFile == NULL ) {
        fprintf( stderr, "%s %d: could not open field.ppm\n", __FILE__,
                 __LINE__ );
        exit( -1 );
    }

    /* Read the image file in its entirety into memory */

    templatePixelIn =  ppm_readppm( pFile, &cCols, &cRows, &pixvalMax );
    if ( ppPixelIn == NULL ) {
        fprintf( stderr, "%s %d: could not read pixel data from field.ppm\n",
                 __FILE__, __LINE__ );
        exit( -1 );
    }
    fclose( pFile );



    /* Copy R,G and B value into array image and template
	for easier to process.
    */


    int i,j;
    for (i = 0; i < 480 ; i++)
    {
  	for ( j = 0; j < 640 ; j++)
	{
	    image[i][j][0] = (int)PPM_GETR(ppPixelIn[i][j]);
	    image[i][j][1] = (int)PPM_GETG(ppPixelIn[i][j]);
            image[i][j][2] = (int)PPM_GETB(ppPixelIn[i][j]);
	 //   printf("coodination (%d,%d) : R = %d   G = %d   B = %d \n",i,j, image[i][j][0] ,image[i][j][1] ,image[i][j][2] );
	}
    }

     for (i = 0; i < 11 ; i++)
    {
  	for ( j = 0; j < 11 ; j++)
	{
	    template1[i][j][0] = (int)PPM_GETR(templatePixelIn[i][j]);
	    template1[i][j][1] = (int)PPM_GETG(templatePixelIn[i][j]);
            template1[i][j][2] = (int)PPM_GETB(templatePixelIn[i][j]);
	    printf("coodination (%d,%d) : R = %d   G = %d   B = %d \n",i,j, template1[i][j][0] ,template1[i][j][1] ,template1[i][j][2] );
	}
    }
    isTemplateMatch (image,template1, 0.999);   

    return 0;
}


/*
for (row = r to height - r )
for (col = r to width - r )
x = match (row, col, template, image)
if x > thatValue then yes
else no
note:y is some value

*/
void isTemplateMatch (
int image[480][640][3],     //array of image
int template[11][11][3],  //array of templates color samples 
double thatValue     // the value that will determine whether is it a matched.
)
{   
    int i;
    int j;
    double x;
    int time = 0, check = 0;
    for( i = 6; i < 480 - 6; i++)
    {
    	for ( j = 6; j < 640 - 6; j++)
	{
	   x = matchTemplate(i,j,template, image);
	   check++;
           // printf("coordination of template matching is at %d %d   value of x is %f\n", i , j,x );
           if ( x > thatValue)
           {    
   		printf("\n\n\n %d %d is one possible coordination of the template !!!!!!!!!! ", i , j );
                time++;
           }
	}
    }
    printf("%d check =  %d",time, check);
}


/*

match
for each template row
for each template col
diff += D (imgPixel, templatePixel)

return 1/diff
note:the smaller the diff value, the better

goto each pixel in the template, and compare that pixel with the pixel from the image (the same coordination)
We will use the function D to compare.
*/

double matchTemplate(
int xCoordination,
int yCoordination,
int template[11][11][3],
int image[480][640][3]
)
{
    int i,j;
    int diff = 0;
        /*check to see whether we try to match the template within the 
    range of the image(640 * 480).
    */
   
    if( xCoordination > 5 && yCoordination > 5 && 
    xCoordination < 640 - 5 && yCoordination < 480 - 5)
    {
    	for( i = xCoordination - 5 ; i <= xCoordination + 5; i++)
	{
	    for( j = yCoordination - 5 ; j <= yCoordination + 5; j++)
            {
	//	printf("math template is at %d,%d \n",i,j);
		diff += D(image[i][j][0], image[i][j][1], image[i][j][2], template[i][j][0], template[i][j][1], template[i][j][2]);
            }
	}
    }
    
    if (diff ==0 ){
    return 0.00001;  // diff is equal to zero when the template is exactly match the image
		}  // return 0.001 instead of return 1 / 0 which will caise error
  
    return (1 / diff);
   
}


/*

D:(imgPixel, templatePixel)
imgPixel has three value in it: R1, G1 and B1.
templatePixel has three value in it: R2, G2 and B2.
input = (R1 G1 B1) , ( R2 G2 B2)

return the sum of RGB => return abs(R1 -R2) + abs (G1 -G2) + abs (B1 - B2)


*/ 


int D (int R1, int R2, int G1, int G2, int B1, int B2 )
{
    return (abs (R1 - R2) + abs (G1 - G2) + abs (B1 - B2) );

}
