// cpx2pgm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <windows.h>
#include "tiffio.h";


typedef struct _comp {
	double re;
    double im;
} complex, *pcomplex; 


BOOL TiffWrite ( BOOL bRaw16,SIZE resolution, void *image, char *filename)
{
ULONG	stripBytes, numStrips, imageWidth, imageHeight;
USHORT	bitsPerSample;
USHORT	samplesPerPixel;
USHORT	photometric;
struct	tiff * m_tiff;


if ((m_tiff = TIFFOpen(filename, "w" )) == NULL )
		return FALSE;

if (bRaw16) {
	bitsPerSample = 16;
	imageHeight = numStrips = resolution.cy;
	stripBytes = resolution.cx * 2;
	imageWidth = resolution.cx;
}else
{
	bitsPerSample = 8;
	imageHeight = numStrips = resolution.cy;
	imageWidth = stripBytes = resolution.cx;
}
samplesPerPixel = 1;
photometric = 1;

	// All required fields for RGB and grayscale, except Xres, Yres, and res-unit.
	TIFFSetField( m_tiff, TIFFTAG_ROWSPERSTRIP, (ULONG) 1 );
	TIFFSetField( m_tiff, TIFFTAG_BITSPERSAMPLE, bitsPerSample );
	TIFFSetField( m_tiff, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel );
	TIFFSetField( m_tiff, TIFFTAG_IMAGEWIDTH,  imageWidth );
	TIFFSetField( m_tiff, TIFFTAG_IMAGELENGTH, imageHeight );
	TIFFSetField( m_tiff, TIFFTAG_PHOTOMETRIC, photometric );
	TIFFSetField( m_tiff, TIFFTAG_PLANARCONFIG, (USHORT) 1 );	// RGBRGB
	TIFFSetField( m_tiff, TIFFTAG_COMPRESSION,  (USHORT) 1 );	// None


if (bRaw16) {
	// Grayscale 16 bit

	USHORT * srcPtr = static_cast< USHORT * >(image);
	ULONG	stripWords;			// unsigned short per strip
	stripWords = stripBytes >> 1;
	for ( ULONG strip = 0; strip < numStrips; strip++ ) {
		TIFFWriteEncodedStrip( m_tiff, strip, srcPtr, stripBytes );
		srcPtr += resolution.cx;
	}
}else
{
	UCHAR * srcPtr = static_cast< UCHAR * >(image);
	for ( ULONG strip = 0; strip < numStrips; strip++ ) {
		TIFFWriteEncodedStrip( m_tiff, strip, srcPtr, stripBytes );
		srcPtr += resolution.cx;
	}
}

TIFFClose( m_tiff );

return TRUE;

}



int _tmain(int argc, _TCHAR* argv[])
{

	FILE *fr;
	pcomplex data;
	char	*dataA,*dataF;
	complex T;
	SIZE resolution;
	int x = 400; //320;
	int y = 320; //240;
	int i,j,c;
	double maxA,maxF;
	char fn[255],fp[255];

	resolution.cx = x;
	resolution.cy = y;
	data = (pcomplex)malloc(x*y*sizeof(complex));
	dataA = (char *)malloc(x*y*sizeof(dataA[0]));
	dataF = (char *)malloc(x*y*sizeof(dataF[0]));
	
	puts("hledani maximalni hodnoty ze vsech snimku");
	maxA = maxF = 0;
	for(c = 0; c < _wtoi(argv[1]) + 1; c++){
		sprintf(fn,".\\%04d.cpx",c);
		fr = fopen(fn,"rb");
		fread((void *)data,sizeof(complex),x*y,fr);
		for(i = 0; i < y; i++)
			for(j = 0; j < x; j++){
				T = *(data + i * x + j);
				if(maxF < 3.5 + atan2(T.im,T.re)) maxF = 3.5 + atan2(T.im,T.re);
 				if(maxA < pow(pow(T.re,2) + pow(T.im,2),0.5)) maxA = pow(pow(T.re,2) + pow(T.im,2),0.5);
			}
		fclose(fr);
		printf("soubor %04d\r",c);


	}
	printf("\nmax ampl = %lf\nmax faze = %lf\n",maxA,maxF);

	for(c = 0; c < _wtoi(argv[1]) + 1; c++){
		sprintf(fn,".\\%04d.cpx",c);
		fr = fopen(fn,"rb");
		fread((void *)data,sizeof(complex),x*y,fr);
		for(i = 0; i < y; i++)
			for(j = 0; j < x; j++){
				T = *(data + i * x + j);
				*(dataF + i * x + j) = (char)(255 / maxF * (3.5 + atan2(T.im,T.re)));
 				*(dataA + i * x + j) = (char)(255 / maxA * pow(pow(T.re,2) + pow(T.im,2),0.5));
			}

		printf("soubor %04d\r",c);
		fclose(fr);

		// ulozeni obrazku amplitudy
		sprintf(fp,".\\amplituda\\%04d.tif",c);
		TiffWrite(FALSE,resolution,(void *)dataA,fp);
		//ulozeni obrazku faze
		sprintf(fp,".\\faze\\%04d.tif",c);
		TiffWrite(FALSE,resolution,(void *)dataF,fp);
	}
	printf("\n");
		//printf("sizeof complex = %d\n",sizeof(complex));

	return 0;
}

