KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > renderable > CompositeRable8Bit


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.ext.awt.image.renderable;
19
20 import java.awt.Composite JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.Shape JavaDoc;
25 import java.awt.color.ColorSpace JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27 import java.awt.geom.Rectangle2D JavaDoc;
28 import java.awt.image.RenderedImage JavaDoc;
29 import java.awt.image.renderable.RenderContext JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.apache.batik.ext.awt.image.CompositeRule;
35 import org.apache.batik.ext.awt.image.GraphicsUtil;
36 import org.apache.batik.ext.awt.image.SVGComposite;
37 import org.apache.batik.ext.awt.image.rendered.CachableRed;
38 import org.apache.batik.ext.awt.image.rendered.CompositeRed;
39 import org.apache.batik.ext.awt.image.rendered.FloodRed;
40
41 /**
42  * Composites a list of images according to a single composite rule.
43  * the image are applied in the order they are in the List given.
44  *
45  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
46  * @version $Id: CompositeRable8Bit.java,v 1.15 2004/08/18 07:13:59 vhardy Exp $
47  */

48 public class CompositeRable8Bit
49     extends AbstractColorInterpolationRable
50     implements CompositeRable, PaintRable {
51
52     protected CompositeRule rule;
53
54     public CompositeRable8Bit(List JavaDoc srcs,
55                               CompositeRule rule,
56                               boolean csIsLinear) {
57         super(srcs);
58
59         setColorSpaceLinear(csIsLinear);
60
61         this.rule = rule;
62     }
63
64       /**
65        * The sources to be composited togeather.
66        * @param srcs The list of images to be composited by the composite rule.
67        */

68     public void setSources(List JavaDoc srcs) {
69         init(srcs, null);
70     }
71
72       /**
73        * Set the composite rule to use for combining the sources.
74        * @param cr Composite rule to use.
75        */

76     public void setCompositeRule(CompositeRule cr) {
77         touch();
78         this.rule = cr;
79     }
80
81       /**
82        * Get the composite rule in use for combining the sources.
83        * @return Composite rule currently in use.
84        */

85     public CompositeRule getCompositeRule() {
86         return this.rule;
87     }
88
89     /**
90      * Should perform the equivilent action as
91      * createRendering followed by drawing the RenderedImage to
92      * Graphics2D, or return false.
93      *
94      * @param g2d The Graphics2D to draw to.
95      * @return true if the paint call succeeded, false if
96      * for some reason the paint failed (in which
97      * case a createRendering should be used).
98      */

99     public boolean paintRable(Graphics2D JavaDoc g2d) {
100         // This optimization only apply if we are using
101
// SrcOver. Otherwise things break...
102
Composite JavaDoc c = g2d.getComposite();
103         if (!SVGComposite.OVER.equals(c))
104             return false;
105
106         // For the over mode we can just draw them in order...
107
if (getCompositeRule() != CompositeRule.OVER)
108             return false;
109
110         ColorSpace JavaDoc crCS = getOperationColorSpace();
111         ColorSpace JavaDoc g2dCS = GraphicsUtil.getDestinationColorSpace(g2d);
112         if ((g2dCS == null) || (g2dCS != crCS)) {
113             return false;
114         }
115
116         // System.out.println("drawImage : " + g2dCS +
117
// crCS);
118
Iterator JavaDoc i = getSources().iterator();
119         while (i.hasNext()) {
120             GraphicsUtil.drawImage(g2d, (Filter)i.next());
121         }
122         return true;
123     }
124
125     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
126         if (srcs.size() == 0)
127             return null;
128
129         // Just copy over the rendering hints.
130
RenderingHints JavaDoc rh = rc.getRenderingHints();
131         if (rh == null) rh = new RenderingHints JavaDoc(null);
132
133         // update the current affine transform
134
AffineTransform JavaDoc at = rc.getTransform();
135
136         Shape JavaDoc aoi = rc.getAreaOfInterest();
137         Rectangle2D JavaDoc aoiR;
138         if (aoi == null)
139             aoiR = getBounds2D();
140         else {
141             aoiR = aoi.getBounds2D();
142             Rectangle2D JavaDoc bounds2d = getBounds2D();
143             if (bounds2d.intersects(aoiR) == false)
144                 return null;
145
146             Rectangle2D.intersect(aoiR, bounds2d, aoiR);
147         }
148
149         Rectangle JavaDoc devRect = at.createTransformedShape(aoiR).getBounds();
150
151         rc = new RenderContext JavaDoc(at, aoiR, rh);
152
153         Vector JavaDoc srcs = new Vector JavaDoc();
154
155         Iterator JavaDoc i = getSources().iterator();
156         while (i.hasNext()) {
157             // Get the source to work with...
158
Filter filt = (Filter)i.next();
159
160             // Get our sources image...
161
RenderedImage JavaDoc ri = filt.createRendering(rc);
162             if (ri != null) {
163                 CachableRed cr;
164                 cr = convertSourceCS(ri);
165                 srcs.add(cr);
166             } else {
167
168                 // Blank image...
169
switch (rule.getRule()) {
170                 case CompositeRule.RULE_IN:
171                     // For Mode IN One blank image kills all output
172
// (including any "future" images to be drawn).
173
return null;
174
175                 case CompositeRule.RULE_OUT:
176                     // For mode OUT blank image clears output
177
// up to this point, so ignore inputs to this point.
178
srcs.clear();
179                     break;
180
181                 case CompositeRule.RULE_ARITHMETIC:
182                     srcs.add(new FloodRed(devRect));
183                     break;
184
185                 default:
186                     // All other cases we simple pretend the image didn't
187
// exist (fully transparent image has no affect).
188
break;
189                 }
190             }
191         }
192
193         if (srcs.size() == 0)
194             return null;
195
196         // System.out.println("Done General: " + rule);
197
CachableRed cr = new CompositeRed(srcs, rule);
198         return cr;
199     }
200 }
201
Popular Tags