Saves the active image in GIF format, or as an animated GIF if the image is a stack.
| 12 | |
| 13 | /** Saves the active image in GIF format, or as an animated GIF if the image is a stack. */ |
| 14 | public class GifWriter implements PlugIn { |
| 15 | static int transparentIndex = Prefs.getTransparentIndex(); |
| 16 | private boolean showErrors = true; |
| 17 | private String error; |
| 18 | |
| 19 | /** Saves the specified image in GIF format or as an animated GIF if the image is a stack. */ |
| 20 | public static String save(ImagePlus imp, String path) { |
| 21 | if (imp==null) |
| 22 | imp = IJ.getImage(); |
| 23 | if (path==null || path.length()==0) |
| 24 | path = SaveDialog.getPath(imp, ".gif"); |
| 25 | if (path==null) |
| 26 | return null; |
| 27 | GifWriter gf = new GifWriter(); |
| 28 | //gf.showErrors = false; |
| 29 | gf.run(imp, path); |
| 30 | return gf.error; |
| 31 | } |
| 32 | |
| 33 | public void run(String path) { |
| 34 | ImagePlus imp = IJ.getImage(); |
| 35 | if (path==null || path.equals("")) { |
| 36 | SaveDialog sd = new SaveDialog("Save as Gif", imp.getTitle(), ".gif"); |
| 37 | if (sd.getFileName()==null) return; |
| 38 | path = sd.getDirectory()+sd.getFileName(); |
| 39 | } |
| 40 | run(imp, path); |
| 41 | } |
| 42 | |
| 43 | @AstroImageJ(reason = "Add ability to return to original slice", modified = true) |
| 44 | private void run(ImagePlus imp, String path) { |
| 45 | ImageStack stack = imp.getStack(); |
| 46 | Overlay overlay = imp.getOverlay(); |
| 47 | int nSlices = stack.size(); |
| 48 | if (nSlices==1) { // save using ImageIO |
| 49 | if (overlay!=null && !imp.tempOverlay()) |
| 50 | imp = imp.flatten(); |
| 51 | try { |
| 52 | writeImage(imp, path, transparentIndex); |
| 53 | } catch (Exception e) { |
| 54 | String msg = e.getMessage(); |
| 55 | if (msg==null || msg.equals("")) |
| 56 | msg = ""+e; |
| 57 | error = msg; |
| 58 | if (showErrors) { |
| 59 | msg = "An error occured writing the file.\n \n" + msg; |
| 60 | if (msg.contains("NullPointerException")) |
| 61 | msg = "Incorrect file path: \""+path+"\""; |
| 62 | IJ.error("GIF Writer", msg); |
| 63 | showErrors = false; |
| 64 | } |
| 65 | } |
| 66 | return; |
| 67 | } |
| 68 | AnimatedGifEncoder2 ge = new AnimatedGifEncoder2(); |
| 69 | if (!ge.setoptions()) |
| 70 | return; |
| 71 | double fps = imp.getCalibration().fps; |
nothing calls this directly
no test coverage detected