1 /*
2  * $RCSfile: SubtractDescriptor.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:45 $
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 "Subtract"
26  * operation.
27  *
28  * <p> The Subtract operation takes two rendered or renderable images,
29  * and for every
30  * pair of pixels, one from each source image of the corresponding
31  * position and band, subtracts the pixel from the second source from
32  * the pixel from the first source. No additional parameters are
33  * required.
34  *
35  * <p> The two source images may have different numbers of bands and
36  * data types. By default, the destination image bounds are the
37  * intersection of the two source image bounds.  If the sources don't
38  * intersect, the destination will have a width and height of 0.
39  *
40  * <p> The default number of bands of the destination image is equal
41  * to the smallest number of bands of the sources, and the data type
42  * is the smallest data type with sufficient range to cover the range
43  * of both source data types (not necessarily the range of their
44  * sums).
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 is subtracted from or into each of the first K bands of the
51  * 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] = clamp(srcs[0][x][y][src0Band] -
60  *                            srcs[1][x][y][src1Band]);
61  * </pre>
62  *
63  * <p><table border=1>
64  * <caption>Resource List</caption>
65  * <tr><th>Name</th>        <th>Value</th></tr>
66  * <tr><td>GlobalName</td>  <td>Subtract</td></tr>
67  * <tr><td>LocalName</td>   <td>Subtract</td></tr>
68  * <tr><td>Vendor</td>      <td>com.lightcrafts.media.jai</td></tr>
69  * <tr><td>Description</td> <td>Subtracts one image from
70  *                              another image.</td></tr>
71  * <tr><td>DocURL</td>      <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/SubtractDescriptor.html</td></tr>
72  * <tr><td>Version</td>     <td>1.0</td></tr>
73  * </table></p>
74  *
75  * <p> No parameters are needed for this operation.
76  *
77  * @see com.lightcrafts.mediax.jai.OperationDescriptor
78  */
79 public class SubtractDescriptor extends OperationDescriptorImpl {
80 
81     /**
82      * The resource strings that provide the general documentation
83      * and specify the parameter list for this operation.
84      */
85     private static final String[][] resources = {
86         {"GlobalName",  "Subtract"},
87         {"LocalName",   "Subtract"},
88         {"Vendor",      "com.lightcrafts.media.jai"},
89         {"Description", JaiI18N.getString("SubtractDescriptor0")},
90         {"DocURL",      "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/SubtractDescriptor.html"},
91         {"Version",     JaiI18N.getString("DescriptorVersion")}
92     };
93 
94     /** Constructor. */
SubtractDescriptor()95     public SubtractDescriptor() {
96         super(resources, 2, null, null, null);
97     }
98 
99     /** Returns <code>true</code> since renderable operation is supported. */
isRenderableSupported()100     public boolean isRenderableSupported() {
101         return true;
102     }
103 
104 
105     /**
106      * Subtracts one image from another image.
107      *
108      * <p>Creates a <code>ParameterBlockJAI</code> from all
109      * supplied arguments except <code>hints</code> and invokes
110      * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
111      *
112      * @see JAI
113      * @see ParameterBlockJAI
114      * @see RenderedOp
115      *
116      * @param source0 <code>RenderedImage</code> source 0.
117      * @param source1 <code>RenderedImage</code> source 1.
118      * @param hints The <code>RenderingHints</code> to use.
119      * May be <code>null</code>.
120      * @return The <code>RenderedOp</code> destination.
121      * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
122      * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
123      */
create(RenderedImage source0, RenderedImage source1, RenderingHints hints)124     public static RenderedOp create(RenderedImage source0,
125                                     RenderedImage source1,
126                                     RenderingHints hints)  {
127         ParameterBlockJAI pb =
128             new ParameterBlockJAI("Subtract",
129                                   RenderedRegistryMode.MODE_NAME);
130 
131         pb.setSource("source0", source0);
132         pb.setSource("source1", source1);
133 
134         return JAI.create("Subtract", pb, hints);
135     }
136 
137     /**
138      * Subtracts one image from another image.
139      *
140      * <p>Creates a <code>ParameterBlockJAI</code> from all
141      * supplied arguments except <code>hints</code> and invokes
142      * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
143      *
144      * @see JAI
145      * @see ParameterBlockJAI
146      * @see RenderableOp
147      *
148      * @param source0 <code>RenderableImage</code> source 0.
149      * @param source1 <code>RenderableImage</code> source 1.
150      * @param hints The <code>RenderingHints</code> to use.
151      * May be <code>null</code>.
152      * @return The <code>RenderableOp</code> destination.
153      * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
154      * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
155      */
createRenderable(RenderableImage source0, RenderableImage source1, RenderingHints hints)156     public static RenderableOp createRenderable(RenderableImage source0,
157                                                 RenderableImage source1,
158                                                 RenderingHints hints)  {
159         ParameterBlockJAI pb =
160             new ParameterBlockJAI("Subtract",
161                                   RenderableRegistryMode.MODE_NAME);
162 
163         pb.setSource("source0", source0);
164         pb.setSource("source1", source1);
165 
166         return JAI.createRenderable("Subtract", pb, hints);
167     }
168 }
169