Class TurbulencePatternRed
- java.lang.Object
-
- org.apache.batik.ext.awt.image.rendered.AbstractRed
-
- org.apache.batik.ext.awt.image.rendered.TurbulencePatternRed
-
- All Implemented Interfaces:
java.awt.image.RenderedImage
,CachableRed
public final class TurbulencePatternRed extends AbstractRed
This class creates a RenderedImage in conformance to the one defined for the feTurbulence filter of the SVG specification. What follows is my high-level description of how the noise is generated. This is not contained in the SVG spec, just the algorithm for doing it. This is provided in the hope that someone will figure out a clever way to accelerate parts of the function. gradient contains a long list of random unit vectors. For each point we are to generate noise for we do two things. first we use the latticeSelector to 'co-mingle' the integer portions of x and y (this allows us to have a one-dimensional array of gradients that appears 2 dimensional, by using the co-mingled index). We do this for [x,y], [x+1,y], [x,y+1], and [x+1, y+1], this gives us the four gradient vectors that surround the point (b00, b10, ...) Next we construct the four vectors from the grid points (where the gradient vectors are defined) [these are rx0, rx1, ry0, ry1]. We then take the dot product between the gradient vectors and the grid point vectors (this gives the portion of the grid point vector that projects along the gradient vector for each grid point). These four dot projects are then combined with linear interpolation. The weight factor for the linear combination is the result of applying the 's' curve function to the fractional part of x and y (rx0, ry0). The S curve function get's it's name because it looks a bit like as 'S' from 0->1.- Version:
- $Id: TurbulencePatternRed.java 1808001 2017-09-11 09:51:29Z ssteiner $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
TurbulencePatternRed.StitchInfo
Inner class to store tile stitching info.
-
Field Summary
Fields Modifier and Type Field Description private double
baseFrequencyX
x-axis base frequency for the noise function along the x-axisprivate double
baseFrequencyY
y-axis base frequency for the noise function along the y-axisprivate static int
BM
private static int
BSize
private int[]
channels
List of channels that the generator produces.private double[]
gradient
private static java.awt.geom.AffineTransform
IDENTITY
Identity transform, default used when null input in the constructor.private boolean
isFractalNoise
Defines whether the filter performs a fractal noise or a turbulence functionprivate int[]
latticeSelector
private int
numOctaves
Number of octaves in the noise functionprivate static double
PerlinN
private static int
RAND_a
private static int
RAND_m
Produces results in the range [1, 2**31 - 2].private static int
RAND_q
private static int
RAND_r
private int
seed
Starting number for the pseudo random number generatorprivate TurbulencePatternRed.StitchInfo
stitchInfo
Used when stitching is onprivate java.awt.geom.Rectangle2D
tile
Defines the tile for the turbulence function, if non-null turns on stitching, so frequencies are adjusted to avoid discontinuities in case frequencies do not match tile boundaries.(package private) double[]
tx
private java.awt.geom.AffineTransform
txf
Defines the tile for the turbulence function(package private) double[]
ty
-
Fields inherited from class org.apache.batik.ext.awt.image.rendered.AbstractRed
bounds, cm, minTileX, minTileY, numXTiles, numYTiles, props, sm, srcs, tileGridXOff, tileGridYOff, tileHeight, tileWidth
-
-
Constructor Summary
Constructors Constructor Description TurbulencePatternRed(double baseFrequencyX, double baseFrequencyY, int numOctaves, int seed, boolean isFractalNoise, java.awt.geom.Rectangle2D tile, java.awt.geom.AffineTransform txf, java.awt.Rectangle devRect, java.awt.color.ColorSpace cs, boolean alpha)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.image.WritableRaster
copyData(java.awt.image.WritableRaster dest)
Generates a Perlin noise pattern into dest Raster.double
getBaseFrequencyX()
double
getBaseFrequencyY()
boolean[]
getChannels()
int
getNumOctaves()
int
getSeed()
java.awt.geom.Rectangle2D
getTile()
private void
initLattice(int seed)
boolean
isFractalNoise()
private static double
lerp(double t, double a, double b)
private void
noise2(double[] noise, double vec0, double vec1)
Generate a pixel of noise corresponding to the point vec0,vec1.private void
noise2Stitch(double[] noise, double vec0, double vec1, TurbulencePatternRed.StitchInfo stitchInfo)
This version of the noise function implements stitching.int
random(int seed)
private static double
s_curve(double t)
int
setupSeed(int seed)
private void
turbulence(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise)
This is the heart of the turbulence calculation.private int
turbulence_4(double pointX, double pointY, double[] fSum)
This is the heart of the turbulence calculation.private void
turbulenceFractal(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise)
This is the heart of the turbulence calculation.private int
turbulenceFractal_4(double pointX, double pointY, double[] fSum)
This is the heart of the turbulence calculation.private void
turbulenceFractalStitch(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise, TurbulencePatternRed.StitchInfo stitchInfo)
This is the heart of the turbulence calculation.private void
turbulenceStitch(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise, TurbulencePatternRed.StitchInfo stitchInfo)
This is the heart of the turbulence calculation.-
Methods inherited from class org.apache.batik.ext.awt.image.rendered.AbstractRed
copyBand, copyToRaster, getBounds, getColorModel, getData, getData, getDependencyRegion, getDirtyRegion, getHeight, getMinTileX, getMinTileY, getMinX, getMinY, getNumXTiles, getNumYTiles, getProperty, getPropertyNames, getSampleModel, getSources, getTile, getTileGridXOffset, getTileGridYOffset, getTileHeight, getTileWidth, getWidth, getXTile, getYTile, init, init, makeTile, updateTileGridInfo
-
-
-
-
Field Detail
-
stitchInfo
private TurbulencePatternRed.StitchInfo stitchInfo
Used when stitching is on
-
IDENTITY
private static final java.awt.geom.AffineTransform IDENTITY
Identity transform, default used when null input in the constructor.
-
baseFrequencyX
private double baseFrequencyX
x-axis base frequency for the noise function along the x-axis
-
baseFrequencyY
private double baseFrequencyY
y-axis base frequency for the noise function along the y-axis
-
numOctaves
private int numOctaves
Number of octaves in the noise function
-
seed
private int seed
Starting number for the pseudo random number generator
-
tile
private java.awt.geom.Rectangle2D tile
Defines the tile for the turbulence function, if non-null turns on stitching, so frequencies are adjusted to avoid discontinuities in case frequencies do not match tile boundaries.
-
txf
private java.awt.geom.AffineTransform txf
Defines the tile for the turbulence function
-
isFractalNoise
private boolean isFractalNoise
Defines whether the filter performs a fractal noise or a turbulence function
-
channels
private int[] channels
List of channels that the generator produces.
-
tx
double[] tx
-
ty
double[] ty
-
RAND_m
private static final int RAND_m
Produces results in the range [1, 2**31 - 2]. Algorithm is: r = (a * r) mod m where a = 16807 and m = 2**31 - 1 = 2147483647 See [Park & Miller], CACM vol. 31 no. 10 p. 1195, Oct. 1988 To test: the algorithm should produce the result 1043618065 as the 10,000th generated number if the original seed is 1.- See Also:
- Constant Field Values
-
RAND_a
private static final int RAND_a
- See Also:
- Constant Field Values
-
RAND_q
private static final int RAND_q
- See Also:
- Constant Field Values
-
RAND_r
private static final int RAND_r
- See Also:
- Constant Field Values
-
BSize
private static final int BSize
- See Also:
- Constant Field Values
-
BM
private static final int BM
- See Also:
- Constant Field Values
-
PerlinN
private static final double PerlinN
- See Also:
- Constant Field Values
-
latticeSelector
private final int[] latticeSelector
-
gradient
private final double[] gradient
-
-
Constructor Detail
-
TurbulencePatternRed
public TurbulencePatternRed(double baseFrequencyX, double baseFrequencyY, int numOctaves, int seed, boolean isFractalNoise, java.awt.geom.Rectangle2D tile, java.awt.geom.AffineTransform txf, java.awt.Rectangle devRect, java.awt.color.ColorSpace cs, boolean alpha)
- Parameters:
baseFrequencyX
- x-axis base frequency for the noise function along the x-axisbaseFrequencyY
- y-axis base frequency for the noise function along the x-axisnumOctaves
- number of octaves in the noise function. Positive integral value.seed
- starting number for the pseudo random number generatorisFractalNoise
- defines whether the filter performs a fractal noise or a turbulence function.tile
- defines the tile size. May be null if stitchTiles is false. Otherwise, should not be null.txf
- The affine transform from device to user space.cs
- The Colorspace to output.alpha
- True if the data should have an alpha channel.
-
-
Method Detail
-
getBaseFrequencyX
public double getBaseFrequencyX()
-
getBaseFrequencyY
public double getBaseFrequencyY()
-
getNumOctaves
public int getNumOctaves()
-
getSeed
public int getSeed()
-
getTile
public java.awt.geom.Rectangle2D getTile()
-
isFractalNoise
public boolean isFractalNoise()
-
getChannels
public boolean[] getChannels()
-
setupSeed
public final int setupSeed(int seed)
-
random
public final int random(int seed)
-
initLattice
private void initLattice(int seed)
-
s_curve
private static final double s_curve(double t)
-
lerp
private static final double lerp(double t, double a, double b)
-
noise2
private final void noise2(double[] noise, double vec0, double vec1)
Generate a pixel of noise corresponding to the point vec0,vec1. See class description for a high level discussion of method. This handles cases where channels <= 4.- Parameters:
noise
- The place to put the generated noise.vec0
- The X coordiate to generate noise forvec1
- The Y coordiate to generate noise for
-
noise2Stitch
private final void noise2Stitch(double[] noise, double vec0, double vec1, TurbulencePatternRed.StitchInfo stitchInfo)
This version of the noise function implements stitching. If any of the lattice is on the right or bottom edge, the function uses the the latice on the other side of the tile, i.e., the left or right edge.- Parameters:
noise
- The place to put the generated noise.vec0
- The X coordiate to generate noise forvec1
- The Y coordiate to generate noise forstitchInfo
- The stitching information for the noise function.
-
turbulence_4
private final int turbulence_4(double pointX, double pointY, double[] fSum)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec. This is special case for 4 bands of output.- Parameters:
pointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixel- Returns:
- The ARGB pixel value.
-
turbulence
private final void turbulence(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.- Parameters:
rgb
- array for the four color componentspointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixelnoise
- array used to avoid reallocating double array for each pixel
-
turbulenceStitch
private final void turbulenceStitch(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise, TurbulencePatternRed.StitchInfo stitchInfo)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.- Parameters:
rgb
- array for the four color componentspointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixelnoise
- array used to avoid reallocating double array for each pixelstitchInfo
- The stitching information for the noise function
-
turbulenceFractal_4
private final int turbulenceFractal_4(double pointX, double pointY, double[] fSum)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec. This handles the case where we are generating 4 channels of noise.- Parameters:
pointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixel- Returns:
- The ARGB pixel
-
turbulenceFractal
private final void turbulenceFractal(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.- Parameters:
rgb
- array for the four color componentspointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixelnoise
- array used to avoid reallocating double array for each pixel
-
turbulenceFractalStitch
private final void turbulenceFractalStitch(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise, TurbulencePatternRed.StitchInfo stitchInfo)
This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.- Parameters:
rgb
- array for the four color componentspointX
- x coordinate of the point to process.pointY
- y coordinate of the point to process.fSum
- array used to avoid reallocating double array for each pixelnoise
- array used to avoid reallocating double array for each pixelstitchInfo
- The stitching information for the noise function
-
copyData
public java.awt.image.WritableRaster copyData(java.awt.image.WritableRaster dest)
Generates a Perlin noise pattern into dest Raster.- Parameters:
dest
- Raster to fill with the pattern.
-
-