Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
FileSaver.java
1 package ij.io;
2 import java.awt.*;
3 import java.io.*;
4 import java.util.zip.*;
5 import ij.*;
6 import ij.process.*;
7 import ij.measure.Calibration;
8 import ij.plugin.filter.Analyzer;
9 import ij.plugin.frame.Recorder;
10 import javax.swing.*;
11 import javax.swing.filechooser.*;
12 
14 public class FileSaver {
15 
16  private static String defaultDirectory = null;
17  private ImagePlus imp;
18  private FileInfo fi;
19  private String name;
20  private String directory;
21 
23  public FileSaver(ImagePlus imp) {
24  this.imp = imp;
25  fi = imp.getFileInfo();
26  }
27 
31  public boolean save() {
32  //if (imp.getURL()!=null)
33  return saveAsTiff();
34  }
35 
36  String getPath(String type, String extension) {
37  JFileChooser fc = new JFileChooser();
38  if (Opener.defaultDirectory != null) {
39  fc.setCurrentDirectory(Opener.defaultDirectory);
40  }
41  File dummyFile = new File("foobar"+extension);
42  for (int i=0; i < Opener.FILE_FILTERS.length; i++) {
43  if (Opener.FILE_FILTERS[i].accept(dummyFile)) {
44  fc.addChoosableFileFilter(Opener.FILE_FILTERS[i]);
45  }
46  }
47  int returnVal = fc.showSaveDialog(IJ.getInstance());
48 
49  if (returnVal!=JFileChooser.APPROVE_OPTION) {
50  return "";
51  }
52  String path = fc.getCurrentDirectory().getPath()+File.separator+fc.getSelectedFile();
53  return path;
54  }
55 
58  public boolean saveAsTiff() {
59  String path = getPath("TIFF", ".tif");
60  if (path==null)
61  return false;
62  if (imp.getStackSize()==1)
63  return saveAsTiff(path);
64  else
65  return saveAsTiffStack(path);
66  }
67 
69  public boolean saveAsTiff(String path) {
70  fi.nImages = 1;
71  fi.description = getDescriptionString();
72  try {
73  TiffEncoder file = new TiffEncoder(fi);
74  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
75  file.write(out);
76  out.close();
77  }
78  catch (IOException e) {
79  showErrorMessage(e);
80  return false;
81  }
82  updateImp(fi, fi.TIFF);
83  return true;
84  }
85 
87  public boolean saveAsTiffStack(String path) {
88  return true;
89  }
90 
93  public boolean saveAsZip() {
94  String path = getPath("TIFF/ZIP", ".zip");
95  if (path==null)
96  return false;
97  else
98  return saveAsZip(path);
99  }
100 
102  public boolean saveAsZip(String path) {
103  //fi.nImages = 1;
104  if (!path.endsWith(".zip"))
105  path = path+".zip";
106  if (name==null)
107  name = imp.getTitle();
108  if (name.endsWith(".zip"))
109  name = name.substring(0,name.length()-4);
110  if (!name.endsWith(".tif"))
111  name = name+".tif";
112  fi.description = getDescriptionString();
113  try {
114  ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
115  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(zos));
116  zos.putNextEntry(new ZipEntry(name));
117  TiffEncoder te = new TiffEncoder(fi);
118  te.write(out);
119  out.close();
120  }
121  catch (IOException e) {
122  showErrorMessage(e);
123  return false;
124  }
125  updateImp(fi, fi.TIFF);
126  return true;
127  }
128 
129  public static boolean okForGif(ImagePlus imp) {
130  int type = imp.getType();
131  if (type==ImagePlus.COLOR_RGB || type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
132  return false;
133  } else
134  return true;
135 
136  }
137 
141  public boolean saveAsGif() {
142  if (!okForGif(imp)) {
143  IJ.error("To save as Gif, the image must be \"8-bit\" or \"8-bit Color\".");
144  return false;
145  }
146  String path = getPath("GIF", ".gif");
147  if (path==null)
148  return false;
149  else
150  return saveAsGif(path);
151  }
152 
155  public boolean saveAsGif(String path) {
156  if (!okForGif(imp))
157  return false;
158  try {
159  byte[] pixels = (byte[])imp.getProcessor().getPixels();
160  GifEncoder encoder = new GifEncoder(fi.width, fi.height, pixels, fi.reds, fi.greens, fi.blues);
161  OutputStream output = new BufferedOutputStream(new FileOutputStream(path));
162  encoder.write(output);
163  output.close();
164  }
165  catch (IOException e) {
166  showErrorMessage(e);
167  return false;
168  }
169  updateImp(fi, fi.GIF_OR_JPG);
170  return true;
171  }
172 
174  public static boolean okForJpeg(ImagePlus imp) {
175  return true;
176  }
177 
180  public boolean saveAsJpeg() {
181  String path = getPath("JPEG", ".jpg");
182  if (path==null)
183  return false;
184  else
185  return saveAsJpeg(path);
186  }
187 
189  public boolean saveAsJpeg(String path) {
190  Object jpegWriter = null;
191  if (IJ.isJava2())
192  IJ.runPlugIn("ij.plugin.JpegWriter", path);
193  else
194  IJ.runPlugIn("Jpeg_Writer", path);
195  if (!(imp.getType()==ImagePlus.GRAY16 || imp.getType()==ImagePlus.GRAY32))
196  updateImp(fi, fi.GIF_OR_JPG);
197  return true;
198  }
199 
202  public boolean saveAsBmp() {
203  String path = getPath("BMP", ".bmp");
204  if (path==null)
205  return false;
206  else
207  return saveAsBmp(path);
208  }
209 
211  public boolean saveAsBmp(String path) {
212  IJ.runPlugIn("ij.plugin.BMP_Writer", path);
213  return true;
214  }
215 
219  public boolean saveAsPng() {
220  if (!IJ.isJava14()) {
221  IJ.showMessage("Save As PNG", "Java 1.4 or later required");
222  return false;
223  }
224  String path = getPath("PNG", ".png");
225  if (path==null)
226  return false;
227  else
228  return saveAsPng(path);
229  }
230 
233  public boolean saveAsPng(String path) {
234  IJ.runPlugIn("ij.plugin.PNG_Writer", path);
235  return true;
236  }
237 
240  public boolean saveAsRaw() {
241  String path = getPath("Raw", ".raw");
242  if (path==null)
243  return false;
244  if (imp.getStackSize()==1)
245  return saveAsRaw(path);
246  else
247  return saveAsRawStack(path);
248  }
249 
251  public boolean saveAsRaw(String path) {
252  fi.nImages = 1;
253  try {
254  ImageWriter file = new ImageWriter(fi);
255  OutputStream out = new BufferedOutputStream(new FileOutputStream(path));
256  file.write(out);
257  out.close();
258  }
259  catch (IOException e) {
260  showErrorMessage(e);
261  return false;
262  }
263  updateImp(fi, fi.RAW);
264  return true;
265  }
266 
268  public boolean saveAsRawStack(String path) {
269  return false;
270  }
271 
274  public boolean saveAsText() {
275  String path = getPath("Text", ".txt");
276  if (path==null)
277  return false;
278  return saveAsText(path);
279  }
280 
282  public boolean saveAsText(String path) {
283  try {
284  Calibration cal = imp.getCalibration();
285  int precision = Analyzer.getPrecision();
286  TextEncoder file = new TextEncoder(imp.getProcessor(), cal, precision);
287  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
288  file.write(out);
289  out.close();
290  }
291  catch (IOException e) {
292  showErrorMessage(e);
293  return false;
294  }
295  return true;
296  }
297 
300  public boolean saveAsLut() {
301  if (imp.getType()==ImagePlus.COLOR_RGB) {
302  IJ.error("RGB Images do not have a LUT.");
303  return false;
304  }
305  String path = getPath("LUT", ".lut");
306  if (path==null)
307  return false;
308  return saveAsLut(path);
309  }
310 
312  public boolean saveAsLut(String path) {
313  LookUpTable lut = imp.createLut();
314  int mapSize = lut.getMapSize();
315  if (mapSize==0) {
316  IJ.error("RGB Images do not have a LUT.");
317  return false;
318  }
319  if (mapSize<256) {
320  IJ.error("Cannot save LUTs with less than 256 entries.");
321  return false;
322  }
323  byte[] reds = lut.getReds();
324  byte[] greens = lut.getGreens();
325  byte[] blues = lut.getBlues();
326  byte[] pixels = new byte[768];
327  for (int i=0; i<256; i++) {
328  pixels[i] = reds[i];
329  pixels[i+256] = greens[i];
330  pixels[i+512] = blues[i];
331  }
332  FileInfo fi = new FileInfo();
333  fi.width = 768;
334  fi.height = 1;
335  fi.pixels = pixels;
336 
337  try {
338  ImageWriter file = new ImageWriter(fi);
339  OutputStream out = new FileOutputStream(path);
340  file.write(out);
341  out.close();
342  }
343  catch (IOException e) {
344  showErrorMessage(e);
345  return false;
346  }
347  return true;
348  }
349 
350  private void updateImp(FileInfo fi, int fileFormat) {
351  imp.changes = false;
352  if (name!=null) {
353  fi.fileFormat = fileFormat;
354  fi.fileName = name;
355  fi.directory = directory;
356  if (fileFormat==fi.TIFF)
357  fi.offset = TiffEncoder.IMAGE_START;
358  fi.description = null;
359  imp.setTitle(name);
360  imp.setFileInfo(fi);
361  }
362  }
363 
364  void showErrorMessage(IOException e) {
365  IJ.error("An error occured writing the file.\n \n" + e);
366  }
367 
369  String getDescriptionString() {
370  StringBuffer sb = new StringBuffer(100);
371  sb.append("ImageJ="+ImageJ.VERSION+"\n");
372  if (fi.nImages>1)
373  sb.append("images="+fi.nImages+"\n");
374  if (fi.unit!=null)
375  sb.append("unit="+fi.unit+"\n");
376  if (fi.valueUnit!=null) {
377  sb.append("cf="+fi.calibrationFunction+"\n");
378  if (fi.coefficients!=null) {
379  for (int i=0; i<fi.coefficients.length; i++)
380  sb.append("c"+i+"="+fi.coefficients[i]+"\n");
381  }
382  sb.append("vunit="+fi.valueUnit+"\n");
383  }
384 
385  // get stack z-spacing and fps
386  if (fi.nImages>1) {
387  if (fi.pixelDepth!=0.0 && fi.pixelDepth!=1.0)
388  sb.append("spacing="+fi.pixelDepth+"\n");
389  if (fi.frameInterval!=0.0) {
390  double fps = 1.0/fi.frameInterval;
391  if ((int)fps==fps)
392  sb.append("fps="+(int)fps+"\n");
393  else
394  sb.append("fps="+fps+"\n");
395  }
396  }
397 
398  // get min and max display values
399  ImageProcessor ip = imp.getProcessor();
400  double min = ip.getMin();
401  double max = ip.getMax();
402  int type = imp.getType();
403  boolean enhancedLut = (type==ImagePlus.GRAY8 || type==ImagePlus.COLOR_256) && (min!=0.0 || max !=255.0);
404  if (enhancedLut || type==ImagePlus.GRAY16 || type==ImagePlus.GRAY32) {
405  sb.append("min="+min+"\n");
406  sb.append("max="+max+"\n");
407  }
408 
409  // get non-zero origins
410  Calibration cal = imp.getCalibration();
411  if (cal.xOrigin!=0.0)
412  sb.append("xorigin="+cal.xOrigin+"\n");
413  if (cal.yOrigin!=0.0)
414  sb.append("yorigin="+cal.yOrigin+"\n");
415  if (cal.zOrigin!=0.0)
416  sb.append("zorigin="+cal.zOrigin+"\n");
417  if (cal.info!=null && cal.info.length()<=64 && cal.info.indexOf('=')==-1 && cal.info.indexOf('\n')==-1)
418  sb.append("info="+cal.info+"\n");
419  sb.append((char)0);
420  return new String(sb);
421  }
422 
423 }