1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.Color ; 21 import java.awt.Graphics2D ; 22 import java.awt.Rectangle ; 23 import java.awt.RenderingHints ; 24 import java.awt.Shape ; 25 import java.awt.geom.AffineTransform ; 26 import java.awt.geom.Rectangle2D ; 27 import java.awt.image.BufferedImage ; 28 import java.awt.image.RenderedImage ; 29 import java.awt.image.renderable.RenderContext ; 30 31 import org.apache.batik.ext.awt.image.GraphicsUtil; 32 import org.apache.batik.ext.awt.image.PadMode; 33 import org.apache.batik.ext.awt.image.rendered.BufferedImageCachableRed; 34 import org.apache.batik.ext.awt.image.rendered.CachableRed; 35 import org.apache.batik.ext.awt.image.rendered.MultiplyAlphaRed; 36 import org.apache.batik.ext.awt.image.rendered.PadRed; 37 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed; 38 39 45 public class ClipRable8Bit 46 extends AbstractRable 47 implements ClipRable { 48 49 protected boolean useAA; 50 51 54 protected Shape clipPath; 55 56 public ClipRable8Bit(Filter src, Shape clipPath) { 57 super(src, null); 58 setClipPath(clipPath); 59 setUseAntialiasedClip(false); 60 } 61 62 public ClipRable8Bit(Filter src, Shape clipPath, boolean useAA) { 63 super(src, null); 64 setClipPath(clipPath); 65 setUseAntialiasedClip(useAA); 66 } 67 68 72 public void setSource(Filter src) { 73 init(src, null); 74 } 75 76 80 public Filter getSource() { 81 return (Filter)getSources().get(0); 82 } 83 84 88 public void setUseAntialiasedClip(boolean useAA) { 89 touch(); 90 this.useAA = useAA; 91 } 92 93 97 public boolean getUseAntialiasedClip() { 98 return useAA; 99 } 100 101 102 107 public void setClipPath(Shape clipPath) { 108 touch(); 109 this.clipPath = clipPath; 110 } 111 112 117 public Shape getClipPath() { 118 return clipPath; 119 } 120 121 124 public Rectangle2D getBounds2D(){ 125 return getSource().getBounds2D(); 126 } 127 128 public RenderedImage createRendering(RenderContext rc) { 129 130 AffineTransform usr2dev = rc.getTransform(); 131 132 RenderingHints rh = rc.getRenderingHints(); 134 if (rh == null) rh = new RenderingHints (null); 135 136 Shape aoi = rc.getAreaOfInterest(); 137 if (aoi == null) aoi = getBounds2D(); 138 139 Rectangle2D rect = getBounds2D(); 140 Rectangle2D clipRect = clipPath.getBounds2D(); 141 Rectangle2D aoiRect = aoi.getBounds2D(); 142 143 if (rect.intersects(clipRect) == false) 144 return null; 145 Rectangle2D.intersect(rect, clipRect, rect); 146 147 148 if (rect.intersects(aoiRect) == false) 149 return null; 150 Rectangle2D.intersect(rect, aoi.getBounds2D(), rect); 151 152 Rectangle devR = usr2dev.createTransformedShape(rect).getBounds(); 153 154 if ((devR.width == 0) || (devR.height == 0)) 155 return null; 156 157 BufferedImage bi = new BufferedImage (devR.width, devR.height, 158 BufferedImage.TYPE_BYTE_GRAY); 159 160 Shape devShape = usr2dev.createTransformedShape(getClipPath()); 161 Rectangle devAOIR; 162 devAOIR = usr2dev.createTransformedShape(aoi).getBounds(); 163 164 Graphics2D g2d = GraphicsUtil.createGraphics(bi, rh); 165 166 if (false) { 167 java.util.Set s = rh.keySet(); 168 java.util.Iterator i = s.iterator(); 169 while (i.hasNext()) { 170 Object o = i.next(); 171 System.out.println("XXX: " + o + " -> " + rh.get(o)); 172 } 173 } 174 g2d.translate(-devR.x, -devR.y); 175 g2d.setPaint(Color.white); 176 g2d.fill(devShape); 177 g2d.dispose(); 178 179 RenderedImage ri; 180 ri = getSource().createRendering(new RenderContext (usr2dev, rect, rh)); 181 182 CachableRed cr, clipCr; 183 cr = RenderedImageCachableRed.wrap(ri); 184 clipCr = new BufferedImageCachableRed(bi, devR.x, devR.y); 185 CachableRed ret = new MultiplyAlphaRed(cr, clipCr); 186 187 ret = new PadRed(ret, devAOIR, PadMode.ZERO_PAD, rh); 189 190 return ret; 191 } 192 } 193 | Popular Tags |