1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
package ij.plugin;
import ij.*;
import ij.gui.*;
import ij.process.*;
import ij.measure.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/** Implements the Image/Stacks/Make Montage command. */
public class MontageMaker implements PlugIn {
private static int columns, rows, first, last, inc, borderWidth;
private static double scale;
private static boolean label;
private static boolean useForegroundColor;
private static int saveID;
private static int saveStackSize;
private static final int defaultFontSize = 12;
private static int fontSize = defaultFontSize;
private boolean hyperstack;
public void run(String arg) {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null || imp.getStackSize()==1) {
error("Stack required");
return;
}
hyperstack = imp.isHyperStack();
if (hyperstack && imp.getNSlices()>1 && imp.getNFrames()>1) {
error("5D hyperstacks are not supported");
return;
}
int channels = imp.getNChannels();
if (!hyperstack && imp.isComposite() && channels>1) {
int channel = imp.getChannel();
CompositeImage ci = (CompositeImage)imp;
int mode = ci.getMode();
if (mode==IJ.COMPOSITE)
ci.setMode(IJ.COLOR);
ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight());
for (int c=1; c<=channels; c++) {
imp.setPosition(c, imp.getSlice(), imp.getFrame());
Image img = imp.getImage();
stack.addSlice(null, new ColorProcessor(img));
}
if (ci.getMode()!=mode)
ci.setMode(mode);
imp.setPosition(channel, imp.getSlice(), imp.getFrame());
Calibration cal = imp.getCalibration();
imp = new ImagePlus(imp.getTitle(), stack);
imp.setCalibration(cal);
}
makeMontage(imp);
imp.updateImage();
saveID = imp.getID();
IJ.register(MontageMaker.class);
}
public void makeMontage(ImagePlus imp) {
int nSlices = imp.getStackSize();
if (hyperstack) {
nSlices = imp.getNSlices();
if (nSlices==1)
nSlices = imp.getNFrames();
}
boolean macro = Macro.getOptions()!=null;
if (macro || columns==0 || !(imp.getID()==saveID || nSlices==saveStackSize)) {
columns = (int)Math.sqrt(nSlices);
rows = columns;
int n = nSlices - columns*rows;
if (n>0) columns += (int)Math.ceil((double)n/rows);
scale = 1.0;
if (imp.getWidth()*columns>800)
scale = 0.5;
if (imp.getWidth()*columns>1600)
scale = 0.25;
inc = 1;
first = 1;
last = nSlices;
}
if (macro) {
fontSize = defaultFontSize;
borderWidth = 0;
label = false;
useForegroundColor = false;
}
saveStackSize = nSlices;
GenericDialog gd = new GenericDialog("Make Montage");
gd.addNumericField("Columns:", columns, 0);
gd.addNumericField("Rows:", rows, 0);
gd.addNumericField("Scale factor:", scale, 2);
if (!hyperstack) {
gd.addNumericField("First slice:", first, 0);
gd.addNumericField("Last slice:", last, 0);
}
gd.addNumericField("Increment:", inc, 0);
gd.addNumericField("Border width:", borderWidth, 0);
gd.addNumericField("Font size:", fontSize, 0);
gd.addCheckbox("Label slices", label);
gd.addCheckbox("Use foreground color", useForegroundColor);
gd.showDialog();
if (gd.wasCanceled())
return;
columns = (int)gd.getNextNumber();
rows = (int)gd.getNextNumber();
scale = gd.getNextNumber();
gd.setSmartRecording(true);
if (!hyperstack) {
first = (int)gd.getNextNumber();
last = (int)gd.getNextNumber();
}
inc = (int)gd.getNextNumber();
borderWidth = (int)gd.getNextNumber();
fontSize = (int)gd.getNextNumber();
if (borderWidth<0) borderWidth = 0;
if (first<1) first = 1;
if (last>nSlices) last = nSlices;
if (first>last)
{first=1; last=nSlices;}
if (inc<1) inc = 1;
if (gd.invalidNumber()) {
error("Invalid number");
return;
}
label = gd.getNextBoolean();
useForegroundColor = gd.getNextBoolean();
ImagePlus imp2 = null;
if (hyperstack)
imp2 = makeHyperstackMontage(imp, columns, rows, scale, inc, borderWidth, label);
else
imp2 = makeMontage2(imp, columns, rows, scale, first, last, inc, borderWidth, label);
if (imp2!=null)
imp2.show();
if (macro) {
fontSize = defaultFontSize;
borderWidth = 0;
label = false;
useForegroundColor = false;
columns = 0;
}
}
/** Creates a montage and displays it. */
public void makeMontage(ImagePlus imp, int columns, int rows, double scale, int first, int last, int inc, int borderWidth, boolean labels) {
ImagePlus imp2 = makeMontage2(imp, columns, rows, scale, first, last, inc, borderWidth, labels);
imp2.show();
}
/** Creates a montage and returns it as an ImagePlus. */
public ImagePlus makeMontage2(ImagePlus imp, int columns, int rows, double scale, int first, int last, int inc, int borderWidth, boolean labels) {
int stackWidth = imp.getWidth();
int stackHeight = imp.getHeight();
int nSlices = imp.getStackSize();
int width = (int)(stackWidth*scale);
int height = (int)(stackHeight*scale);
int montageWidth = width*columns + borderWidth*(columns-1);
int montageHeight = height*rows + borderWidth*(rows-1);
ImageProcessor ip = imp.getProcessor();
ImageProcessor montage = ip.createProcessor(montageWidth, montageHeight);
ImagePlus imp2 = new ImagePlus("Montage", montage);
imp2.setCalibration(imp.getCalibration());
montage = imp2.getProcessor();
Color fgColor=Color.white;
Color bgColor = Color.black;
if (useForegroundColor) {
fgColor = Toolbar.getForegroundColor();
bgColor = Toolbar.getBackgroundColor();
} else {
boolean whiteBackground = false;
if ((ip instanceof ByteProcessor) || (ip instanceof ColorProcessor)) {
ip.setRoi(0, stackHeight-12, stackWidth, 12);
ImageStatistics stats = ImageStatistics.getStatistics(ip, Measurements.MODE, null);
ip.resetRoi();
whiteBackground = stats.mode>=200;
if (imp.isInvertedLut())
whiteBackground = !whiteBackground;
}
if (whiteBackground) {
fgColor=Color.black;
bgColor = Color.white;
}
}
montage.setColor(bgColor);
montage.fill();
montage.setColor(fgColor);
montage.setFont(new Font("SansSerif", Font.PLAIN, fontSize));
montage.setAntialiasedText(true);
ImageStack stack = imp.getStack();
int x = 0;
int y = 0;
ImageProcessor aSlice;
int slice = first;
while (slice<=last) {
aSlice = stack.getProcessor(slice);
if (scale!=1.0) {
aSlice.setInterpolationMethod(ImageProcessor.BILINEAR);
boolean averageWhenDownSizing = width<200;
aSlice = aSlice.resize(width, height, averageWhenDownSizing);
}
montage.insert(aSlice, x, y);
String label = stack.getShortSliceLabel(slice);
if (labels)
drawLabel(montage, slice, label, x, y, width, height, borderWidth);
x += width + borderWidth;
if (x>=montageWidth) {
x = 0;
y += height + borderWidth;;
if (y>=montageHeight)
break;
}
IJ.showProgress((double)(slice-first)/(last-first));
slice += inc;
}
if (borderWidth>0) {
for (x=width; x<montageWidth; x+=width+borderWidth) {
montage.setRoi(x, 0, borderWidth, montageHeight);
montage.fill();
}
for (y=height; y<montageHeight; y+=height+borderWidth) {
montage.setRoi(0, y, montageWidth, borderWidth);
montage.fill();
}
}
IJ.showProgress(1.0);
Calibration cal = imp2.getCalibration();
if (cal.scaled()) {
cal.pixelWidth /= scale;
cal.pixelHeight /= scale;
}
imp2.setProperty("Info", "xMontage="+columns+"\nyMontage="+rows+"\n");
return imp2;
}
/** Creates a hyperstack montage and returns it as an ImagePlus. */
private ImagePlus makeHyperstackMontage(ImagePlus imp, int columns, int rows, double scale, int inc, int borderWidth, boolean labels) {
ImagePlus[] channels = ChannelSplitter.split(imp);
int n = channels.length;
ImagePlus[] montages = new ImagePlus[n];
for (int i=0; i<n; i++) {
int last = channels[i].getStackSize();
montages[i] = makeMontage2(channels[i], columns, rows, scale, 1, last, inc, borderWidth, labels);
}
ImagePlus montage = (new RGBStackMerge()).mergeHyperstacks(montages, false);
montage.setCalibration(montages[0].getCalibration());
montage.setTitle("Montage");
return montage;
}
private void error(String msg) {
IJ.error("Make Montage", msg);
}
void drawLabel(ImageProcessor montage, int slice, String label, int x, int y, int width, int height, int borderWidth) {
if (label!=null && !label.equals("") && montage.getStringWidth(label)>=width) {
do {
label = label.substring(0, label.length()-1);
} while (label.length()>1 && montage.getStringWidth(label)>=width);
}
if (label==null || label.equals(""))
label = ""+slice;
int swidth = montage.getStringWidth(label);
x += width/2 - swidth/2;
y -= borderWidth/2;
y += height;
montage.drawString(label, x, y);
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
}
|