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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
package ij.plugin;
import ij.*;
import ij.macro.Interpreter;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.measure.*;
import ij.plugin.filter.*;
import ij.plugin.frame.Recorder;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import java.awt.image.ColorModel;
/** This plugin, which concatenates two or more images or stacks,
* implements the Image/Stacks/Tools/Concatenate command.
* Has the option of viewing the concatenated stack as a 4D image.
* @author Jon Jackson j.jackson # ucl.ac.uk
*/
public class Concatenator implements PlugIn, ItemListener{
public String pluginName = "Concatenator";
public int maxEntries = 18; // limit number of entries to fit on screen
private static boolean all_option = false;
private boolean keep = false;
private static boolean keep_option = false;
private boolean batch = false;
private boolean macro = false;
private boolean im4D = true;
private static boolean im4D_option = true;
private String[] imageTitles;
private ImagePlus[] images;
private Vector choices;
private Checkbox allWindows;
private final String none = "-- None --";
private String newtitle = "Untitled";
private ImagePlus newImp;
private int stackSize;
private double min = 0, max = Float.MAX_VALUE;
private int maxWidth, maxHeight;
private boolean showingDialog;
/** Optional string argument sets the name dialog boxes if called from another plugin. */
public void run(String arg) {
macro = !arg.equals("");
if (!showDialog())
return;
newImp = concatenate(images, keep);
if (newImp!=null)
newImp.show();
}
/** Displays a dialog requiring user to choose images and
returns ImagePlus of concatenated images. */
public ImagePlus run() {
if (!showDialog())
return null;
newImp = createHypervol();
return newImp;
}
/** Concatenates two images, stacks or hyperstacks. */
public static ImagePlus run(ImagePlus img1, ImagePlus img2) {
ImagePlus[] images = new ImagePlus[2];
images[0]=img1; images[1]=img2;
return (new Concatenator()).concatenate(images, false);
}
/** Concatenates three images, stacks or hyperstacks. */
public static ImagePlus run(ImagePlus img1, ImagePlus img2, ImagePlus img3) {
ImagePlus[] images = new ImagePlus[3];
images[0]=img1; images[1]=img2; images[2]=img3;
return (new Concatenator()).concatenate(images, false);
}
/** Concatenates four images, stacks or hyperstacks. */
public static ImagePlus run(ImagePlus img1, ImagePlus img2, ImagePlus img3, ImagePlus img4) {
ImagePlus[] images = new ImagePlus[4];
images[0]=img1; images[1]=img2; images[2]=img3; images[2]=img4;
return (new Concatenator()).concatenate(images, false);
}
/** Concatenates five images, stacks or hyperstacks. */
public static ImagePlus run(ImagePlus img1, ImagePlus img2, ImagePlus img3, ImagePlus img4, ImagePlus img5) {
ImagePlus[] images = new ImagePlus[5];
images[0]=img1; images[1]=img2; images[2]=img3; images[2]=img4; images[5]=img5;
return (new Concatenator()).concatenate(images, false);
}
/** Concatenates two or more images, stacks or hyperstacks.
* @param images Array of source images
* @return Returns the concatenated images as an ImagePlus
*/
public static ImagePlus run(ImagePlus[] images) {
return (new Concatenator()).concatenate(images, false);
}
/*
// Why does this not work with Java 6?
public static ImagePlus run(ImagePlus... args) {
return (new Concatenator()).concatenate(args, false);
}
*/
/** Concatenates two or more images or stacks. */
public ImagePlus concatenate(ImagePlus[] ims, boolean keepIms) {
images = ims;
imageTitles = new String[ims.length];
for (int i = 0; i < ims.length; i++) {
if (ims[i] != null) {
imageTitles[i] = ims[i].getTitle();
} else {
IJ.error(pluginName, "Null ImagePlus passed to concatenate(...) method");
return null;
}
}
keep = keepIms;
batch = true;
ImagePlus imp0 = images[0];
if (imp0.isComposite() || imp0.getNChannels()>1)
newImp = concatenateHyperstacks(images, newtitle, keep);
else
newImp = createHypervol();
if (Recorder.scriptMode()) {
String args = "imp1";
for (int i=1; i<images.length; i++)
args += ", imp"+(i+1);
Recorder.recordCall("imp"+(images.length+1)+" = Concatenator.run("+args+");");
}
return newImp;
}
/** Concatenate two images or stacks. */
public ImagePlus concatenate(ImagePlus imp1, ImagePlus imp2, boolean keep) {
images = new ImagePlus[2];
images[0] = imp1;
images[1] = imp2;
return concatenate(images, keep);
}
private ImagePlus createHypervol() {
boolean firstImage = true;
boolean duplicated;
Properties[] propertyArr = new Properties[images.length];
ImagePlus currentImp = null;
ImageStack concat_Stack = null;
stackSize = 0;
int dataType = 0, width= 0, height = 0;
Calibration cal = null;
int count = 0;
findMaxDimensions(images);
for (int i = 0; i < images.length; i++) {
if (images[i] != null) { // Should only find null imp if user has closed an image after starting plugin (unlikely...)
currentImp = images[i];
if (firstImage) { // Initialise based on first image
//concat_Imp = images[i];
cal = currentImp.getCalibration();
width = currentImp.getWidth();
height = currentImp.getHeight();
stackSize = currentImp.getNSlices();
dataType = currentImp.getType();
ColorModel cm = currentImp.getProcessor().getColorModel();
concat_Stack = new ImageStack(maxWidth, maxHeight, cm);
min = currentImp.getProcessor().getMin();
max = currentImp.getProcessor().getMax();
firstImage = false;
}
// Safety Checks
boolean unequalSizes = currentImp.getNSlices()!=stackSize;
if (unequalSizes)
im4D = false;
if (currentImp.getType() != dataType) {
IJ.log("Omitting " + imageTitles[i] + " - image type not matched");
continue;
}
// concatenate
duplicated = isDuplicated(currentImp, i);
concat(concat_Stack, currentImp.getStack(), (keep || duplicated));
propertyArr[count] = currentImp.getProperties();
imageTitles[count] = currentImp.getTitle();
if (! (keep || duplicated)) {
currentImp.changes = false;
currentImp.hide();
}
count++;
}
}
// Copy across info fields
ImagePlus imp = new ImagePlus(newtitle, concat_Stack);
imp.setCalibration(cal);
imp.setProperty("Number of Stacks", new Integer(count));
imp.setProperty("Stacks Properties", propertyArr);
imp.setProperty("Image Titles", imageTitles);
imp.getProcessor().setMinAndMax(min, max);
if (im4D) {
imp.setDimensions(1, stackSize, imp.getStackSize()/stackSize);
imp.setOpenAsHyperStack(true);
}
return imp;
}
// taken from WSR's Concatenator_.java
private void concat(ImageStack stack3, ImageStack stack1, boolean dup) {
int slice = 1;
int size = stack1.getSize();
for (int i = 1; i <= size; i++) {
ImageProcessor ip = stack1.getProcessor(slice);
String label = stack1.getSliceLabel(slice);
if (dup) {
ip = ip.duplicate();
slice++;
} else
stack1.deleteSlice(slice);
ImageProcessor ip2 = ip;
if (ip.getWidth()!=maxWidth || ip.getHeight()!=maxHeight) {
ip2 = ip.createProcessor(maxWidth, maxHeight);
ip2.insert(ip, (maxWidth-ip.getWidth())/2, (maxHeight-ip.getHeight())/2);
}
stack3.addSlice(label, ip2);
}
}
/** Obsolete, replaced by concatenate(images,keep) and Concatenator.run(images). */
public ImagePlus concatenateHyperstacks(ImagePlus[] images, String newTitle, boolean keep) {
int n = images.length;
int width = images[0].getWidth();
int height = images[0].getHeight();
int bitDepth = images[0].getBitDepth();
int channels = images[0].getNChannels();
int slices = images[0].getNSlices();
int frames = images[0].getNFrames();
boolean concatSlices = slices>1 && frames==1;
boolean keepCalibration = true;
Calibration cal = images[0].getCalibration();
maxWidth = width;
maxHeight = height;
for (int i=1; i<n; i++) {
if (images[i].getNFrames()>1)
concatSlices = false;
if (images[i].getBitDepth()!=bitDepth
|| images[i].getNChannels()!=channels
|| (!concatSlices && images[i].getNSlices()!=slices)) {
IJ.error(pluginName, "Images do not all have the same dimensions or type");
return null;
}
Calibration cal2 = images[i].getCalibration();
if (cal2.pixelWidth!=cal.pixelWidth
|| cal2.pixelHeight!=cal.pixelHeight
|| cal2.pixelDepth != cal.pixelDepth)
keepCalibration = false;
if (images[i].getWidth()>maxWidth)
maxWidth = images[i].getWidth();
if (images[i].getHeight()>maxHeight)
maxHeight = images[i].getHeight();
}
ImageStack stack2 = new ImageStack(maxWidth, maxHeight);
int slices2=0, frames2=0;
for (int i=0;i<n;i++) {
ImageStack stack = images[i].getStack();
slices = images[i].getNSlices();
if (concatSlices) {
slices = images[i].getNSlices();
slices2 += slices;
frames2 = frames;
} else {
frames = images[i].getNFrames();
frames2 += frames;
slices2 = slices;
}
for (int f=1; f<=frames; f++) {
for (int s=1; s<=slices; s++) {
for (int c=1; c<=channels; c++) {
int index = (f-1)*channels*slices + (s-1)*channels + c;
ImageProcessor ip = stack.getProcessor(index);
if (keep)
ip = ip.duplicate();
String label = stack.getSliceLabel(index);
ImageProcessor ip2 = ip;
if (ip.getWidth()!=maxWidth || ip.getHeight()!=maxHeight) {
ip2 = ip.createProcessor(maxWidth, maxHeight);
ip2.insert(ip, (maxWidth-ip.getWidth())/2, (maxHeight-ip.getHeight())/2);
}
stack2.addSlice(label, ip2);
}
}
}
}
ImagePlus imp2 = new ImagePlus(newTitle, stack2);
imp2.setDimensions(channels, slices2, frames2);
if (channels>1) {
int mode = 0;
if (images[0].isComposite())
mode = ((CompositeImage)images[0]).getMode();
imp2 = new CompositeImage(imp2, mode);
((CompositeImage)imp2).copyLuts(images[0]);
}
if (channels>1 && frames2>1)
imp2.setOpenAsHyperStack(true);
if (keepCalibration)
imp2.setCalibration(cal);
if (!keep) {
for (int i=0; i<n; i++) {
images[i].changes = false;
images[i].close();
}
}
return imp2;
}
private boolean showDialog() {
boolean all_windows = false;
batch = Interpreter.isBatchMode();
macro = macro || (IJ.isMacro()&&Macro.getOptions()!=null);
if (Menus.commandInUse("Stack to Image5D") && !batch)
im4D = true;
showingDialog = Macro.getOptions()==null;
if (macro) {
String options = Macro.getOptions();
if (options.contains("stack1")&&options.contains("stack2"))
Macro.setOptions(options.replaceAll("stack", "image"));
int macroImageCount = 0;
options = Macro.getOptions();
while (true) {
if (options.contains("image"+(macroImageCount+1)))
macroImageCount++;
else
break;
}
maxEntries = macroImageCount;
}
// Checks
int[] wList = WindowManager.getIDList();
if (wList==null) {
IJ.error("No windows are open.");
return false;
} else if (wList.length < 2) {
IJ.error("Two or more windows must be open");
return false;
}
int nImages = wList.length;
String[] titles = new String[nImages];
String[] titles_none = new String[nImages + 1];
for (int i=0; i<nImages; i++) {
ImagePlus imp = WindowManager.getImage(wList[i]);
if (imp!=null) {
titles[i] = imp.getTitle();
titles_none[i] = imp.getTitle();
} else {
titles[i] = "";
titles_none[i] = "";
}
}
titles_none[nImages] = none;
GenericDialog gd = new GenericDialog(pluginName);
gd.addCheckbox("All_open windows", all_option);
gd.addChoice("Image1:", titles, titles[0]);
gd.addChoice("Image2:", titles, titles[1]);
for (int i = 2; i < ((nImages+1)<maxEntries?(nImages+1):maxEntries); i++)
gd.addChoice("Image" + (i+1)+":", titles_none, titles_none[i]);
gd.addStringField("Title:", newtitle, 16);
gd.addCheckbox("Keep original images", keep_option);
gd.addCheckbox("Open as 4D_image", im4D_option);
if (!macro) { // Monitor user selections
choices = gd.getChoices();
for (Enumeration e = choices.elements() ; e.hasMoreElements() ;)
((Choice)e.nextElement()).addItemListener(this);
Vector v = gd.getCheckboxes();
allWindows = (Checkbox)v.firstElement();
allWindows.addItemListener(this);
if (all_option) itemStateChanged(new ItemEvent(allWindows, ItemEvent.ITEM_STATE_CHANGED, null, ItemEvent.SELECTED));
}
gd.showDialog();
if (gd.wasCanceled())
return false;
all_windows = gd.getNextBoolean();
all_option = all_windows;
gd.setSmartRecording(true);
newtitle = gd.getNextString();
gd.setSmartRecording(false);
keep = gd.getNextBoolean();
keep_option = keep;
im4D = gd.getNextBoolean();
im4D_option = im4D;
ImagePlus[] tmpImpArr = new ImagePlus[nImages+1];
String[] tmpStrArr = new String[nImages+1];
int index=0, count = 0;
for (int i=0; i<=nImages; i++) { // compile a list of images to concatenate from user selection
if (all_windows) { // Useful to not have to specify images in batch mode
index = i;
} else {
if (i == ((nImages+1)<maxEntries?(nImages+1):maxEntries) ) break;
gd.setSmartRecording(i==nImages);
index = gd.getNextChoiceIndex();
}
if (index >= nImages) break; // reached the 'none' string or handled all images (in case of all_windows)
if (! titles[index].equals("")) {
tmpStrArr[count] = titles[index];
tmpImpArr[count] = WindowManager.getImage(wList[index]);
count++;
}
}
if (count<2) {
IJ.error(pluginName, "Please select at least 2 images");
return false;
}
imageTitles = new String[count];
images = new ImagePlus[count];
System.arraycopy(tmpStrArr, 0, imageTitles, 0, count);
System.arraycopy(tmpImpArr, 0, images, 0, count);
return true;
}
// test if this imageplus appears again in the list
boolean isDuplicated(ImagePlus imp, int index) {
int length = images.length;
if (index >= length - 1) return false;
for (int i = index + 1; i < length; i++) {
if (imp == images[i]) return true;
}
return false;
}
public void itemStateChanged(ItemEvent ie) {
Choice c;
if (ie.getSource() == allWindows) { // User selected / unselected 'all windows' button
int count = 0;
if (allWindows.getState()) {
for (Enumeration e = choices.elements() ; e.hasMoreElements() ;) {
c = (Choice)e.nextElement();
c.select(count++);
c.setEnabled(false);
}
} else {
for (Enumeration e = choices.elements() ; e.hasMoreElements() ;) {
c = (Choice)e.nextElement();
c.setEnabled(true);
}
}
} else { // User image selection triggered event
boolean foundNone = false;
// All image choices after an occurance of 'none' are reset to 'none'
for (Enumeration e = choices.elements() ; e.hasMoreElements() ;) {
c = (Choice)e.nextElement();
if (! foundNone) {
c.setEnabled(true);
if (c.getSelectedItem().equals(none)) foundNone = true;
} else { // a previous choice was 'none'
c.select(none);
c.setEnabled(false);
}
}
}
}
public void setIm5D(boolean bool) {
im4D_option = bool;
im4D = bool;
}
private void findMaxDimensions(ImagePlus[] images) {
boolean first = true;
ImagePlus currentImp = null;
int dataType = 0;
maxWidth = maxHeight = 0;
for (int i = 0; i < images.length; i++) {
if (images[i] != null) {
currentImp = images[i];
if (first) {
dataType = currentImp.getType();
first = false;
}
if (currentImp.getType() != dataType)
continue;
if (currentImp.getWidth()>maxWidth)
maxWidth = currentImp.getWidth();
if (currentImp.getHeight()>maxHeight)
maxHeight = currentImp.getHeight();
}
}
}
}
|