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