InkRenderer.java

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                 // anti-aliased, high quality rendering
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                 // render a white canvas
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                 // transform the graphics such that we are in destUnits' pixels per inch, so that when we
00113                 // draw 72 Graphics2D pixels from now on, it will equal the correct number of output pixels
00114                 // in the JPEG.
00115                 // 
00116                 // TODO: Joel said that this next line is a bug, and ruins his blog app's ink alignment
00117                 // Can we figure out why? Why do we not need this line?
00118                 graphics2D.setTransform(AffineTransform.getScaleInstance(scale, scale));
00119 
00120                 // render a white canvas
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                 // render a white canvas
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 }

Generated on Sat Apr 14 18:21:36 2007 for R3 Paper Toolkit by  doxygen 1.4.7