1 18 package org.apache.batik.gvt.filter; 19 20 import java.awt.AlphaComposite ; 21 import java.awt.Graphics2D ; 22 import java.awt.Rectangle ; 23 import java.awt.RenderingHints ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.image.BufferedImage ; 27 import java.awt.image.ColorModel ; 28 import java.awt.image.SampleModel ; 29 import java.awt.image.WritableRaster ; 30 31 import org.apache.batik.ext.awt.image.GraphicsUtil; 32 import org.apache.batik.ext.awt.image.rendered.AbstractRed; 33 import org.apache.batik.ext.awt.image.rendered.AbstractTiledRed; 34 import org.apache.batik.ext.awt.image.rendered.CachableRed; 35 import org.apache.batik.gvt.GraphicsNode; 36 37 44 public class GraphicsNodeRed8Bit extends AbstractRed { 45 46 49 private GraphicsNode node; 50 51 private AffineTransform node2dev; 52 53 private RenderingHints hints; 54 55 private boolean usePrimitivePaint; 56 57 public GraphicsNodeRed8Bit(GraphicsNode node, 58 AffineTransform node2dev, 59 boolean usePrimitivePaint, 60 RenderingHints hints) { 61 super(); 63 this.node = node; 64 this.node2dev = node2dev; 65 this.hints = hints; 66 this.usePrimitivePaint = usePrimitivePaint; 67 68 71 AffineTransform at = node2dev; 72 Rectangle2D bounds2D = node.getPrimitiveBounds(); 73 if (bounds2D == null) bounds2D = new Rectangle2D.Float (0,0,1,1); 74 if (!usePrimitivePaint) { 75 AffineTransform nodeAt = node.getTransform(); 82 if (nodeAt != null) { 83 at = (AffineTransform )at.clone(); 84 at.concatenate(nodeAt); 85 } 86 } 87 Rectangle bounds = at.createTransformedShape(bounds2D).getBounds(); 88 90 ColorModel cm = createColorModel(); 91 92 int defSz = AbstractTiledRed.getDefaultTileSize(); 93 94 int tgX = defSz*(int)Math.floor(bounds.x/defSz); 96 int tgY = defSz*(int)Math.floor(bounds.y/defSz); 97 98 int tw = (bounds.x+bounds.width)-tgX; 99 if (tw > defSz) tw = defSz; 100 int th = (bounds.y+bounds.height)-tgY; 101 if (th > defSz) th = defSz; 102 if ((tw <= 0) || (th <= 0)) { 103 tw = 1; 104 th = 1; 105 } 106 107 SampleModel sm = cm.createCompatibleSampleModel(tw, th); 109 110 init((CachableRed)null, bounds, cm, sm, tgX, tgY, null); 112 } 113 114 public WritableRaster copyData(WritableRaster wr) { 115 genRect(wr); 116 return wr; 117 } 118 119 public void genRect(WritableRaster wr) { 120 BufferedImage offScreen 122 = new BufferedImage (cm, 123 wr.createWritableTranslatedChild(0,0), 124 cm.isAlphaPremultiplied(), 125 null); 126 127 Graphics2D g = GraphicsUtil.createGraphics(offScreen, hints); 128 g.setComposite(AlphaComposite.Clear); 129 g.fillRect(0, 0, wr.getWidth(), wr.getHeight()); 130 g.setComposite(AlphaComposite.SrcOver); 131 g.translate(-wr.getMinX(), -wr.getMinY()); 132 133 g.transform(node2dev); 135 136 137 if (usePrimitivePaint) { 139 node.primitivePaint(g); 140 } 141 else { 142 node.paint (g); 143 } 144 145 g.dispose(); 146 } 147 148 static final boolean onMacOSX; 149 static { 150 onMacOSX = ("Mac OS X".equals(System.getProperty("os.name"))); 152 } 153 154 public ColorModel createColorModel() { 155 if (onMacOSX) 156 return GraphicsUtil.sRGB_Pre; 157 return GraphicsUtil.sRGB_Unpre; 158 } 159 } 160 161 162 163 164 | Popular Tags |