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