//@"Comp_profile_macro.txt"@Ver 3.2   (2009.07.22)
//@@Author; Ryo Higashide
//@MTFvt@CvO 	 
//@摜ɑ΂đIROĨf[^_āCvt@C擾D


run("Duplicate...","title=[Angle measurement.img]");
run("Duplicate...","title=[Angle measurement_2.img]");

Device=getNumber("Device : slit =1 , edge =2 , chart =3",1);
dire=getNumber("direction : vertical=1,horizontal=2",1);

if (dire==1){
	run("Rotate 90 Degrees Left");
}	
else if (dire==2){
}
else{
	exit("error1 : direction input error");
}

width=getWidth();
height=getHeight();
pp=getNumber("pixel size",0.100);		
ANG=getNumber("angle",2.67);		
ww=getNumber("bin",0.020);	
p=pp*1000;
w=ww*1000;

if (ANG>0){
	r=2;
	ANG=abs(ANG);
}
else if (ANG<0){
	r=1;
	ANG=abs(ANG);
}
else{ 
	exit("error5 : ANG = 0");
}

TAN=tan(ANG*PI/180);

POSImax=p*(width-1)+p*TAN*(height-1);
nmax=floor(POSImax/w)+1;
PRO=newArray(nmax);				
COUNT=newArray(nmax);
COM_PRO=newArray(nmax);					

for (I=0;I<width;I++){
	for (J=0;J<height;J++){

if(r==1){					
	POSI=p*I+p*TAN*J;
}
else{
	POSI=p*I+p*TAN*(height-1-J);
}

n=floor(POSI/w);
if(n>=nmax){							
		exit("Input error 1. Please make  'Window'  into a bigger value.")		
}			
PRO[n]=PRO[n]+getPixel(I,J);		
COUNT[n]=COUNT[n]+1;				

	}
}


for (s=0;s<nmax;s++){		
	if(COUNT[s]==0){							
		exit("Input error 3. Please make  'Window'  into a bigger value.")		
			}	
}									

for (s=0;s<nmax;s++){		
	COM_PRO[s]=PRO[s]/COUNT[s];
}


if(Device==1){
	SMAX=0;
	SMIN=COM_PRO[0];

	for (s=0;s<nmax;s++){
		if(COM_PRO[s]>SMAX){
			SMAX=COM_PRO[s];
		}
		else if(COM_PRO[s]<SMIN){
			SMIN=COM_PRO[s];
		}
	}

	SHALF=(SMAX-SMIN)/2+SMIN;

	for (s=0;s<nmax-1;s++){
		if(COM_PRO[s]<=SHALF && COM_PRO[s+1]>SHALF){
			THR1=s;
		}
		else if(COM_PRO[s]>SHALF && COM_PRO[s+1]<=SHALF){
			THR2=s;
		}
	}	
		
	CENT1024=floor((THR1+THR2)/2);

	if(CENT1024<512){
		exit(" error2 ; comp profile date < 1024 ");
	}

	for (s=0;s<1024;s++){
		print(COM_PRO[CENT1024-512+s]);
	}
}	

else if(Device==2){
	Aave = 0;		
    	Bave = 0;		
	Acont= 0;
	for (J=0;J<height;J++){
		for (I=0;I<5;I++){
    			Aave=Aave+getPixel(I,J);
        		Acont=Acont+1;
		}       
	}    
	Aave=Aave/Acont;
    
    	Bcont = 0;
	for (J=0;J<height;J++){
		for (I=width-5;I<width;I++){
			Bave = Bave+getPixel(I,J);
			Bcont = Bcont+1;
        	}
	}		
	Bave=Bave/Bcont;

	THR=(Aave+Bave)/2;
	
	if(Aave>Bave){
		for (s=0;s<nmax-1;s++){
			if(COM_PRO[s]>=THR && COM_PRO[s+1]<THR){
				CENT1024=s;
			}
		}
	}

	else if(Aave<=Bave){			
		for (s=0;s<nmax-1;s++){
			if(COM_PRO[s]<THR && COM_PRO[s+1]>=THR){
				CENT1024=s;
			}
		}
	}

	if(CENT1024<512){
		exit(" error2 ; comp profile date < 1024 ");
	}

	for (s=0;s<1025;s++){
		print(COM_PRO[CENT1024-512+s]);
	}
}	

else if(Device==3){
	for (s=0;s<nmax;s++){				
		print(COM_PRO[s]);
	}
}




/*
Ɛ
{vOɕsĂ҂͂̏C̐ӔCC`܂D܂Cgpɂ
Ȃ鑹Qɑ΂ĂC҂͈؂̐ӔC܂DgpɂĔS
̎Ԃz肵CgpҊel̐ӔCɂĂgpD

]ځC
҂ɋȂC{vO]ڂ邱Ƃւ܂D܂Cςі{vOꕔ
Ƃđgݍ񂾃\tgEFApƂĔ̔邱Ƃւ܂D

쌠
{vOɊւ钘쌠́C҂ł铌o@ɋA܂D
										*/
