if (IJ.debugMode) IJ.log("FolderOpener: "+directory+" ("+list.length+" files)");
int width=0, height=0, stackSize=1, bitDepth=0;
ImageStack stack = null;
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
Calibration cal = null;
boolean allSameCalibration = true;
IJ.resetEscape();
try {
for (int i=0; i<list.length; i++) {
IJ.redirectErrorMessages();
ImagePlus imp = (new Opener()).openImage(directory, list[i]);
if (imp!=null) {
width = imp.getWidth();
height = imp.getHeight();
bitDepth = imp.getBitDepth();
fi = imp.getOriginalFileInfo();
if (arg==null) {
if (!showDialog(imp, list))
return;
} else {
n = list.length;
start = 1;
increment = 1;
}
break;
}
}
if (width==0) {
IJ.error("Import Sequence", "This folder does not appear to contain any TIFF,\n"
+ "JPEG, BMP, DICOM, GIF, FITS or PGM files.");
return;
}
if (filter!=null && (filter.equals("") || filter.equals("*")))
filter = null;
if (filter!=null) {
int filteredImages = 0;
for (int i=0; i<list.length; i++) {
if (isRegex&&list[i].matches(filter))
filteredImages++;
else if (list[i].indexOf(filter)>=0)
filteredImages++;
else
list[i] = null;
}
if (filteredImages==0) {
if (isRegex)
IJ.error("Import Sequence", "None of the file names match the regular expression.");
else
IJ.error("Import Sequence", "None of the "+list.length+" files contain\n the string '"+filter+"' in their name.");
return;
}
String[] list2 = new String[filteredImages];
int j = 0;
for (int i=0; i<list.length; i++) {
if (list[i]!=null)
list2[j++] = list[i];
}
list = list2;
}
if (sortFileNames)
list = sortFileList(list);
if (n<1)
n = list.length;
if (start<1 || start>list.length)
start = 1;
if (start+n-1>list.length)
n = list.length-start+1;
int count = 0;
int counter = 0;
ImagePlus imp = null;
for (int i=start-1; i<list.length; i++) {
if ((counter++%increment)!=0)
continue;
Opener opener = new Opener();
opener.setSilentMode(true);
IJ.redirectErrorMessages();
if (!openAsVirtualStack||stack==null)
imp = opener.openImage(directory, list[i]);
if (imp!=null && stack==null) {
width = imp.getWidth();
height = imp.getHeight();
stackSize = imp.getStackSize();
bitDepth = imp.getBitDepth();
cal = imp.getCalibration();
if (convertToRGB) bitDepth = 24;
ColorModel cm = imp.getProcessor().getColorModel();
if (openAsVirtualStack) {
stack = new VirtualStack(width, height, cm, directory);
((VirtualStack)stack).setBitDepth(bitDepth);
} else if (scale<100.0)
stack = new ImageStack((int)(width*scale/100.0), (int)(height*scale/100.0), cm);
else
stack = new ImageStack(width, height, cm);
info1 = (String)imp.getProperty("Info");
}
if (imp==null)
continue;
if (imp.getWidth()!=width || imp.getHeight()!=height) {
IJ.log(list[i] + ": wrong size; "+width+"x"+height+" expected, "+imp.getWidth()+"x"+imp.getHeight()+" found");
continue;
}
String label = imp.getTitle();
if (stackSize==1) {
String info = (String)imp.getProperty("Info");
if (info!=null)
label += "\n" + info;
}
if (imp.getCalibration().pixelWidth!=cal.pixelWidth)
allSameCalibration = false;
ImageStack inputStack = imp.getStack();
for (int slice=1; slice<=stackSize; slice++) {
ImageProcessor ip = inputStack.getProcessor(slice);
String label2 = label;
if (stackSize>1) {
String sliceLabel = inputStack.getSliceLabel(slice);
if (sliceLabel!=null)
label2=sliceLabel;
else if (label2!=null && !label2.equals(""))
label2 += ":"+slice;
}
int bitDepth2 = imp.getBitDepth();
if (!openAsVirtualStack) {
if (convertToRGB) {
ip = ip.convertToRGB();
bitDepth2 = 24;
}
if (bitDepth2!=bitDepth) {
if (bitDepth==8) {
ip = ip.convertToByte(true);
bitDepth2 = 8;
} else if (bitDepth==24) {
ip = ip.convertToRGB();
bitDepth2 = 24;
}
}
if (bitDepth2!=bitDepth) {
IJ.log(list[i] + ": wrong bit depth; "+bitDepth+" expected, "+bitDepth2+" found");
break;
}
}
if (slice==1) count++;
IJ.showStatus(count+"/"+n);
IJ.showProgress(count, n);
if (scale<100.0)
ip = ip.resize((int)(width*scale/100.0), (int)(height*scale/100.0));
if (ip.getMin()<min) min = ip.getMin();
if (ip.getMax()>max) max = ip.getMax();
//if (depth>1) label2 = null;
if (openAsVirtualStack) {
if (slice==1) ((VirtualStack)stack).addSlice(list[i]);
} else
stack.addSlice(label2, ip);
}
if (count>=n)
break;
if (IJ.escapePressed())
{IJ.beep(); break;}
//System.gc();
}
} catch(OutOfMemoryError e) {
IJ.outOfMemory("FolderOpener");
if (stack!=null) stack.trim();
}
if (stack!=null && stack.getSize()>0) {
ImagePlus imp2 = new ImagePlus(title, stack);
if (imp2.getType()==ImagePlus.GRAY16 || imp2.getType()==ImagePlus.GRAY32)
imp2.getProcessor().setMinAndMax(min, max);
if (fi==null)
fi = new FileInfo();
fi.fileFormat = FileInfo.UNKNOWN;
fi.fileName = "";
fi.directory = directory;
imp2.setFileInfo(fi); // saves FileInfo of the first image
if (allSameCalibration) {
// use calibration from first image
if (scale!=100.0 && cal.scaled()) {
cal.pixelWidth /= scale/100.0;
cal.pixelHeight /= scale/100.0;
}
if (cal.pixelWidth!=1.0 && cal.pixelDepth==1.0)
cal.pixelDepth = cal.pixelWidth;
if (cal.pixelWidth<=0.0001 && cal.getUnit().equals("cm")) {
cal.pixelWidth *= 10000.0;
cal.pixelHeight *= 10000.0;
cal.pixelDepth *= 10000.0;
cal.setUnit("um");
}
imp2.setCalibration(cal);
}
if (info1!=null && info1.lastIndexOf("7FE0,0010")>0) {
stack = DicomTools.sort(stack);