001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019:
020: package org.apache.batik.ext.awt.image.renderable;
021:
022: import java.awt.Rectangle;
023: import java.awt.Shape;
024: import java.awt.color.ColorSpace;
025: import java.awt.geom.AffineTransform;
026: import java.awt.geom.NoninvertibleTransformException;
027: import java.awt.geom.Rectangle2D;
028: import java.awt.image.RenderedImage;
029: import java.awt.image.renderable.RenderContext;
030:
031: import org.apache.batik.ext.awt.image.rendered.TurbulencePatternRed;
032:
033: /**
034: * Creates a sourceless image from a turbulence function.
035: *
036: * @author <a href="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
037: * @version $Id: TurbulenceRable8Bit.java 478276 2006-11-22 18:33:37Z dvholten $
038: */
039: public class TurbulenceRable8Bit extends
040: AbstractColorInterpolationRable implements TurbulenceRable {
041:
042: int seed = 0; // Seed value to pseudo rand num gen.
043: int numOctaves = 1; // number of octaves in turbulence function
044: double baseFreqX = 0; // Frequency in X/Y directions
045: double baseFreqY = 0;
046: boolean stitched = false; // True if tiles are stitched
047: boolean fractalNoise = false; // True if fractal noise should be used.
048:
049: Rectangle2D region;
050:
051: public TurbulenceRable8Bit(Rectangle2D region) {
052: super ();
053: this .region = region;
054: }
055:
056: public TurbulenceRable8Bit(Rectangle2D region, int seed,
057: int numOctaves, double baseFreqX, double baseFreqY,
058: boolean stitched, boolean fractalNoise) {
059: super ();
060: this .seed = seed;
061: this .numOctaves = numOctaves;
062: this .baseFreqX = baseFreqX;
063: this .baseFreqY = baseFreqY;
064: this .stitched = stitched;
065: this .fractalNoise = fractalNoise;
066: this .region = region;
067: }
068:
069: /**
070: * Get the turbulence region
071: */
072: public Rectangle2D getTurbulenceRegion() {
073: return (Rectangle2D) region.clone();
074: }
075:
076: /**
077: * Get the turbulence region
078: */
079: public Rectangle2D getBounds2D() {
080: return (Rectangle2D) region.clone();
081: }
082:
083: /**
084: * Get the current seed value for the pseudo random number generator.
085: * @return The current seed value for the pseudo random number generator.
086: */
087: public int getSeed() {
088: return seed;
089: }
090:
091: /**
092: * Get the current number of octaves for the noise function .
093: * @return The current number of octaves for the noise function .
094: */
095: public int getNumOctaves() {
096: return numOctaves;
097: }
098:
099: /**
100: * Get the current base fequency in x direction.
101: * @return The current base fequency in x direction.
102: */
103: public double getBaseFrequencyX() {
104: return baseFreqX;
105: }
106:
107: /**
108: * Get the current base fequency in y direction.
109: * @return The current base fequency in y direction.
110: */
111: public double getBaseFrequencyY() {
112: return baseFreqY;
113: }
114:
115: /**
116: * Returns true if the turbulence function is currently stitching tiles.
117: * @return true if the turbulence function is currently stitching tiles.
118: */
119: public boolean isStitched() {
120: return stitched;
121: }
122:
123: /**
124: * Returns true if the turbulence function is using fractal noise,
125: * instead of turbulence noise.
126: * @return true if the turbulence function is using fractal noise,
127: * instead of turbulence noise.
128: */
129: public boolean isFractalNoise() {
130: return fractalNoise;
131: }
132:
133: /**
134: * Sets the turbulence region
135: * @param turbulenceRegion region to fill with turbulence function.
136: */
137: public void setTurbulenceRegion(Rectangle2D turbulenceRegion) {
138: touch();
139: this .region = turbulenceRegion;
140: }
141:
142: /**
143: * Set the seed value for the pseudo random number generator.
144: * @param seed The new seed value for the pseudo random number generator.
145: */
146: public void setSeed(int seed) {
147: touch();
148: this .seed = seed;
149: }
150:
151: /**
152: * Set the number of octaves for the noise function .
153: * @param numOctaves The new number of octaves for the noise function .
154: */
155: public void setNumOctaves(int numOctaves) {
156: touch();
157: this .numOctaves = numOctaves;
158: }
159:
160: /**
161: * Set the base fequency in x direction.
162: * @param baseFreqX The new base fequency in x direction.
163: */
164: public void setBaseFrequencyX(double baseFreqX) {
165: touch();
166: this .baseFreqX = baseFreqX;
167: }
168:
169: /**
170: * Set the base fequency in y direction.
171: * @param baseFreqY The new base fequency in y direction.
172: */
173: public void setBaseFrequencyY(double baseFreqY) {
174: touch();
175: this .baseFreqY = baseFreqY;
176: }
177:
178: /**
179: * Set stitching state for tiles.
180: * @param stitched true if the turbulence operator should stitch tiles.
181: */
182: public void setStitched(boolean stitched) {
183: touch();
184: this .stitched = stitched;
185: }
186:
187: /**
188: * Turns on/off fractal noise.
189: * @param fractalNoise true if fractal noise should be used.
190: */
191: public void setFractalNoise(boolean fractalNoise) {
192: touch();
193: this .fractalNoise = fractalNoise;
194: }
195:
196: public RenderedImage createRendering(RenderContext rc) {
197:
198: Rectangle2D aoiRect;
199: Shape aoi = rc.getAreaOfInterest();
200: if (aoi == null) {
201: aoiRect = getBounds2D();
202: } else {
203: Rectangle2D rect = getBounds2D();
204: aoiRect = aoi.getBounds2D();
205: if (!aoiRect.intersects(rect))
206: return null;
207: Rectangle2D.intersect(aoiRect, rect, aoiRect);
208: }
209:
210: AffineTransform usr2dev = rc.getTransform();
211:
212: // Compute size of raster image in device space.
213: // System.out.println("Turbulence aoi : " + aoi);
214: // System.out.println("Scale X : " + usr2dev.getScaleX() + " scaleY : " + usr2dev.getScaleY());
215: // System.out.println("Turbulence aoi dev : " + usr2dev.createTransformedShape(aoi).getBounds());
216: final Rectangle devRect = usr2dev.createTransformedShape(
217: aoiRect).getBounds();
218:
219: if ((devRect.width <= 0) || (devRect.height <= 0))
220: return null;
221:
222: ColorSpace cs = getOperationColorSpace();
223:
224: Rectangle2D tile = null;
225: if (stitched)
226: tile = (Rectangle2D) region.clone();
227:
228: AffineTransform patternTxf = new AffineTransform();
229: try {
230: patternTxf = usr2dev.createInverse();
231: } catch (NoninvertibleTransformException e) {
232: }
233:
234: return new TurbulencePatternRed(baseFreqX, baseFreqY,
235: numOctaves, seed, fractalNoise, tile, patternTxf,
236: devRect, cs, true);
237: }
238: }
|