3 import java.awt.image.*;
7 import java.util.zip.*;
8 import java.util.Iterator;
9 import java.util.Locale;
11 import javax.swing.filechooser.*;
15 import ij.plugin.frame.*;
16 import ij.text.TextWindow;
18 import java.util.LinkedList;
19 import java.util.ArrayList;
26 private static final int UNKNOWN=0,TIFF=1,DICOM=2,FITS=3,PGM=4,JPEG=5,
27 GIF=6,LUT=7,BMP=8,ZIP=9,JAVA_OR_TEXT=10,ROI=11,TEXT=12,PNG=13,TIFF_AND_DICOM=14,CUSTOM=15;
28 private static final String[] types = {
"unknown",
"tif",
"dcm",
"fits",
"pgm",
29 "jpg",
"gif",
"bmp",
"png"};
34 ArrayList filters =
new ArrayList();
35 LinkedList extensions =
new LinkedList();
36 extensions.add(
"gif");
39 extensions.add(
"jpg");
40 extensions.add(
"jpeg");
43 extensions.add(
"png");
46 extensions.add(
"bmp");
49 extensions.add(
"tif");
50 extensions.add(
"tiff");
53 extensions.add(
"dcm");
54 extensions.add(
"dicom");
55 filters.add(
new FileExtensionFilter(
"Digital Image and Communications in Medicine", extensions));
57 extensions.add(
"fits");
60 extensions.add(
"pgm");
62 LinkedList allExtensions =
new LinkedList();
63 for (Iterator itr=filters.iterator(); itr.hasNext(); ) {
65 for (
int i=0; i < e.length; i++) {
66 allExtensions.add(e[i]);
70 ALL_FILES_FILTER.setShowExtensions(
false);
73 FILE_FILTERS[0] = ALL_FILES_FILTER;
74 for (
int i=0; i < filters.size(); i++) {
80 public static File defaultDirectory = null;
81 private static int fileType;
94 JFileChooser fc =
new JFileChooser();
95 if (defaultDirectory != null) {
96 fc.setCurrentDirectory(defaultDirectory);
98 for (
int i=0; i < FILE_FILTERS.length; i++) {
99 fc.addChoosableFileFilter(FILE_FILTERS[i]);
101 fc.setFileFilter(ALL_FILES_FILTER);
102 fc.setAcceptAllFileFilterUsed(
false);
104 defaultDirectory = fc.getCurrentDirectory();
106 if (returnVal!=JFileChooser.APPROVE_OPTION) {
109 String path = fc.getSelectedFile().toString();
120 public boolean open(String path) {
130 String msg =
"File is not in a valid format, or it was not found.";
131 if (path!=null && path.length()<=64)
132 msg +=
" \n \n "+path;
148 if (path==null || path.equals(
""))
150 else if (path.indexOf(
"://")>0)
153 img =
openImage(getDir(path), getName(path));
171 String path = directory+name;
172 fileType = getFileType(path,name);
174 IJ.
log(
"openImage: \""+types[fileType]+
"\", "+path);
181 if (imp.getWidth()!=0)
return imp;
else return null;
191 if (imp.getWidth()!=0)
return imp;
else return null;
194 if (imp.getWidth()!=0)
return imp;
else return null;
199 imp = openJpegOrGif(directory, name);
200 if (imp!=null&&imp.getWidth()!=0)
return imp;
else return null;
203 if (imp.getWidth()!=0)
return imp;
else return null;
206 if (imp.getWidth()!=0)
return imp;
else return null;
207 case UNKNOWN:
case TEXT:
210 if (imp==null)
return null;
211 if (imp.getWidth()>0 && imp.getHeight()>0) {
215 if (imp.getWidth()==-1)
234 int index = url.lastIndexOf(
'/');
236 index = url.lastIndexOf(
'\\');
238 name = url.substring(index+1);
240 throw new MalformedURLException(
"Invalid URL: "+url);
241 URL u =
new URL(url);
244 if (url.endsWith(
".tif") || url.endsWith(
".TIF"))
245 imp =
openTiff(u.openStream(), name);
246 else if (url.endsWith(
".zip"))
248 else if (url.endsWith(
".dcm")) {
250 if (imp!=null && imp.getWidth()==0) imp = null;
252 imp = openJpegOrGifUsingURL(name, u);
255 }
catch (Exception e) {
256 String msg = e.getMessage();
257 if (msg==null || msg.equals(
""))
265 ImagePlus openZip(URL url)
throws IOException {
267 URLConnection uc = url.openConnection();
268 int fileSize = uc.getContentLength();
270 InputStream in = uc.getInputStream();
271 ZipInputStream zin =
new ZipInputStream(in);
272 ByteArrayOutputStream out =
new ByteArrayOutputStream();
273 byte[] buf =
new byte[4096];
274 ZipEntry entry = zin.getNextEntry();
277 String name = entry.getName();
279 if (!name.endsWith(
".tif"))
280 throw new IOException(
"This ZIP archive does not appear to contain a TIFF file");
287 out.write(buf, 0, len);
292 byte[] bytes = out.toByteArray();
294 return openTiff(
new ByteArrayInputStream(bytes), name);
297 ImagePlus openJpegOrGifUsingURL(String title, URL url) {
300 Image img = Toolkit.getDefaultToolkit().getImage(url);
302 ImagePlus imp =
new ImagePlus(title, img);
308 ImagePlus openJpegOrGif(String dir, String name) {
309 ImagePlus imp = null;
310 Image img = Toolkit.getDefaultToolkit().getImage(dir+name);
313 imp =
new ImagePlus(name, img);
314 }
catch (IllegalStateException e) {
318 if (imp.getType()==ImagePlus.COLOR_RGB)
320 FileInfo fi =
new FileInfo();
321 fi.fileFormat = fi.GIF_OR_JPG;
334 int width = ip.getWidth();
335 int height = ip.getHeight();
336 int[] pixels = (
int[])ip.getPixels();
338 for (
int y=0; y<(height-8); y++) {
340 for (
int x=0; x<(width-8); x++) {
341 c = pixels[offset+x];
342 r = (c&0xff0000)>>16;
345 if (!((r==g)&&(g==b))) {
360 if (
IJ.debugMode) td.enableDebugging();
362 try {info = td.getTiffInfo();}
363 catch (IOException e) {
364 String msg = e.getMessage();
365 if (msg==null||msg.equals(
"")) msg =
""+e;
371 return openTiff2(info);
380 if (
IJ.debugMode) td.enableDebugging();
381 info = td.getTiffInfo();
382 }
catch (FileNotFoundException e) {
383 IJ.
showMessage(
"TiffDecoder",
"File not found: "+e.getMessage());
385 }
catch (Exception e) {
389 return openTiff2(info);
392 public String getName(String path) {
393 int i = path.lastIndexOf(
'/');
395 i = path.lastIndexOf(
'\\');
397 return path.substring(i+1);
402 public String getDir(String path) {
403 int i = path.lastIndexOf(
'/');
405 i = path.lastIndexOf(
'\\');
407 return path.substring(0, i+1);
415 ImagePlus openTiff2(FileInfo[] info) {
418 ImagePlus imp = null;
420 IJ.log(info[0].info);
421 FileOpener fo =
new FileOpener(info[0]);
422 imp = fo.open(
false);
432 int getFileType(String path, String name) {
434 byte[] buf =
new byte[132];
436 is =
new FileInputStream(path);
437 is.read(buf, 0, 132);
439 }
catch (IOException e) {
440 IJ.error(
"Couldn't open path "+path);
444 int b0=buf[0]&255, b1=buf[1]&255, b2=buf[2]&255, b3=buf[3]&255;
448 if (buf[128]==68 && buf[129]==73 && buf[130]==67 && buf[131]==77
449 && ((b0==73 && b1==73)||(b0==77 && b1==77)))
450 return TIFF_AND_DICOM;
453 if (name.endsWith(
".lsm"))
455 if (b0==73 && b1==73 && b2==42 && b3==0)
459 if (b0==77 && b1==77 && b2==0 && b3==42)
463 if (b0==255 && b1==216 && b2==255)
467 if (b0==71 && b1==73 && b2==70 && b3==56)
471 if (buf[128]==68 && buf[129]==73 && buf[130]==67 && buf[131]==77) {
476 name = name.toLowerCase(Locale.US);
477 if ((b0==8||b0==2) && b1==0 && b3==0 && !name.endsWith(
".spe"))
481 if (b0==83 && b1==73 && b2==77 && b3==80)
485 if (b0==80&&(b1==50||b1==53)&&(b2==10||b2==13||b2==32||b2==9))
489 if (name.endsWith(
".lut"))
493 if (b0==66 && b1==77 && name.endsWith(
".bmp"))
497 if (b0==137 && b1==80 && b2==78 && b3==71 && IJ.isJava2())
501 if (name.endsWith(
".zip"))
505 if (name.endsWith(
".java") || name.endsWith(
".txt"))
509 if (b0==73 && b1==111)
513 boolean isText =
true;
514 for (
int i=0; i<10; i++) {
516 if ((c<32&&c!=9&&c!=10&&c!=13) || c>126) {
528 ColorModel createColorModel(FileInfo fi) {
529 if (fi.fileType==FileInfo.COLOR8 && fi.lutSize>0)
530 return new IndexColorModel(8, fi.lutSize, fi.reds, fi.greens, fi.blues);
532 return LookUpTable.createGrayscaleColorModel(fi.whiteIsZero);
536 InputStream createInputStream(FileInfo fi)
throws IOException, MalformedURLException {
537 if (fi.inputStream!=null)
538 return fi.inputStream;
539 else if (fi.url!=null && !fi.url.equals(
""))
540 return new URL(fi.url+fi.fileName).openStream();
542 File f =
new File(fi.directory + fi.fileName);
543 if (f==null || f.isDirectory())
546 return new FileInputStream(f);