JAL-1432 updated copyright notices
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.util;
20
21 import java.io.*;
22 import javax.imageio.*;
23
24 import java.awt.*;
25 import java.awt.image.*;
26
27 import org.jibble.epsgraphics.*;
28 import jalview.gui.*;
29 import jalview.io.*;
30
31 public class ImageMaker
32 {
33   public static final int EPS = 0;
34
35   public static final int PNG = 1;
36
37   int type = -1;
38
39   EpsGraphics2D pg;
40
41   Graphics graphics;
42
43   FileOutputStream out;
44
45   BufferedImage bi;
46
47   public ImageMaker(Component parent, int type, String title, int width,
48           int height, File file, String EPStitle)
49   {
50     this.type = type;
51
52     if (file == null)
53     {
54       JalviewFileChooser chooser;
55       chooser = type == EPS ? getEPSChooser() : getPNGChooser();
56
57       chooser.setFileView(new jalview.io.JalviewFileView());
58       chooser.setDialogTitle(title);
59       chooser.setToolTipText("Save");
60
61       int value = chooser.showSaveDialog(parent);
62
63       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
64       {
65         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
66                 .getSelectedFile().getParent());
67
68         file = chooser.getSelectedFile();
69       }
70     }
71
72     if (file != null)
73     {
74       try
75       {
76         out = new FileOutputStream(file);
77
78         if (type == EPS)
79         {
80           setupEPS(width, height, EPStitle);
81         }
82         else
83         {
84           setupPNG(width, height);
85         }
86       } catch (Exception ex)
87       {
88         System.out.println("Error creating "
89                 + (type == EPS ? "EPS" : "PNG") + " file.");
90       }
91     }
92   }
93
94   public Graphics getGraphics()
95   {
96     return graphics;
97   }
98
99   void setupPNG(int width, int height)
100   {
101     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
102     graphics = bi.getGraphics();
103     Graphics2D ig2 = (Graphics2D) graphics;
104     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
105             RenderingHints.VALUE_ANTIALIAS_ON);
106   }
107
108   public void writeImage()
109   {
110     try
111     {
112       switch (type)
113       {
114       case EPS:
115         pg.flush();
116         pg.close();
117         break;
118       case PNG:
119         ImageIO.write(bi, "png", out);
120         out.close();
121         break;
122       }
123     } catch (Exception ex)
124     {
125       ex.printStackTrace();
126     }
127   }
128
129   void setupEPS(int width, int height, String title)
130   {
131     boolean accurateText = true;
132
133     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
134             "Prompt each time");
135
136     // If we need to prompt, and if the GUI is visible then
137     // Prompt for EPS rendering style
138     if (renderStyle.equalsIgnoreCase("Prompt each time")
139             && !(System.getProperty("java.awt.headless") != null && System
140                     .getProperty("java.awt.headless").equals("true")))
141     {
142       EPSOptions eps = new EPSOptions();
143       renderStyle = eps.getValue();
144
145       if (renderStyle == null || eps.cancelled)
146       {
147         return;
148       }
149     }
150
151     if (renderStyle.equalsIgnoreCase("text"))
152     {
153       accurateText = false;
154     }
155
156     try
157     {
158       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
159       Graphics2D ig2 = (Graphics2D) pg;
160       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
161               RenderingHints.VALUE_ANTIALIAS_ON);
162
163       pg.setAccurateTextMode(accurateText);
164
165       graphics = pg;
166     } catch (Exception ex)
167     {
168     }
169   }
170
171   JalviewFileChooser getPNGChooser()
172   {
173     return new jalview.io.JalviewFileChooser(
174             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
175             { "png" }, new String[]
176             { "Portable network graphics" }, "Portable network graphics");
177   }
178
179   JalviewFileChooser getEPSChooser()
180   {
181     return new jalview.io.JalviewFileChooser(
182             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
183             { "eps" }, new String[]
184             { "Encapsulated Postscript" }, "Encapsulated Postscript");
185   }
186 }