//==============================================================================
//
// pgmstretch.c
//
// use:
// pgmstretch left right 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
	int nn;
	
	int nL, nR; 
	
	byte* pIm2;  // pointer to image matrix with profile
	
	if(nArg==1) 
	{
		puts("stretching of image brightness");
		puts("pgmstretch left right file.pgm result.pgm");
		exit(1);
	}

	nL = atoi(ppArg[1]);
	nR = atoi(ppArg[2]);
	
	ReadPGM(ppArg[3], &pIm, &nW, &nH);
	
	nSize = nW * nH;
	pIm2 = (byte*) malloc(nSize); 
	
	for(i=0; i<nSize; i++)
	{
		nn = (int) pIm[i]; 
		nn = (nn - nL) * 255 / (nR - nL);
		if(nn<0) nn = 0;
		if(nn>255) nn = 255;
		pIm2[i] = nn;
	}

	WritePGM(ppArg[4], pIm2, nW, nH);
	
	free(pIm2);
	free(pIm);
	
	return 0;
}