00001 package edu.stanford.hci.r3.render.ink;
00002
00003 import java.awt.BasicStroke;
00004 import java.awt.Color;
00005 import java.awt.Graphics2D;
00006 import java.awt.Stroke;
00007 import java.awt.geom.AffineTransform;
00008 import java.io.File;
00009 import java.util.List;
00010
00011 import javax.media.jai.TiledImage;
00012
00013 import edu.stanford.hci.r3.pen.ink.Ink;
00014 import edu.stanford.hci.r3.pen.ink.InkStroke;
00015 import edu.stanford.hci.r3.units.Pixels;
00016 import edu.stanford.hci.r3.units.Points;
00017 import edu.stanford.hci.r3.units.Units;
00018 import edu.stanford.hci.r3.util.DebugUtils;
00019 import edu.stanford.hci.r3.util.MathUtils;
00020 import edu.stanford.hci.r3.util.graphics.GraphicsUtils;
00021 import edu.stanford.hci.r3.util.graphics.ImageUtils;
00022 import edu.stanford.hci.r3.util.graphics.JAIUtils;
00023
00035 public class InkRenderer {
00036
00037
00038 private Ink ink;
00039
00040 private RenderingTechnique renderingTechnique = new RenderingTechniqueHybrid();
00041
00042 private boolean invertedColors = false;
00043
00044 public InkRenderer() {
00045
00046 }
00047
00051 public InkRenderer(Ink theInk) {
00052 ink = theInk;
00053 }
00054
00058 public void renderToG2D(Graphics2D g2d) {
00059 if (ink == null) {
00060 DebugUtils.println("Ink Object is NULL");
00061 return;
00062 }
00063
00064
00065 g2d.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00066 Color inkColor = ink.getColor();
00067 if (invertedColors) {
00068 g2d.setColor(new Color(255 - inkColor.getRed(), 255 - inkColor.getGreen(), 255 - inkColor
00069 .getBlue()));
00070 } else {
00071 g2d.setColor(inkColor);
00072 }
00073
00074 final List<InkStroke> strokes = ink.getStrokes();
00075 renderingTechnique.render(g2d, strokes);
00076 }
00077
00085 public void renderToJPEG(File destJPEGFile, int widthPixels, int heightPixels) {
00086 final TiledImage image = JAIUtils.createWritableBufferWithoutAlpha(widthPixels, heightPixels);
00087 final Graphics2D graphics2D = image.createGraphics();
00088 graphics2D.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00089
00090
00091 graphics2D.setColor(Color.WHITE);
00092 graphics2D.fillRect(0, 0, widthPixels, heightPixels);
00093
00094 renderToG2D(graphics2D);
00095 graphics2D.dispose();
00096 ImageUtils.writeImageToJPEG(image.getAsBufferedImage(), destJPEGFile);
00097 DebugUtils.println("Wrote the File");
00098 }
00099
00103 public void renderToJPEG(File destJPEGFile, Pixels resolutionPixelsPerInch, Units width, Units height) {
00104 final double scale = Points.ONE.getConversionTo(resolutionPixelsPerInch);
00105
00106 final int w = MathUtils.rint(width.getValueIn(resolutionPixelsPerInch));
00107 final int h = MathUtils.rint(height.getValueIn(resolutionPixelsPerInch));
00108 final TiledImage image = JAIUtils.createWritableBufferWithoutAlpha(w, h);
00109 final Graphics2D graphics2D = image.createGraphics();
00110 graphics2D.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00111
00112
00113
00114
00115
00116
00117
00118 graphics2D.setTransform(AffineTransform.getScaleInstance(scale, scale));
00119
00120
00121 graphics2D.setColor(Color.WHITE);
00122 graphics2D.fillRect(0, 0, w, h);
00123
00124 renderToG2D(graphics2D);
00125 graphics2D.dispose();
00126 ImageUtils.writeImageToJPEG(image.getAsBufferedImage(), destJPEGFile);
00127 DebugUtils.println("Wrote the File");
00128 }
00129
00133 public void renderToJPEGRecentered(File destFile) {
00134 double minX = ink.getMinX();
00135 double minY = ink.getMinY();
00136 double maxX = ink.getMaxX();
00137 double maxY = ink.getMaxY();
00138 int w = (int) (maxX - minX) + 20;
00139 int h = (int) (maxY - minY) + 20;
00140 final TiledImage image = JAIUtils.createWritableBufferWithoutAlpha(w, h);
00141 final Graphics2D graphics2D = image.createGraphics();
00142 graphics2D.setRenderingHints(GraphicsUtils.getBestRenderingHints());
00143
00144
00145 graphics2D.setColor(Color.WHITE);
00146 graphics2D.fillRect(0, 0, w, h);
00147 graphics2D.translate(-(minX - 10), -(minY - 10));
00148
00149 renderToG2D(graphics2D);
00150 graphics2D.dispose();
00151 ImageUtils.writeImageToJPEG(image.getAsBufferedImage(), destFile);
00152 DebugUtils.println("Wrote the File");
00153 }
00154
00158 public void setInk(Ink theInk) {
00159 ink = theInk;
00160 }
00161
00162 public void setRenderingTechnique(RenderingTechnique rt) {
00163 renderingTechnique = rt;
00164 }
00165
00166 public void useCatmullRomRendering() {
00167 setRenderingTechnique(new RenderingTechniqueCatmullRom());
00168 }
00169
00170 public void useLineRendering() {
00171 setRenderingTechnique(new RenderingTechniqueLinear());
00172 }
00173
00174 public void useQuadraticRendering() {
00175 setRenderingTechnique(new RenderingTechniqueCatmullRom());
00176 }
00177
00178 public void useInvertedInkColors() {
00179 invertedColors = true;
00180 }
00181 }