73 /** 74 * Edge condition constants. 75 */ 76 77 /** 78 * Pixels at the edge of the destination image are set to zero. This 79 * is the default. 80 */ 81 82 @Native public static final int EDGE_ZERO_FILL = 0; 83 84 /** 85 * Pixels at the edge of the source image are copied to 86 * the corresponding pixels in the destination without modification. 87 */ 88 @Native public static final int EDGE_NO_OP = 1; 89 90 /** 91 * Constructs a ConvolveOp given a Kernel, an edge condition, and a 92 * RenderingHints object (which may be null). 93 * @param kernel the specified <code>Kernel</code> 94 * @param edgeCondition the specified edge condition 95 * @param hints the specified <code>RenderingHints</code> object 96 * @see Kernel 97 * @see #EDGE_NO_OP 98 * @see #EDGE_ZERO_FILL 99 * @see java.awt.RenderingHints 100 */ 101 public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) { 102 this.kernel = kernel; 103 this.edgeHint = edgeCondition; 104 this.hints = hints; 105 } 106 107 /** 108 * Constructs a ConvolveOp given a Kernel. The edge condition 109 * will be EDGE_ZERO_FILL. 110 * @param kernel the specified <code>Kernel</code> 111 * @see Kernel 112 * @see #EDGE_ZERO_FILL 113 */ 114 public ConvolveOp(Kernel kernel) { 115 this.kernel = kernel; 116 this.edgeHint = EDGE_ZERO_FILL; 117 } 118 119 /** 120 * Returns the edge condition. 121 * @return the edge condition of this <code>ConvolveOp</code>. 122 * @see #EDGE_NO_OP 123 * @see #EDGE_ZERO_FILL 124 */ 125 public int getEdgeCondition() { 126 return edgeHint; 127 } 128 129 /** 130 * Returns the Kernel. 131 * @return the <code>Kernel</code> of this <code>ConvolveOp</code>. 132 */ 133 public final Kernel getKernel() { 134 return (Kernel) kernel.clone(); 135 } 136 137 /** 138 * Performs a convolution on BufferedImages. Each component of the 139 * source image will be convolved (including the alpha component, if 140 * present). 141 * If the color model in the source image is not the same as that 142 * in the destination image, the pixels will be converted 143 * in the destination. If the destination image is null, 144 * a BufferedImage will be created with the source ColorModel. 145 * The IllegalArgumentException may be thrown if the source is the 146 * same as the destination. 147 * @param src the source <code>BufferedImage</code> to filter 148 * @param dst the destination <code>BufferedImage</code> for the 149 * filtered <code>src</code> 150 * @return the filtered <code>BufferedImage</code> 151 * @throws NullPointerException if <code>src</code> is <code>null</code> 152 * @throws IllegalArgumentException if <code>src</code> equals 153 * <code>dst</code> 154 * @throws ImagingOpException if <code>src</code> cannot be filtered 155 */ 156 public final BufferedImage filter (BufferedImage src, BufferedImage dst) { 157 if (src == null) { 158 throw new NullPointerException("src image is null"); 159 } 160 if (src == dst) { 161 throw new IllegalArgumentException("src image cannot be the "+ 162 "same as the dst image"); 163 } 164 165 boolean needToConvert = false; 166 ColorModel srcCM = src.getColorModel(); 167 ColorModel dstCM; 168 BufferedImage origDst = dst; 169 170 // Can't convolve an IndexColorModel. Need to expand it 171 if (srcCM instanceof IndexColorModel) { 172 IndexColorModel icm = (IndexColorModel) srcCM; 173 src = icm.convertToIntDiscrete(src.getRaster(), false); 174 srcCM = src.getColorModel(); 204 } 205 else if (origDst != dst) { 206 java.awt.Graphics2D g = origDst.createGraphics(); 207 try { 208 g.drawImage(dst, 0, 0, null); 209 } finally { 210 g.dispose(); 211 } 212 } 213 214 return origDst; 215 } 216 217 /** 218 * Performs a convolution on Rasters. Each band of the source Raster 219 * will be convolved. 220 * The source and destination must have the same number of bands. 221 * If the destination Raster is null, a new Raster will be created. 222 * The IllegalArgumentException may be thrown if the source is 223 * the same as the destination. 224 * @param src the source <code>Raster</code> to filter 225 * @param dst the destination <code>WritableRaster</code> for the 226 * filtered <code>src</code> 227 * @return the filtered <code>WritableRaster</code> 228 * @throws NullPointerException if <code>src</code> is <code>null</code> 229 * @throws ImagingOpException if <code>src</code> and <code>dst</code> 230 * do not have the same number of bands 231 * @throws ImagingOpException if <code>src</code> cannot be filtered 232 * @throws IllegalArgumentException if <code>src</code> equals 233 * <code>dst</code> 234 */ 235 public final WritableRaster filter (Raster src, WritableRaster dst) { 236 if (dst == null) { 237 dst = createCompatibleDestRaster(src); 238 } 239 else if (src == dst) { 240 throw new IllegalArgumentException("src image cannot be the "+ 241 "same as the dst image"); 242 } 243 else if (src.getNumBands() != dst.getNumBands()) { 244 throw new ImagingOpException("Different number of bands in src "+ 245 " and dst Rasters"); 246 } 247 248 if (ImagingLib.filter(this, src, dst) == null) { 249 throw new ImagingOpException ("Unable to convolve src image"); 250 } 251 252 return dst; 253 } 254 255 /** 256 * Creates a zeroed destination image with the correct size and number 257 * of bands. If destCM is null, an appropriate ColorModel will be used. 258 * @param src Source image for the filter operation. 259 * @param destCM ColorModel of the destination. Can be null. 260 * @return a destination <code>BufferedImage</code> with the correct 261 * size and number of bands. 262 */ 263 public BufferedImage createCompatibleDestImage(BufferedImage src, 264 ColorModel destCM) { 265 BufferedImage image; 266 267 int w = src.getWidth(); 268 int h = src.getHeight(); 269 270 WritableRaster wr = null; 271 272 if (destCM == null) { 273 destCM = src.getColorModel(); 274 // Not much support for ICM 275 if (destCM instanceof IndexColorModel) { 276 destCM = ColorModel.getRGBdefault(); 277 } else { 278 /* Create destination image as similar to the source 279 * as it possible... 280 */ | 73 /** 74 * Edge condition constants. 75 */ 76 77 /** 78 * Pixels at the edge of the destination image are set to zero. This 79 * is the default. 80 */ 81 82 @Native public static final int EDGE_ZERO_FILL = 0; 83 84 /** 85 * Pixels at the edge of the source image are copied to 86 * the corresponding pixels in the destination without modification. 87 */ 88 @Native public static final int EDGE_NO_OP = 1; 89 90 /** 91 * Constructs a ConvolveOp given a Kernel, an edge condition, and a 92 * RenderingHints object (which may be null). 93 * @param kernel the specified {@code Kernel} 94 * @param edgeCondition the specified edge condition 95 * @param hints the specified {@code RenderingHints} object 96 * @see Kernel 97 * @see #EDGE_NO_OP 98 * @see #EDGE_ZERO_FILL 99 * @see java.awt.RenderingHints 100 */ 101 public ConvolveOp(Kernel kernel, int edgeCondition, RenderingHints hints) { 102 this.kernel = kernel; 103 this.edgeHint = edgeCondition; 104 this.hints = hints; 105 } 106 107 /** 108 * Constructs a ConvolveOp given a Kernel. The edge condition 109 * will be EDGE_ZERO_FILL. 110 * @param kernel the specified {@code Kernel} 111 * @see Kernel 112 * @see #EDGE_ZERO_FILL 113 */ 114 public ConvolveOp(Kernel kernel) { 115 this.kernel = kernel; 116 this.edgeHint = EDGE_ZERO_FILL; 117 } 118 119 /** 120 * Returns the edge condition. 121 * @return the edge condition of this {@code ConvolveOp}. 122 * @see #EDGE_NO_OP 123 * @see #EDGE_ZERO_FILL 124 */ 125 public int getEdgeCondition() { 126 return edgeHint; 127 } 128 129 /** 130 * Returns the Kernel. 131 * @return the {@code Kernel} of this {@code ConvolveOp}. 132 */ 133 public final Kernel getKernel() { 134 return (Kernel) kernel.clone(); 135 } 136 137 /** 138 * Performs a convolution on BufferedImages. Each component of the 139 * source image will be convolved (including the alpha component, if 140 * present). 141 * If the color model in the source image is not the same as that 142 * in the destination image, the pixels will be converted 143 * in the destination. If the destination image is null, 144 * a BufferedImage will be created with the source ColorModel. 145 * The IllegalArgumentException may be thrown if the source is the 146 * same as the destination. 147 * @param src the source {@code BufferedImage} to filter 148 * @param dst the destination {@code BufferedImage} for the 149 * filtered {@code src} 150 * @return the filtered {@code BufferedImage} 151 * @throws NullPointerException if {@code src} is {@code null} 152 * @throws IllegalArgumentException if {@code src} equals 153 * {@code dst} 154 * @throws ImagingOpException if {@code src} cannot be filtered 155 */ 156 public final BufferedImage filter (BufferedImage src, BufferedImage dst) { 157 if (src == null) { 158 throw new NullPointerException("src image is null"); 159 } 160 if (src == dst) { 161 throw new IllegalArgumentException("src image cannot be the "+ 162 "same as the dst image"); 163 } 164 165 boolean needToConvert = false; 166 ColorModel srcCM = src.getColorModel(); 167 ColorModel dstCM; 168 BufferedImage origDst = dst; 169 170 // Can't convolve an IndexColorModel. Need to expand it 171 if (srcCM instanceof IndexColorModel) { 172 IndexColorModel icm = (IndexColorModel) srcCM; 173 src = icm.convertToIntDiscrete(src.getRaster(), false); 174 srcCM = src.getColorModel(); 204 } 205 else if (origDst != dst) { 206 java.awt.Graphics2D g = origDst.createGraphics(); 207 try { 208 g.drawImage(dst, 0, 0, null); 209 } finally { 210 g.dispose(); 211 } 212 } 213 214 return origDst; 215 } 216 217 /** 218 * Performs a convolution on Rasters. Each band of the source Raster 219 * will be convolved. 220 * The source and destination must have the same number of bands. 221 * If the destination Raster is null, a new Raster will be created. 222 * The IllegalArgumentException may be thrown if the source is 223 * the same as the destination. 224 * @param src the source {@code Raster} to filter 225 * @param dst the destination {@code WritableRaster} for the 226 * filtered {@code src} 227 * @return the filtered {@code WritableRaster} 228 * @throws NullPointerException if {@code src} is {@code null} 229 * @throws ImagingOpException if {@code src} and {@code dst} 230 * do not have the same number of bands 231 * @throws ImagingOpException if {@code src} cannot be filtered 232 * @throws IllegalArgumentException if {@code src} equals 233 * {@code dst} 234 */ 235 public final WritableRaster filter (Raster src, WritableRaster dst) { 236 if (dst == null) { 237 dst = createCompatibleDestRaster(src); 238 } 239 else if (src == dst) { 240 throw new IllegalArgumentException("src image cannot be the "+ 241 "same as the dst image"); 242 } 243 else if (src.getNumBands() != dst.getNumBands()) { 244 throw new ImagingOpException("Different number of bands in src "+ 245 " and dst Rasters"); 246 } 247 248 if (ImagingLib.filter(this, src, dst) == null) { 249 throw new ImagingOpException ("Unable to convolve src image"); 250 } 251 252 return dst; 253 } 254 255 /** 256 * Creates a zeroed destination image with the correct size and number 257 * of bands. If destCM is null, an appropriate ColorModel will be used. 258 * @param src Source image for the filter operation. 259 * @param destCM ColorModel of the destination. Can be null. 260 * @return a destination {@code BufferedImage} with the correct 261 * size and number of bands. 262 */ 263 public BufferedImage createCompatibleDestImage(BufferedImage src, 264 ColorModel destCM) { 265 BufferedImage image; 266 267 int w = src.getWidth(); 268 int h = src.getHeight(); 269 270 WritableRaster wr = null; 271 272 if (destCM == null) { 273 destCM = src.getColorModel(); 274 // Not much support for ICM 275 if (destCM instanceof IndexColorModel) { 276 destCM = ColorModel.getRGBdefault(); 277 } else { 278 /* Create destination image as similar to the source 279 * as it possible... 280 */ |