//==============================================================================
//
// pgmdiffs.c
//
// use:
// pgmdiffs file1.pgm file2.pgm result.pgm
//
//==============================================================================

#include <stdio.h>
#include "pgm.h"

int main(int nArg, char** ppArg)
{
	int i, ii;
	int nW, nH; // image width and height
	int nB;     // image brightness
	byte* pIm;  // pointer to image matrix
	int nSize;  // image size in bytes
	int nn;
	
	byte* pIm2;  // pointer to second image matrix 
	
	byte* pIm3;  // pointer to result image matrix 
	
	if(nArg==1) 
	{
		puts("absolute difference between two pgm files with same width&height with saturation ");
		puts("pgmdiffs file1.pgm file2.pgm result.pgm");
		exit(1);
	}
	
	ReadPGM(ppArg[1], &pIm,  &nW, &nH);
	ReadPGM(ppArg[2], &pIm2, &nW, &nH);
	
	nSize = nW * nH;
	pIm3 = (byte*) malloc(nSize); 
	
	for(i=0; i<nSize; i++)
	{
		nn = abs( (int) pIm[i] - pIm2[i] ); 
		pIm3[i] = nn;
	}

	WritePGM(ppArg[3], pIm3, nW, nH);
	
	free(pIm3);
	free(pIm2);
	free(pIm);
	
	return 0;
}