1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import java.awt.Graphics; |
24 |
|
import java.awt.Graphics2D; |
25 |
|
import java.awt.RenderingHints; |
26 |
|
import java.awt.image.BufferedImage; |
27 |
|
import java.io.File; |
28 |
|
import java.io.FileOutputStream; |
29 |
|
import java.io.IOException; |
30 |
|
|
31 |
|
import javax.imageio.ImageIO; |
32 |
|
|
33 |
|
import org.jfree.graphics2d.svg.SVGGraphics2D; |
34 |
|
import org.jfree.graphics2d.svg.SVGHints; |
35 |
|
import org.jibble.epsgraphics.EpsGraphics2D; |
36 |
|
|
37 |
|
import jalview.bin.Console; |
38 |
|
import jalview.io.JalviewFileChooser; |
39 |
|
import jalview.util.imagemaker.BitmapImageSizing; |
40 |
|
|
|
|
| 0% |
Uncovered Elements: 129 (129) |
Complexity: 42 |
Complexity Density: 0.45 |
|
41 |
|
public class ImageMaker |
42 |
|
{ |
43 |
|
public static final String SVG_DESCRIPTION = "Scalable Vector Graphics"; |
44 |
|
|
45 |
|
public static final String SVG_EXTENSION = "svg"; |
46 |
|
|
47 |
|
public static final String EPS_DESCRIPTION = "Encapsulated Postscript"; |
48 |
|
|
49 |
|
public static final String EPS_EXTENSION = "eps"; |
50 |
|
|
51 |
|
public static final String PNG_EXTENSION = "png"; |
52 |
|
|
53 |
|
public static final String PNG_DESCRIPTION = "Portable network graphics"; |
54 |
|
|
55 |
|
EpsGraphics2D pg; |
56 |
|
|
57 |
|
Graphics graphics; |
58 |
|
|
59 |
|
FileOutputStream out; |
60 |
|
|
61 |
|
BufferedImage bi; |
62 |
|
|
63 |
|
TYPE type; |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
65 |
|
public enum TYPE |
66 |
|
{ |
67 |
|
EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION, |
68 |
|
EPS_DESCRIPTION), |
69 |
|
PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION, |
70 |
|
PNG_DESCRIPTION), |
71 |
|
SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION); |
72 |
|
|
73 |
|
public final String name; |
74 |
|
|
75 |
|
public final String label; |
76 |
|
|
77 |
|
public final String extension; |
78 |
|
|
79 |
|
public final String description; |
80 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
81 |
0 |
TYPE(String name, String label, String ext, String desc)... |
82 |
|
{ |
83 |
0 |
this.name = name; |
84 |
0 |
this.label = label; |
85 |
0 |
this.extension = ext; |
86 |
0 |
this.description = desc; |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0 |
public String getName()... |
90 |
|
{ |
91 |
0 |
return name; |
92 |
|
} |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
public JalviewFileChooser getFileChooser()... |
95 |
|
{ |
96 |
0 |
return new JalviewFileChooser(extension, description); |
97 |
|
} |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
0 |
public String getLabel()... |
100 |
|
{ |
101 |
0 |
return label; |
102 |
|
} |
103 |
|
|
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
@param |
111 |
|
@param |
112 |
|
@param |
113 |
|
@param |
114 |
|
@param |
115 |
|
@param |
116 |
|
@throws |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.31 |
|
118 |
0 |
public ImageMaker(TYPE imageType, int width, int height, File file,... |
119 |
|
String fileTitle, boolean useLineart, BitmapImageSizing userBis) |
120 |
|
throws IOException |
121 |
|
{ |
122 |
0 |
this.type = imageType; |
123 |
|
|
124 |
0 |
out = new FileOutputStream(file); |
125 |
0 |
switch (imageType) |
126 |
|
{ |
127 |
0 |
case SVG: |
128 |
0 |
setupSVG(width, height, useLineart); |
129 |
0 |
break; |
130 |
0 |
case EPS: |
131 |
0 |
setupEPS(width, height, fileTitle, useLineart); |
132 |
0 |
break; |
133 |
0 |
case PNG: |
134 |
0 |
setupPNG(width, height, userBis); |
135 |
0 |
break; |
136 |
0 |
default: |
137 |
|
} |
138 |
|
} |
139 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
0 |
public Graphics getGraphics()... |
141 |
|
{ |
142 |
0 |
return graphics; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0.28 |
|
150 |
0 |
public void writeImage()... |
151 |
|
{ |
152 |
0 |
try |
153 |
|
{ |
154 |
0 |
switch (type) |
155 |
|
{ |
156 |
0 |
case EPS: |
157 |
0 |
pg.flush(); |
158 |
0 |
pg.close(); |
159 |
0 |
break; |
160 |
0 |
case SVG: |
161 |
0 |
String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument(); |
162 |
0 |
out.write(svgData.getBytes()); |
163 |
0 |
out.flush(); |
164 |
0 |
out.close(); |
165 |
0 |
break; |
166 |
0 |
case PNG: |
167 |
0 |
ImageIO.write(bi, PNG_EXTENSION, out); |
168 |
0 |
out.flush(); |
169 |
0 |
out.close(); |
170 |
0 |
break; |
171 |
|
} |
172 |
|
} catch (Exception ex) |
173 |
|
{ |
174 |
0 |
ex.printStackTrace(); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
@param |
182 |
|
@param |
183 |
|
@param |
184 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
185 |
0 |
protected void setupPNG(int width, int height, BitmapImageSizing userBis)... |
186 |
|
{ |
187 |
0 |
if (width == 0 || height == 0) |
188 |
0 |
return; |
189 |
|
|
190 |
0 |
BitmapImageSizing bis = ImageMaker.getScaleWidthHeight(width, height, |
191 |
|
userBis); |
192 |
0 |
float usescale = bis.scale(); |
193 |
0 |
int usewidth = bis.width(); |
194 |
0 |
int useheight = bis.height(); |
195 |
|
|
196 |
0 |
bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB); |
197 |
0 |
graphics = bi.getGraphics(); |
198 |
0 |
Graphics2D ig2 = (Graphics2D) graphics; |
199 |
0 |
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
200 |
|
RenderingHints.VALUE_ANTIALIAS_ON); |
201 |
0 |
if (usescale > 0.0f) |
202 |
|
{ |
203 |
0 |
ig2.scale(usescale, usescale); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@param |
213 |
|
@param |
214 |
|
|
215 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
216 |
0 |
protected void setupSVG(int width, int height, boolean useLineart)... |
217 |
|
{ |
218 |
0 |
SVGGraphics2D g2 = new SVGGraphics2D(width, height); |
219 |
0 |
if (useLineart) |
220 |
|
{ |
221 |
0 |
g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, |
222 |
|
SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); |
223 |
|
} |
224 |
0 |
graphics = g2; |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
@param |
232 |
|
@param |
233 |
|
@param |
234 |
|
@param |
235 |
|
|
236 |
|
@throws |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
238 |
0 |
protected void setupEPS(int width, int height, String title,... |
239 |
|
boolean useLineart) throws IOException |
240 |
|
{ |
241 |
0 |
pg = new EpsGraphics2D(title, out, 0, 0, width, height); |
242 |
0 |
Graphics2D ig2 = pg; |
243 |
0 |
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
244 |
|
RenderingHints.VALUE_ANTIALIAS_ON); |
245 |
0 |
pg.setAccurateTextMode(useLineart); |
246 |
0 |
graphics = pg; |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
@param |
255 |
|
|
256 |
|
@param |
257 |
|
|
258 |
|
@param |
259 |
|
|
260 |
|
@param |
261 |
|
|
262 |
|
@param |
263 |
|
|
264 |
|
@return |
265 |
|
|
266 |
|
|
|
|
| 0% |
Uncovered Elements: 34 (34) |
Complexity: 14 |
Complexity Density: 0.64 |
|
267 |
0 |
public static BitmapImageSizing getScaleWidthHeight(int width, int height,... |
268 |
|
float scale, int bitmapwidth, int bitmapheight) |
269 |
|
{ |
270 |
0 |
float usescale = 0.0f; |
271 |
0 |
int usewidth = width; |
272 |
0 |
int useheight = height; |
273 |
|
|
274 |
0 |
if ((width == 0 && bitmapwidth > 0) |
275 |
|
|| (height == 0 && bitmapheight > 0)) |
276 |
|
{ |
277 |
|
|
278 |
0 |
return BitmapImageSizing.nullBitmapImageSizing(); |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
0 |
if (scale > 0.0f) |
283 |
|
{ |
284 |
0 |
usescale = scale; |
285 |
0 |
usewidth = Math.round(scale * width); |
286 |
0 |
useheight = Math.round(scale * height); |
287 |
|
} |
288 |
0 |
if (bitmapwidth > 0) |
289 |
|
{ |
290 |
0 |
float wscale = (float) bitmapwidth / width; |
291 |
0 |
if (wscale > 0.0f && (usescale == 0.0f || wscale < usescale)) |
292 |
|
{ |
293 |
0 |
usescale = wscale; |
294 |
0 |
usewidth = bitmapwidth; |
295 |
0 |
useheight = Math.round(usescale * height); |
296 |
|
} |
297 |
|
} |
298 |
0 |
if (bitmapheight > 0) |
299 |
|
{ |
300 |
0 |
float hscale = (float) bitmapheight / height; |
301 |
0 |
if (hscale > 0.0f && (usescale == 0.0f || hscale < usescale)) |
302 |
|
{ |
303 |
0 |
usescale = hscale; |
304 |
0 |
usewidth = Math.round(usescale * width); |
305 |
0 |
useheight = bitmapheight; |
306 |
|
} |
307 |
|
} |
308 |
0 |
return new BitmapImageSizing(usescale, usewidth, useheight, false); |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@return |
318 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
319 |
0 |
public static BitmapImageSizing getScaleWidthHeight(int width, int height,... |
320 |
|
BitmapImageSizing bis) |
321 |
|
{ |
322 |
0 |
return ImageMaker.getScaleWidthHeight(width, height, bis.scale(), |
323 |
|
bis.width(), bis.height()); |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
@param |
332 |
|
@param |
333 |
|
@param |
334 |
|
@return |
335 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 10 |
Complexity Density: 0.56 |
|
336 |
0 |
public static BitmapImageSizing parseScaleWidthHeightStrings(... |
337 |
|
String scaleS, String widthS, String heightS) |
338 |
|
{ |
339 |
0 |
if (scaleS == null && widthS == null && heightS == null) |
340 |
|
{ |
341 |
|
|
342 |
|
|
343 |
0 |
return BitmapImageSizing.defaultBitmapImageSizing(); |
344 |
|
} |
345 |
|
|
346 |
0 |
float scale = 0.0f; |
347 |
0 |
int width = 0; |
348 |
0 |
int height = 0; |
349 |
|
|
350 |
0 |
if (scaleS != null) |
351 |
|
{ |
352 |
0 |
try |
353 |
|
{ |
354 |
0 |
scale = Float.parseFloat(scaleS); |
355 |
|
} catch (NumberFormatException e) |
356 |
|
{ |
357 |
0 |
Console.warn("Did not understand scale '" + scaleS |
358 |
|
+ "', won't be used."); |
359 |
|
} |
360 |
|
} |
361 |
0 |
if (widthS != null) |
362 |
|
{ |
363 |
0 |
try |
364 |
|
{ |
365 |
0 |
width = Integer.parseInt(widthS); |
366 |
|
} catch (NumberFormatException e) |
367 |
|
{ |
368 |
0 |
Console.warn("Did not understand width '" + widthS |
369 |
|
+ "', won't be used."); |
370 |
|
} |
371 |
|
} |
372 |
0 |
if (heightS != null) |
373 |
|
{ |
374 |
0 |
try |
375 |
|
{ |
376 |
0 |
height = Integer.parseInt(heightS); |
377 |
|
} catch (NumberFormatException e) |
378 |
|
{ |
379 |
0 |
Console.warn("Did not understand height '" + heightS |
380 |
|
+ "', won't be used."); |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
0 |
return new BitmapImageSizing(scale, width, height, false); |
385 |
|
} |
386 |
|
} |