3 import java.awt.image.*;
33 private int width, height;
41 if (
IJ.debugMode)
IJ.
log(
"FileOpener: "+fi);
58 switch (fi.fileType) {
62 pixels = readPixels(fi);
63 if (pixels==null)
return null;
64 ip =
new ByteProcessor(width, height, (byte[])pixels, cm);
69 pixels = readPixels(fi);
70 if (pixels==null)
return null;
71 ip =
new ShortProcessor(width, height, (
short[])pixels, cm);
77 pixels = readPixels(fi);
78 if (pixels==null)
return null;
79 ip =
new FloatProcessor(width, height, (
float[])pixels, cm);
86 pixels = readPixels(fi);
87 if (pixels==null)
return null;
105 if (fi.fileFormat==fi.GIF_OR_JPG) {
107 img = Toolkit.getDefaultToolkit().getImage(fi.directory + fi.fileName);
114 if (fi.fileFormat==fi.DICOM) {
122 if (fi.fileFormat==fi.BMP) {
134 if (fi.url==null || fi.url.equals(
""))
135 IJ.
showStatus(
"Loading: " + fi.directory + fi.fileName);
138 Object pixels = readPixels(fi);
139 if (pixels==null)
return;
141 switch (fi.fileType) {
145 ip =
new ByteProcessor(width, height, (byte[])pixels, cm);
150 ip =
new ShortProcessor(width, height, (
short[])pixels, cm);
155 ip =
new FloatProcessor(width, height, (
float[])pixels, cm);
162 img = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(width, height, (
int[])pixels, 0, width));
171 if (
IJ.debugMode)
IJ.
log(
"16-bit signed");
172 double[] coeff =
new double[2];
178 Properties props = decodeDescriptionString();
180 if (fi.pixelWidth>0.0 && fi.unit!=null) {
187 if (fi.valueUnit!=null) {
188 int f = fi.calibrationFunction;
189 if ((f>=Calibration.STRAIGHT_LINE && f<=Calibration.LOG2 && fi.coefficients!=null)
190 ||f==Calibration.UNCALIBRATED_OD)
194 if (fi.frameInterval!=0.0)
200 cal.
xOrigin = getDouble(props,
"xorigin");
201 cal.
yOrigin = getDouble(props,
"yorigin");
202 cal.
zOrigin = getDouble(props,
"zorigin");
203 cal.
info = props.getProperty(
"info");
205 double displayMin = getDouble(props,
"min");
206 double displayMax = getDouble(props,
"max");
207 if (!(displayMin==0.0&&displayMax==0.0)) {
210 if (type==ImagePlus.GRAY8 || type==ImagePlus.COLOR_256)
211 ip.setMinAndMax(displayMin, displayMax);
212 else if (type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
213 if (ip.getMin()!=displayMin || ip.getMax()!=displayMax)
214 ip.setMinAndMax(displayMin, displayMax);
222 return new IndexColorModel(8, fi.lutSize, fi.reds, fi.greens, fi.blues);
224 return LookUpTable.createGrayscaleColorModel(fi.whiteIsZero);
229 if (fi.inputStream!=null)
230 return fi.inputStream;
231 else if (fi.url!=null && !fi.url.equals(
""))
232 return new URL(fi.url+fi.fileName).openStream();
234 if (fi.directory.length()>0 && !fi.directory.endsWith(
Prefs.
separator))
236 File f =
new File(fi.directory + fi.fileName);
237 if (f==null || f.isDirectory() || !validateFileInfo(f, fi))
240 return new FileInputStream(f);
244 static boolean validateFileInfo(File f,
FileInfo fi) {
247 if (fi.width<=0 || fi.height<0) {
248 error(
"Width or height <= 0.", fi, offset, length);
251 if (offset>=0 && offset<1000)
254 error(
"Offset is negative.", fi, offset, length);
257 if (fi.fileType==FileInfo.BITMAP)
261 size = fi.nImages>1?size:size/4;
262 if (fi.height==1) size = 0;
263 if (offset+size>length) {
264 error(
"Offset + image size > file length.", fi, offset, length);
270 static void error(String msg, FileInfo fi,
long offset,
long length) {
271 IJ.showMessage(
"FileOpener",
"FileInfo parameter error. \n"
273 +
" Width: " + fi.width +
"\n"
274 +
" Height: " + fi.height +
"\n"
275 +
" Offset: " + offset +
"\n"
276 +
" Bytes/pixel: " + fi.getBytesPerPixel() +
"\n"
277 +(length>0?
" File length: " + length +
"\n":
"")
283 Object readPixels(FileInfo fi) {
284 Object pixels = null;
289 ImageReader reader =
new ImageReader(fi);
290 pixels = reader.readPixels(is);
293 catch (Exception e) {
294 IJ.log(
"FileOpener.readPixels(): " + e);
299 public Properties decodeDescriptionString() {
300 if (fi.description==null || fi.description.length()<7)
303 IJ.log(
"Image Description: " +
new String(fi.description).replace(
'\n',
' '));
304 if (!fi.description.startsWith(
"ImageJ"))
306 Properties props =
new Properties();
307 InputStream is =
new ByteArrayInputStream(fi.description.getBytes());
308 try {props.load(is); is.close();}
309 catch (IOException e) {
return null;}
310 fi.unit = props.getProperty(
"unit",
"");
311 Double n = getNumber(props,
"cf");
312 if (n!=null) fi.calibrationFunction = n.intValue();
313 double c[] =
new double[5];
315 for (
int i=0; i<5; i++) {
316 n = getNumber(props,
"c"+i);
318 c[i] = n.doubleValue();
322 fi.coefficients =
new double[count];
323 for (
int i=0; i<count; i++)
324 fi.coefficients[i] = c[i];
326 fi.valueUnit = props.getProperty(
"vunit");
327 n = getNumber(props,
"images");
328 if (n!=null && n.doubleValue()>1.0)
329 fi.nImages = (int)n.doubleValue();
331 double spacing = getDouble(props,
"spacing");
333 fi.pixelDepth = spacing;
334 n = getNumber(props,
"fps");
335 double fps = getDouble(props,
"fps");
337 fi.frameInterval = 1.0/fps;
342 private Double getNumber(Properties props, String key) {
343 String s = props.getProperty(key);
346 return Double.valueOf(s);
347 }
catch (NumberFormatException e) {}
352 private double getDouble(Properties props, String key) {
353 Double n = getNumber(props, key);
354 return n!=null?n.doubleValue():0.0;