KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > filter > MaskRable8Bit


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.gvt.filter;
19
20 import java.awt.geom.Rectangle2D JavaDoc;
21 import java.awt.image.RenderedImage JavaDoc;
22 import java.awt.image.renderable.RenderContext JavaDoc;
23
24 import org.apache.batik.ext.awt.image.GraphicsUtil;
25 import org.apache.batik.ext.awt.image.PadMode;
26 import org.apache.batik.ext.awt.image.renderable.AbstractRable;
27 import org.apache.batik.ext.awt.image.renderable.Filter;
28 import org.apache.batik.ext.awt.image.renderable.FilterAsAlphaRable;
29 import org.apache.batik.ext.awt.image.renderable.PadRable;
30 import org.apache.batik.ext.awt.image.renderable.PadRable8Bit;
31 import org.apache.batik.ext.awt.image.rendered.CachableRed;
32 import org.apache.batik.ext.awt.image.rendered.MultiplyAlphaRed;
33 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed;
34 import org.apache.batik.gvt.GraphicsNode;
35
36 /**
37  * MaskRable implementation
38  *
39  * @author <a HREF="mailto:Thomas.DeWeese@Kodak.com">Thomas DeWeese</a>
40  * @version $Id: MaskRable8Bit.java,v 1.8 2005/03/27 08:58:34 cam Exp $
41  */

42 public class MaskRable8Bit
43     extends AbstractRable
44     implements Mask {
45
46     /**
47      * The node who's outline specifies our mask.
48      */

49     protected GraphicsNode mask;
50
51     /**
52      * Region to which the mask applies
53      */

54     protected Rectangle2D JavaDoc filterRegion;
55
56     public MaskRable8Bit(Filter src, GraphicsNode mask,
57                              Rectangle2D JavaDoc filterRegion) {
58         super(src, null);
59         setMaskNode(mask);
60         setFilterRegion(filterRegion);
61     }
62
63     /**
64      * The source to be masked by the mask node.
65      * @param src The Image to be masked.
66      */

67     public void setSource(Filter src) {
68         init(src, null);
69     }
70
71     /**
72      * This returns the current image being masked by the mask node.
73      * @return The image to mask
74      */

75     public Filter getSource() {
76         return (Filter)getSources().get(0);
77     }
78
79     /**
80      * The region to which this mask applies
81      */

82     public Rectangle2D JavaDoc getFilterRegion(){
83         return (Rectangle2D JavaDoc)filterRegion.clone();
84     }
85
86     /**
87      * Returns the filter region to which this mask applies
88      */

89     public void setFilterRegion(Rectangle2D JavaDoc filterRegion){
90         if(filterRegion == null){
91             throw new IllegalArgumentException JavaDoc();
92         }
93
94         this.filterRegion = filterRegion;
95     }
96
97     /**
98      * Set the masking image to that described by gn.
99      * If gn is an rgba image then the alpha is premultiplied and then
100      * the rgb is converted to alpha via the standard feColorMatrix
101      * rgb to luminance conversion.
102      * In the case of an rgb only image, just the rgb to luminance
103      * conversion is performed.
104      * @param mask The graphics node that defines the mask image.
105      */

106     public void setMaskNode(GraphicsNode mask) {
107         touch();
108         this.mask = mask;
109     }
110
111       /**
112        * Returns the Graphics node that the mask operation will use to
113        * define the masking image.
114        * @return The graphics node that defines the mask image.
115        */

116     public GraphicsNode getMaskNode() {
117         return mask;
118     }
119
120     /**
121      * Pass-through: returns the source's bounds
122      */

123     public Rectangle2D JavaDoc getBounds2D(){
124         return (Rectangle2D JavaDoc)filterRegion.clone();
125     }
126
127     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
128         //
129
// Get the mask content
130
//
131
Filter maskSrc = getMaskNode().getGraphicsNodeRable(true);
132         PadRable maskPad = new PadRable8Bit(maskSrc, getBounds2D(),
133                                                 PadMode.ZERO_PAD);
134         maskSrc = new FilterAsAlphaRable(maskPad);
135         RenderedImage JavaDoc ri = maskSrc.createRendering(rc);
136         if (ri == null)
137             return null;
138
139         CachableRed maskCr = RenderedImageCachableRed.wrap(ri);
140
141         //
142
// Get the masked content
143
//
144
PadRable maskedPad = new PadRable8Bit(getSource(),
145                                                   getBounds2D(),
146                                                   PadMode.ZERO_PAD);
147
148         ri = maskedPad.createRendering(rc);
149         if (ri == null)
150             return null;
151
152         CachableRed cr;
153         cr = GraphicsUtil.wrap(ri);
154         cr = GraphicsUtil.convertToLsRGB(cr);
155
156         // org.apache.batik.test.gvt.ImageDisplay.showImage("Src: ", cr);
157
// org.apache.batik.test.gvt.ImageDisplay.showImage("Mask: ", maskCr);
158

159         CachableRed ret = new MultiplyAlphaRed(cr, maskCr);
160
161         // org.apache.batik.test.gvt.ImageDisplay.showImage("Masked: ", ret);
162

163
164         // ret = new PadRed(ret, cr.getBounds(), PadMode.ZERO_PAD, rh);
165

166         return ret;
167     }
168 }
169
Popular Tags