//==============================================================================
//
// pgmrot90m.c
//
// put image to another big image (left top corner is nRow, nCol)
//
// use:
//	pgmtopgm file.pgm  bigfile.pgm   nRow  nCol  
//
//==============================================================================

#include <stdio.h>
#include "pgm.h"

//==============================================================================
int main(int nArg, char** ppArg)
{
	int i, j, ii, ii2;
	int nW, nH; // image width and height
	int nW2, nH2; // image width and height
	byte* pIm;  // pointer to image matrix
	byte* pIm2; // pointer to second image matrix 
	int nR, nC;
	
	if(nArg==1) 
	{
		puts("put image to another big image (left top corner is nRow, nCol)");
		puts("pgmtopgm file.pgm  bigfile.pgm   nRow  nCol ");
		exit(1);
	}

	ReadPGM(ppArg[1], &pIm,  &nW, &nH);
	
	ReadPGM(ppArg[2], &pIm2,  &nW2, &nH2);
	
	if(nW>nW2 || nH>nH2)
	{
		free(pIm2);
		free(pIm);
		exit(1);
	}
	
	nR = atoi(ppArg[3]);
	nC = atoi(ppArg[4]);	
		
	for(i=nR; i<(nR+nH); i++)
	{
		for(j=nC; j<(nC+nW); j++)
		{
			ii2 = i*nW2 + j;
			ii= (i-nR)*nW + (j-nC);
			pIm2[ii2] = pIm[ii];
		}
	}
	
	WritePGM(ppArg[2], pIm2, nW2, nH2);

	free(pIm2);
	free(pIm);
	
	return 0;
}