c93f8e8f446312628c468f6bde13b7ea5f3ea59d
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import java.io.*;
24 import javax.imageio.*;
25
26 import java.awt.*;
27 import java.awt.image.*;
28
29 import org.jibble.epsgraphics.*;
30 import jalview.gui.*;
31 import jalview.io.*;
32
33 public class ImageMaker
34 {
35   public static final int EPS = 0;
36
37   public static final int PNG = 1;
38
39   int type = -1;
40
41   EpsGraphics2D pg;
42
43   Graphics graphics;
44
45   FileOutputStream out;
46
47   BufferedImage bi;
48
49   public ImageMaker(Component parent, int type, String title, int width,
50           int height, File file, String EPStitle)
51   {
52     this.type = type;
53
54     if (file == null)
55     {
56       JalviewFileChooser chooser;
57       chooser = type == EPS ? getEPSChooser() : getPNGChooser();
58
59       chooser.setFileView(new jalview.io.JalviewFileView());
60       chooser.setDialogTitle(title);
61       chooser.setToolTipText(MessageManager.getString("action.save"));
62
63       int value = chooser.showSaveDialog(parent);
64
65       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
66       {
67         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
68                 .getSelectedFile().getParent());
69
70         file = chooser.getSelectedFile();
71       }
72     }
73
74     if (file != null)
75     {
76       try
77       {
78         out = new FileOutputStream(file);
79
80         if (type == EPS)
81         {
82           setupEPS(width, height, EPStitle);
83         }
84         else
85         {
86           setupPNG(width, height);
87         }
88       } catch (Exception ex)
89       {
90         System.out.println("Error creating "
91                 + (type == EPS ? "EPS" : "PNG") + " file.");
92       }
93     }
94   }
95
96   public Graphics getGraphics()
97   {
98     return graphics;
99   }
100
101   void setupPNG(int width, int height)
102   {
103     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
104     graphics = bi.getGraphics();
105     Graphics2D ig2 = (Graphics2D) graphics;
106     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
107             RenderingHints.VALUE_ANTIALIAS_ON);
108   }
109
110   public void writeImage()
111   {
112     try
113     {
114       switch (type)
115       {
116       case EPS:
117         pg.flush();
118         pg.close();
119         break;
120       case PNG:
121         ImageIO.write(bi, "png", out);
122         out.close();
123         break;
124       }
125     } catch (Exception ex)
126     {
127       ex.printStackTrace();
128     }
129   }
130
131   void setupEPS(int width, int height, String title)
132   {
133     boolean accurateText = true;
134
135     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
136             "Prompt each time");
137
138     // If we need to prompt, and if the GUI is visible then
139     // Prompt for EPS rendering style
140     if (renderStyle.equalsIgnoreCase("Prompt each time")
141             && !(System.getProperty("java.awt.headless") != null && System
142                     .getProperty("java.awt.headless").equals("true")))
143     {
144       EPSOptions eps = new EPSOptions();
145       renderStyle = eps.getValue();
146
147       if (renderStyle == null || eps.cancelled)
148       {
149         return;
150       }
151     }
152
153     if (renderStyle.equalsIgnoreCase("text"))
154     {
155       accurateText = false;
156     }
157
158     try
159     {
160       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
161       Graphics2D ig2 = (Graphics2D) pg;
162       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
163               RenderingHints.VALUE_ANTIALIAS_ON);
164
165       pg.setAccurateTextMode(accurateText);
166
167       graphics = pg;
168     } catch (Exception ex)
169     {
170     }
171   }
172
173   JalviewFileChooser getPNGChooser()
174   {
175     return new jalview.io.JalviewFileChooser(
176             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
177             { "png" }, new String[]
178             { "Portable network graphics" }, "Portable network graphics");
179   }
180
181   JalviewFileChooser getEPSChooser()
182   {
183     return new jalview.io.JalviewFileChooser(
184             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
185             { "eps" }, new String[]
186             { "Encapsulated Postscript" }, "Encapsulated Postscript");
187   }
188 }