KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 1999-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
19 package org.apache.batik.ext.awt.image.renderable;
20
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.color.ColorSpace JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.NoninvertibleTransformException JavaDoc;
26 import java.awt.geom.Rectangle2D JavaDoc;
27 import java.awt.image.RenderedImage JavaDoc;
28 import java.awt.image.renderable.RenderContext JavaDoc;
29
30 import org.apache.batik.ext.awt.image.rendered.TurbulencePatternRed;
31
32 /**
33  * Creates a sourceless image from a turbulence function.
34  *
35  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
36  * @version $Id: TurbulenceRable8Bit.java,v 1.12 2005/03/27 08:58:33 cam Exp $
37  */

38 public class TurbulenceRable8Bit
39     extends AbstractColorInterpolationRable
40     implements TurbulenceRable {
41
42     int seed = 0; // Seed value to pseudo rand num gen.
43
int numOctaves = 1; // number of octaves in turbulence function
44
double baseFreqX = 0; // Frequency in X/Y directions
45
double baseFreqY = 0;
46     boolean stitched = false; // True if tiles are stitched
47
boolean fractalNoise = false; // True if fractal noise should be used.
48

49     Rectangle2D JavaDoc region;
50
51     public TurbulenceRable8Bit(Rectangle2D JavaDoc region) {
52         super();
53         this.region = region;
54     }
55
56     public TurbulenceRable8Bit(Rectangle2D JavaDoc region,
57                                    int seed,
58                                    int numOctaves,
59                                    double baseFreqX,
60                                    double baseFreqY,
61                                    boolean stitched,
62                                    boolean fractalNoise) {
63         super();
64         this.seed = seed;
65         this.numOctaves = numOctaves;
66         this.baseFreqX = baseFreqX;
67         this.baseFreqY = baseFreqY;
68         this.stitched = stitched;
69         this.fractalNoise = fractalNoise;
70         this.region = region;
71     }
72
73     /**
74      * Get the turbulence region
75      */

76     public Rectangle2D JavaDoc getTurbulenceRegion() {
77         return (Rectangle2D JavaDoc)region.clone();
78     }
79
80     /**
81      * Get the turbulence region
82      */

83     public Rectangle2D JavaDoc getBounds2D() {
84         return (Rectangle2D JavaDoc)region.clone();
85     }
86
87     /**
88      * Get the current seed value for the pseudo random number generator.
89      * @return The current seed value for the pseudo random number generator.
90      */

91     public int getSeed() {
92         return seed;
93     }
94
95     /**
96      * Get the current number of octaves for the noise function .
97      * @return The current number of octaves for the noise function .
98      */

99     public int getNumOctaves() {
100         return numOctaves;
101     }
102
103     /**
104      * Get the current base fequency in x direction.
105      * @return The current base fequency in x direction.
106      */

107     public double getBaseFrequencyX() {
108         return baseFreqX;
109     }
110
111     /**
112      * Get the current base fequency in y direction.
113      * @return The current base fequency in y direction.
114      */

115     public double getBaseFrequencyY() {
116         return baseFreqY;
117     }
118
119     /**
120      * Returns true if the turbulence function is currently stitching tiles.
121      * @return true if the turbulence function is currently stitching tiles.
122      */

123     public boolean isStitched() {
124         return stitched;
125     }
126
127     /**
128      * Returns true if the turbulence function is using fractal noise,
129      * instead of turbulence noise.
130      * @return true if the turbulence function is using fractal noise,
131      * instead of turbulence noise.
132      */

133     public boolean isFractalNoise() {
134         return fractalNoise;
135     }
136
137     /**
138      * Sets the turbulence region
139      * @param turbulenceRegion region to fill with turbulence function.
140      */

141     public void setTurbulenceRegion(Rectangle2D JavaDoc turbulenceRegion) {
142         touch();
143         this.region = turbulenceRegion;
144     }
145
146     /**
147      * Set the seed value for the pseudo random number generator.
148      * @param seed The new seed value for the pseudo random number generator.
149      */

150     public void setSeed(int seed) {
151         touch();
152         this.seed = seed;
153     }
154
155     /**
156      * Set the number of octaves for the noise function .
157      * @param numOctaves The new number of octaves for the noise function .
158      */

159     public void setNumOctaves(int numOctaves) {
160         touch();
161         this.numOctaves = numOctaves;
162     }
163
164     /**
165      * Set the base fequency in x direction.
166      * @param baseFreqX The new base fequency in x direction.
167      */

168     public void setBaseFrequencyX(double baseFreqX) {
169         touch();
170         this.baseFreqX = baseFreqX;
171     }
172
173     /**
174      * Set the base fequency in y direction.
175      * @param baseFreqY The new base fequency in y direction.
176      */

177     public void setBaseFrequencyY(double baseFreqY) {
178         touch();
179         this.baseFreqY = baseFreqY;
180     }
181
182     /**
183      * Set stitching state for tiles.
184      * @param stitched true if the turbulence operator should stitch tiles.
185      */

186     public void setStitched(boolean stitched) {
187         touch();
188         this.stitched = stitched;
189     }
190
191     /**
192      * Turns on/off fractal noise.
193      * @param fractalNoise true if fractal noise should be used.
194      */

195     public void setFractalNoise(boolean fractalNoise) {
196         touch();
197         this.fractalNoise = fractalNoise;
198     }
199
200     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc){
201
202         Rectangle2D JavaDoc aoiRect;
203         Shape JavaDoc aoi = rc.getAreaOfInterest();
204         if(aoi == null){
205             aoiRect = getBounds2D();
206         } else {
207             Rectangle2D JavaDoc rect = getBounds2D();
208             aoiRect = aoi.getBounds2D();
209             if (aoiRect.intersects(rect) == false)
210                 return null;
211             Rectangle2D.intersect(aoiRect, rect, aoiRect);
212         }
213
214         AffineTransform JavaDoc usr2dev = rc.getTransform();
215
216         // Compute size of raster image in device space.
217
// System.out.println("Turbulence aoi : " + aoi);
218
// System.out.println("Scale X : " + usr2dev.getScaleX() + " scaleY : " + usr2dev.getScaleY());
219
// System.out.println("Turbulence aoi dev : " + usr2dev.createTransformedShape(aoi).getBounds());
220
final Rectangle JavaDoc devRect
221             = usr2dev.createTransformedShape(aoiRect).getBounds();
222
223         if ((devRect.width <= 0) ||
224             (devRect.height <= 0))
225             return null;
226
227         ColorSpace JavaDoc cs = getOperationColorSpace();
228         
229         Rectangle2D JavaDoc tile = null;
230         if (stitched)
231             tile = (Rectangle2D JavaDoc)region.clone();
232
233         AffineTransform JavaDoc patternTxf = new AffineTransform JavaDoc();
234         try{
235             patternTxf = usr2dev.createInverse();
236         }catch(NoninvertibleTransformException JavaDoc e){
237         }
238
239         return new TurbulencePatternRed
240             (baseFreqX, baseFreqY, numOctaves, seed, fractalNoise,
241              tile, patternTxf, devRect, cs, true);
242     }
243 }
244
Popular Tags