//==============================================================================
//
// pgmups2.c
//
// use:
// pgmups2  file.pgm result.pgm
//
//==============================================================================

#include <stdio.h>
#include "pgm.h"


//==============================================================================
int main(int nArg, char** ppArg)
{
	int i, j;
	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("two times image upsampling (zooming)");
		puts("pgmups2  file.pgm result.pgm");
		exit(1);
	}

	ReadPGM(ppArg[1], &pIm,  &nW, &nH);
	
	nSize = nW * nH;
	pIm2 = (byte*) malloc(4*nSize); 
	
	for(i=0; i<nSize; i++)
	{
		nB = pIm[i];
		nX = i % nW;
		nY = i / nW;
		pIm2[2*nY*nW*2 + 2*nX] = nB;
		pIm2[2*nY*nW*2 + 2*nX+1] = nB;
		pIm2[(2*nY+1)*nW*2 + 2*nX] = nB;
		pIm2[(2*nY+1)*nW*2 + 2*nX+1] = nB;
	}
	
	WritePGM(ppArg[2], pIm2, 2*nW, 2*nH);
	
	free(pIm2);
	free(pIm);
	
	return 0;
}