KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SVGRescaleOp


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.svggen;
19
20 import java.awt.Rectangle JavaDoc;
21 import java.awt.image.BufferedImageOp JavaDoc;
22 import java.awt.image.RescaleOp JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * Utility class that converts a RescaleOp object into
29  * an SVG filter descriptor. The SVG filter corresponding
30  * to a RescaleOp is an feComponentTransfer, with a type
31  * set to 'linear', the slopes equal to the RescapeOp
32  * scaleFactors and the intercept equal to the RescapeOp
33  * offsets.
34  *
35  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
36  * @version $Id: SVGRescaleOp.java,v 1.14 2005/03/27 08:58:35 cam Exp $
37  * @see org.apache.batik.svggen.SVGBufferedImageOp
38  */

39 public class SVGRescaleOp extends AbstractSVGFilterConverter {
40
41     /**
42      * @param generatorContext used to build Elements
43      */

44     public SVGRescaleOp(SVGGeneratorContext generatorContext) {
45         super(generatorContext);
46     }
47
48     /**
49      * Converts a Java 2D API BufferedImageOp into
50      * a set of attribute/value pairs and related definitions
51      *
52      * @param filter BufferedImageOp filter to be converted
53      * @param filterRect Rectangle, in device space, that defines the area
54      * to which filtering applies. May be null, meaning that the
55      * area is undefined.
56      * @return descriptor of the attributes required to represent
57      * the input filter
58      * @see org.apache.batik.svggen.SVGFilterDescriptor
59      */

60     public SVGFilterDescriptor toSVG(BufferedImageOp JavaDoc filter,
61                                      Rectangle JavaDoc filterRect) {
62         if(filter instanceof RescaleOp JavaDoc)
63             return toSVG((RescaleOp JavaDoc)filter);
64         else
65             return null;
66     }
67
68     /**
69      * @param rescaleOp the RescaleOp to be converted
70      * @return a description of the SVG filter corresponding to
71      * rescaleOp. The definition of the feComponentTransfer
72      * filter in put in feComponentTransferDefSet
73      */

74     public SVGFilterDescriptor toSVG(RescaleOp JavaDoc rescaleOp) {
75         // Reuse definition if rescaleOp has already been converted
76
SVGFilterDescriptor filterDesc =
77             (SVGFilterDescriptor)descMap.get(rescaleOp);
78
79         Document JavaDoc domFactory = generatorContext.domFactory;
80
81         if (filterDesc == null) {
82             //
83
// First time filter is converted: create its corresponding
84
// SVG filter
85
//
86
Element JavaDoc filterDef = domFactory.createElementNS(SVG_NAMESPACE_URI,
87                                                            SVG_FILTER_TAG);
88             Element JavaDoc feComponentTransferDef =
89                 domFactory.createElementNS(SVG_NAMESPACE_URI,
90                                            SVG_FE_COMPONENT_TRANSFER_TAG);
91
92             // Append transfer function for each component, setting
93
// the attributes corresponding to the scale and offset.
94
// Because we are using a RescaleOp as a BufferedImageOp,
95
// the scaleFactors must be either:
96
// + 1, in which case the same scale is applied to the
97
// Red, Green and Blue components,
98
// + 3, in which case the scale factors apply to the
99
// Red, Green and Blue components
100
// + 4, in which case the scale factors apply to the
101
// Red, Green, Blue and Alpha components
102
float offsets[] = rescaleOp.getOffsets(null);
103             float scaleFactors[] = rescaleOp.getScaleFactors(null);
104             if(offsets.length != scaleFactors.length)
105                 throw new SVGGraphics2DRuntimeException(ERR_SCALE_FACTORS_AND_OFFSETS_MISMATCH);
106
107             if(offsets.length != 1 &&
108                offsets.length != 3 &&
109                offsets.length != 4)
110                 throw new SVGGraphics2DRuntimeException(ERR_ILLEGAL_BUFFERED_IMAGE_RESCALE_OP);
111
112             Element JavaDoc feFuncR = domFactory.createElementNS(SVG_NAMESPACE_URI,
113                                                          SVG_FE_FUNC_R_TAG);
114             Element JavaDoc feFuncG = domFactory.createElementNS(SVG_NAMESPACE_URI,
115                                                          SVG_FE_FUNC_G_TAG);
116             Element JavaDoc feFuncB = domFactory.createElementNS(SVG_NAMESPACE_URI,
117                                                          SVG_FE_FUNC_B_TAG);
118             Element JavaDoc feFuncA = null;
119             String JavaDoc type = SVG_LINEAR_VALUE;
120
121             if(offsets.length == 1){
122                 String JavaDoc slope = doubleString(scaleFactors[0]);
123                 String JavaDoc intercept = doubleString(offsets[0]);
124                 feFuncR.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
125                 feFuncG.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
126                 feFuncB.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
127                 feFuncR.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope);
128                 feFuncG.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope);
129                 feFuncB.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE, slope);
130                 feFuncR.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept);
131                 feFuncG.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept);
132                 feFuncB.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE, intercept);
133             }
134             else if(offsets.length >= 3){
135                 feFuncR.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
136                 feFuncG.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
137                 feFuncB.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
138                 feFuncR.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE,
139                                        doubleString(scaleFactors[0]));
140                 feFuncG.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE,
141                                        doubleString(scaleFactors[1]));
142                 feFuncB.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE,
143                                        doubleString(scaleFactors[2]));
144                 feFuncR.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE,
145                                        doubleString(offsets[0]));
146                 feFuncG.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE,
147                                        doubleString(offsets[1]));
148                 feFuncB.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE,
149                                        doubleString(offsets[2]));
150
151                 if(offsets.length == 4){
152                     feFuncA = domFactory.createElementNS(SVG_NAMESPACE_URI,
153                                                          SVG_FE_FUNC_A_TAG);
154                     feFuncA.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
155                     feFuncA.setAttributeNS(null, SVG_SLOPE_ATTRIBUTE,
156                                          doubleString(scaleFactors[3]));
157                     feFuncA.setAttributeNS(null, SVG_INTERCEPT_ATTRIBUTE,
158                                          doubleString(offsets[3]));
159                 }
160             }
161
162             feComponentTransferDef.appendChild(feFuncR);
163             feComponentTransferDef.appendChild(feFuncG);
164             feComponentTransferDef.appendChild(feFuncB);
165             if(feFuncA != null)
166                 feComponentTransferDef.appendChild(feFuncA);
167
168             filterDef.appendChild(feComponentTransferDef);
169
170             filterDef.
171                 setAttributeNS(null, ATTR_ID,
172                                generatorContext.idGenerator.
173                                generateID(ID_PREFIX_FE_COMPONENT_TRANSFER));
174
175             //
176
// Create a filter descriptor
177
//
178

179             // Process filter attribute
180
StringBuffer JavaDoc filterAttrBuf = new StringBuffer JavaDoc(URL_PREFIX);
181             filterAttrBuf.append(SIGN_POUND);
182             filterAttrBuf.append(filterDef.getAttributeNS(null, ATTR_ID));
183             filterAttrBuf.append(URL_SUFFIX);
184
185             filterDesc = new SVGFilterDescriptor(filterAttrBuf.toString(),
186                                                  filterDef);
187
188             defSet.add(filterDef);
189             descMap.put(rescaleOp, filterDesc);
190         }
191
192         return filterDesc;
193     }
194 }
195
Popular Tags