// Macro for ImageJ:
// Construction of absorbance differnce images from intensity images 
// by Naoto Kakuta, Tokyo Metropolitan University 2017/11/30.

//Slice (frame) number used for reference intensity (Iref) construction
srefmin = 2; //minimum slice number
srefmax = 5; //maximum slice number

//File name of current intensity (I) images (stack Tiff)
diri = getDirectory("image");  //directory of I image
fni = getTitle; //file name of I image
pathi = diri+fni; //path of I image

//File name of absorbance difference (DA) images (stack Tiff)
dira = diri; //directory of DA image = directory of current image
fna = "da-"+fni; //file name of DA image
patha = dira+fna; //path of DA image

//Smoothing
run("Multiply...", "value=16 stack"); //12-bit(0-4095) to 16-bit(0-65520) 
run("Smooth", "stack"); //3x3 pixel smoothing

//Iref definision
run("Z Project...", "start="+srefmin+" stop="+srefmax+" projection=[Average Intensity]"); //averaging

//DA calculation
imageCalculator("Divide create 32-bit stack", fni,"AVG_"+fni); //=I/Iref
run("Log", "stack"); //=ln(I/Iref)
run("Multiply...", "value=-0.434300 stack"); //=-log10(I/Iref) = DA

//Save and display
run("Save", "save=["+patha+"]"); //save DA image as stack Tiff

close();
close();
close();

open(patha); //open DA image stack
run("Royal"); //colorization (Royal)
//run("Jet"); //colorization (Jet)

//
//------ END -------