1 18 package org.apache.batik.ext.awt.image.renderable; 19 20 import java.awt.RenderingHints ; 21 import java.awt.Shape ; 22 import java.awt.geom.AffineTransform ; 23 import java.awt.geom.Rectangle2D ; 24 import java.awt.image.RenderedImage ; 25 import java.awt.image.renderable.RenderContext ; 26 import java.util.List ; 27 28 import org.apache.batik.ext.awt.image.ARGBChannel; 29 import org.apache.batik.ext.awt.image.GraphicsUtil; 30 import org.apache.batik.ext.awt.image.rendered.AffineRed; 31 import org.apache.batik.ext.awt.image.rendered.CachableRed; 32 import org.apache.batik.ext.awt.image.rendered.DisplacementMapRed; 33 34 41 public class DisplacementMapRable8Bit 42 extends AbstractColorInterpolationRable 43 implements DisplacementMapRable { 44 45 48 private double scale; 49 50 54 private ARGBChannel xChannelSelector; 55 56 60 private ARGBChannel yChannelSelector; 61 62 public DisplacementMapRable8Bit(List sources, 63 double scale, 64 ARGBChannel xChannelSelector, 65 ARGBChannel yChannelSelector){ 66 setSources(sources); 67 setScale(scale); 68 setXChannelSelector(xChannelSelector); 69 setYChannelSelector(yChannelSelector); 70 } 71 72 public Rectangle2D getBounds2D(){ 73 return ((Filter)(getSources().elementAt(0))).getBounds2D(); 74 } 75 76 80 public void setScale(double scale){ 81 touch(); 82 this.scale = scale; 83 } 84 85 88 public double getScale(){ 89 return scale; 90 } 91 92 95 public void setSources(List sources){ 96 if(sources.size() != 2){ 97 throw new IllegalArgumentException (); 98 } 99 init(sources, null); 100 } 101 102 108 public void setXChannelSelector(ARGBChannel xChannelSelector){ 109 if(xChannelSelector == null){ 110 throw new IllegalArgumentException (); 111 } 112 touch(); 113 this.xChannelSelector = xChannelSelector; 114 } 115 116 119 public ARGBChannel getXChannelSelector(){ 120 return xChannelSelector; 121 } 122 123 129 public void setYChannelSelector(ARGBChannel yChannelSelector){ 130 if(yChannelSelector == null){ 131 throw new IllegalArgumentException (); 132 } 133 touch(); 134 this.yChannelSelector = yChannelSelector; 135 } 136 137 140 public ARGBChannel getYChannelSelector(){ 141 return yChannelSelector; 142 } 143 144 public RenderedImage createRendering(RenderContext rc) { 145 Filter displaced = (Filter)getSources().elementAt(0); 147 Filter map = (Filter)getSources().elementAt(1); 149 150 RenderingHints rh = rc.getRenderingHints(); 151 if (rh == null) rh = new RenderingHints (null); 152 153 AffineTransform at = rc.getTransform(); 155 156 double sx = at.getScaleX(); 159 double sy = at.getScaleY(); 160 161 double shx = at.getShearX(); 162 double shy = at.getShearY(); 163 164 double tx = at.getTranslateX(); 165 double ty = at.getTranslateY(); 166 167 double atScaleX = Math.sqrt(sx*sx + shy*shy); 169 double atScaleY = Math.sqrt(sy*sy + shx*shx); 170 171 float scaleX = (float)(scale*atScaleX); 174 float scaleY = (float)(scale*atScaleY); 175 176 if ((scaleX == 0) && (scaleY == 0)) 179 return displaced.createRendering(rc); 180 181 185 AffineTransform srcAt 186 = AffineTransform.getScaleInstance(atScaleX, atScaleY); 187 188 Shape origAOI = rc.getAreaOfInterest(); 189 if (origAOI == null) 190 origAOI = getBounds2D(); 191 192 Rectangle2D aoiR = origAOI.getBounds2D(); 193 194 RenderContext srcRc = new RenderContext (srcAt, aoiR, rh); 195 RenderedImage mapRed = map.createRendering(srcRc); 196 197 if (mapRed == null) return null; 198 199 aoiR = new Rectangle2D.Double (aoiR.getX() - scale/2, 202 aoiR.getY() - scale/2, 203 aoiR.getWidth() + scale, 204 aoiR.getHeight() + scale); 205 206 Rectangle2D displacedRect = displaced.getBounds2D(); 207 if (aoiR.intersects(displacedRect) == false) 208 return null; 209 210 aoiR = aoiR.createIntersection(displacedRect); 211 srcRc = new RenderContext (srcAt, aoiR, rh); 212 RenderedImage displacedRed = displaced.createRendering(srcRc); 213 214 if (displacedRed == null) return null; 215 216 mapRed = convertSourceCS(mapRed); 217 218 222 CachableRed cr = new DisplacementMapRed 223 (GraphicsUtil.wrap(displacedRed), 224 GraphicsUtil.wrap(mapRed), 225 xChannelSelector, yChannelSelector, 226 scaleX, scaleY, rh); 227 AffineTransform resAt 232 = new AffineTransform (sx/atScaleX, shy/atScaleX, 233 shx/atScaleY, sy/atScaleY, 234 tx, ty); 235 236 if(!resAt.isIdentity()) 237 cr = new AffineRed(cr, resAt, rh); 238 239 return cr; 240 241 242 } 243 244 254 public Shape getDependencyRegion(int srcIndex, Rectangle2D outputRgn){ 255 return super.getDependencyRegion(srcIndex, outputRgn); 259 } 260 261 272 public Shape getDirtyRegion(int srcIndex, Rectangle2D inputRgn){ 273 return super.getDirtyRegion(srcIndex, inputRgn); 277 } 278 279 } 280 | Popular Tags |