1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.Point ; 21 import java.awt.RenderingHints ; 22 import java.awt.Shape ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.geom.Rectangle2D ; 25 import java.awt.image.BufferedImage ; 26 import java.awt.image.ColorModel ; 27 import java.awt.image.Raster ; 28 import java.awt.image.RenderedImage ; 29 import java.awt.image.WritableRaster ; 30 import java.awt.image.renderable.RenderContext ; 31 32 import org.apache.batik.ext.awt.image.PadMode; 33 import org.apache.batik.ext.awt.image.rendered.AffineRed; 34 import org.apache.batik.ext.awt.image.rendered.BufferedImageCachableRed; 35 import org.apache.batik.ext.awt.image.rendered.CachableRed; 36 import org.apache.batik.ext.awt.image.rendered.MorphologyOp; 37 import org.apache.batik.ext.awt.image.rendered.PadRed; 38 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed; 39 40 47 public class MorphologyRable8Bit 48 extends AbstractRable 49 implements MorphologyRable { 50 53 private double radiusX, radiusY; 54 55 59 private boolean doDilation; 60 61 public MorphologyRable8Bit(Filter src, 62 double radiusX, 63 double radiusY, 64 boolean doDilation){ 65 super(src, null); 66 setRadiusX(radiusX); 67 setRadiusY(radiusY); 68 setDoDilation(doDilation); 69 } 70 71 74 public Filter getSource(){ 75 return (Filter)getSources().get(0); 76 } 77 78 82 public void setSource(Filter src){ 83 init(src, null); 84 } 85 86 89 public Rectangle2D getBounds2D(){ 90 return getSource().getBounds2D(); 91 } 92 93 97 public void setRadiusX(double radiusX){ 98 if(radiusX <= 0){ 99 throw new IllegalArgumentException (); 100 } 101 102 touch(); 103 this.radiusX = radiusX; 104 } 105 106 110 public void setRadiusY(double radiusY){ 111 if(radiusY <= 0){ 112 throw new IllegalArgumentException (); 113 } 114 115 touch(); 116 this.radiusY = radiusY; 117 } 118 119 124 public void setDoDilation(boolean doDilation){ 125 touch(); 126 this.doDilation = doDilation; 127 } 128 129 132 public boolean getDoDilation(){ 133 return doDilation; 134 } 135 136 139 public double getRadiusX(){ 140 return radiusX; 141 } 142 143 146 public double getRadiusY(){ 147 return radiusY; 148 } 149 150 public RenderedImage createRendering(RenderContext rc) { 151 RenderingHints rh = rc.getRenderingHints(); 153 if (rh == null) rh = new RenderingHints (null); 154 155 AffineTransform at = rc.getTransform(); 157 158 double sx = at.getScaleX(); 162 double sy = at.getScaleY(); 163 164 double shx = at.getShearX(); 165 double shy = at.getShearY(); 166 167 double tx = at.getTranslateX(); 168 double ty = at.getTranslateY(); 169 170 double scaleX = Math.sqrt(sx*sx + shy*shy); 172 double scaleY = Math.sqrt(sy*sy + shx*shx); 173 174 AffineTransform srcAt; 175 srcAt = AffineTransform.getScaleInstance(scaleX, scaleY); 176 177 int radX = (int)Math.round(radiusX*scaleX); 178 int radY = (int)Math.round(radiusY*scaleY); 179 180 MorphologyOp op = null; 181 if(radX > 0 && radY > 0){ 182 op = new MorphologyOp(radX, radY, doDilation); 183 } 184 185 AffineTransform resAt; 188 resAt = new AffineTransform (sx/scaleX, shy/scaleX, 191 shx/scaleY, sy/scaleY, 192 tx, ty); 193 194 Shape aoi = rc.getAreaOfInterest(); 195 if(aoi == null) { 196 aoi = getBounds2D(); 197 } 198 199 Rectangle2D r = aoi.getBounds2D(); 200 r = new Rectangle2D.Double (r.getX()-radX/scaleX, 201 r.getY()-radY/scaleY, 202 r.getWidth() +2*radX/scaleX, 203 r.getHeight()+2*radY/scaleY); 204 205 RenderedImage ri; 206 ri = getSource().createRendering(new RenderContext (srcAt, r, rh)); 207 if (ri == null) 208 return null; 209 210 CachableRed cr; 211 cr = new RenderedImageCachableRed(ri); 212 213 Shape devShape = srcAt.createTransformedShape(aoi.getBounds2D()); 214 r = devShape.getBounds2D(); 215 r = new Rectangle2D.Double (r.getX()-radX, 216 r.getY()-radY, 217 r.getWidth() +2*radX, 218 r.getHeight()+2*radY); 219 cr = new PadRed(cr, r.getBounds(), PadMode.ZERO_PAD, rh); 220 221 223 ColorModel cm = ri.getColorModel(); 224 225 Raster rr = cr.getData(); 229 Point pt = new Point (0,0); 230 WritableRaster wr = Raster.createWritableRaster(rr.getSampleModel(), 231 rr.getDataBuffer(), 232 pt); 233 234 BufferedImage srcBI; 235 srcBI = new BufferedImage (cm, wr, cm.isAlphaPremultiplied(), null); 236 237 BufferedImage destBI; 238 if(op != null){ 239 destBI = op.filter(srcBI, null); 240 } 241 else{ 242 destBI = srcBI; 243 } 244 245 final int rrMinX = cr.getMinX(); 246 final int rrMinY = cr.getMinY(); 247 248 cr = new BufferedImageCachableRed(destBI, rrMinX, rrMinY); 249 250 if (!resAt.isIdentity()) 251 cr = new AffineRed(cr, resAt, rh); 252 253 255 return cr; 256 } 257 258 268 public Shape getDependencyRegion(int srcIndex, Rectangle2D outputRgn){ 269 return super.getDependencyRegion(srcIndex, outputRgn); 273 } 274 275 286 public Shape getDirtyRegion(int srcIndex, Rectangle2D inputRgn){ 287 return super.getDirtyRegion(srcIndex, inputRgn); 291 } 292 293 } 294 | Popular Tags |