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 |
|
|
|
|
| 79.1% |
Uncovered Elements: 27 (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 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
81 |
54 |
TYPE(String name, String label, String ext, String desc)... |
82 |
|
{ |
83 |
54 |
this.name = name; |
84 |
54 |
this.label = label; |
85 |
54 |
this.extension = ext; |
86 |
54 |
this.description = desc; |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
86 |
public String getName()... |
90 |
|
{ |
91 |
86 |
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 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 4 |
Complexity Density: 0.31 |
|
118 |
46 |
public ImageMaker(TYPE imageType, int width, int height, File file,... |
119 |
|
String fileTitle, boolean useLineart, BitmapImageSizing userBis) |
120 |
|
throws IOException |
121 |
|
{ |
122 |
46 |
this.type = imageType; |
123 |
|
|
124 |
46 |
out = new FileOutputStream(file); |
125 |
46 |
switch (imageType) |
126 |
|
{ |
127 |
2 |
case SVG: |
128 |
2 |
setupSVG(width, height, useLineart); |
129 |
2 |
break; |
130 |
10 |
case EPS: |
131 |
10 |
setupEPS(width, height, fileTitle, useLineart); |
132 |
10 |
break; |
133 |
34 |
case PNG: |
134 |
34 |
setupPNG(width, height, userBis); |
135 |
34 |
break; |
136 |
0 |
default: |
137 |
|
} |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
48 |
public Graphics getGraphics()... |
141 |
|
{ |
142 |
48 |
return graphics; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
|
|
| 94.4% |
Uncovered Elements: 1 (18) |
Complexity: 5 |
Complexity Density: 0.28 |
|
150 |
46 |
public void writeImage()... |
151 |
|
{ |
152 |
46 |
try |
153 |
|
{ |
154 |
46 |
switch (type) |
155 |
|
{ |
156 |
10 |
case EPS: |
157 |
10 |
pg.flush(); |
158 |
10 |
pg.close(); |
159 |
10 |
break; |
160 |
2 |
case SVG: |
161 |
2 |
String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument(); |
162 |
2 |
out.write(svgData.getBytes()); |
163 |
2 |
out.flush(); |
164 |
2 |
out.close(); |
165 |
2 |
break; |
166 |
34 |
case PNG: |
167 |
34 |
ImageIO.write(bi, PNG_EXTENSION, out); |
168 |
34 |
out.flush(); |
169 |
34 |
out.close(); |
170 |
34 |
break; |
171 |
|
} |
172 |
|
} catch (Exception ex) |
173 |
|
{ |
174 |
0 |
ex.printStackTrace(); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
@param |
182 |
|
@param |
183 |
|
@param |
184 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
185 |
34 |
protected void setupPNG(int width, int height, BitmapImageSizing userBis)... |
186 |
|
{ |
187 |
34 |
if (width == 0 || height == 0) |
188 |
0 |
return; |
189 |
|
|
190 |
34 |
BitmapImageSizing bis = ImageMaker.getScaleWidthHeight(width, height, |
191 |
|
userBis); |
192 |
34 |
float usescale = bis.scale(); |
193 |
34 |
int usewidth = bis.width(); |
194 |
34 |
int useheight = bis.height(); |
195 |
|
|
196 |
34 |
bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB); |
197 |
34 |
graphics = bi.getGraphics(); |
198 |
34 |
Graphics2D ig2 = (Graphics2D) graphics; |
199 |
34 |
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
200 |
|
RenderingHints.VALUE_ANTIALIAS_ON); |
201 |
34 |
if (usescale > 0.0f) |
202 |
|
{ |
203 |
4 |
ig2.scale(usescale, usescale); |
204 |
|
} |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
@param |
212 |
|
@param |
213 |
|
@param |
214 |
|
|
215 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
216 |
2 |
protected void setupSVG(int width, int height, boolean useLineart)... |
217 |
|
{ |
218 |
2 |
SVGGraphics2D g2 = new SVGGraphics2D(width, height); |
219 |
2 |
if (useLineart) |
220 |
|
{ |
221 |
0 |
g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, |
222 |
|
SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); |
223 |
|
} |
224 |
2 |
graphics = g2; |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
@param |
232 |
|
@param |
233 |
|
@param |
234 |
|
@param |
235 |
|
|
236 |
|
@throws |
237 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
238 |
10 |
protected void setupEPS(int width, int height, String title,... |
239 |
|
boolean useLineart) throws IOException |
240 |
|
{ |
241 |
10 |
pg = new EpsGraphics2D(title, out, 0, 0, width, height); |
242 |
10 |
Graphics2D ig2 = pg; |
243 |
10 |
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
244 |
|
RenderingHints.VALUE_ANTIALIAS_ON); |
245 |
10 |
pg.setAccurateTextMode(useLineart); |
246 |
10 |
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 |
|
|
|
|
| 47.1% |
Uncovered Elements: 18 (34) |
Complexity: 14 |
Complexity Density: 0.64 |
|
267 |
44 |
public static BitmapImageSizing getScaleWidthHeight(int width, int height,... |
268 |
|
float scale, int bitmapwidth, int bitmapheight) |
269 |
|
{ |
270 |
44 |
float usescale = 0.0f; |
271 |
44 |
int usewidth = width; |
272 |
44 |
int useheight = height; |
273 |
|
|
274 |
44 |
if ((width == 0 && bitmapwidth > 0) |
275 |
|
|| (height == 0 && bitmapheight > 0)) |
276 |
|
{ |
277 |
|
|
278 |
0 |
return BitmapImageSizing.nullBitmapImageSizing(); |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
44 |
if (scale > 0.0f) |
283 |
|
{ |
284 |
8 |
usescale = scale; |
285 |
8 |
usewidth = Math.round(scale * width); |
286 |
8 |
useheight = Math.round(scale * height); |
287 |
|
} |
288 |
44 |
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 |
44 |
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 |
44 |
return new BitmapImageSizing(usescale, usewidth, useheight, false); |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@return |
318 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
319 |
44 |
public static BitmapImageSizing getScaleWidthHeight(int width, int height,... |
320 |
|
BitmapImageSizing bis) |
321 |
|
{ |
322 |
44 |
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 |
|
|
|
|
| 88.5% |
Uncovered Elements: 3 (26) |
Complexity: 10 |
Complexity Density: 0.56 |
|
336 |
48 |
public static BitmapImageSizing parseScaleWidthHeightStrings(... |
337 |
|
String scaleS, String widthS, String heightS) |
338 |
|
{ |
339 |
48 |
if (scaleS == null && widthS == null && heightS == null) |
340 |
|
{ |
341 |
|
|
342 |
|
|
343 |
38 |
return BitmapImageSizing.defaultBitmapImageSizing(); |
344 |
|
} |
345 |
|
|
346 |
10 |
float scale = 0.0f; |
347 |
10 |
int width = 0; |
348 |
10 |
int height = 0; |
349 |
|
|
350 |
10 |
if (scaleS != null) |
351 |
|
{ |
352 |
8 |
try |
353 |
|
{ |
354 |
8 |
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 |
10 |
if (widthS != null) |
362 |
|
{ |
363 |
3 |
try |
364 |
|
{ |
365 |
3 |
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 |
10 |
if (heightS != null) |
373 |
|
{ |
374 |
3 |
try |
375 |
|
{ |
376 |
3 |
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 |
10 |
return new BitmapImageSizing(scale, width, height, false); |
385 |
|
} |
386 |
|
} |