//==============================================================================
//
// pgmprof.c
//
// use:
// pgmprof file.pgm nrow profile.pgm
//
//==============================================================================

#include <stdio.h>
#include "pgm.h"

#define MAX_LEN   10000

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 image matrix with profile
	int nX, nY;
	int nn;
	
	int prof[MAX_LEN];

	if(nArg==1) 
	{
		puts("image row profile (in text file) and it's graph");
		puts("pgmprof file.pgm nrow profile.pgm");
		exit(1);
	}
	
	ReadPGM(ppArg[1], &pIm, &nW, &nH);
	nn = atoi(ppArg[2]);
//printf("nn=%d\n", nn);
	
	if(nW>MAX_LEN  ||  nn>=nH) exit(1);
	
	pIm2 = (byte*) malloc(nW*512); //image height 512
	
	nSize = nW * nH;
	
	for(i=0; i<nW; i++)
	{
		ii = nn * nW + i;
		prof[i] = 2 * (int)pIm[ii] ;  //image height 512
//printf("i=%4d   prof=%d   %d\n", i, prof[i], pIm[ii]);
	}
	
	for(i=0; i<512; i++)
	{
		for(ii=0; ii<nW; ii++)
		{
			nn = 511-i;
			if(prof[ii] >= nn) pIm2[i*nW+ii] = 255;
		}
	}

	WritePGM(ppArg[3], pIm2, nW, 512);
	
	free(pIm2);
	free(pIm);
	
	return 0;
}