1 /* 2 * $RCSfile: ConvolveDescriptor.java,v $ 3 * 4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 5 * 6 * Use is subject to license terms. 7 * 8 * $Revision: 1.1 $ 9 * $Date: 2005/02/11 04:57:32 $ 10 * $State: Exp $ 11 */ 12 package com.lightcrafts.mediax.jai.operator; 13 import com.lightcrafts.media.jai.util.AreaOpPropertyGenerator; 14 import java.awt.RenderingHints; 15 import java.awt.image.RenderedImage; 16 import com.lightcrafts.mediax.jai.JAI; 17 import com.lightcrafts.mediax.jai.KernelJAI; 18 import com.lightcrafts.mediax.jai.OperationDescriptorImpl; 19 import com.lightcrafts.mediax.jai.ParameterBlockJAI; 20 import com.lightcrafts.mediax.jai.PropertyGenerator; 21 import com.lightcrafts.mediax.jai.RenderedOp; 22 import com.lightcrafts.mediax.jai.registry.RenderedRegistryMode; 23 24 /** 25 * An <code>OperationDescriptor</code> describing the "Convolve" operation. 26 * 27 * <p> Convolution is a spatial operation that computes each output 28 * sample by multiplying elements of a kernel with the samples 29 * surrounding a particular source sample. 30 * 31 * <p> For each destination sample, the kernel is rotated 180 degrees 32 * and its "key element," or origin, is placed over the source pixel 33 * corresponding with the destination pixel. The kernel elements are 34 * multiplied with the source pixels beneath them, and the resulting 35 * products are summed together to produce the destination sample 36 * value. 37 * 38 * <p> Pseudocode for the convolution operation on a single sample 39 * dst[x][y] is as follows, assuming the kernel is of size width x height 40 * and has already been rotated through 180 degrees. The kernel's Origin 41 * element is located at position (xOrigin, yOrigin): 42 * 43 * <pre> 44 * dst[x][y] = 0; 45 * for (int i = -xOrigin; i < -xOrigin + width; i++) { 46 * for (int j = -yOrigin; j < -yOrigin + height; j++) { 47 * dst[x][y] += src[x + i][y + j]*kernel[xOrigin + i][yOrigin + j]; 48 * } 49 * } 50 * </pre> 51 * 52 * <p> Convolution, like any neighborhood operation, leaves a band of 53 * pixels around the edges undefined. For example, for a 3x3 kernel 54 * only four kernel elements and four source pixels contribute to the 55 * convolution pixel at the corners of the source image. Pixels that 56 * do not allow the full kernel to be applied to the source are not 57 * included in the destination image. A "Border" operation may be used 58 * to add an appropriate border to the source image in order to avoid 59 * shrinkage of the image boundaries. 60 * 61 * <p> The kernel may not be bigger in any dimension than the image data. 62 * 63 * It should be noted that this operation automatically adds a 64 * value of <code>Boolean.TRUE</code> for the 65 * <code>JAI.KEY_REPLACE_INDEX_COLOR_MODEL</code> to the given 66 * <code>configuration</code> so that the operation is performed 67 * on the pixel values instead of being performed on the indices into 68 * the color map if the source(s) have an <code>IndexColorModel</code>. 69 * This addition will take place only if a value for the 70 * <code>JAI.KEY_REPLACE_INDEX_COLOR_MODEL</code> has not already been 71 * provided by the user. Note that the <code>configuration</code> Map 72 * is cloned before the new hint is added to it. The operation can be 73 * smart about the value of the <code>JAI.KEY_REPLACE_INDEX_COLOR_MODEL</code> 74 * <code>RenderingHints</code>, i.e. while the default value for the 75 * <code>JAI.KEY_REPLACE_INDEX_COLOR_MODEL</code> is 76 * <code>Boolean.TRUE</code>, in some cases the operator could set the 77 * default. 78 * 79 * <p><table border=1> 80 * <caption>Resource List</caption> 81 * <tr><th>Name</th> <th>Value</th></tr> 82 * <tr><td>GlobalName</td> <td>Convolve</td></tr> 83 * <tr><td>LocalName</td> <td>Convolve</td></tr> 84 * <tr><td>Vendor</td> <td>com.lightcrafts.media.jai</td></tr> 85 * <tr><td>Description</td> <td>Performs kernel-based convolution 86 * on an image.</td></tr> 87 * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ConvolveDescriptor.html</td></tr> 88 * <tr><td>Version</td> <td>1.0</td></tr> 89 * <tr><td>arg0Desc</td> <td>The convolution kernel.</td></tr> 90 * </table></p> 91 * 92 * <p><table border=1> 93 * <caption>Parameter List</caption> 94 * <tr><th>Name</th> <th>Class Type</th> 95 * <th>Default Value</th></tr> 96 * <tr><td>kernel</td> <td>com.lightcrafts.mediax.jai.KernelJAI</td> 97 * <td>NO_PARAMETER_DEFAULT</td> 98 * </table></p> 99 * 100 * @see com.lightcrafts.mediax.jai.OperationDescriptor 101 * @see com.lightcrafts.mediax.jai.KernelJAI 102 */ 103 public class ConvolveDescriptor extends OperationDescriptorImpl { 104 105 /** 106 * The resource strings that provide the general documentation and 107 * specify the parameter list for a Convolve operation. 108 */ 109 private static final String[][] resources = { 110 {"GlobalName", "Convolve"}, 111 {"LocalName", "Convolve"}, 112 {"Vendor", "com.lightcrafts.media.jai"}, 113 {"Description", JaiI18N.getString("ConvolveDescriptor0")}, 114 {"DocURL", "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ConvolveDescriptor.html"}, 115 {"Version", JaiI18N.getString("DescriptorVersion")}, 116 {"arg0Desc", JaiI18N.getString("ConvolveDescriptor1")} 117 }; 118 119 /** The parameter names for the Convolve operation. */ 120 private static final String[] paramNames = { 121 "kernel" 122 }; 123 124 /** The parameter class types for the Convolve operation. */ 125 private static final Class[] paramClasses = { 126 com.lightcrafts.mediax.jai.KernelJAI.class 127 }; 128 129 /** The parameter default values for the Convolve operation. */ 130 private static final Object[] paramDefaults = { 131 NO_PARAMETER_DEFAULT 132 }; 133 134 /** Constructor. */ ConvolveDescriptor()135 public ConvolveDescriptor() { 136 super(resources, 1, paramClasses, paramNames, paramDefaults); 137 } 138 139 /** 140 * Returns an array of <code>PropertyGenerators</code> implementing 141 * property inheritance for the "Convolve" operation. 142 * 143 * @return An array of property generators. 144 */ getPropertyGenerators()145 public PropertyGenerator[] getPropertyGenerators() { 146 PropertyGenerator[] pg = new PropertyGenerator[1]; 147 pg[0] = new AreaOpPropertyGenerator(); 148 return pg; 149 } 150 151 152 /** 153 * Performs kernel-based convolution on an image. 154 * 155 * <p>Creates a <code>ParameterBlockJAI</code> from all 156 * supplied arguments except <code>hints</code> and invokes 157 * {@link JAI#create(String,ParameterBlock,RenderingHints)}. 158 * 159 * @see JAI 160 * @see ParameterBlockJAI 161 * @see RenderedOp 162 * 163 * @param source0 <code>RenderedImage</code> source 0. 164 * @param kernel The convolution kernel. 165 * @param hints The <code>RenderingHints</code> to use. 166 * May be <code>null</code>. 167 * @return The <code>RenderedOp</code> destination. 168 * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>. 169 * @throws IllegalArgumentException if <code>kernel</code> is <code>null</code>. 170 */ create(RenderedImage source0, KernelJAI kernel, RenderingHints hints)171 public static RenderedOp create(RenderedImage source0, 172 KernelJAI kernel, 173 RenderingHints hints) { 174 ParameterBlockJAI pb = 175 new ParameterBlockJAI("Convolve", 176 RenderedRegistryMode.MODE_NAME); 177 178 pb.setSource("source0", source0); 179 180 pb.setParameter("kernel", kernel); 181 182 return JAI.create("Convolve", pb, hints); 183 } 184 } 185