/*
# mrcManipulate_kaya : $Revision$  
# $Date$ 
# Created by $Author$
# Usage : mrcManipulate_kaya
# Attention
#   $Loccker$
#  	$State$ 
#
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>                  
#define GLOBAL_DECLARATION
#include "../inc/config.h"
#include "Random.h"
#include "Memory.h"
#include <time.h>


#define DEBUG
#include "genUtil.h"
#include "mrcImage.h"
#include "lmrcSinogramFETcorrelationMap.h"

/*
Example:
typedef struct lmrcManipulate_kayaInfo {
	float a;
	int   b;
} lmrcManipulate_kayaInfo;

typedef enum lmrcManipulate_kayaMode {
	a=0,
	b=1
} lmrcManipulate_kayaMode;
*/

int
main(int argc, char* argv[]) 
{
	mrcManipulate_kayaInfo info;
	mrcImage in,in2, out, out2;
	int h,i,j,k,l,x,y;
	int kah[20]={4,9,-7,5,2,-7,-1,-8,-2,9,6,3,-3,0,-1,2,0,-5,4,-7},lah[20]={3,5,4,1,8,1,2,1,7,3,7,5,0,4,6,7,3,4,1,0};
	double data,data1,data2,data3;
	double len,ka,la,kb,lb,kc,lc,curx,cury,temx,temy;
	time_t t;

	init0(&info);
    argCheck(&info, argc, argv);
    init1(&info);

	DEBUGPRINT("Program Start\n");
	mrcFileRead(&in, info.In, "in main",0);
	//mrcFileRead(&in2, info.In2, "in main",0);

	
	out.Header=in.Header;
	/*out2.Header=in2.Header;
	out.HeaderN.x=128;
	out.HeaderN.y=128;
	out2.HeaderN.x=128;
	out2.HeaderN.y=20;
	*/mrcInit(&out,NULL);
	/*mrcInit(&out2,NULL);
	

	for(i=0;i<in.HeaderN.x;i++){
		for(j=0;j<in.HeaderN.x;j++){
			mrcPixelDataSet(&out,i,j,0,0.0, mrcPixelRePart);
		}}
	for(h=0;h<20;h++){
		fprintf(stdout,"k=%d,l=%d\n",kah[h],lah[h]);
		ka=(double)kah[h];
		la=(double)lah[h];
		len=pow((ka*ka+la*la),0.5);
		kb=ka/len; kc=-la/len; lb=la/len; lc=ka/len;
		fprintf(stdout,"ka:%2.1f la:%2.1f len:%5.3f kb:%4.3f lb:%4.3f kc:%4.3f lc:%4.3f\n",ka,la,len,kb,lb,kc,lc);
		for(i=-64;i<64;i++){
			temx=63+i*kc; temy=63+i*lc;
			data=0.0;
			for(j=-64;j<64;j++){
				curx=temx+j*kb; cury=temy+j*lb;
				k=(int)curx; l=(int)cury;
				k+=(int)(2*(curx-(double)k)); l+=(int)(2*(cury-(double)l));
				mrcPixelDataGet(&in,k,l,0,&data1, mrcPixelRePart, mrcPixelHowNearest);
				data1/=255; data+=data1;
			}
			mrcPixelDataSet(&out2,i+64,h,0,data, mrcPixelRePart);
			for(j=-64;j<64;j++){
				curx=temx+j*kb; cury=temy+j*lb;
				k=(int)curx; l=(int)cury;
				k+=(int)(2*(curx-(double)k)); l+=(int)(2*(cury-(double)l));
				mrcPixelDataGet(&out,k,l,0,&data1, mrcPixelRePart, mrcPixelHowNearest);
				data1+=data/255;
				mrcPixelDataSet(&out,k,l,0,data1, mrcPixelRePart);
				curx+=kc;cury+=lc;k=(int)curx; l=(int)cury;
				k+=(int)(2*(curx-(double)k)); l+=(int)(2*(cury-(double)l));
				mrcPixelDataGet(&out,k,l,0,&data1, mrcPixelRePart, mrcPixelHowNearest);
				data1-=data/255/2;mrcPixelDataSet(&out,k,l,0,data1, mrcPixelRePart);
				curx-=kc*2;cury-=lc*2;k=(int)curx; l=(int)cury;
				k+=(int)(2*(curx-(double)k)); l+=(int)(2*(cury-(double)l));
				mrcPixelDataGet(&out,k,l,0,&data1, mrcPixelRePart, mrcPixelHowNearest);
				data1-=data/255/2;mrcPixelDataSet(&out,k,l,0,data1, mrcPixelRePart);
				
			}
		}
	}
	*/

	srandomByTime();
	x=(int)randomUniformGet(-7.999,7.999,0);
	y=(int)randomUniformGet(-7.999,7.999,0);
	

	
	/*data3=0.0;
	for(i=0;i<in.HeaderN.x;i++){
		mrcPixelDataGet(&in,i-1,i,0,&data, mrcPixelRePart, mrcPixelHowNearest);
		mrcPixelDataGet(&in,i,i,0,&data1, mrcPixelRePart, mrcPixelHowNearest);
		mrcPixelDataGet(&in,i+1,i,0,&data2, mrcPixelRePart, mrcPixelHowNearest);
		data3+=(data+data1+data2)/108;
	}
	fprintf(stdout,"%f\n",data3);
	*/	

	/*for(i=0;i<100;i++){
		for(j=0;j<100;j++){
			data1=0.0;
			for(k=0;k<100;k++){
				mrcPixelDataGet(&in,i,j,k,&data, mrcPixelRePart, mrcPixelHowNearest);
				data1+=data/100;
			}
			mrcPixelDataSet(&out,i,j,0,data1, mrcPixelRePart);
		}}
	*/
	/*
	for(i=0;i<9;i++){
		mrcPixelDataGet(&in,i,0,0,&data, mrcPixelRePart, mrcPixelHowNearest);
		mrcPixelDataSet(&out,i,0,0,data, mrcPixelRePart);
		
	}
	mrcPixelDataSet(&out,0,0,0,0.0, mrcPixelRePart);
	*/
	for(i=0;i<in.HeaderN.x;i++){
		for(j=0;j<in.HeaderN.y;j++){
			mrcPixelDataGet(&in,i,j,0,&data, mrcPixelRePart, mrcPixelHowNearest);
			mrcPixelDataSet(&out,i+x,j+y,0,data, mrcPixelRePart);
		}
	}
	
	mrcStatDataSet(&out,0);
			
	/*
	lmrcSinogramFETcorrelationMapCounterMatching(&in, &out, 0);
	*/	
	//mrcStatDataSet(&out,0);
	//mrcStatDataSet(&out2,0);
	mrcFileWrite(&out, info.Out, "in main" ,0);
	//mrcFileWrite(&out2, info.Out2, "in main" ,0);
	

	exit(EXIT_SUCCESS);
}

void
additionalUsage()
{
	fprintf(stderr, "----- Additional Usage -----\n");
}
