01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.ext.awt.image.renderable;
20:
21: import java.awt.RenderingHints;
22: import java.awt.Shape;
23: import java.awt.geom.AffineTransform;
24: import java.awt.geom.Rectangle2D;
25: import java.awt.image.RenderedImage;
26: import java.awt.image.renderable.RenderContext;
27:
28: import org.apache.batik.ext.awt.ColorSpaceHintKey;
29: import org.apache.batik.ext.awt.RenderingHintsKeyExt;
30: import org.apache.batik.ext.awt.image.rendered.CachableRed;
31: import org.apache.batik.ext.awt.image.rendered.FilterAlphaRed;
32: import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed;
33:
34: /**
35: * FilterAlphaRable implementation.
36: *
37: * This will take any source Filter and convert it to an alpha channel
38: * image according to the SVG SourceAlpha Filter description.
39: * This sets RGB to black and Alpha to the source image's alpha channel.
40: *
41: * @author <a href="mailto:Thomas.DeWeese@Kodak.com">Thomas DeWeese</a>
42: * @version $Id: FilterAlphaRable.java 475477 2006-11-15 22:44:28Z cam $
43: */
44: public class FilterAlphaRable extends AbstractRable {
45:
46: public FilterAlphaRable(Filter src) {
47: super (src, null);
48: }
49:
50: public Filter getSource() {
51: return (Filter) getSources().get(0);
52: }
53:
54: /**
55: * Pass-through: returns the source's bounds
56: */
57: public Rectangle2D getBounds2D() {
58: return getSource().getBounds2D();
59: }
60:
61: public RenderedImage createRendering(RenderContext rc) {
62: // Source gets my usr2dev transform
63: AffineTransform at = rc.getTransform();
64:
65: // Just copy over the rendering hints.
66: RenderingHints rh = rc.getRenderingHints();
67: if (rh == null)
68: rh = new RenderingHints(null);
69:
70: // if we didn't have an aoi specify our bounds as the aoi.
71: Shape aoi = rc.getAreaOfInterest();
72: if (aoi == null)
73: aoi = getBounds2D();
74:
75: // We only want it's alpha channel...
76: rh.put(RenderingHintsKeyExt.KEY_COLORSPACE,
77: ColorSpaceHintKey.VALUE_COLORSPACE_ALPHA);
78:
79: RenderedImage ri;
80: ri = getSource()
81: .createRendering(new RenderContext(at, aoi, rh));
82:
83: if (ri == null) {
84: return null;
85: }
86:
87: CachableRed cr = RenderedImageCachableRed.wrap(ri);
88:
89: Object val = cr
90: .getProperty(ColorSpaceHintKey.PROPERTY_COLORSPACE);
91: if (val == ColorSpaceHintKey.VALUE_COLORSPACE_ALPHA)
92: return cr; // It listened to us...
93:
94: return new FilterAlphaRed(cr);
95: }
96: }
|