
ij.plugin.RGBStackMerge Maven / Gradle / Ivy
package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.image.*;
/** This plugin implements the Image/Color/Merge Channels command. */
public class RGBStackMerge implements PlugIn {
private static String none = "*None*";
private static int maxChannels = 7;
private static String[] colors = {"red", "green", "blue", "gray", "cyan", "magenta", "yellow"};
private static boolean staticCreateComposite = true;
private static boolean staticKeep;
private static boolean staticIgnoreLuts;
private ImagePlus imp;
private byte[] blank;
private boolean ignoreLuts;
private boolean autoFillDisabled;
private String firstChannelName;
public void run(String arg) {
imp = WindowManager.getCurrentImage();
mergeStacks();
}
public static ImagePlus mergeChannels(ImagePlus[] images, boolean keepSourceImages) {
RGBStackMerge rgbsm = new RGBStackMerge();
return rgbsm.mergeHyperstacks(images, keepSourceImages);
}
/** Combines up to seven grayscale stacks into one RGB or composite stack. */
public void mergeStacks() {
int[] wList = WindowManager.getIDList();
if (wList==null) {
error("No images are open.");
return;
}
String[] titles = new String[wList.length+1];
for (int i=0; i1) {
error("Source hyperstacks cannot have more than 1 channel.");
return;
}
if (img.getNSlices()!=slices || img.getNFrames()!=frames) {
error("Source hyperstacks must have the same dimensions.");
return;
}
mergeHyperstacks = true;
} // isHyperStack
if (img.getWidth()!=width || images[i].getHeight()!=height) {
error("The source images or stacks must have the same width and height.");
return;
}
if (createComposite && img.getBitDepth()!=bitDepth) {
error("The source images must have the same bit depth.");
return;
}
}
ImageStack[] stacks = new ImageStack[maxChannels];
for (int i=0; i2)
extraIChannels++;
if (images[i].getBitDepth()==24)
isRGB = true;
}
}
if (isRGB && extraIChannels>0) {
imp2 = mergeUsingRGBProjection(images, createComposite);
} else if ((createComposite&&!isRGB) || mergeHyperstacks) {
imp2 = mergeHyperstacks(images, keep);
if (imp2==null) return;
} else {
ImageStack rgb = mergeStacks(width, height, stackSize, stacks[0], stacks[1], stacks[2], keep);
imp2 = new ImagePlus("RGB", rgb);
if (createComposite) {
imp2 = CompositeConverter.makeComposite(imp2);
imp2.setTitle("Composite");
}
}
for (int i=0; i=0; i--) {
if (titles!=null && titles[i].startsWith(str) && (firstChannelName==null||titles[i].contains(firstChannelName))) {
name = titles[i];
if (channel==1)
firstChannelName = name.substring(3);
break;
}
}
if (name==null) {
for (int i=titles.length-1; i>=0; i--) {
int index = titles[i].indexOf(colors[channel-1]);
if (titles!=null && index!=-1 && (firstChannelName==null||titles[i].contains(firstChannelName))) {
name = titles[i];
if (channel==1 && index>0)
firstChannelName = name.substring(0, index-1);
break;
}
}
}
if (channel==1 && name==null)
autoFillDisabled = true;
if (name!=null)
return name;
else
return none;
}
public ImagePlus mergeHyperstacks(ImagePlus[] images, boolean keep) {
int n = images.length;
int channels = 0;
for (int i=0; i1?"Merged":"Composite";
ImagePlus imp2 = new ImagePlus(title, stack2);
imp2.setDimensions(channels, slices, frames);
imp2 = new CompositeImage(imp2, IJ.COMPOSITE);
boolean allGrayLuts = true;
for (int c=0; c
© 2015 - 2025 Weber Informatics LLC | Privacy Policy