| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| 22 |
import java.awt.BasicStroke; |
| 23 |
import java.awt.Canvas; |
| 24 |
import java.awt.Color; |
| 25 |
import java.awt.Graphics; |
| 26 |
import java.awt.Graphics2D; |
| 27 |
import java.awt.Image; |
| 28 |
import java.awt.Rectangle; |
| 29 |
import java.awt.RenderingHints; |
| 30 |
import java.awt.Stroke; |
| 31 |
import java.awt.geom.AffineTransform; |
| 32 |
import java.awt.geom.GeneralPath; |
| 33 |
import java.awt.image.BufferedImage; |
| 34 |
import java.awt.image.ConvolveOp; |
| 35 |
import java.awt.image.Kernel; |
| 36 |
import java.awt.image.RenderedImage; |
| 37 |
import java.io.File; |
| 38 |
import java.io.IOException; |
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Collections; |
| 41 |
import java.util.Iterator; |
| 42 |
|
| 43 |
import javax.imageio.ImageIO; |
| 44 |
|
| •45 |
import org.stathissideris.ascii2image.core.ConversionOptions; |
| 46 |
import org.stathissideris.ascii2image.core.RenderingOptions; |
| 47 |
import org.stathissideris.ascii2image.core.Shape3DOrderingComparator; |
| •48 |
import org.stathissideris.ascii2image.core.ShapeAreaComparator; |
| 49 |
import org.stathissideris.ascii2image.text.TextGrid; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class BitmapRenderer { |
| 56 |
|
| 57 |
private static final boolean DEBUG = false; |
| •58 |
private static final boolean DEBUG_LINES = false; |
| 59 |
|
| 60 |
private static final String IDREGEX = "^.+_vfill$"; |
| 61 |
|
| 62 |
Stroke normalStroke; |
| 63 |
Stroke dashStroke; |
| 64 |
|
| •65 |
public static void main(String[] args) throws Exception { |
| 66 |
|
| 67 |
|
| 68 |
long startTime = System.currentTimeMillis(); |
| 69 |
|
| 70 |
ConversionOptions options = new ConversionOptions(); |
| 71 |
|
| 72 |
TextGrid grid = new TextGrid(); |
| 73 |
|
| 74 |
String filename = "bug18.txt"; |
| 75 |
|
| 76 |
grid.loadFrom("tests/text/"+filename); |
| 77 |
|
| 78 |
Diagram diagram = new Diagram(grid, options); |
| 79 |
new BitmapRenderer().renderToPNG(diagram, "tests/images/"+filename+".png", options.renderingOptions); |
| 80 |
long endTime = System.currentTimeMillis(); |
| 81 |
long totalTime = (endTime - startTime) / 1000; |
| 82 |
System.out.println("Done in "+totalTime+"sec"); |
| 83 |
|
| 84 |
File workDir = new File("tests/images"); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
private boolean renderToPNG(Diagram diagram, String filename, RenderingOptions options){ |
| 89 |
RenderedImage image = renderToImage(diagram, options); |
| 90 |
|
| 91 |
try { |
| 92 |
File file = new File(filename); |
| 93 |
ImageIO.write(image, "png", file); |
| 94 |
} catch (IOException e) { |
| 95 |
|
| 96 |
System.err.println("Error: Cannot write to file "+filename); |
| 97 |
return false; |
| 98 |
} |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| •102 |
public RenderedImage renderToImage(Diagram diagram, RenderingOptions options){ |
| 103 |
BufferedImage image; |
| 104 |
if(options.needsTransparency()) { |
| 105 |
image = new BufferedImage( |
| 106 |
diagram.getWidth(), |
| 107 |
diagram.getHeight(), |
| 108 |
BufferedImage.TYPE_INT_ARGB); |
| 109 |
} else { |
| 110 |
image = new BufferedImage( |
| 111 |
diagram.getWidth(), |
| 112 |
diagram.getHeight(), |
| 113 |
BufferedImage.TYPE_INT_RGB); |
| •114 |
} |
| 115 |
|
| 116 |
return render(diagram, image, options); |
| 117 |
} |
| 118 |
|
| 119 |
public RenderedImage render(Diagram diagram, BufferedImage image, RenderingOptions options){ |
| 120 |
RenderedImage renderedImage = image; |
| 121 |
Graphics2D g2 = image.createGraphics(); |
| 122 |
|
| 123 |
Object antialiasSetting = RenderingHints.VALUE_ANTIALIAS_OFF; |
| 124 |
if(options.performAntialias()) |
| 125 |
antialiasSetting = RenderingHints.VALUE_ANTIALIAS_ON; |
| 126 |
|
| 127 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting); |
| 128 |
|
| •129 |
g2.setColor(options.getBackgroundColor()); |
| 130 |
|
| 131 |
g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); |
| 136 |
|
| •137 |
ArrayList<DiagramShape> shapes = diagram.getAllDiagramShapes(); |
| 138 |
|
| 139 |
if(DEBUG) System.out.println("Rendering "+shapes.size()+" shapes (groups flattened)"); |
| 140 |
|
| •141 |
Iterator<DiagramShape> shapesIt; |
| 142 |
if(options.dropShadows()){ |
| 143 |
|
| 144 |
shapesIt = shapes.iterator(); |
| 145 |
while(shapesIt.hasNext()){ |
| •146 |
DiagramShape shape = shapesIt.next(); |
| 147 |
|
| 148 |
if(shape.getPoints().isEmpty()) continue; |
| 149 |
|
| 150 |
|
| 151 |
GeneralPath path; |
| •152 |
path = shape.makeIntoRenderPath(diagram, options); |
| 153 |
|
| 154 |
float offset = diagram.getMinimumOfCellDimension() / 3.333f; |
| 155 |
|
| 156 |
if(path != null |
| 157 |
&& shape.dropsShadow() |
| 158 |
&& shape.getType() != DiagramShape.TYPE_CUSTOM){ |
| 159 |
GeneralPath shadow = new GeneralPath(path); |
| 160 |
AffineTransform translate = new AffineTransform(); |
| 161 |
translate.setToTranslation(offset, offset); |
| 162 |
shadow.transform(translate); |
| 163 |
g2.setColor(new Color(150,150,150)); |
| 164 |
g2.fill(shadow); |
| 165 |
|
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if(true) { |
| 173 |
int blurRadius = 6; |
| 174 |
int blurRadius2 = blurRadius * blurRadius; |
| 175 |
float blurRadius2F = blurRadius2; |
| 176 |
float weight = 1.0f / blurRadius2F; |
| 177 |
float[] elements = new float[blurRadius2]; |
| 178 |
for (int k = 0; k < blurRadius2; k++) |
| 179 |
elements[k] = weight; |
| 180 |
Kernel myKernel = new Kernel(blurRadius, blurRadius, elements); |
| 181 |
|
| 182 |
|
| 183 |
ConvolveOp simpleBlur = |
| 184 |
new ConvolveOp(myKernel, ConvolveOp.EDGE_NO_OP, null); |
| 185 |
|
| 186 |
BufferedImage destination = |
| 187 |
new BufferedImage( |
| 188 |
image.getWidth(), |
| 189 |
image.getHeight(), |
| 190 |
image.getType()); |
| 191 |
|
| 192 |
simpleBlur.filter(image, (BufferedImage) destination); |
| 193 |
|
| 194 |
|
| 195 |
g2 = (Graphics2D) destination.getGraphics(); |
| 196 |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting); |
| 197 |
renderedImage = (RenderedImage) destination; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2; |
| 205 |
|
| 206 |
|
| 207 |
float strokeWeight = diagram.getMinimumOfCellDimension() / 10; |
| 208 |
|
| 209 |
normalStroke = |
| 210 |
new BasicStroke( |
| 211 |
strokeWeight, |
| 212 |
|
| 213 |
BasicStroke.CAP_ROUND, |
| 214 |
BasicStroke.JOIN_ROUND |
| 215 |
); |
| 216 |
|
| 217 |
dashStroke = |
| 218 |
new BasicStroke( |
| 219 |
strokeWeight, |
| 220 |
BasicStroke.CAP_BUTT, |
| 221 |
BasicStroke.JOIN_ROUND, |
| 222 |
0, |
| 223 |
new float[] {dashInterval}, |
| 224 |
0 |
| 225 |
); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| •231 |
ArrayList<DiagramShape> storageShapes = new ArrayList<DiagramShape>(); |
| 232 |
shapesIt = shapes.iterator(); |
| 233 |
while(shapesIt.hasNext()){ |
| 234 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 235 |
if(shape.getType() == DiagramShape.TYPE_STORAGE) { |
| 236 |
storageShapes.add(shape); |
| 237 |
continue; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
Collections.sort(storageShapes, new Shape3DOrderingComparator()); |
| 249 |
|
| 250 |
g2.setStroke(normalStroke); |
| 251 |
shapesIt = storageShapes.iterator(); |
| 252 |
while(shapesIt.hasNext()){ |
| 253 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 254 |
|
| 255 |
GeneralPath path; |
| •256 |
path = shape.makeIntoRenderPath(diagram, options); |
| 257 |
|
| 258 |
if(!shape.isStrokeDashed()) { |
| 259 |
if(shape.getFillColor() != null) |
| 260 |
g2.setColor(shape.getFillColor()); |
| 261 |
else |
| 262 |
g2.setColor(Color.white); |
| 263 |
g2.fill(path); |
| 264 |
} |
| 265 |
|
| 266 |
if(shape.isStrokeDashed()) |
| 267 |
g2.setStroke(dashStroke); |
| 268 |
else |
| 269 |
g2.setStroke(normalStroke); |
| 270 |
g2.setColor(shape.getStrokeColor()); |
| 271 |
g2.draw(path); |
| 272 |
} |
| 273 |
|
| •274 |
|
| 275 |
Collections.sort(shapes, new ShapeAreaComparator()); |
| 276 |
|
| 277 |
|
| •278 |
ArrayList<DiagramShape> pointMarkers = new ArrayList<DiagramShape>(); |
| 279 |
shapesIt = shapes.iterator(); |
| 280 |
while(shapesIt.hasNext()){ |
| 281 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 282 |
if(shape.getType() == DiagramShape.TYPE_POINT_MARKER) { |
| 283 |
pointMarkers.add(shape); |
| 284 |
continue; |
| 285 |
} |
| 286 |
if(shape.getType() == DiagramShape.TYPE_STORAGE) { |
| 287 |
continue; |
| 288 |
} |
| 289 |
if(shape.getType() == DiagramShape.TYPE_CUSTOM){ |
| 290 |
renderCustomShape(shape, g2); |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
if(shape.getPoints().isEmpty()) continue; |
| 295 |
|
| 296 |
int size = shape.getPoints().size(); |
| 297 |
|
| 298 |
GeneralPath path; |
| •299 |
path = shape.makeIntoRenderPath(diagram, options); |
| 300 |
|
| 301 |
|
| 302 |
if(path != null && shape.isClosed() && !shape.isStrokeDashed()){ |
| 303 |
if(shape.getFillColor() != null) |
| 304 |
g2.setColor(shape.getFillColor()); |
| 305 |
else |
| 306 |
g2.setColor(Color.white); |
| 307 |
g2.fill(path); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
if(shape.getType() != DiagramShape.TYPE_ARROWHEAD){ |
| 312 |
g2.setColor(shape.getStrokeColor()); |
| 313 |
if(shape.isStrokeDashed()) |
| 314 |
g2.setStroke(dashStroke); |
| 315 |
else |
| 316 |
g2.setStroke(normalStroke); |
| 317 |
g2.draw(path); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
g2.setStroke(normalStroke); |
| 324 |
shapesIt = pointMarkers.iterator(); |
| 325 |
while(shapesIt.hasNext()){ |
| 326 |
DiagramShape shape = (DiagramShape) shapesIt.next(); |
| 327 |
|
| 328 |
|
| 329 |
GeneralPath path; |
| •330 |
path = shape.makeIntoRenderPath(diagram, options); |
| 331 |
|
| 332 |
g2.setColor(Color.white); |
| 333 |
g2.fill(path); |
| 334 |
g2.setColor(shape.getStrokeColor()); |
| 335 |
g2.draw(path); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| •342 |
Iterator<DiagramText> textIt = diagram.getTextObjects().iterator(); |
| 343 |
while(textIt.hasNext()){ |
| •344 |
DiagramText text = textIt.next(); |
| 345 |
g2.setFont(text.getFont()); |
| 346 |
if(text.hasOutline()){ |
| 347 |
g2.setColor(text.getOutlineColor()); |
| 348 |
g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos()); |
| 349 |
g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos()); |
| 350 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1); |
| 351 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1); |
| 352 |
} |
| 353 |
g2.setColor(text.getColor()); |
| 354 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos()); |
| 355 |
} |
| 356 |
|
| •357 |
if(options.renderDebugLines() || DEBUG_LINES){ |
| 358 |
Stroke debugStroke = |
| 359 |
new BasicStroke( |
| 360 |
1, |
| 361 |
BasicStroke.CAP_ROUND, |
| 362 |
BasicStroke.JOIN_ROUND |
| 363 |
); |
| 364 |
g2.setStroke(debugStroke); |
| 365 |
g2.setColor(new Color(170, 170, 170)); |
| 366 |
g2.setXORMode(Color.white); |
| 367 |
for(int x = 0; x < diagram.getWidth(); x += diagram.getCellWidth()) |
| 368 |
g2.drawLine(x, 0, x, diagram.getHeight()); |
| 369 |
for(int y = 0; y < diagram.getHeight(); y += diagram.getCellHeight()) |
| 370 |
g2.drawLine(0, y, diagram.getWidth(), y); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
g2.dispose(); |
| 375 |
|
| 376 |
return renderedImage; |
| 377 |
} |
| 378 |
|
| •379 |
private RenderedImage renderTextLayer(ArrayList<DiagramText> textObjects, int width, int height){ |
| 380 |
TextCanvas canvas = new TextCanvas(textObjects); |
| 381 |
Image image = canvas.createImage(width, height); |
| 382 |
Graphics g = image.getGraphics(); |
| 383 |
canvas.paint(g); |
| 384 |
return (RenderedImage) image; |
| 385 |
} |
| 386 |
|
| 387 |
private class TextCanvas extends Canvas { |
| •388 |
ArrayList<DiagramText> textObjects; |
| 389 |
|
| •390 |
public TextCanvas(ArrayList<DiagramText> textObjects){ |
| 391 |
this.textObjects = textObjects; |
| 392 |
} |
| 393 |
|
| 394 |
public void paint(Graphics g){ |
| 395 |
Graphics g2 = (Graphics2D) g; |
| •396 |
Iterator<DiagramText> textIt = textObjects.iterator(); |
| 397 |
while(textIt.hasNext()){ |
| 398 |
DiagramText text = (DiagramText) textIt.next(); |
| 399 |
g2.setFont(text.getFont()); |
| 400 |
if(text.hasOutline()){ |
| 401 |
g2.setColor(text.getOutlineColor()); |
| 402 |
g2.drawString(text.getText(), text.getXPos() + 1, text.getYPos()); |
| 403 |
g2.drawString(text.getText(), text.getXPos() - 1, text.getYPos()); |
| 404 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos() + 1); |
| 405 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos() - 1); |
| 406 |
} |
| 407 |
g2.setColor(text.getColor()); |
| 408 |
g2.drawString(text.getText(), text.getXPos(), text.getYPos()); |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
private void renderCustomShape(DiagramShape shape, Graphics2D g2){ |
| 414 |
CustomShapeDefinition definition = shape.getDefinition(); |
| 415 |
|
| 416 |
Rectangle bounds = shape.getBounds(); |
| 417 |
|
| 418 |
if(definition.hasBorder()){ |
| 419 |
g2.setColor(shape.getStrokeColor()); |
| 420 |
if(shape.isStrokeDashed()) |
| 421 |
g2.setStroke(dashStroke); |
| 422 |
else |
| 423 |
g2.setStroke(normalStroke); |
| 424 |
g2.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y); |
| 425 |
g2.drawLine(bounds.x + bounds.width, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height); |
| 426 |
g2.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height); |
| 427 |
g2.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height); |
| 428 |
|
| 429 |
|
| 430 |
} |
| 431 |
|
| •432 |
|
| 433 |
if(definition.getFilename().endsWith(".png")){ |
| 434 |
renderCustomPNGShape(shape, g2); |
| 435 |
} else if(definition.getFilename().endsWith(".svg")){ |
| •436 |
renderCustomSVGShape(shape, g2); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| •440 |
private void renderCustomSVGShape(DiagramShape shape, Graphics2D g2){ |
| 441 |
CustomShapeDefinition definition = shape.getDefinition(); |
| 442 |
Rectangle bounds = shape.getBounds(); |
| 443 |
Image graphic; |
| 444 |
try { |
| 445 |
if(shape.getFillColor() == null) { |
| 446 |
graphic = ImageHandler.instance().renderSVG( |
| 447 |
definition.getFilename(), bounds.width, bounds.height, definition.stretches()); |
| 448 |
} else { |
| 449 |
graphic = ImageHandler.instance().renderSVG( |
| 450 |
definition.getFilename(), bounds.width, bounds.height, definition.stretches(), IDREGEX, shape.getFillColor()); |
| 451 |
} |
| 452 |
g2.drawImage(graphic, bounds.x, bounds.y, null); |
| 453 |
} catch (IOException e) { |
| 454 |
e.printStackTrace(); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
private void renderCustomPNGShape(DiagramShape shape, Graphics2D g2){ |
| 459 |
CustomShapeDefinition definition = shape.getDefinition(); |
| 460 |
Rectangle bounds = shape.getBounds(); |
| 461 |
Image graphic = ImageHandler.instance().loadImage(definition.getFilename()); |
| 462 |
|
| 463 |
int xPos, yPos, width, height; |
| 464 |
|
| 465 |
if(definition.stretches()){ |
| 466 |
xPos = bounds.x; yPos = bounds.y; |
| 467 |
width = bounds.width; height = bounds.height; |
| 468 |
} else { |
| 469 |
int newHeight = bounds.width * graphic.getHeight(null) / graphic.getWidth(null); |
| 470 |
if(newHeight < bounds.height){ |
| 471 |
height = newHeight; |
| 472 |
width = bounds.width; |
| 473 |
xPos = bounds.x; |
| 474 |
yPos = bounds.y + bounds.height / 2 - graphic.getHeight(null) / 2; |
| 475 |
} else { |
| 476 |
width = graphic.getWidth(null) * bounds.height / graphic.getHeight(null); |
| 477 |
height = bounds.height; |
| 478 |
xPos = bounds.x + bounds.width / 2 - graphic.getWidth(null) / 2; |
| 479 |
yPos = bounds.y; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
g2.drawImage(graphic, xPos, yPos, width, height, null); |
| 484 |
} |
| 485 |
|
| 486 |
public static boolean isColorDark(Color color){ |
| 487 |
int brightness = Math.max(color.getRed(), color.getGreen()); |
| 488 |
brightness = Math.max(color.getBlue(), brightness); |
| 489 |
if(brightness < 200) { |
| 490 |
if(DEBUG) System.out.println("Color "+color+" is dark"); |
| 491 |
return true; |
| 492 |
} |
| 493 |
if(DEBUG) System.out.println("Color "+color+" is not dark"); |
| 494 |
return false; |
| 495 |
} |
| 496 |
} |