1 /*
2  * $RCSfile: DivideDescriptor.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:34 $
10  * $State: Exp $
11  */
12 package com.lightcrafts.mediax.jai.operator;
13 import java.awt.RenderingHints;
14 import java.awt.image.RenderedImage;
15 import java.awt.image.renderable.RenderableImage;
16 import com.lightcrafts.mediax.jai.JAI;
17 import com.lightcrafts.mediax.jai.OperationDescriptorImpl;
18 import com.lightcrafts.mediax.jai.ParameterBlockJAI;
19 import com.lightcrafts.mediax.jai.RenderableOp;
20 import com.lightcrafts.mediax.jai.RenderedOp;
21 import com.lightcrafts.mediax.jai.registry.RenderableRegistryMode;
22 import com.lightcrafts.mediax.jai.registry.RenderedRegistryMode;
23 
24 /**
25  * An <code>OperationDescriptor</code> describing the "Divide" operation.
26  *
27  * <p> The Divide operation takes two rendered or renderable images,
28  * and for every pair of pixels, one from each source image of the
29  * corresponding position and band, divides the pixel from the first
30  * source by the pixel from the second source. No additional parameters
31  * are required for this operation.
32  *
33  * <p> In case of division by 0, if the numerator is 0, then the result
34  * is set to 0; otherwise, the result is set to the maximum value
35  * supported by the destination data type.
36  *
37  * <p> The two source images may have different number of bands and data
38  * types. By default, the destination image bound is the intersection
39  * of the two source image bounds. If the two sources don't intersect,
40  * the destination will have a width and a height of 0.
41  *
42  * <p> The default number of bands of the destination image is the same
43  * as the least number of bands of the sources, and the data type is the
44  * biggest data type of the sources.
45  *
46  * <p> As a special case, if one of the source images has N bands (N >
47  * 1), the other source has 1 band, and an <code>ImageLayout</code>
48  * hint is provided containing a destination <code>SampleModel</code>
49  * with K bands (1 < K <= N), then the single band of the 1-banded
50  * source will be divided by or into to each of the first K bands of
51  * the N-band source.
52  *
53  * <p> If the result of the operation underflows/overflows the
54  * minimum/maximum value supported by the destination data type, then
55  * it will be clamped to the minimum/maximum value respectively.
56  *
57  * <p> The destination pixel values are defined by the pseudocode:
58  * <pre>
59  * dst[x][y][dstBand] = srcs[0][x][y][src0Band]/srcs[1][x][y][src1Band];
60  * </pre>
61  *
62  * <p><table border=1>
63  * <caption>Resource List</caption>
64  * <tr><th>Name</th>        <th>Value</th></tr>
65  * <tr><td>GlobalName</td>  <td>divide</td></tr>
66  * <tr><td>LocalName</td>   <td>divide</td></tr>
67  * <tr><td>Vendor</td>      <td>com.lightcrafts.media.jai</td></tr>
68  * <tr><td>Description</td> <td>Dividies one image by
69  *                              another image.</td></tr>
70  * <tr><td>DocURL</td>      <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/DivideDescriptor.html</td></tr>
71  * <tr><td>Version</td>     <td>1.0</td></tr>
72  * </table></p>
73  *
74  * <p> No parameters are needed for this operation.
75  *
76  * @see com.lightcrafts.mediax.jai.OperationDescriptor */
77 public class DivideDescriptor extends OperationDescriptorImpl {
78 
79     /**
80      * The resource strings that provide the general documentation
81      * and specify the parameter list for this operation.
82      */
83     private static final String[][] resources = {
84         {"GlobalName",  "Divide"},
85         {"LocalName",   "Divide"},
86         {"Vendor",      "com.lightcrafts.media.jai"},
87         {"Description", JaiI18N.getString("DivideDescriptor0")},
88         {"DocURL",      "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/DivideDescriptor.html"},
89         {"Version",     JaiI18N.getString("DescriptorVersion")}
90     };
91 
92     /** Constructor. */
DivideDescriptor()93     public DivideDescriptor() {
94         super(resources, 2, null, null, null);
95     }
96 
97     /** Returns <code>true</code> since renderable operation is supported. */
isRenderableSupported()98     public boolean isRenderableSupported() {
99         return true;
100     }
101 
102 
103     /**
104      * Divides one image by another image.
105      *
106      * <p>Creates a <code>ParameterBlockJAI</code> from all
107      * supplied arguments except <code>hints</code> and invokes
108      * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
109      *
110      * @see JAI
111      * @see ParameterBlockJAI
112      * @see RenderedOp
113      *
114      * @param source0 <code>RenderedImage</code> source 0.
115      * @param source1 <code>RenderedImage</code> source 1.
116      * @param hints The <code>RenderingHints</code> to use.
117      * May be <code>null</code>.
118      * @return The <code>RenderedOp</code> destination.
119      * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
120      * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
121      */
create(RenderedImage source0, RenderedImage source1, RenderingHints hints)122     public static RenderedOp create(RenderedImage source0,
123                                     RenderedImage source1,
124                                     RenderingHints hints)  {
125         ParameterBlockJAI pb =
126             new ParameterBlockJAI("Divide",
127                                   RenderedRegistryMode.MODE_NAME);
128 
129         pb.setSource("source0", source0);
130         pb.setSource("source1", source1);
131 
132         return JAI.create("Divide", pb, hints);
133     }
134 
135     /**
136      * Divides one image by another image.
137      *
138      * <p>Creates a <code>ParameterBlockJAI</code> from all
139      * supplied arguments except <code>hints</code> and invokes
140      * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
141      *
142      * @see JAI
143      * @see ParameterBlockJAI
144      * @see RenderableOp
145      *
146      * @param source0 <code>RenderableImage</code> source 0.
147      * @param source1 <code>RenderableImage</code> source 1.
148      * @param hints The <code>RenderingHints</code> to use.
149      * May be <code>null</code>.
150      * @return The <code>RenderableOp</code> destination.
151      * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
152      * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
153      */
createRenderable(RenderableImage source0, RenderableImage source1, RenderingHints hints)154     public static RenderableOp createRenderable(RenderableImage source0,
155                                                 RenderableImage source1,
156                                                 RenderingHints hints)  {
157         ParameterBlockJAI pb =
158             new ParameterBlockJAI("Divide",
159                                   RenderableRegistryMode.MODE_NAME);
160 
161         pb.setSource("source0", source0);
162         pb.setSource("source1", source1);
163 
164         return JAI.createRenderable("Divide", pb, hints);
165     }
166 }
167