Class TurbulencePatternRed
java.lang.Object
org.apache.batik.ext.awt.image.rendered.AbstractRed
org.apache.batik.ext.awt.image.rendered.TurbulencePatternRed
- All Implemented Interfaces:
RenderedImage
,CachableRed
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 ClassesModifier and TypeClassDescription(package private) static final class
Inner class to store tile stitching info. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate double
x-axis base frequency for the noise function along the x-axisprivate double
y-axis base frequency for the noise function along the y-axisprivate static final int
private static final int
private int[]
List of channels that the generator produces.private final double[]
private static final AffineTransform
Identity transform, default used when null input in the constructor.private boolean
Defines whether the filter performs a fractal noise or a turbulence functionprivate final int[]
private int
Number of octaves in the noise functionprivate static final double
private static final int
private static final int
Produces results in the range [1, 2**31 - 2].private static final int
private static final int
private int
Starting number for the pseudo random number generatorprivate TurbulencePatternRed.StitchInfo
Used when stitching is onprivate Rectangle2D
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[]
private AffineTransform
Defines the tile for the turbulence function(package private) double[]
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
ConstructorsConstructorDescriptionTurbulencePatternRed
(double baseFrequencyX, double baseFrequencyY, int numOctaves, int seed, boolean isFractalNoise, Rectangle2D tile, AffineTransform txf, Rectangle devRect, ColorSpace cs, boolean alpha) -
Method Summary
Modifier and TypeMethodDescriptioncopyData
(WritableRaster dest) Generates a Perlin noise pattern into dest Raster.double
double
boolean[]
int
int
getSeed()
getTile()
private void
initLattice
(int seed) boolean
private static final double
lerp
(double t, double a, double b) private final void
noise2
(double[] noise, double vec0, double vec1) Generate a pixel of noise corresponding to the point vec0,vec1.private final void
noise2Stitch
(double[] noise, double vec0, double vec1, TurbulencePatternRed.StitchInfo stitchInfo) This version of the noise function implements stitching.final int
random
(int seed) private static final double
s_curve
(double t) final int
setupSeed
(int seed) private final void
turbulence
(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise) This is the heart of the turbulence calculation.private final int
turbulence_4
(double pointX, double pointY, double[] fSum) This is the heart of the turbulence calculation.private final void
turbulenceFractal
(int[] rgb, double pointX, double pointY, double[] fSum, double[] noise) This is the heart of the turbulence calculation.private final int
turbulenceFractal_4
(double pointX, double pointY, double[] fSum) This is the heart of the turbulence calculation.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.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.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 Details
-
stitchInfo
Used when stitching is on -
IDENTITY
Identity transform, default used when null input in the constructor. -
baseFrequencyX
private double baseFrequencyXx-axis base frequency for the noise function along the x-axis -
baseFrequencyY
private double baseFrequencyYy-axis base frequency for the noise function along the y-axis -
numOctaves
private int numOctavesNumber of octaves in the noise function -
seed
private int seedStarting number for the pseudo random number generator -
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
Defines the tile for the turbulence function -
isFractalNoise
private boolean isFractalNoiseDefines whether the filter performs a fractal noise or a turbulence function -
channels
private int[] channelsList of channels that the generator produces. -
tx
double[] tx -
ty
double[] ty -
RAND_m
private static final int RAND_mProduces 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:
-
RAND_a
private static final int RAND_a- See Also:
-
RAND_q
private static final int RAND_q- See Also:
-
RAND_r
private static final int RAND_r- See Also:
-
BSize
private static final int BSize- See Also:
-
BM
private static final int BM- See Also:
-
PerlinN
private static final double PerlinN- See Also:
-
latticeSelector
private final int[] latticeSelector -
gradient
private final double[] gradient
-
-
Constructor Details
-
TurbulencePatternRed
public TurbulencePatternRed(double baseFrequencyX, double baseFrequencyY, int numOctaves, int seed, boolean isFractalNoise, Rectangle2D tile, AffineTransform txf, Rectangle devRect, 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 Details
-
getBaseFrequencyX
public double getBaseFrequencyX() -
getBaseFrequencyY
public double getBaseFrequencyY() -
getNumOctaves
public int getNumOctaves() -
getSeed
public int getSeed() -
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
Generates a Perlin noise pattern into dest Raster.- Parameters:
dest
- Raster to fill with the pattern.
-