package ij;
import ij.process.*;
import ij.io.*;
import ij.gui.ImageCanvas;
import ij.util.Tools;
import java.io.*;
import java.awt.*;
import java.awt.image.ColorModel;
import java.util.Properties;
public class VirtualStack extends ImageStack {
private static final int INITIAL_SIZE = 100;
private String path;
private int nSlices;
private String[] names;
private String[] labels;
private int bitDepth;
private Properties properties;
public VirtualStack() { }
public VirtualStack(int width, int height, ColorModel cm, String path) {
super(width, height, cm);
this.path = path;
names = new String[INITIAL_SIZE];
labels = new String[INITIAL_SIZE];
}
public VirtualStack(int width, int height, int slices) {
super(width, height, null);
nSlices = slices;
bitDepth = 8;
}
public void addSlice(String name) {
if (name==null)
throw new IllegalArgumentException("'name' is null!");
nSlices++;
if (nSlices==names.length) {
String[] tmp = new String[nSlices*2];
System.arraycopy(names, 0, tmp, 0, nSlices);
names = tmp;
tmp = new String[nSlices*2];
System.arraycopy(labels, 0, tmp, 0, nSlices);
labels = tmp;
}
names[nSlices-1] = name;
}
public void addSlice(String sliceLabel, Object pixels) {
}
public void addSlice(String sliceLabel, ImageProcessor ip) {
}
public void addSlice(String sliceLabel, ImageProcessor ip, int n) {
}
public void deleteSlice(int n) {
if (n<1 || n>nSlices)
throw new IllegalArgumentException("Argument out of range: "+n);
if (nSlices<1)
return;
for (int i=n; i<nSlices; i++)
names[i-1] = names[i];
names[nSlices-1] = null;
nSlices--;
}
public void deleteLastSlice() {
if (nSlices>0)
deleteSlice(nSlices);
}
public Object getPixels(int n) {
ImageProcessor ip = getProcessor(n);
if (ip!=null)
return ip.getPixels();
else
return null;
}
public void setPixels(Object pixels, int n) {
}
public ImageProcessor getProcessor(int n) {
if (path==null) {
ImageProcessor ip = new ByteProcessor(getWidth(), getHeight());
label(ip, ""+n, Color.white);
return ip;
}
Opener opener = new Opener();
opener.setSilentMode(true);
IJ.redirectErrorMessages(true);
ImagePlus imp = opener.openImage(path, names[n-1]);
IJ.redirectErrorMessages(false);
ImageProcessor ip = null;
int depthThisImage = 0;
if (imp!=null) {
int w = imp.getWidth();
int h = imp.getHeight();
int type = imp.getType();
ColorModel cm = imp.getProcessor().getColorModel();
String info = (String)imp.getProperty("Info");
if (info!=null && !(info.startsWith("Software")||info.startsWith("ImageDescription")))
labels[n-1] = info;
depthThisImage = imp.getBitDepth();
ip = imp.getProcessor();
ip.setOverlay(imp.getOverlay());
properties = imp.getProperty("FHT")!=null?imp.getProperties():null;
} else {
File f = new File(path, names[n-1]);
String msg = f.exists()?"Error opening ":"File not found: ";
ip = new ByteProcessor(getWidth(), getHeight());
ip.invert();
label(ip, msg+names[n-1], Color.black);
depthThisImage = 8;
}
if (depthThisImage!=bitDepth) {
switch (bitDepth) {
case 8: ip=ip.convertToByte(true); break;
case 16: ip=ip.convertToShort(true); break;
case 24: ip=ip.convertToRGB(); break;
case 32: ip=ip.convertToFloat(); break;
}
}
if (ip.getWidth()!=getWidth() || ip.getHeight()!=getHeight()) {
ImageProcessor ip2 = ip.createProcessor(getWidth(), getHeight());
ip2.insert(ip, 0, 0);
ip = ip2;
}
return ip;
}
private void label(ImageProcessor ip, String msg, Color color) {
int size = getHeight()/20;
if (size<9) size=9;
Font font = new Font("Helvetica", Font.PLAIN, size);
ip.setFont(font);
ip.setAntialiasedText(true);
ip.setColor(color);
ip.drawString(msg, size, size*2);
}
public int saveChanges(int n) {
return -1;
}
public int getSize() {
return nSlices;
}
public String getSliceLabel(int n) {
if (labels==null)
return null;
String label = labels[n-1];
if (label==null)
return names[n-1];
else if (label.length()<=60)
return label;
else
return names[n-1]+"\n"+label;
}
public Object[] getImageArray() {
return null;
}
public void setSliceLabel(String label, int n) {
}
public boolean isVirtual() {
return true;
}
public void trim() {
}
public String getDirectory() {
return path;
}
public String getFileName(int n) {
return names[n-1];
}
public void setBitDepth(int bitDepth) {
this.bitDepth = bitDepth;
}
public int getBitDepth() {
return bitDepth;
}
public ImageStack sortDicom(String[] strings, String[] info, int maxDigits) {
int n = getSize();
String[] names2 = new String[n];
for (int i=0; i<n; i++)
names2[i] = names[i];
for (int i=0; i<n; i++) {
int slice = (int)Tools.parseDouble(strings[i].substring(strings[i].length()-maxDigits), 0.0);
if (slice==0) return null;
names[i] = names2[slice-1];
labels[i] = info[slice-1];
}
return this;
}
public Properties getProperties() {
return properties;
}
}