4 import java.util.zip.*;
7 import ij.measure.Calibration;
8 import ij.plugin.filter.Analyzer;
9 import ij.plugin.frame.Recorder;
11 import javax.swing.filechooser.*;
16 private static String defaultDirectory = null;
20 private String directory;
36 String getPath(String type, String extension) {
37 JFileChooser fc =
new JFileChooser();
38 if (
Opener.defaultDirectory != null) {
39 fc.setCurrentDirectory(
Opener.defaultDirectory);
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]);
47 int returnVal = fc.showSaveDialog(IJ.getInstance());
49 if (returnVal!=JFileChooser.APPROVE_OPTION) {
52 String path = fc.getCurrentDirectory().getPath()+File.separator+fc.getSelectedFile();
59 String path = getPath(
"TIFF",
".tif");
71 fi.description = getDescriptionString();
74 DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(path)));
78 catch (IOException e) {
82 updateImp(fi, fi.TIFF);
94 String path = getPath(
"TIFF/ZIP",
".zip");
104 if (!path.endsWith(
".zip"))
108 if (name.endsWith(
".zip"))
109 name = name.substring(0,name.length()-4);
110 if (!name.endsWith(
".tif"))
112 fi.description = getDescriptionString();
114 ZipOutputStream zos =
new ZipOutputStream(
new FileOutputStream(path));
115 DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(zos));
116 zos.putNextEntry(
new ZipEntry(name));
121 catch (IOException e) {
125 updateImp(fi, fi.TIFF);
129 public static boolean okForGif(
ImagePlus imp) {
142 if (!okForGif(imp)) {
143 IJ.
error(
"To save as Gif, the image must be \"8-bit\" or \"8-bit Color\".");
146 String path = getPath(
"GIF",
".gif");
161 OutputStream output =
new BufferedOutputStream(
new FileOutputStream(path));
162 encoder.
write(output);
165 catch (IOException e) {
169 updateImp(fi, fi.GIF_OR_JPG);
181 String path = getPath(
"JPEG",
".jpg");
190 Object jpegWriter = null;
196 updateImp(fi, fi.GIF_OR_JPG);
203 String path = getPath(
"BMP",
".bmp");
220 if (!
IJ.isJava14()) {
224 String path = getPath(
"PNG",
".png");
241 String path = getPath(
"Raw",
".raw");
255 OutputStream out =
new BufferedOutputStream(
new FileOutputStream(path));
259 catch (IOException e) {
263 updateImp(fi, fi.RAW);
275 String path = getPath(
"Text",
".txt");
287 DataOutputStream out =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(path)));
291 catch (IOException e) {
302 IJ.
error(
"RGB Images do not have a LUT.");
305 String path = getPath(
"LUT",
".lut");
314 int mapSize = lut.getMapSize();
316 IJ.
error(
"RGB Images do not have a LUT.");
320 IJ.
error(
"Cannot save LUTs with less than 256 entries.");
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++) {
329 pixels[i+256] = greens[i];
330 pixels[i+512] = blues[i];
339 OutputStream out =
new FileOutputStream(path);
343 catch (IOException e) {
350 private void updateImp(
FileInfo fi,
int fileFormat) {
353 fi.fileFormat = fileFormat;
355 fi.directory = directory;
356 if (fileFormat==fi.TIFF)
358 fi.description = null;
364 void showErrorMessage(IOException e) {
365 IJ.error(
"An error occured writing the file.\n \n" + e);
369 String getDescriptionString() {
370 StringBuffer sb =
new StringBuffer(100);
371 sb.append(
"ImageJ="+ImageJ.VERSION+
"\n");
373 sb.append(
"images="+fi.nImages+
"\n");
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");
382 sb.append(
"vunit="+fi.valueUnit+
"\n");
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;
392 sb.append(
"fps="+(
int)fps+
"\n");
394 sb.append(
"fps="+fps+
"\n");
400 double min = ip.getMin();
401 double max = ip.getMax();
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");
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");
420 return new String(sb);