2015-04-07 18:26:58 +00:00
|
|
|
/* ========================================================================
|
|
|
|
* PlantUML : a free UML diagram generator
|
|
|
|
* ========================================================================
|
|
|
|
*
|
2019-01-16 18:34:41 +00:00
|
|
|
* (C) Copyright 2009-2020, Arnaud Roques
|
2015-04-07 18:26:58 +00:00
|
|
|
*
|
2016-03-06 16:47:34 +00:00
|
|
|
* Project Info: http://plantuml.com
|
2015-04-07 18:26:58 +00:00
|
|
|
*
|
2017-03-15 19:13:31 +00:00
|
|
|
* If you like this project or if you find it useful, you can support us at:
|
|
|
|
*
|
|
|
|
* http://plantuml.com/patreon (only 1$ per month!)
|
|
|
|
* http://plantuml.com/paypal
|
|
|
|
*
|
2015-04-07 18:26:58 +00:00
|
|
|
* This file is part of PlantUML.
|
|
|
|
*
|
|
|
|
* PlantUML is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PlantUML distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
|
|
* License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Original Author: Arnaud Roques
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package net.sourceforge.plantuml.ugraphic;
|
|
|
|
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.geom.AffineTransform;
|
|
|
|
import java.awt.geom.Dimension2D;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
2019-09-14 18:12:04 +00:00
|
|
|
import java.util.Random;
|
2015-04-07 18:26:58 +00:00
|
|
|
import java.util.Set;
|
2016-09-29 19:51:18 +00:00
|
|
|
import java.util.concurrent.Semaphore;
|
2015-04-07 18:26:58 +00:00
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
|
|
|
import net.sourceforge.plantuml.AnimatedGifEncoder;
|
|
|
|
import net.sourceforge.plantuml.CMapData;
|
2016-11-04 21:39:29 +00:00
|
|
|
import net.sourceforge.plantuml.ColorParam;
|
2018-07-27 21:56:46 +00:00
|
|
|
import net.sourceforge.plantuml.CornerParam;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.Dimension2DDouble;
|
|
|
|
import net.sourceforge.plantuml.EmptyImageBuilder;
|
|
|
|
import net.sourceforge.plantuml.FileFormat;
|
2015-05-31 18:56:03 +00:00
|
|
|
import net.sourceforge.plantuml.FileFormatOption;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.FileUtils;
|
2016-11-04 21:39:29 +00:00
|
|
|
import net.sourceforge.plantuml.ISkinParam;
|
|
|
|
import net.sourceforge.plantuml.LineParam;
|
2016-02-07 21:13:01 +00:00
|
|
|
import net.sourceforge.plantuml.OptionFlags;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.Url;
|
|
|
|
import net.sourceforge.plantuml.anim.AffineTransformation;
|
|
|
|
import net.sourceforge.plantuml.anim.Animation;
|
|
|
|
import net.sourceforge.plantuml.api.ImageDataComplex;
|
|
|
|
import net.sourceforge.plantuml.api.ImageDataSimple;
|
2016-07-25 19:25:28 +00:00
|
|
|
import net.sourceforge.plantuml.braille.UGraphicBraille;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.core.ImageData;
|
|
|
|
import net.sourceforge.plantuml.eps.EpsStrategy;
|
2016-08-25 20:45:37 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.StringBounder;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.graphic.UDrawable;
|
|
|
|
import net.sourceforge.plantuml.mjpeg.MJPEGGenerator;
|
2016-11-04 21:39:29 +00:00
|
|
|
import net.sourceforge.plantuml.skin.rose.Rose;
|
2020-05-07 14:12:08 +00:00
|
|
|
import net.sourceforge.plantuml.style.ClockwiseTopRightBottomLeft;
|
2020-03-18 10:50:02 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColor;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorBackground;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorGradient;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorSimple;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.color.HColorUtils;
|
2016-02-07 21:13:01 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.crossing.UGraphicCrossing;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.eps.UGraphicEps;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.g2d.UGraphicG2d;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.hand.UGraphicHandwritten;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.html5.UGraphicHtml5;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.svg.UGraphicSvg;
|
|
|
|
import net.sourceforge.plantuml.ugraphic.tikz.UGraphicTikz;
|
2018-06-25 19:05:58 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.txt.UGraphicTxt;
|
2015-04-07 18:26:58 +00:00
|
|
|
import net.sourceforge.plantuml.ugraphic.visio.UGraphicVdx;
|
|
|
|
|
|
|
|
public class ImageBuilder {
|
|
|
|
|
|
|
|
private final ColorMapper colorMapper;
|
|
|
|
private final double dpiFactor;
|
2020-03-18 10:50:02 +00:00
|
|
|
private final HColor mybackcolor;
|
2015-04-07 18:26:58 +00:00
|
|
|
private final String metadata;
|
|
|
|
private final String warningOrError;
|
2020-05-07 14:12:08 +00:00
|
|
|
private final double top;
|
|
|
|
private final double right;
|
|
|
|
private final double bottom;
|
|
|
|
private final double left;
|
2016-03-06 16:47:34 +00:00
|
|
|
private final Animation animation;
|
2015-04-07 18:26:58 +00:00
|
|
|
private final boolean useHandwritten;
|
|
|
|
|
|
|
|
private UDrawable udrawable;
|
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
private final UStroke borderStroke;
|
|
|
|
private final HColor borderColor;
|
|
|
|
private final double borderCorner;
|
2018-10-21 19:44:14 +00:00
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
private final boolean svgDimensionStyle;
|
2019-09-14 18:12:04 +00:00
|
|
|
private boolean randomPixel;
|
2016-11-04 21:39:29 +00:00
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
public static ImageBuilder buildA(ColorMapper colorMapper, boolean useHandwritten, Animation animation,
|
|
|
|
String metadata, String warningOrError, double dpiFactor, HColor mybackcolor) {
|
|
|
|
return new ImageBuilder(colorMapper, useHandwritten, animation, metadata, warningOrError, dpiFactor,
|
|
|
|
mybackcolor, ClockwiseTopRightBottomLeft.none());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ImageBuilder buildB(ColorMapper colorMapper, boolean useHandwritten,
|
|
|
|
ClockwiseTopRightBottomLeft margins, Animation animation, String metadata, String warningOrError,
|
|
|
|
double dpiFactor, HColor mybackcolor) {
|
|
|
|
return new ImageBuilder(colorMapper, useHandwritten, animation, metadata, warningOrError, dpiFactor,
|
|
|
|
mybackcolor, margins);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ImageBuilder buildC(ISkinParam skinParam, ClockwiseTopRightBottomLeft margins, Animation animation,
|
|
|
|
String metadata, String warningOrError, double dpiFactor, HColor mybackcolor) {
|
|
|
|
return new ImageBuilder(skinParam, animation, metadata, warningOrError, dpiFactor, mybackcolor, margins);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ImageBuilder buildD(ISkinParam skinParam, ClockwiseTopRightBottomLeft margins, Animation animation,
|
|
|
|
String metadata, String warningOrError, double dpiFactor) {
|
|
|
|
return new ImageBuilder(skinParam, animation, metadata, warningOrError, dpiFactor,
|
|
|
|
skinParam.getBackgroundColor(false), margins);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ImageBuilder(ColorMapper colorMapper, boolean useHandwritten, Animation animation, String metadata,
|
|
|
|
String warningOrError, double dpiFactor, HColor mybackcolor, ClockwiseTopRightBottomLeft margins) {
|
|
|
|
this.top = margins.getTop();
|
|
|
|
this.right = margins.getRight();
|
|
|
|
this.bottom = margins.getBottom();
|
|
|
|
this.left = margins.getLeft();
|
|
|
|
this.animation = animation;
|
|
|
|
this.metadata = metadata;
|
|
|
|
this.warningOrError = warningOrError;
|
|
|
|
this.dpiFactor = dpiFactor;
|
|
|
|
|
|
|
|
this.borderColor = null;
|
|
|
|
this.borderCorner = 0;
|
|
|
|
|
2018-03-09 21:37:34 +00:00
|
|
|
this.svgDimensionStyle = true;
|
2015-04-07 18:26:58 +00:00
|
|
|
this.colorMapper = colorMapper;
|
|
|
|
this.mybackcolor = mybackcolor;
|
|
|
|
this.useHandwritten = useHandwritten;
|
2020-05-07 14:12:08 +00:00
|
|
|
|
2016-11-04 21:39:29 +00:00
|
|
|
this.borderStroke = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
private ImageBuilder(ISkinParam skinParam, Animation animation, String metadata, String warningOrError,
|
|
|
|
double dpiFactor, HColor mybackcolor, ClockwiseTopRightBottomLeft margins) {
|
|
|
|
this.top = margins.getTop();
|
|
|
|
this.right = margins.getRight();
|
|
|
|
this.bottom = margins.getBottom();
|
|
|
|
this.left = margins.getLeft();
|
|
|
|
this.animation = animation;
|
|
|
|
this.metadata = metadata;
|
|
|
|
this.warningOrError = warningOrError;
|
|
|
|
this.dpiFactor = dpiFactor;
|
|
|
|
|
2016-11-04 21:39:29 +00:00
|
|
|
final Rose rose = new Rose();
|
|
|
|
this.borderColor = rose.getHtmlColor(skinParam, ColorParam.diagramBorder);
|
2018-04-06 20:36:30 +00:00
|
|
|
this.borderCorner = skinParam.getRoundCorner(CornerParam.diagramBorder, null);
|
2016-11-04 21:39:29 +00:00
|
|
|
|
2018-03-09 21:37:34 +00:00
|
|
|
this.svgDimensionStyle = skinParam.svgDimensionStyle();
|
2020-05-07 14:12:08 +00:00
|
|
|
this.colorMapper = skinParam.getColorMapper();
|
|
|
|
this.mybackcolor = mybackcolor;
|
2016-11-04 21:39:29 +00:00
|
|
|
this.useHandwritten = skinParam.handwritten();
|
2020-05-07 14:12:08 +00:00
|
|
|
|
|
|
|
final UStroke thickness = skinParam.getThickness(LineParam.diagramBorder, null);
|
|
|
|
if (thickness == null && borderColor != null) {
|
|
|
|
this.borderStroke = new UStroke();
|
|
|
|
} else {
|
|
|
|
this.borderStroke = thickness;
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 16:47:34 +00:00
|
|
|
public void setUDrawable(UDrawable udrawable) {
|
2015-04-07 18:26:58 +00:00
|
|
|
this.udrawable = udrawable;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:30:16 +00:00
|
|
|
public ImageData writeImageTOBEMOVED(FileFormatOption fileFormatOption, long seed, OutputStream os)
|
|
|
|
throws IOException {
|
2015-05-31 18:56:03 +00:00
|
|
|
final FileFormat fileFormat = fileFormatOption.getFileFormat();
|
2015-04-07 18:26:58 +00:00
|
|
|
if (fileFormat == FileFormat.MJPEG) {
|
2017-10-07 09:46:53 +00:00
|
|
|
return writeImageMjpeg(os, fileFormatOption.getDefaultStringBounder());
|
2015-04-07 18:26:58 +00:00
|
|
|
} else if (fileFormat == FileFormat.ANIMATED_GIF) {
|
2017-10-07 09:46:53 +00:00
|
|
|
return writeImageAnimatedGif(os, fileFormatOption.getDefaultStringBounder());
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
2017-04-19 18:30:16 +00:00
|
|
|
return writeImageInternal(fileFormatOption, seed, os, animation);
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 21:39:29 +00:00
|
|
|
private static Semaphore SEMAPHORE_SMALL;
|
|
|
|
private static Semaphore SEMAPHORE_BIG;
|
2016-09-29 19:51:18 +00:00
|
|
|
private static int MAX_PRICE = 0;
|
|
|
|
|
|
|
|
public static void setMaxPixel(int max) {
|
2016-11-04 21:39:29 +00:00
|
|
|
MAX_PRICE = max / 2;
|
|
|
|
SEMAPHORE_SMALL = new Semaphore(MAX_PRICE, true);
|
|
|
|
SEMAPHORE_BIG = new Semaphore(MAX_PRICE, true);
|
2016-09-29 19:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private int getPrice(FileFormatOption fileFormatOption, Dimension2D dim) {
|
2016-11-04 21:39:29 +00:00
|
|
|
// if (fileFormatOption.getFileFormat() != FileFormat.PNG) {
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
if (MAX_PRICE == 0) {
|
2016-09-29 19:51:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-03-18 10:50:02 +00:00
|
|
|
final int price = Math.min(MAX_PRICE,
|
|
|
|
((int) (dim.getHeight() * dpiFactor)) * ((int) (dim.getWidth() * dpiFactor)));
|
2016-09-29 19:51:18 +00:00
|
|
|
return price;
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:39:29 +00:00
|
|
|
private Semaphore getSemaphore(int price) {
|
|
|
|
if (price == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (price == MAX_PRICE) {
|
|
|
|
return SEMAPHORE_BIG;
|
|
|
|
}
|
|
|
|
return SEMAPHORE_SMALL;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:30:16 +00:00
|
|
|
private ImageData writeImageInternal(FileFormatOption fileFormatOption, long seed, OutputStream os,
|
|
|
|
Animation animationArg) throws IOException {
|
2016-08-25 20:45:37 +00:00
|
|
|
Dimension2D dim = getFinalDimension(fileFormatOption.getDefaultStringBounder());
|
2015-04-07 18:26:58 +00:00
|
|
|
double dx = 0;
|
|
|
|
double dy = 0;
|
2016-03-06 16:47:34 +00:00
|
|
|
if (animationArg != null) {
|
|
|
|
final MinMax minmax = animation.getMinMax(dim);
|
|
|
|
animationArg.setDimension(dim);
|
2015-04-07 18:26:58 +00:00
|
|
|
dim = minmax.getDimension();
|
|
|
|
dx = -minmax.getMinX();
|
|
|
|
dy = -minmax.getMinY();
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:39:29 +00:00
|
|
|
final int price = getPrice(fileFormatOption, dim);
|
|
|
|
final Semaphore semaphore = getSemaphore(price);
|
|
|
|
if (semaphore != null) {
|
2016-09-29 19:51:18 +00:00
|
|
|
try {
|
2016-11-04 21:39:29 +00:00
|
|
|
semaphore.acquire(price);
|
2016-09-29 19:51:18 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2017-04-19 18:30:16 +00:00
|
|
|
final UGraphic2 ug = createUGraphic(fileFormatOption, seed, dim, animationArg, dx, dy);
|
2016-11-04 21:39:29 +00:00
|
|
|
UGraphic ug2 = ug;
|
2020-05-07 14:12:08 +00:00
|
|
|
// if (externalMargin1 > 0) {
|
|
|
|
// ug2 = ug2.apply(new UTranslate(externalMargin1, externalMargin1));
|
|
|
|
// }
|
2016-11-04 21:39:29 +00:00
|
|
|
if (borderStroke != null) {
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor color = borderColor == null ? HColorUtils.BLACK : borderColor;
|
2020-05-07 14:12:08 +00:00
|
|
|
// final URectangle shape = new URectangle(dim.getWidth() - externalMargin() - borderStroke.getThickness(),
|
|
|
|
// dim.getHeight() - externalMargin() - borderStroke.getThickness()).rounded(borderCorner);
|
|
|
|
final URectangle shape = new URectangle(dim.getWidth() - borderStroke.getThickness(),
|
|
|
|
dim.getHeight() - borderStroke.getThickness()).rounded(borderCorner);
|
2020-04-19 16:04:39 +00:00
|
|
|
ug2.apply(color).apply(borderStroke).draw(shape);
|
2016-11-04 21:39:29 +00:00
|
|
|
}
|
2019-09-14 18:12:04 +00:00
|
|
|
if (randomPixel) {
|
|
|
|
drawRandomPoint(ug2);
|
|
|
|
}
|
2020-05-07 14:12:08 +00:00
|
|
|
// if (externalMargin1 > 0) {
|
|
|
|
// ug2 = ug2.apply(new UTranslate(externalMargin2, externalMargin2));
|
|
|
|
// }
|
|
|
|
ug2 = ug2.apply(new UTranslate(left, top));
|
2016-11-04 21:39:29 +00:00
|
|
|
final UGraphic ugDecored = handwritten(ug2);
|
2016-09-29 19:51:18 +00:00
|
|
|
udrawable.drawU(ugDecored);
|
|
|
|
ugDecored.flushUg();
|
|
|
|
ug.writeImageTOBEMOVED(os, metadata, 96);
|
|
|
|
os.flush();
|
|
|
|
|
|
|
|
if (ug instanceof UGraphicG2d) {
|
|
|
|
final Set<Url> urls = ((UGraphicG2d) ug).getAllUrlsEncountered();
|
|
|
|
if (urls.size() > 0) {
|
|
|
|
final CMapData cmap = CMapData.cmapString(urls, dpiFactor);
|
|
|
|
return new ImageDataComplex(dim, cmap, warningOrError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ImageDataSimple(dim);
|
|
|
|
} finally {
|
2016-11-04 21:39:29 +00:00
|
|
|
if (semaphore != null) {
|
|
|
|
semaphore.release(price);
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:12:04 +00:00
|
|
|
private void drawRandomPoint(UGraphic ug2) {
|
|
|
|
final Random rnd = new Random();
|
|
|
|
final int red = rnd.nextInt(40);
|
|
|
|
final int green = rnd.nextInt(40);
|
|
|
|
final int blue = rnd.nextInt(40);
|
|
|
|
final Color c = new Color(red, green, blue);
|
2020-03-18 10:50:02 +00:00
|
|
|
final HColor color = new HColorSimple(c, false);
|
2020-04-19 16:04:39 +00:00
|
|
|
ug2.apply(color).apply(color.bg()).draw(new URectangle(1, 1));
|
2019-09-14 18:12:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:12:08 +00:00
|
|
|
// private double externalMargin() {
|
|
|
|
// return 2 * (externalMargin1 + externalMargin2);
|
|
|
|
// }
|
2016-11-04 21:39:29 +00:00
|
|
|
|
2016-08-25 20:45:37 +00:00
|
|
|
public Dimension2D getFinalDimension(StringBounder stringBounder) {
|
2019-05-24 19:59:31 +00:00
|
|
|
final Dimension2D dim;
|
2020-05-07 14:12:08 +00:00
|
|
|
|
2019-09-14 18:12:04 +00:00
|
|
|
final LimitFinder limitFinder = new LimitFinder(stringBounder, true);
|
|
|
|
udrawable.drawU(limitFinder);
|
|
|
|
dim = new Dimension2DDouble(limitFinder.getMaxX(), limitFinder.getMaxY());
|
2020-05-07 14:12:08 +00:00
|
|
|
|
|
|
|
// return new Dimension2DDouble(dim.getWidth() + 1 + margin1 + margin2 + externalMargin(),
|
|
|
|
// dim.getHeight() + 1 + margin1 + margin2 + externalMargin());
|
|
|
|
return new Dimension2DDouble(dim.getWidth() + 1 + left + right, dim.getHeight() + 1 + top + bottom);
|
2016-03-06 16:47:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 18:26:58 +00:00
|
|
|
private UGraphic handwritten(UGraphic ug) {
|
|
|
|
if (useHandwritten) {
|
|
|
|
return new UGraphicHandwritten(ug);
|
|
|
|
}
|
2016-02-07 21:13:01 +00:00
|
|
|
if (OptionFlags.OMEGA_CROSSING) {
|
|
|
|
return new UGraphicCrossing(ug);
|
|
|
|
} else {
|
|
|
|
return ug;
|
|
|
|
}
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:45:37 +00:00
|
|
|
private ImageData writeImageMjpeg(OutputStream os, StringBounder stringBounder) throws IOException {
|
2015-04-07 18:26:58 +00:00
|
|
|
|
2016-08-25 20:45:37 +00:00
|
|
|
final LimitFinder limitFinder = new LimitFinder(stringBounder, true);
|
2015-04-07 18:26:58 +00:00
|
|
|
udrawable.drawU(limitFinder);
|
2020-05-07 14:12:08 +00:00
|
|
|
final Dimension2D dim = new Dimension2DDouble(limitFinder.getMaxX() + 1 + left + right,
|
|
|
|
limitFinder.getMaxY() + 1 + top + bottom);
|
2015-04-07 18:26:58 +00:00
|
|
|
|
|
|
|
final File f = new File("c:/tmp.avi");
|
|
|
|
|
|
|
|
final int nbframe = 100;
|
|
|
|
|
2020-03-18 10:50:02 +00:00
|
|
|
final MJPEGGenerator m = new MJPEGGenerator(f, getAviImage(null).getWidth(null),
|
|
|
|
getAviImage(null).getHeight(null), 12.0, nbframe);
|
2015-04-07 18:26:58 +00:00
|
|
|
for (int i = 0; i < nbframe; i++) {
|
|
|
|
// AffineTransform at = AffineTransform.getRotateInstance(1.0);
|
|
|
|
AffineTransform at = AffineTransform.getTranslateInstance(dim.getWidth() / 2, dim.getHeight() / 2);
|
|
|
|
at.rotate(90.0 * Math.PI / 180.0 * i / 100);
|
|
|
|
at.translate(-dim.getWidth() / 2, -dim.getHeight() / 2);
|
|
|
|
// final AffineTransform at = AffineTransform.getTranslateInstance(i, 0);
|
|
|
|
// final ImageIcon ii = new ImageIcon(getAviImage(at));
|
|
|
|
// m.addImage(ii.getImage());
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
m.finishAVI();
|
|
|
|
|
|
|
|
FileUtils.copyToStream(f, os);
|
|
|
|
|
|
|
|
return new ImageDataSimple(dim);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:45:37 +00:00
|
|
|
private ImageData writeImageAnimatedGif(OutputStream os, StringBounder stringBounder) throws IOException {
|
2015-04-07 18:26:58 +00:00
|
|
|
|
2016-08-25 20:45:37 +00:00
|
|
|
final LimitFinder limitFinder = new LimitFinder(stringBounder, true);
|
2015-04-07 18:26:58 +00:00
|
|
|
udrawable.drawU(limitFinder);
|
2020-05-07 14:12:08 +00:00
|
|
|
final Dimension2D dim = new Dimension2DDouble(limitFinder.getMaxX() + 1 + left + right,
|
|
|
|
limitFinder.getMaxY() + 1 + top + bottom);
|
2015-04-07 18:26:58 +00:00
|
|
|
|
2016-03-06 16:47:34 +00:00
|
|
|
final MinMax minmax = animation.getMinMax(dim);
|
2015-04-07 18:26:58 +00:00
|
|
|
|
|
|
|
final AnimatedGifEncoder e = new AnimatedGifEncoder();
|
|
|
|
// e.setQuality(1);
|
|
|
|
e.setRepeat(0);
|
|
|
|
e.start(os);
|
|
|
|
// e.setDelay(1000); // 1 frame per sec
|
|
|
|
// e.setDelay(100); // 10 frame per sec
|
|
|
|
e.setDelay(60); // 16 frame per sec
|
|
|
|
// e.setDelay(50); // 20 frame per sec
|
|
|
|
|
2016-03-06 16:47:34 +00:00
|
|
|
for (AffineTransformation at : animation.getAll()) {
|
2015-04-07 18:26:58 +00:00
|
|
|
final ImageIcon ii = new ImageIcon(getAviImage(at));
|
|
|
|
e.addFrame((BufferedImage) ii.getImage());
|
|
|
|
}
|
|
|
|
e.finish();
|
|
|
|
return new ImageDataSimple(dim);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private Image getAviImage(AffineTransformation affineTransform) throws IOException {
|
|
|
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
2017-04-19 18:30:16 +00:00
|
|
|
writeImageInternal(new FileFormatOption(FileFormat.PNG), 42, baos, Animation.singleton(affineTransform));
|
2015-04-07 18:26:58 +00:00
|
|
|
baos.close();
|
|
|
|
|
|
|
|
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
final Image im = ImageIO.read(bais);
|
|
|
|
bais.close();
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:30:16 +00:00
|
|
|
private UGraphic2 createUGraphic(FileFormatOption fileFormatOption, long seed, final Dimension2D dim,
|
|
|
|
Animation animationArg, double dx, double dy) {
|
2015-05-31 18:56:03 +00:00
|
|
|
final FileFormat fileFormat = fileFormatOption.getFileFormat();
|
2015-04-07 18:26:58 +00:00
|
|
|
switch (fileFormat) {
|
|
|
|
case PNG:
|
2020-04-26 18:31:41 +00:00
|
|
|
return createUGraphicPNG(colorMapper, dpiFactor, dim, mybackcolor, animationArg, dx, dy,
|
|
|
|
fileFormatOption.getWatermark());
|
2015-04-07 18:26:58 +00:00
|
|
|
case SVG:
|
2017-02-15 21:34:36 +00:00
|
|
|
return createUGraphicSVG(colorMapper, dpiFactor, dim, mybackcolor, fileFormatOption.getSvgLinkTarget(),
|
2019-11-03 17:40:03 +00:00
|
|
|
fileFormatOption.getHoverColor(), seed, fileFormatOption.getPreserveAspectRatio());
|
2015-04-07 18:26:58 +00:00
|
|
|
case EPS:
|
|
|
|
return new UGraphicEps(colorMapper, EpsStrategy.getDefault2());
|
|
|
|
case EPS_TEXT:
|
|
|
|
return new UGraphicEps(colorMapper, EpsStrategy.WITH_MACRO_AND_TEXT);
|
|
|
|
case HTML5:
|
|
|
|
return new UGraphicHtml5(colorMapper);
|
|
|
|
case VDX:
|
|
|
|
return new UGraphicVdx(colorMapper);
|
|
|
|
case LATEX:
|
2017-10-07 09:46:53 +00:00
|
|
|
return new UGraphicTikz(colorMapper, dpiFactor, true, fileFormatOption.getTikzFontDistortion());
|
2016-03-06 16:47:34 +00:00
|
|
|
case LATEX_NO_PREAMBLE:
|
2017-10-07 09:46:53 +00:00
|
|
|
return new UGraphicTikz(colorMapper, dpiFactor, false, fileFormatOption.getTikzFontDistortion());
|
2016-07-25 19:25:28 +00:00
|
|
|
case BRAILLE_PNG:
|
|
|
|
return new UGraphicBraille(colorMapper, fileFormat);
|
2019-04-21 20:40:01 +00:00
|
|
|
case UTXT:
|
2018-06-25 19:05:58 +00:00
|
|
|
case ATXT:
|
|
|
|
return new UGraphicTxt();
|
2015-04-07 18:26:58 +00:00
|
|
|
default:
|
|
|
|
throw new UnsupportedOperationException(fileFormat.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:31:41 +00:00
|
|
|
private UGraphic2 createUGraphicSVG(ColorMapper colorMapper, double scale, Dimension2D dim, final HColor suggested,
|
2019-11-03 17:40:03 +00:00
|
|
|
String svgLinkTarget, String hover, long seed, String preserveAspectRatio) {
|
2020-04-26 18:31:41 +00:00
|
|
|
HColor backColor = HColorUtils.WHITE;
|
|
|
|
if (suggested instanceof HColorSimple) {
|
|
|
|
backColor = suggested;
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
final UGraphicSvg ug;
|
2020-04-26 18:31:41 +00:00
|
|
|
if (suggested instanceof HColorGradient) {
|
|
|
|
ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, (HColorGradient) suggested, false, scale,
|
2019-11-03 17:40:03 +00:00
|
|
|
svgLinkTarget, hover, seed, preserveAspectRatio);
|
2020-04-26 18:31:41 +00:00
|
|
|
} else if (backColor == null || colorMapper.toColor(backColor).equals(Color.WHITE)) {
|
2020-03-18 10:50:02 +00:00
|
|
|
ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, false, scale, svgLinkTarget, hover, seed,
|
|
|
|
preserveAspectRatio);
|
2015-04-07 18:26:58 +00:00
|
|
|
} else {
|
2020-04-26 18:31:41 +00:00
|
|
|
ug = new UGraphicSvg(svgDimensionStyle, dim, colorMapper, colorMapper.toSvg(backColor), false, scale,
|
2019-11-03 17:40:03 +00:00
|
|
|
svgLinkTarget, hover, seed, preserveAspectRatio);
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
return ug;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private UGraphic2 createUGraphicPNG(ColorMapper colorMapper, double dpiFactor, final Dimension2D dim,
|
2020-04-26 18:31:41 +00:00
|
|
|
HColor mybackcolor, Animation affineTransforms, double dx, double dy, String watermark) {
|
2015-04-07 18:26:58 +00:00
|
|
|
Color backColor = Color.WHITE;
|
2020-03-18 10:50:02 +00:00
|
|
|
if (mybackcolor instanceof HColorSimple) {
|
2020-04-26 18:31:41 +00:00
|
|
|
backColor = colorMapper.toColor(mybackcolor);
|
2020-03-18 10:50:02 +00:00
|
|
|
} else if (mybackcolor instanceof HColorBackground) {
|
2015-04-07 18:26:58 +00:00
|
|
|
backColor = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-03-18 10:50:02 +00:00
|
|
|
* if (rotation) { builder = new EmptyImageBuilder((int) (dim.getHeight() *
|
|
|
|
* dpiFactor), (int) (dim.getWidth() * dpiFactor), backColor); graphics2D =
|
|
|
|
* builder.getGraphics2D(); graphics2D.rotate(-Math.PI / 2);
|
2015-04-07 18:26:58 +00:00
|
|
|
* graphics2D.translate(-builder.getBufferedImage().getHeight(), 0); } else {
|
|
|
|
*/
|
2020-04-26 18:31:41 +00:00
|
|
|
final EmptyImageBuilder builder = new EmptyImageBuilder(watermark, (int) (dim.getWidth() * dpiFactor),
|
2015-04-07 18:26:58 +00:00
|
|
|
(int) (dim.getHeight() * dpiFactor), backColor);
|
|
|
|
final Graphics2D graphics2D = builder.getGraphics2D();
|
|
|
|
|
|
|
|
// }
|
2020-03-18 10:50:02 +00:00
|
|
|
final UGraphicG2d ug = new UGraphicG2d(colorMapper, graphics2D, dpiFactor,
|
|
|
|
affineTransforms == null ? null : affineTransforms.getFirst(), dx, dy);
|
2015-04-07 18:26:58 +00:00
|
|
|
ug.setBufferedImage(builder.getBufferedImage());
|
|
|
|
final BufferedImage im = ((UGraphicG2d) ug).getBufferedImage();
|
2020-03-18 10:50:02 +00:00
|
|
|
if (mybackcolor instanceof HColorGradient) {
|
2020-04-26 18:31:41 +00:00
|
|
|
ug.apply(mybackcolor.bg()).draw(new URectangle(im.getWidth() / dpiFactor, im.getHeight() / dpiFactor));
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ug;
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:12:04 +00:00
|
|
|
public void setRandomPixel(boolean randomPixel) {
|
|
|
|
this.randomPixel = randomPixel;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:26:58 +00:00
|
|
|
}
|