//These macros were written by Steve Rothery
//FILM - Facility for Imaging in Light Microscopy, Imperial College London
//http://www3.imperial.ac.uk/imagingfacility/


//////////////////////////////////////////////////////////////1

macro "Image Information Summary Action Tool - Cbbg V00ff C00g O00ff  T7e14i T8e14i T7d14i T8d14i" 
{
//get info
fn=getTitle();
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);
Stack.getUnits(X, Y, Z, Time, value);
getVoxelSize(px,py,pz,unit);

//for timee series
if (ImageFrames>1){
frInt=Stack.getFrameInterval();
ft=Time;
fr=frInt;
if (fr>1 && ft=="sec"){
	fr=fr/60;
	ft="min";
}	
if (fr>1 && ft=="min"){
	fr=fr/60;
	ft="Hr";	
}
frRate=round(1/fr);
dur=ImageFrames/frRate;
}

//Bit depth
bd1=bitDepth();
bd=bd1;
if (bd1==24){
	bd=8;
}


setBatchMode(true);
///////////////////////graphs/////////////////////////


//for multichannel and or multislice
if (ImageChannels>1){
	run("Make Composite");	
for (Ch=1;Ch<=ImageChannels;Ch++){
	selectWindow(fn);
	run("Duplicate...", "title=int duplicate channels="+Ch);
	selectWindow("int");
	col=lookup();
	if (ImageSlices>1 || ImageFrames>1){
	Stack.getStatistics(voxelCount, mean, min, max, stdDev);
	getRawStatistics(area1, mean1, min1, max1, std1, histogram);
	}
	else{
	getRawStatistics(area, mean, min, max, std, histogram);	
	}	
close();
Plot_Histogram();
}
//stack histograms
run("Images to Stack", "name=Histogram title=Histogram");
run("Make Montage...", "columns=1 rows="+ImageChannels+" scale=1 border=2");
selectWindow("Histogram");
close();
selectWindow("Montage");
rename("Graphs");
getDimensions(gWidth, gHeight, gChannels, gSlices, gFrames);
}


//single channel and or mutlislice
else{
		if (ImageSlices>1 || ImageFrames>1){
	Stack.getStatistics(voxelCount, mean, min, max, stdDev);  
	getRawStatistics(area1, mean1, min1, max1, std1, histogram);
	}
	else{
	getRawStatistics(npix, mean, min, max, std, histogram);	
	}
Ch="1";
col="Red";
Plot_Histogram();
rename("Graphs");
getDimensions(gWidth, gHeight, gChannels, gSlices, gFrames);	
}



////////////////////////////////images////////////////


if (ImageChannels>1){
for (Ch=1;Ch<=ImageChannels;Ch++){
selectWindow(fn);
run("Duplicate...", "title=Dup duplicate channels="+Ch);
Image_Process();
rename("Chan "+Ch);
}


//stack Images
run("Images to Stack", "name=Images title=Chan");
run("Make Montage...", "columns=1 rows="+ImageChannels+" scale=.5 border=2");
selectWindow("Images");
close();

}
//for single channel
else{
	selectWindow(fn);
	run("Duplicate...", "title=Dup duplicate");
Image_Process();	
}


//////////////scale and join//////////
selectWindow("Montage");
run("RGB Color");
rename("Thumbnail");
getDimensions(ImWidth, ImHeight, ImChannels, ImSlices, ImFrames);
sc=gHeight/ImHeight;
run("Scale...", "x="+sc+" y="+sc+" width="+ImWidth*sc+" height="+ImHeight*sc+" interpolation=Bilinear average create");
rename("Images");
run("Combine...", "stack1=Graphs stack2=Images");
rename(fn+" Summary");
selectWindow("Thumbnail");
close();
selectWindow(fn+" Summary");
setBatchMode("show");


///////////////end/////////

function Image_Process(){
//for Z and Time
if (ImageSlices>1 && ImageFrames>1){
run("Z Project...", "projection=[Max Intensity] all");
selectWindow("Dup");
close();
selectWindow("MAX_Dup");
rename("Dup");
}

//for z or time
if (ImageSlices>1 || ImageFrames>1){
selectWindow("Dup");
run("Z Project...", "projection=[Max Intensity]");
selectWindow("Dup");
close();
selectWindow("MAX_Dup");
}
rename("Montage");
if (bd1!=24){

setMinAndMax(0, pow(2,bd));
run("HiLo");
}
}


function Plot_Histogram(){
//plot routine
	Array.getStatistics(histogram, Gmin, Gmax, Gmean, GstdDev);
	Plot.create("Histogram Channel "+Ch, "Intensity", "Count");
		Plot.setLineWidth(2);
//		Plot.setColor(col,"#ddddff");
		Plot.setColor(col,col);		
		Plot.add("bars", histogram);
		Plot.setColor("Black"); 
		Plot.addText("Px="+px+" "+unit, 0.15, 0.1);
		Plot.addText("Py="+py+" "+unit, 0.15, 0.2);
		Plot.addText("Pz="+pz+" "+unit, 0.15, 0.3);
		Plot.addText("Frames="+ImageFrames, 0.15, 0.4);
		Plot.addText("Size X="+ImageWidth, 0.5, 0.1);
		Plot.addText("Size Y="+ImageHeight, 0.5, 0.2);
		Plot.addText("Z slices="+ImageSlices, 0.5, 0.3);
		Plot.addText("Channel "+Ch, 0.8, 0.1);
		Plot.addText("Min="+min, 0.8, 0.2);
		Plot.addText("Max="+max, 0.8, 0.3);
		Plot.addText("Bit Depth="+bd1, 0.8, 0.4);
		Plot.setLimits(0, pow(2,bd),0, Gmax+Gmax*0.05);
if (ImageFrames>1){
		Plot.addText(frRate+" frames / "+ft, 0.15, 0.5); 
		Plot.addText("Interval="+frInt+" "+Time, 0.5, 0.4);
		Plot.addText("duration="+dur+" "+ft, 0.5, 0.5);
}	
	Plot.show();
}	


function lookup(){
getLut(reds, greens, blues);
Array.getStatistics(reds, min, max, mean, stdDev);
re=max;
RED=toHex(re);
if (re<17){
RED=RED+"0";	
}
Array.getStatistics(greens, min, max, mean, stdDev);
gr=max;
GREEN=toHex(gr);
if (gr<17){
GREEN=GREEN+"0";	
}
Array.getStatistics(blues, min, max, mean, stdDev);
bl=max;
BLUE=toHex(bl);
if (bl<17){
BLUE=BLUE+"0";	
}
col="#"+RED+GREEN+BLUE;
if (col=="#ffffff"){
col="Black";
}
return col;
}

}



///////////////////////////////////////////////////////////////////////2

macro "Reset Brightness & Contrast Action Tool - C00g T2g08m Tbg08m Cg00 T2a08r T6a08a Tba08w C000 T0408B T7408& Te408C"
{
//initialise
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);
Bit=bitDepth();

if (Bit==24){
	ans=getString("This is an RGB image. Do you want to make it composite", "No");
	if (ans=="No"){
		exit();
		}
		run("Make Composite");
}

cols=newArray("Blue","Green","Red","Magenta","Cyan","Yellow","Light Blue", "Grays");
newcol=newArray(ImageChannels);


//get info from user
Dialog.create("Options");
	Option = newArray("min/max", "full scale");
	Dialog.addRadioButtonGroup("Display output - Full scale (raw) or Min/Max ", Option, 1, 2, "min/max");
	Dialog.addCheckbox("Colour channels?", false);
for (Chan=1;Chan<=ImageChannels;Chan++){	
	Dialog.addChoice("Channel "+Chan+" Colour:", cols, cols[Chan-1]);
}
Dialog.show();

Type=Dialog.getRadioButton();
AddColor=Dialog.getCheckbox();
for (Chan=1;Chan<=ImageChannels;Chan++){	
	newcol[Chan-1]=Dialog.getChoice();
}


//main process
setBatchMode(true);


for (Chan=1;Chan<=ImageChannels;Chan++){
	if (ImageChannels>1){
	Stack.setChannel(Chan);
	}
if (ImageSlices>1 || ImageFrames>1){
	Stack.setSlice(ImageSlices/2);
	Stack.setFrame(ImageFrames/2);
	run("Duplicate...", "duplicate channels="+Chan);
	Stack.getStatistics(voxelCount, mean, min, max, stdDev);
	close();
	SetDisplay();
}	
else{
	getRawStatistics(nPixels, mean, min, max, std, histogram);
	SetDisplay();
}
}

setBatchMode("Show");

}

function SetDisplay(){
if (Type=="full scale"){
	setMinAndMax(0, pow(2,Bit));
}
else {
	setMinAndMax(min,max);
}
	if (AddColor==true){  
	run(newcol[Chan-1]);
}
}




//////////////////////////////////////////////////////////////////3

macro "Cytofluorogram from JACoP data Action Tool -C00g H0f54f1cd0h00 C0g0 H0h76e2ab0h00 Cg00 H0h88c38b0h00 Cg00  C000 L000h L0hhh"
{

msg = "Before running this macro \n \n Run the JACoP analysis, checking the Cytofluorochrome option. \n \n Keep the generated Plot and log windows open";
waitForUser("Cytofluorogram Macro", msg);	
waitForUser("", "select one of the original images");	
bd=bitDepth(); 


//chech to see if results table exists and rename if not
if (isOpen("Results")==false){
	waitForUser("", "select original plot window created by JACoP");	
    plwn=getTitle();
    Plot.setStyle(0, "blue,none,1.0,Dot");
    Plot.showValues();
//    selectWindow(plwn);
//    close();
}
if (isOpen("Plot Values")==true){
IJ.renameResults("Plot Values","Results");
}

Channel1="Channel 1";
Channel2="Channel 2";
coef="";

//get info from log window
if (isOpen("Log")==true){
loginfo=getInfo("log");
start1=indexOf(loginfo, "Image A:");
if (start1>1){
end1=indexOf(loginfo, "\nImage B:");
start2=indexOf(loginfo, "Correlation coefficient:");
end2=indexOf(loginfo, "\n\n");
start3=indexOf(loginfo, "a:");
end3=indexOf(loginfo, "\nb:");
start4=indexOf(loginfo, "b:");
end4=indexOf(loginfo, "\nCorrelat");
Channel2=substring(loginfo, start1+9, end1);
Channel1=substring(loginfo, end1+9, end2);
coef="Correlation Coefficient="+substring(loginfo, start2+24, start2+30);
b=parseFloat(substring(loginfo, start3+3, end3));
a=parseFloat(substring(loginfo, start4+3, end4));


}
}
//initialise
setFont("Arial", 12, "bold antialiased");
cols=newArray("Blue","Green","Red","Magenta","Cyan","Yellow","Light Blue","White");

//get info from user
Dialog.create("Options");
	bit = newArray("8", "16");
	Dialog.addRadioButtonGroup("Image Bit depth?", bit, 1, 2, bd);
	Dialog.addCheckbox("Display full Bit depth range", false);	
	Dialog.addString("Channel 1 name (y Axis):", Channel1, 30);
	Dialog.addChoice("Channel 1 Colour (X Axis):", cols, cols[2]);
	Dialog.addString("Channel 2 name (Y Axis):", Channel2, 30);
	Dialog.addChoice("Channel 2 Colour (Yaxis):", cols, cols[1]);
	Dialog.addCheckbox("run smooth filter on the plot", false);
	Dialog.addCheckbox("draw gridlines", true);
	Dialog.addCheckbox("Inculde title", true);
	Dialog.addString("Title:", "Co-Localisation Cytofluorogram", 30);
	fonts=newArray("Calibri","Serif","Arial","SansSerif","Times New Roman");
	Dialog.addChoice("Font", fonts);
	Dialog.addNumber("Correlation Line thickness", 2);
Dialog.show();

//get options
bd=Dialog.getRadioButton();
Full_range=Dialog.getCheckbox();
Ch1Name=Dialog.getString(); 
Ch1=Dialog.getChoice();
Ch2Name=Dialog.getString();
Ch2=Dialog.getChoice();
sm=Dialog.getCheckbox();
gl=Dialog.getCheckbox();
title=Dialog.getCheckbox();
titleName=Dialog.getString();
font=Dialog.getChoice();
ln=Dialog.getNumber();


Ch1NameLen=185-3*(lengthOf(Ch1Name)+11);
Ch2NameLen=185-3*(lengthOf(Ch2Name)+11);

//Y scale offset
offset=25;
if (bd==16){
	offset=12;
}
bt=pow(2,bd)/256;
btx=bt;
bty=bt;




//get data
Xval=Table.getColumn("X1");  
Array.getStatistics(Xval, Xmin, Xmax, Xmean, XstdDev);
Yval=Table.getColumn("Y1");
Array.getStatistics(Yval, Ymin, Ymax, Ymean, YstdDev);

if (Full_range==false){
btx=pow(2,bd)/256*Xmax/pow(2,bd);
bty=pow(2,bd)/256*Ymax/pow(2,bd);
}


setBatchMode("hide");
//create plot image
newImage("Cyto_temp", "16-bit black", 255, 255, 1);
setForegroundColor("White");
selectWindow("Cyto_temp");
for (res=0;res<Xval.length;res++){
val=getPixel(Xval[res]/btx, Yval[res]/bty);
val=val+1;
setPixel(Xval[res]/btx, Yval[res]/bty, val);	
}


//flip plot and add lut and min max
selectWindow("Cyto_temp");
run("Flip Vertically");
run("Canvas Size...", "width=355 height=355 position=Center zero");
run("Canvas Size...", "width=400 height=355 position=Center-Left zero");
run("royal");
run("Enhance Contrast", "saturated=0.35");

//smooth
if (sm==true){
	run("Smooth");
}


//add calibtratioon bar
makeRectangle(330, 100, 1, 1);
run("Calibration Bar...", "location=[At Selection] fill=Black label=White number=5 decimal=0 font=12 zoom=1");
run("Select None");
selectWindow("Cyto_temp");
close();


//add grid lines
selectWindow("Cyto_temp with bar");
if (gl==true){
setForegroundColor(65,65,65);
setLineWidth(1);
if (bd==8){
for (Grln=1;Grln<5;Grln++){
drawLine(50+(50*Grln), 50, 50+(50*Grln), 306);
drawLine(50, 50+(50*Grln), 306, 50+(50*Grln));
}
}
if (bd==16){
for (Grln=1;Grln<7;Grln++){
drawLine(50+(39*Grln), 50, 50+(39*Grln), 306);
drawLine(50, 50+(39*Grln), 306, 50+(39*Grln));
}
}
}

//add axis
setForegroundColor(255,255,255);
setLineWidth(2);
drawRect(50,50,256,256);


run("Line Width...", "line="+ln);
selectWindow("Results");
xVal=Table.getColumn("X0");
yVal=Table.getColumn("Y0");
Array.getStatistics(xVal, min, X_max, mean, stdDev);
Array.getStatistics(yVal, min, Y_max, mean, stdDev);

//draw line
selectWindow("Cyto_temp with bar");
X1=0;
Y1=a/Y_max*256;
X2=256;
Y2=((a+X_max*b)/Y_max)*256;
drawLine(50, 305-Y1, 306, 305-Y2);


//draw title
selectWindow("Cyto_temp with bar");
if (title==true){
setFont(font, 14, "bold antialiased");
drawString(titleName, 80, 25);
}
setFont(font, 13, "antialiased");
drawString(coef, 95, 66);
drawString("counts", 330, 98);

// x axis
run("Colors...", "foreground="+Ch2+" background=black selection="+Ch2);
drawString(Ch2Name+" (Intensity)", Ch2NameLen, 340);
drawString("0", 48, 325);
drawString(round(btx*256), 295, 325);


//y axis
run("Colors...", "foreground="+Ch1+" background=black selection="+Ch1);
drawString("0", 38, 310);
drawString(round(bty*256), offset, 55);

setBatchMode("exit and display");

//add Y title
selectWindow("Cyto_temp with bar");
run("Rotate 90 Degrees Right");
drawString(Ch1Name+" (Intensity)", Ch1NameLen, 25);
run("Rotate 90 Degrees Left");

//finish up
selectWindow("Cyto_temp with bar");
rename("Cytofluorogram");
run("Size...", "width=500 height=444 constrain average interpolation=Bilinear");
run("Sharpen");

selectWindow("Cytofluorogram");
setFont("Arial", 12, "antialiased");
run("Line Width...", "line=1");
setForegroundColor("White");
}


///////////////////////////////////////////////////////////////4

macro "Colour Scale Reference for Orientation J Action Tool -C888 F10ch Cg00 L424e C0g0 L489d C00g L4893 C0gg L48a8 C888 V2744"
{


//initialise
roiManager("Reset");
roiManager("Set Color", "white");

//create image
newImage("Radial", "8-bit black", 300, 300, 1);

txtCol=newArray("white","red","green","blue","cyan","magenta","yellow");


//get constants
Dialog.create("Overlay options");
Dialog.addNumber("Angle change (degrees):", 10);
Dialog.addNumber("LineWidth:", 3);
Dialog.addNumber("Title Text size:", 13);
Dialog.addChoice("Text Colour", txtCol);
Dialog.show(); 


angstep=Dialog.getNumber();
lwidth=Dialog.getNumber();
textsize=Dialog.getNumber();
text_Col=Dialog.getChoice();

run("Colors...", "foreground=white background=black selection="+text_Col);

//create radial array
angnum=180/angstep;
makeLine(50, 150, 250, 150);
roiManager("Set Line Width", lwidth);
roiManager("Add");
for (rot=0;rot<angnum-1;rot++){
	roiManager("Select",rot);
	roiManager("Set Line Width", lwidth);
	run("Rotate...", "rotate angle="+angstep);
	roiManager("Add");
}


//flatten
roiManager("Show All");
run("Flatten");
run("8-bit");
rename("Radial1");

//clear image
run("Specify...", "width=75 height=75 x=150 y=150 oval constrain centered");
setBackgroundColor(0, 0, 0);
run("Clear", "slice");
run("Select None");
makeRectangle(0, 0, 148, 300);
run("Clear", "slice");
run("Select None");

//messasge
title = "Get info";
msg = "Run Orientation J Plugin - Analysis \n \n Use these settings to generate image \n \n Hue:  'Orientation'\n Saturation:  'Coherency'\n Brightness:  'Original Image'\n \n and select OK to continue";
waitForUser(title, msg);

//add scale and text
rename("ColourScale1");
setFont("Arial", textsize, "bold antialiased");
Overlay.drawString("90", 142, 46);
Overlay.drawString("- 90", 138, 272);
Overlay.drawString("0", 257, 158);
Overlay.drawString("45", 225, 80);
Overlay.drawString("- 45", 222, 240);
Overlay.show();
run("Flatten");

//crop
makeRectangle(125, 0, 160, 300);
run("Crop");
rename("ColourScale");


//finish up
selectWindow("ColourScale1");
close();
selectWindow("Radial1");
close();
selectWindow("Radial");
close();


setBackgroundColor(0, 0, 0);
newImage("b", "8-bit ramp", 180, 18, 1);
run("Spectrum");
run("Rotate 90 Degrees Left");
run("RGB Color");
run("Canvas Size...", "width=50 height=240 position=Center zero");
run("Canvas Size...", "width=90 height=240 position=Center-Left zero");

for (l=1;l<20;l++){
	Overlay.drawLine(35, 20+l*10, 40, 20+l*10);
}

setFont("Arial", textsize, "bold antialiased");
Overlay.drawString("90", 60, 38);
Overlay.drawString("45", 60, 83);
Overlay.drawString("0", 60, 129);
Overlay.drawString("- 45", 50, 172);
Overlay.drawString("- 90", 50, 218);
Overlay.show();
run("Flatten");
rename("Bar");
selectWindow("b");
close();

run("Canvas Size...", "width=90 height=300 position=Center-Left zero");
setBackgroundColor(255, 255, 255);
run("Canvas Size...", "width=92 height=300 position=Center-Left");
run("Combine...", "stack1=Bar stack2=ColourScale");

setBackgroundColor(0, 0, 0);

}



////////////////////////////////////////////////5

macro "Number Skeletons from Analyse Skeleton output Action Tool -C00g P0g1f5b94g2 L94a0 P5bbed8 L1f06 Lbedg C000 T14101" 
{
//number skeletons and add to roimanager from analyse skeleton plugin output
requires("1.52r");
waitForUser("","Select the Labeled Skeletons image \n then press ok");

roiManager("reset");
run("Select None");

max=getValue("Max");


for (i=1;i<max+1;i++){
	setThreshold(i, i);
run("Analyze Particles...", "add");
}

n=roiManager("count");
for (roi=0;roi<n;roi++){
	roiManager("select", roi);
	roiManager("rename", "Skeleton "+roi+1);
}
}


/////////////////////////////////////////////////6



macro  "Anaglyph 3D Action Tool - C99g F00hh C0g0 T1d123 T9d12D Cg00 T2e123 Tae12D" 
{

getDimensions(width, height, channels, slices, frames);
if (channels>1 || slices==1){
		
	exit(" This macro only works on a single channel Z-stack");
}


	
Dialog.create("Anaglyph 3D");
	Option=newArray("Red/Green", "Red/Cyan");
	Dialog.addRadioButtonGroup("Which Colour Combination ?", Option, 1, 2, "Red/Green"); 
	Dialog.addNumber("Angle of image separation (usually 5-10 degrees):", 6)
Dialog.show();
cl=Dialog.getRadioButton();

angle=Dialog.getNumber();

start=-(angle/2);



run("3D Project...", "projection=[Brightest Point] axis=Y-Axis slice=1 initial=start total=angle rotation=angle lower=1 upper=255 opacity=0 surface=100 interior=0 all");
run("Stack to Hyperstack...", "order=xyczt(default) channels=2 slices=1 frames=frames display=Composite");

Stack.setChannel(1)
run("Red");
Stack.setChannel(2)
run("Green");

if (cl=="Red/Cyan"){
Stack.setChannel(2)
run("Cyan");	
}
}

///////////////////////////////////////////////////7

macro "Add Graphics to movie Action Tool -Cg00 V5177 V1255 C00g H6ah6he00 L883h L489h C555 F06c9  Cggg T4e08A"
{

ver=IJ.getFullVersion;
//initialise
getDimensions(width, height, channels, slices, frames);
//select slices/frames

//make rgb

setTool("Arrow");
roiManager("Reset");
roiManager("Associate", "true");

title = "Help";
msg = "Select start frame, draw arrow or add text  \n  then select OK";
waitForUser(title, msg);

Stack.getPosition(channel, slice, frame);

tot=frames;
curr=frame;
if (frames==1){
	tot=slices;
	curr=slice;
}


//get info
Dialog.create("Frame select");
  Dialog.addMessage("Select frame range");
  Dialog.addNumber("Start Frame/Slice ",curr);
  Dialog.addNumber("End Frame/Slice", tot);
Dialog.show;
start=Dialog.getNumber();
finish=Dialog.getNumber();


//add rois and flatten
roi=0;
for (i=start;i<=finish;i++){
if (slices==1){
	Stack.setFrame(i);
}
if (frames==1){
	Stack.setSlice(i);
}
//	print("");
	roiManager("Add");
if (ver>"1.52u"){
	roiManager("select", roi);
RoiManager.setPosition(i);
roi=roi+1;
}

	
}

roiManager("Show All");
run("Flatten", "stack");

}




//////////////////////////////////////////////////8

macro "Move Graphis in a Movie Action Tool - Cg00 V5177 V1255 C00g H6ah6he00 L883h L489h C555 F06c9  Cggg T4e08M"
{

//get info
//fn=getTitle();
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);


//check not time and z
if (ImageSlices>1 && ImageFrames>1){
	exit("This macro will only work on a time sereis or Z-stack, not both");
}

roiManager("Show All with labels");
	roiManager("UseNames", "true");

//dialog
n=roiManager("Count");
format=false;
Dialog.create("Options");
	opt = newArray("Yes","No");
	if (n>0){
		Dialog.addChoice("Do you want to keep existing ROI's", opt, "Yes")
	}
if (ImageSlices>ImageFrames){	
	Dialog.addMessage("This Image has:\n     "+ImageSlices+" Slices\n     "+ImageFrames+ " Frames");
	Dialog.addCheckbox("Change Image series from slices to frames?", true);
}

Dialog.addChoice("Show ROI labels", opt, "Yes");
Dialog.addMessage("The macro will processes the image series frame by frame");
Dialog.addMessage("Graphics can be added or edited for each frame \n using the ROI Manager\nTIP: move the Action Required window to one side \n to prevent it hiding other windows");
Dialog.addMessage("Use the ROI Manager to: \nrename ROI's <rename>\nfor text attributes use <More> <Labels> ");
Dialog.show;

//answers
if (n>0){
answer=Dialog.getChoice();
if (answer=="No"){
roiManager("Reset");	
}
}

if (ImageSlices>ImageFrames){
format=Dialog.getCheckbox();	
}


label_show=Dialog.getChoice();
LS="without";
if (label_show=="Yes"){
LS="with";
}


//check for RGB
if (bitDepth()!="RGB"){
	 run("RGB Color", "slices keep");
}

fn=getTitle();

//get total number of frames
total=ImageSlices;
if (ImageFrames>ImageSlices){
	total=ImageFrames;
}


//sawp frames for slices
if (format==true){
	run("Properties...", "slices="+ImageFrames+" frames="+ImageSlices);
}
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);

//select first image and draw rois and name them
selectWindow(fn);
Stack.setPosition(1, 1, 1);
roiManager("Show All "+LS+" labels");
waitForUser("Add rois and rename them");
roiManager("Deselect");
roiManager("Remove Channel Info");
roiManager("Remove Slice Info");
roiManager("Remove Frame Info");
roiManager("Show All "+LS+" labels");
run("Flatten", "slice");
rename("fr1");


//create individual frames
sl=1;
fr=1;
for (im=2;im<=total;im++){
if (ImageFrames>1){
	fr=im;
}
if (ImageSlices>1){
	sl=im;
}
selectWindow(fn);
Stack.setPosition(1, sl, fr);
roiManager("Show All with labels");
waitForUser("Move or edit ROIs");
roiManager("Deselect");
roiManager("Remove Frame Info");
roiManager("Deselect");
roiManager("Show All "+LS+" labels");
run("Flatten", "slice");
rename("fr"+im);
}

//create movie
run("Images to Stack", "name=New_Movie title=fr use");
if (format==true){
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);	
	run("Properties...", "slices="+ImageFrames+" frames="+ImageSlices);
}
roiManager("UseNames", "false");

}





//////////////////////////////////////////////////9

macro "Inset a single image into a larger image/series  Action Tool - C77a F11hh  Cdg4  Faa77 Cggg T3e12S"
{
//inserts a fixed image into a larger image/series


//source
waitForUser("Get info", "Select image to copy \n and select OK to continue");
source=getTitle();
bdSource=bitDepth();
getDimensions(origSourceWidth, origSourceHeight, SourceChannels, SourceSlices, SourceFrames);
run("Duplicate...", "title=insert duplicate");
if (bdSource!=24){
	run("Flatten");
	rename("insert");	
}

//add border
Dialog.create("Border Options");
	Dialog.addCheckbox("Do you want to add a border", true);
	Dialog.addMessage("Select colour for border");
	Colour = newArray("white", "black","red", "green", "blue", "magenta", "yellow", "cyan" );
	Dialog.addRadioButtonGroup("Select colour for border", Colour, 1, 1, "White");
	Dialog.addNumber("Border thickness: ", 2, 0, 4, "pixels" );  
Dialog.show(); 
addborder=Dialog.getCheckbox();
bordercolor = Dialog.getRadioButton(); 
border = Dialog.getNumber(); 

if (addborder==true){
run("Colors...", "background=" + bordercolor);
selectWindow("insert");
newsize="width=" + (origSourceWidth+(border*2)) + " height=" + (origSourceHeight+(border*2)) + " position=Center";
run("Canvas Size...", newsize );
}
getDimensions(SourceWidth, SourceHeight, SourceChannels, SourceSlices, SourceFrames);
//selectWindow("insert");
run("Copy");



//Destination
waitForUser("Get info", "Select destination Image\n and select OK to continue");
destination=getTitle();
getDimensions(DestinationWidth, DestinationHeight, DestinationChannels, DestinationSlices, DestinationFrames);


if (DestinationFrames==1 && DestinationSlices!=1){
convert=getBoolean("Image has only 1 frame, but " + DestinationSlices + " slicess, do you want to convert slices to frames");
if (convert==1){
run("Stack to Hyperstack...", "order=xyczt(default) channels=1 slices=" + DestinationFrames + " frames=" + DestinationSlices + " display=Color");
}
}

getDimensions(DestinationWidth, DestinationHeight, DestinationChannels, DestinationSlices, DestinationFrames);
run("Duplicate...", "title=Dest duplicate");
bdDestination=bitDepth();
if (DestinationChannels==1){
	run("RGB Color");
}
bdDestination=bitDepth();
if (bdDestination!=24){
	run("Flatten");	
}
rename("Dest");

//paste image
selectWindow("Dest");
makeRectangle(0,0,origSourceWidth,origSourceHeight);

//move to location

waitForUser("Get info", "Move pasted iamge to required location  \n and select OK to continue");

getSelectionBounds(x, y, width, height);

if (DestinationFrames>1){

for (Fr=1;Fr<=DestinationFrames;Fr++){
	Stack.setFrame(Fr);
	makeRectangle(x,y,SourceWidth,SourceHeight);
	run("Paste");	
}
}
else{
makeRectangle(x,y,SourceWidth,SourceHeight);
	run("Paste");
}
run("Select None");
rename(destination+" with insert");

selectWindow("insert");
close();

}

//////////////////////////////////////////////////////////////10


macro "Insert Z stack Action Tool - C77a F11hh  Cdg4  Faa77 Cggg T3e12Z " 
{

// select image to copy
waitForUser("Source", "Select source image\n then select OK to continue");
srce=getTitle();

getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);

//selet image  destination
waitForUser("Destination", "select destination image\n then select OK to continue");
dest=getTitle();

//create and move ROI
run("Specify...", "width=ImageWidth height=ImageHeight x=1 y=1");
waitForUser("Roi", "Move Roi to final position \n then select OK to continue");
getBoundingRect(newx, newy, roiwidth, roiheight);
run("Select None");

//insert stack

run("Insert...", "source=["+srce+"] destination=["+dest+"] x=newx y=newy");
}
}


//////////////////////////////////////////////////11

macro "Copy overlay to all slices or frames Action Tool -C000 R00f8 R08f8 T4g08z Tbg08f Cg00 H44b48c00 " 
{
getDimensions(width, height, channels, slices, frames);
n=roiManager("count");

if (n<1){
	exit("No Rois");
	}

if (channels>1){
run("Make Composite");
run("RGB Color", "slices keep");
}


//set roi to position 1
for (roi=0;roi<n;roi++){
roiManager("Select",roi);
Stack.setPosition(1, 1, 1);
roiManager("Update");
roiManager("rename", "Slice/frame "+1+" Roi "+roi)
}

//check image
if (slices==1 && frames==1){
	exit("This macro does not work on a Z time series");
	}
if (slices>1 && frames==1){
	total=slices;
	type1=1;
	type2=0;
	}
if (slices==1 && frames>1){
	total=frames;
	type1=0;
	type2=1;
	}

//main process - copy rois to each slice/frame
for (slfr=2;slfr<=total;slfr++){

for (roi=0;roi<n;roi++){
roiManager("Select",roi);
roiManager("Add");
roiManager("Select",roi+n*(slfr-1));
Stack.setPosition(1, slfr*type1, slfr*type2);
roiManager("Update");
roiManager("rename", "Slice/frame "+slfr+" Roi "+roi)
}

}

//flatten
roiManager("Show All without labels");
run("Flatten", "stack");
}



/////////////////////////////////////////////////////////12

macro "Join a Single Channel Image to a Time Series Action Tool -C000 T0d09C T6d09+ Lece9 Le8h7 C00g Oa499  " 
{
	

//create a new time series image
waitForUser("","Select Time series Image");
fn=getTitle();
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);
getVoxelSize(px, py, pz, unit);
Bit=bitDepth() ;  //( 8, 16 ,24 (RGB) ,32 )


waitForUser("","Select Single Image to add to series");
fn2=getTitle();
getDimensions(InsertWidth, InsertHeight, InsertChannels, InsertSlices, InsertFrames);
getVoxelSize(px, py, pz, unit);
Bit2=bitDepth() ;  //( 8, 16 ,24 (RGB) ,32 )
getLut(InsertReds, InsertGreens, InsertBlues);


if (Bit==24 || Bit2==24){
	exit("This macro does not work on RGB images");
}

if (ImageWidth!=InsertWidth || ImageHeight!=InsertHeight || Bit!=Bit2){
	exit("Images need to have the same dimensions and bit depth");
}

if (InsertChannels>1 || InsertSlices>1 || InsertFrames>1){
	exit("Single image has more than 1 channel, Slice or Frame");
}

//setBatchMode("hide");

selectWindow(fn);
run("Duplicate...", "title=Orig duplicate");
newImage("RFP", Bit+"-bit black", ImageWidth, ImageHeight, 1,1, ImageFrames);
setLut(InsertReds, InsertGreens, InsertBlues);
//copy single image into new imge series
selectWindow(fn2);
run("Copy");



selectWindow("RFP");
for (fr=1;fr<=ImageFrames;fr++){
	Stack.setFrame(fr);
	run("Paste");
}

Dialog.create("Change Channel Look-up Tables")
	Dialog.addMessage("Select Colour for Added channel");
	Colour = newArray("Red", "Green", "Blue", "Cyan", "Magenta", "Yellow", "Grays","Original LUT");
	Dialog.addRadioButtonGroup("Colour", Colour, 3, 1, "Original LUT");
Dialog.show(); 



newcol = Dialog.getRadioButton();
if(newcol!="Original LUT"){
run(newcol);
}

//merge time series
mrg="c1=Orig c2=RFP create";
selectWindow("Orig");
//check for multichannel
if (ImageChannels>1){
run("Split Channels");	
mrg="";
for (chan=1;chan<=ImageChannels;chan++){
mrg=mrg+"c"+chan+"=C"+chan+"-Orig ";	
}
mrg=mrg+"c"+ImageChannels+1+"=RFP create";
}

run("Merge Channels...", mrg);
getDimensions(newWidth, newHeight, newChannels, newSlices, newFrames);

//reset min max
for (chan=1;chan<=newChannels;chan++){
Stack.setChannel(chan);
resetMinAndMax;
}
rename(fn+" Merged");
//setBatchMode("exit and display");

}




/////////////////////////////////////////////////////////13

macro "Set LUT to wavelength colour Action Tool - C66g T1708L F0d33 C0dd F3d33 T7708U C0g0  F6d33 Cgg0 F9d33 Cg00 Te708T Fcd33 Cd08 Ffd33 "
{

if (bitDepth()==24){
	exit("This macro does not work on RGB images"); 
}

wavelength=newArray(350, 355, 360, 365, 370, 375, 380, 385, 390, 395, 400, 405, 410, 415, 420, 425, 430, 435, 440, 445, 
450, 455, 460, 465, 470, 475, 480, 485, 490, 495, 500, 505, 510, 515, 520, 525, 530, 535, 540, 545, 
550, 555, 560, 565, 570, 575, 580, 585, 590, 595, 600, 605, 610, 615, 620, 625, 630, 635, 640, 645, 
650, 655, 660, 665, 670, 675, 680, 685, 690, 695, 700, 705, 710, 715, 720, 725, 730, 735, 740, 745, 
750, 755, 760, 765, 770, 775, 780, 785, 790, 795, 800); 
SpectrumRed=newArray(148, 148, 148, 148, 148, 148, 148, 160, 168, 172, 175, 174, 171, 165, 155, 153, 113, 83, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 212, 106, 127, 145, 160, 174, 186, 
198, 209, 219, 228, 237, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 249, 244, 239, 233, 228, 222, 216, 210, 203, 
196, 189, 182, 174, 166, 157, 148, 148, 148, 148, 148); 
SpectrumGreen=newArray(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 
123, 148, 168, 186, 202, 217, 230, 242, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 
255, 255, 255, 255, 255, 255, 255, 255, 236, 226, 216, 204, 193, 180, 165, 150, 131, 109, 80, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); 
SpectrumBlue=newArray(148, 148, 148, 148, 148, 148, 148, 166, 182, 196, 210, 222, 233, 244, 255, 255, 255, 255, 255, 255, 
255, 255, 255, 255, 255, 255, 255, 255, 255, 224, 186, 136, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); 


wl=getNumber("Input wavelength (350-800nm)", 525);

if (wl<350){
	wl=350;
}
if (wl>800){
	wl=800;
}

ref=(wl-350)/5;

redmax=SpectrumRed[ref];
greenmax=SpectrumGreen[ref];
bluemax=SpectrumBlue[ref];

red=newArray(256);
green=newArray(256);
blue=newArray(256);

for (i=0;i<256;i++){
	red[i]=floor(i*(redmax+1)/256);
	green[i]=floor(i*(greenmax+1)/256);
	blue[i]=floor(i*(bluemax+1)/256);
}

setLut(red, green, blue);
}


////////////////////////////////////////////////14

macro "Rolling & Sequential projections Action Tool - C000 F0044 F5044 Fa044 Ff044 C00g F0d44 F5d44 Fad44 Ffd44 Cg00 L226d L726d Lc26d C000 L88h8 Lf6h8 Lfah8"
{
	
fn=getTitle();
roiManager("reset");
run("Clear Results");
run("Select None");
getDimensions(ImageWidth, ImageHeight, ImageChannels, ImageSlices, ImageFrames);
intval=Stack.getFrameInterval();
getVoxelSize(px, py, pz, unit);
Stack.getUnits(Xu, Yu, Zu, Tu, Vu);

if (ImageFrames==1 && ImageSlices==1){
	exit("This is not a stack ot time series image");
}

if (ImageFrames>1 && ImageSlices>1){
	exit("This macro does not work on a combined Z abd times series");
}

switch=true;
frames=ImageFrames;
if (ImageSlices>1 && ImageFrames==1){
switch=false;
frames=ImageSlices;
}

//dialog
gr=newArray("3","5","7","9","11","13","15");
methods=newArray("Rolling","Sequential");
proj=newArray("Max Intensity","Average Intensity","Min Intensity","Sum Slices");

Dialog.create("Options");
Dialog.addMessage("This macro will create a new enhanced stack either by grouping");
Dialog.addMessage("a rolling projection around each image slice/frame eg 1-5, 2-6, 3-7..... ");
Dialog.addMessage("or by grouping sequential projections eg 1-5, 6-10, 11-15.... ");
Dialog.addRadioButtonGroup("Method", methods, 1, 2, methods[0]);
Dialog.addRadioButtonGroup("Rolling Group Size (images)", gr, 1, 4, gr[0]);
Dialog.addNumber("Sequential Group Size", 2, 0, 1, "(images)");
Dialog.addRadioButtonGroup("Group Projection Type", proj, 4, 1, proj[0]);
Dialog.addCheckbox("Convert Sum Images to 16-bit", true);

Dialog.show();
//inputs
Method=Dialog.getRadioButton();
Roll_group_num=parseInt(Dialog.getRadioButton());
Seq_group_num=Dialog.getNumber();
proj_type=Dialog.getRadioButton();
conv=Dialog.getCheckbox();

//values
Seq_centre_im=(Roll_group_num+1)/2;
range=(Roll_group_num-Seq_centre_im);
join=substring(proj_type, 0, 3);
if (join=="Ave"){
	join="AVG";
}
join=join.toUpperCase;

//extraction
if (Method=="Rolling"){
	for (im=Seq_centre_im;im<=frames-range;im++){
	selectWindow(fn);
	run("Z Project...", "start="+im-range+" stop="+im+range+" projection=["+proj_type+"]");
	}
}

if (Method=="Sequential"){
	for (im=1;im<=frames;im+=Seq_group_num) {
	selectWindow(fn);
	run("Z Project...", "start="+im+" stop="+im+Seq_group_num-1+" projection=["+proj_type+"]");
	}
	if (switch==true){
	intval=intval*Seq_group_num;
	}
	if (switch==false){
	pz=pz*Seq_group_num;
	}	
}

//re-stack
run("Images to Stack", "  title="+join+" use");
if (switch==true){
run("Re-order Hyperstack ...", "channels=[Channels (c)] slices=[Frames (t)] frames=[Slices (z)]");
}
if (conv==true){
	run("16-bit");
}

Stack.setTUnit(Tu);
Stack.setFrameInterval(intval);
setVoxelSize(px, py, pz, unit);
rename(fn+" "+Method+" ("+join+" projection of "+Roll_group_num+" images)");

}




////////////////////////////////////////////////15
