1 7 8 package java.awt.image; 9 10 import java.awt.color.ICC_Profile ; 11 import java.awt.geom.Rectangle2D ; 12 import java.awt.Rectangle ; 13 import java.awt.RenderingHints ; 14 import java.awt.geom.Point2D ; 15 import sun.awt.image.ImagingLib; 16 17 51 public class ConvolveOp implements BufferedImageOp , RasterOp { 52 Kernel kernel; 53 int edgeHint; 54 RenderingHints hints; 55 58 59 63 64 public static final int EDGE_ZERO_FILL = 0; 65 66 70 public static final int EDGE_NO_OP = 1; 71 72 83 public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) { 84 this.kernel = kernel; 85 this.edgeHint = edgeCondition; 86 this.hints = hints; 87 } 88 89 96 public ConvolveOp(Kernel kernel) { 97 this.kernel = kernel; 98 this.edgeHint = EDGE_ZERO_FILL; 99 } 100 101 107 public int getEdgeCondition() { 108 return edgeHint; 109 } 110 111 115 public final Kernel getKernel() { 116 return (Kernel ) kernel.clone(); 117 } 118 119 138 public final BufferedImage filter (BufferedImage src, BufferedImage dst) { 139 if (src == null) { 140 throw new NullPointerException ("src image is null"); 141 } 142 if (src == dst) { 143 throw new IllegalArgumentException ("src image cannot be the "+ 144 "same as the dst image"); 145 } 146 147 boolean needToConvert = false; 148 ColorModel srcCM = src.getColorModel(); 149 ColorModel dstCM; 150 BufferedImage origDst = dst; 151 152 if (srcCM instanceof IndexColorModel ) { 154 IndexColorModel icm = (IndexColorModel ) srcCM; 155 src = icm.convertToIntDiscrete(src.getRaster(), false); 156 srcCM = src.getColorModel(); 157 } 158 159 if (dst == null) { 160 dst = createCompatibleDestImage(src, null); 161 dstCM = srcCM; 162 origDst = dst; 163 } 164 else { 165 dstCM = dst.getColorModel(); 166 if (srcCM.getColorSpace().getType() != 167 dstCM.getColorSpace().getType()) 168 { 169 needToConvert = true; 170 dst = createCompatibleDestImage(src, null); 171 dstCM = dst.getColorModel(); 172 } 173 else if (dstCM instanceof IndexColorModel ) { 174 dst = createCompatibleDestImage(src, null); 175 dstCM = dst.getColorModel(); 176 } 177 } 178 179 if (ImagingLib.filter(this, src, dst) == null) { 180 throw new ImagingOpException ("Unable to convolve src image"); 181 } 182 183 if (needToConvert) { 184 ColorConvertOp ccop = new ColorConvertOp (hints); 185 ccop.filter(dst, origDst); 186 } 187 else if (origDst != dst) { 188 java.awt.Graphics2D g = origDst.createGraphics(); 189 try { 190 g.drawImage(dst, 0, 0, null); 191 } finally { 192 g.dispose(); 193 } 194 } 195 196 return origDst; 197 } 198 199 217 public final WritableRaster filter (Raster src, WritableRaster dst) { 218 if (dst == null) { 219 dst = createCompatibleDestRaster(src); 220 } 221 else if (src == dst) { 222 throw new IllegalArgumentException ("src image cannot be the "+ 223 "same as the dst image"); 224 } 225 else if (src.getNumBands() != dst.getNumBands()) { 226 throw new ImagingOpException ("Different number of bands in src "+ 227 " and dst Rasters"); 228 } 229 230 if (ImagingLib.filter(this, src, dst) == null) { 231 throw new ImagingOpException ("Unable to convolve src image"); 232 } 233 234 return dst; 235 } 236 237 245 public BufferedImage createCompatibleDestImage(BufferedImage src, 246 ColorModel destCM) { 247 BufferedImage image; 248 if (destCM == null) { 249 destCM = src.getColorModel(); 250 if (destCM instanceof IndexColorModel ) { 252 destCM = ColorModel.getRGBdefault(); 253 } 254 } 255 256 int w = src.getWidth(); 257 int h = src.getHeight(); 258 image = new BufferedImage (destCM, 259 destCM.createCompatibleWritableRaster(w, h), 260 destCM.isAlphaPremultiplied(), null); 261 262 return image; 263 } 264 265 269 public WritableRaster createCompatibleDestRaster(Raster src) { 270 return src.createCompatibleWritableRaster(); 271 } 272 273 278 public final Rectangle2D getBounds2D(BufferedImage src) { 279 return getBounds2D(src.getRaster()); 280 } 281 282 287 public final Rectangle2D getBounds2D(Raster src) { 288 return src.getBounds(); 289 } 290 291 297 public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) { 298 if (dstPt == null) { 299 dstPt = new Point2D.Float (); 300 } 301 dstPt.setLocation(srcPt.getX(), srcPt.getY()); 302 303 return dstPt; 304 } 305 306 309 public final RenderingHints getRenderingHints() { 310 return hints; 311 } 312 } 313 314 315 | Popular Tags |