//==============================================================================
//
// pgmmirror.c
//
// use:
// pgmmirror file.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
	byte* pIm2;  // pointer to second image matrix 
	int nX, nY;
	
	if(nArg==1) 
	{
		puts("mirrowing transform of an image");
		puts("pgmmirror file.pgm result.pgm");
		exit(1);
	}

	ReadPGM(ppArg[1], &pIm,  &nW, &nH);
	
	nSize = nW * nH;
	pIm2 = (byte*) malloc(nSize); 
	
	for(i=0; i<nSize; i = i+nW)
	{
		for(ii=0; ii<(nW); ii++)
		{
			nB = pIm[i + ii];
			pIm2[i+(nW-1-ii)] = nB;
		}
	}

	WritePGM(ppArg[2], pIm2, nW, nH);
	
	free(pIm2);
	free(pIm);
	
	return 0;
}