Debian Codegraph @

src/org/stathissideris/ascii2image/graphics/BitmapRenderer.java

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * DiTAA - Diagrams Through Ascii Art
3 *
4 * Copyright (C) 2004 Efstathios Sideris
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 */
21 package org.stathissideris.ascii2image.graphics;
22
23 import java.awt.BasicStroke;
24 import java.awt.Canvas;
25 import java.awt.Color;
26 import java.awt.Graphics;
27 import java.awt.Graphics2D;
28 import java.awt.Image;
29 import java.awt.Rectangle;
30 import java.awt.RenderingHints;
31 import java.awt.Stroke;
32 import java.awt.geom.AffineTransform;
33 import java.awt.geom.GeneralPath;
34 import java.awt.image.BufferedImage;
35 import java.awt.image.ConvolveOp;
36 import java.awt.image.Kernel;
37 import java.awt.image.RenderedImage;
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Iterator;
43
44 import javax.imageio.ImageIO;
45
46 import org.stathissideris.ascii2image.core.RenderingOptions;
47 import org.stathissideris.ascii2image.core.Shape3DOrderingComparator;
48
49 /**
50 *
51 * @author Efstathios Sideris
52 */
53 public class BitmapRenderer {
54
55 private static final boolean DEBUG = false;
56
57 private static final String IDREGEX = "^.+_vfill$";
58
59 Stroke normalStroke;
60 Stroke dashStroke;
61
62 // public static void main(String[] args) throws Exception {
63 //
64 //
65 // long startTime = System.currentTimeMillis();
66 //
67 // ConversionOptions options = new ConversionOptions();
68 //
69 // TextGrid grid = new TextGrid();
70 //
71 // String filename = "dak_orgstruktur_vs_be.ditaa.OutOfMemoryError.edit.txt";
72 //
73 // grid.loadFrom("tests/text/"+filename);
74 //
75 // Diagram diagram = new Diagram(grid, options);
76 // new BitmapRenderer().renderToPNG(diagram, "tests/images/"+filename+".png", options.renderingOptions);
77 // long endTime = System.currentTimeMillis();
78 // long totalTime = (endTime - startTime) / 1000;
79 // System.out.println("Done in "+totalTime+"sec");
80 //
81 // File workDir = new File("tests/images");
82 // //Process p = Runtime.getRuntime().exec("display "+filename+".png", null, workDir);
83 // }
84
85 private boolean renderToPNG(Diagram diagram, String filename, RenderingOptions options){
86 RenderedImage image = renderToImage(diagram, options);
87
88 try {
89 File file = new File(filename);
90 ImageIO.write(image, "png", file);
91 } catch (IOException e) {
92 //e.printStackTrace();
93 System.err.println("Error: Cannot write to file "+filename);
94 return false;
95 }
96 return true;
97 }
98
99 public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){
100 BufferedImage image = new BufferedImage(
101 diagram.getWidth(),
102 diagram.getHeight(),
103 BufferedImage.TYPE_INT_RGB);
104
105 return render(diagram, image, options);
106 }
107
108 public RenderedImage render(Diagram diagram, BufferedImage image, RenderingOptions options){
109 RenderedImage renderedImage = image;
110 Graphics2D g2 = image.createGraphics();
111
112 Object antialiasSetting = RenderingHints.VALUE_ANTIALIAS_OFF;
113 if(options.performAntialias())
114 antialiasSetting = RenderingHints.VALUE_ANTIALIAS_ON;
115
116 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
117
118 g2.setColor(Color.white);
119 //TODO: find out why the next line does not work
120 g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10);
121 /*for(int y = 0; y < diagram.getHeight(); y ++)
122 g2.drawLine(0, y, diagram.getWidth(), y);*/
123
124 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
125
126 ArrayList shapes = diagram.getAllDiagramShapes();
127
128 if(DEBUG) System.out.println("Rendering "+shapes.size()+" shapes (groups flattened)");
129
130 Iterator shapesIt;
131 if(options.dropShadows()){
132 //render shadows
133 shapesIt = shapes.iterator();
134 while(shapesIt.hasNext()){
135 DiagramShape shape = (DiagramShape) shapesIt.next();
136
137 if(shape.getPoints().isEmpty()) continue;
138
139 //GeneralPath path = shape.makeIntoPath();
140 GeneralPath path;
141 path = shape.makeIntoRenderPath(diagram);
142
143 float offset = diagram.getMinimumOfCellDimension() / 3.333f;
144
145 if(path != null
146 && shape.dropsShadow()
147 && shape.getType() != DiagramShape.TYPE_CUSTOM){
148 GeneralPath shadow = new GeneralPath(path);
149 AffineTransform translate = new AffineTransform();
150 translate.setToTranslation(offset, offset);
151 shadow.transform(translate);
152 g2.setColor(new Color(150,150,150));
153 g2.fill(shadow);
154
155 }
156 }
157
158
159 //blur shadows
160
161 if(true) {
162 int blurRadius = 6;
163 int blurRadius2 = blurRadius * blurRadius;
164 float blurRadius2F = blurRadius2;
165 float weight = 1.0f / blurRadius2F;
166 float[] elements = new float[blurRadius2];
167 for (int k = 0; k < blurRadius2; k++)
168 elements[k] = weight;
169 Kernel myKernel = new Kernel(blurRadius, blurRadius, elements);
170
171 //if EDGE_NO_OP is not selected, EDGE_ZERO_FILL is the default which creates a black border
172 ConvolveOp simpleBlur =
173 new ConvolveOp(myKernel, ConvolveOp.EDGE_NO_OP, null);
174
175 BufferedImage destination =
176 new BufferedImage(
177 image.getWidth(),
178 image.getHeight(),
179 image.getType());
180
181 simpleBlur.filter(image, (BufferedImage) destination);
182
183 //destination = destination.getSubimage(blurRadius/2, blurRadius/2, image.getWidth(), image.getHeight());
184 g2 = (Graphics2D) destination.getGraphics();
185 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
186 renderedImage = (RenderedImage) destination;
187 }
188 }
189
190
191 //fill and stroke
192
193 float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2;
194 //Stroke normalStroke = g2.getStroke();
195
196 float strokeWeight = diagram.getMinimumOfCellDimension() / 10;
197
198 normalStroke =
199 new BasicStroke(
200 strokeWeight,
201 //10,
202 BasicStroke.CAP_ROUND,
203 BasicStroke.JOIN_ROUND
204 );
205
206 dashStroke =
207 new BasicStroke(
208 strokeWeight,
209 BasicStroke.CAP_BUTT,
210 BasicStroke.JOIN_ROUND,
211 0,
212 new float[] {dashInterval},
213 0
214 );
215
216 //TODO: at this stage we should draw the open shapes first in order to make sure they are at the bottom (this is useful for the {mo} shape)
217
218
219 //find storage shapes
220 ArrayList storageShapes = new ArrayList();
221 shapesIt = shapes.iterator();
222 while(shapesIt.hasNext()){
223 DiagramShape shape = (DiagramShape) shapesIt.next();
224 if(shape.getType() == DiagramShape.TYPE_STORAGE) {
225 storageShapes.add(shape);
226 continue;
227 }
228 }
229
230 //render storage shapes
231 //special case since they are '3d' and should be
232 //rendered bottom to top
233 //TODO: known bug: if a storage object is within a bigger normal box, it will be overwritten in the main drawing loop
234 //(BUT this is not possible since tags are applied to all shapes overlaping shapes)
235
236
237 Collections.sort(storageShapes, new Shape3DOrderingComparator());
238
239 g2.setStroke(normalStroke);
240 shapesIt = storageShapes.iterator();
241 while(shapesIt.hasNext()){
242 DiagramShape shape = (DiagramShape) shapesIt.next();
243
244 GeneralPath path;
245 path = shape.makeIntoRenderPath(diagram);
246
247 if(!shape.isStrokeDashed()) {
248 if(shape.getFillColor() != null)
249 g2.setColor(shape.getFillColor());
250 else
251 g2.setColor(Color.white);
252 g2.fill(path);
253 }
254
255 if(shape.isStrokeDashed())
256 g2.setStroke(dashStroke);
257 else
258 g2.setStroke(normalStroke);
259 g2.setColor(shape.getStrokeColor());
260 g2.draw(path);
261 }
262
263
264 //render the rest of the shapes
265 ArrayList pointMarkers = new ArrayList();
266 shapesIt = shapes.iterator();
267 while(shapesIt.hasNext()){
268 DiagramShape shape = (DiagramShape) shapesIt.next();
269 if(shape.getType() == DiagramShape.TYPE_POINT_MARKER) {
270 pointMarkers.add(shape);
271 continue;
272 }
273 if(shape.getType() == DiagramShape.TYPE_STORAGE) {
274 continue;
275 }
276 if(shape.getType() == DiagramShape.TYPE_CUSTOM){
277 renderCustomShape(shape, g2);
278 continue;
279 }
280
281 if(shape.getPoints().isEmpty()) continue;
282
283 int size = shape.getPoints().size();
284
285 GeneralPath path;
286 path = shape.makeIntoRenderPath(diagram);
287
288 //fill
289 if(path != null && shape.isClosed() && !shape.isStrokeDashed()){
290 if(shape.getFillColor() != null)
291 g2.setColor(shape.getFillColor());
292 else
293 g2.setColor(Color.white);
294 g2.fill(path);
295 }
296
297 //draw
298 if(shape.getType() != DiagramShape.TYPE_ARROWHEAD){
299 g2.setColor(shape.getStrokeColor());
300 if(shape.isStrokeDashed())
301 g2.setStroke(dashStroke);
302 else
303 g2.setStroke(normalStroke);
304 g2.draw(path);
305 }
306 }
307
308 //render point markers
309
310 g2.setStroke(normalStroke);
311 shapesIt = pointMarkers.iterator();
312 while(shapesIt.hasNext()){
313 DiagramShape shape = (DiagramShape) shapesIt.next();
314 //if(shape.getType() != DiagramShape.TYPE_POINT_MARKER) continue;
315
316 GeneralPath path;
317 path = shape.makeIntoRenderPath(diagram);
318
319 g2.setColor(Color.white);
320 g2.fill(path);
321 g2.setColor(shape.getStrokeColor());
322 g2.draw(path);
323 }
324
325 //handle text
326 //g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
327 //renderTextLayer(diagram.getTextObjects().iterator());
328
329 Iterator textIt = diagram.getTextObjects().iterator();
330 while(textIt.hasNext()){
331 DiagramText text = (DiagramText) textIt.next();
332 g2.setFont(text.getFont());
333 if(text.hasOutline()){
334 g2.setColor(text.getOutlineColor());
335 g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos());
336 g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos());
337 g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1);
338 g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1);
339 }
340 g2.setColor(text.getColor());
341 g2.drawString(text.getText(), text.getXPos(), text.getYPos());
342 }
343
344 if(options.renderDebugLines() || DEBUG){
345 Stroke debugStroke =
346 new BasicStroke(
347 1,
348 BasicStroke.CAP_ROUND,
349 BasicStroke.JOIN_ROUND
350 );
351 g2.setStroke(debugStroke);
352 g2.setColor(new Color(170, 170, 170));
353 g2.setXORMode(Color.white);
354 for(int x = 0; x < diagram.getWidth(); x += diagram.getCellWidth())
355 g2.drawLine(x, 0, x, diagram.getHeight());
356 for(int y = 0; y < diagram.getHeight(); y += diagram.getCellHeight())
357 g2.drawLine(0, y, diagram.getWidth(), y);
358 }
359
360
361 g2.dispose();
362
363 return renderedImage;
364 }
365
366 private RenderedImage renderTextLayer(ArrayList textObjects, int width, int height){
367 TextCanvas canvas = new TextCanvas(textObjects);
368 Image image = canvas.createImage(width, height);
369 Graphics g = image.getGraphics();
370 canvas.paint(g);
371 return (RenderedImage) image;
372 }
373
374 private class TextCanvas extends Canvas {
375 ArrayList textObjects;
376
377 public TextCanvas(ArrayList textObjects){
378 this.textObjects = textObjects;
379 }
380
381 public void paint(Graphics g){
382 Graphics g2 = (Graphics2D) g;
383 Iterator textIt = textObjects.iterator();
384 while(textIt.hasNext()){
385 DiagramText text = (DiagramText) textIt.next();
386 g2.setFont(text.getFont());
387 if(text.hasOutline()){
388 g2.setColor(text.getOutlineColor());
389 g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos());
390 g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos());
391 g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1);
392 g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1);
393 }
394 g2.setColor(text.getColor());
395 g2.drawString(text.getText(), text.getXPos(), text.getYPos());
396 }
397 }
398 }
399
400 private void renderCustomShape(DiagramShape shape, Graphics2D g2){
401 CustomShapeDefinition definition = shape.getDefinition();
402
403 Rectangle bounds = shape.getBounds();
404
405 if(definition.hasBorder()){
406 g2.setColor(shape.getStrokeColor());
407 if(shape.isStrokeDashed())
408 g2.setStroke(dashStroke);
409 else
410 g2.setStroke(normalStroke);
411 g2.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y);
412 g2.drawLine(bounds.x + bounds.width, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height);
413 g2.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height);
414 g2.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);
415
416 // g2.drawRect(bounds.x, bounds.y, bounds.width, bounds.height); //looks different!
417 }
418
419 //TODO: custom shape distintion relies on filename extension. Make this more intelligent
420 if(definition.getFilename().endsWith(".png")){
421 renderCustomPNGShape(shape, g2);
422 } else if(definition.getFilename().endsWith(".svg")){
423 // renderCustomSVGShape(shape, g2);
424 throw new UnsupportedOperationException();
425 }
426 }
427
428 // private void renderCustomSVGShape(DiagramShape shape, Graphics2D g2){
429 // CustomShapeDefinition definition = shape.getDefinition();
430 // Rectangle bounds = shape.getBounds();
431 // Image graphic;
432 // try {
433 // if(shape.getFillColor() == null) {
434 // graphic = ImageHandler.instance().renderSVG(
435 // definition.getFilename(), bounds.width, bounds.height, definition.stretches());
436 // } else {
437 // graphic = ImageHandler.instance().renderSVG(
438 // definition.getFilename(), bounds.width, bounds.height, definition.stretches(), IDREGEX, shape.getFillColor());
439 // }
440 // g2.drawImage(graphic, bounds.x, bounds.y, null);
441 // } catch (IOException e) {
442 // e.printStackTrace();
443 // }
444 // }
445
446 private void renderCustomPNGShape(DiagramShape shape, Graphics2D g2){
447 CustomShapeDefinition definition = shape.getDefinition();
448 Rectangle bounds = shape.getBounds();
449 Image graphic = ImageHandler.instance().loadImage(definition.getFilename());
450
451 int xPos, yPos, width, height;
452
453 if(definition.stretches()){ //occupy all available space
454 xPos = bounds.x; yPos = bounds.y;
455 width = bounds.width; height = bounds.height;
456 } else { //decide how to fit
457 int newHeight = bounds.width * graphic.getHeight(null) / graphic.getWidth(null);
458 if(newHeight < bounds.height){ //expand to fit width
459 height = newHeight;
460 width = bounds.width;
461 xPos = bounds.x;
462 yPos = bounds.y + bounds.height / 2 - graphic.getHeight(null) / 2;
463 } else { //expand to fit height
464 width = graphic.getWidth(null) * bounds.height / graphic.getHeight(null);
465 height = bounds.height;
466 xPos = bounds.x + bounds.width / 2 - graphic.getWidth(null) / 2;
467 yPos = bounds.y;
468 }
469 }
470
471 g2.drawImage(graphic, xPos, yPos, width, height, null);
472 }
473
474 public static boolean isColorDark(Color color){
475 int brightness = Math.max(color.getRed(), color.getGreen());
476 brightness = Math.max(color.getBlue(), brightness);
477 if(brightness < 200) {
478 if(DEBUG) System.out.println("Color "+color+" is dark");
479 return true;
480 }
481 if(DEBUG) System.out.println("Color "+color+" is not dark");
482 return false;
483 }
484 }