//==============================================================================
//
// pgmrot90m.c
//
// use:
// pgmrot90m 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("left 90 degrees rotation of square image");
		puts("pgmrot90m 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++)
	{
		nX = i % nW;
		nY = i / nW;
		nB = pIm[i];
		ii =  nX*nW+nY;
		if(ii<nSize) pIm2[ ii] = nB;
	}

	WritePGM(ppArg[2], pIm2, nW, nH);
	
	free(pIm2);
	free(pIm);
	
	return 0;
}