Class SVGMultipleGradientPaintContext
- java.lang.Object
-
- com.github.weisj.jsvg.paint.impl.jdk.SVGMultipleGradientPaintContext
-
- All Implemented Interfaces:
java.awt.PaintContext
- Direct Known Subclasses:
SVGRadialGradientPaintContext
abstract class SVGMultipleGradientPaintContext extends java.lang.Object implements java.awt.PaintContextThis is the superclass for all PaintContexts which use a multiple color gradient to fill in their raster. It provides the actual color interpolation functionality. Subclasses only have to deal with using the gradient to fill pixels in a raster.
-
-
Field Summary
Fields Modifier and Type Field Description protected floata00Elements of the inverse transform matrix.protected floata01protected floata02protected floata10protected floata11protected floata12protected static java.lang.ref.WeakReference<java.awt.image.Raster>cachedThe cached raster, which is reusable among instances.protected static java.awt.image.ColorModelcachedModelThe cached ColorModel.protected java.awt.MultipleGradientPaint.ColorSpaceTypecolorSpaceThe ColorSpace in which to perform the interpolationprotected java.awt.MultipleGradientPaint.CycleMethodcycleMethodThe method to use when painting out of the gradient bounds.protected intfastGradientArraySizeSize of gradients array for scaling the 0-1 index when looking up colors the fast way.private float[]fractionsFractions array.protected int[]gradientArray which contains the interpolated color values for each interval, used by calculateSingleArrayGradient().protected static intGRADIENT_SIZEConstant number of max colors between any 2 arbitrary colors.protected static intGRADIENT_SIZE_INDEXprivate int[][]gradientsArray of gradient arrays, one array for each interval.protected booleanisSimpleLookupThis boolean specifies whether we are in simple lookup mode, where an input value between 0 and 1 may be used to directly index into a single array of gradient colors.private static intMAX_GRADIENT_ARRAY_SIZEMaximum length of the fast single-array.private static floatMIN_INTERVAL_LENGTHprotected java.awt.image.ColorModelmodelThe PaintContext's ColorModel.private float[]normalizedIntervalsNormalized intervals array.protected java.awt.image.RastersavedRaster is reused whenever possible.private inttransparencyTestUsed to determine if gradient colors are all opaque.private static java.awt.image.ColorModelXRGB_MODELColor model used if gradient colors are all opaque.
-
Constructor Summary
Constructors Modifier Constructor Description protectedSVGMultipleGradientPaintContext(@NotNull SVGMultipleGradientPaint mgp, @NotNull java.awt.geom.AffineTransform t, float @NotNull [] fractions, @NotNull java.awt.Color @NotNull [] colors, java.awt.MultipleGradientPaint.CycleMethod cycleMethod, java.awt.MultipleGradientPaint.ColorSpaceType colorSpace)Constructor for MultipleGradientPaintContext superclass.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private voidcalculateLookupData(java.awt.Color[] colors)This function is the meat of this class.private voidcalculateMultipleArrayGradient(java.awt.Color[] colors)SLOW LOOKUP METHODprivate voidcalculateSingleArrayGradient(java.awt.Color[] colors, float minInterval)FAST LOOKUP METHODvoiddispose()protected abstract voidfillRaster(int[] pixels, int off, int adjust, int x, int y, int w, int h)private static java.awt.image.RastergetCachedRaster(java.awt.image.ColorModel cm, int w, int h)Took this cacheRaster code from GradientPaint.java.awt.image.ColorModelgetColorModel()java.awt.image.RastergetRaster(int x, int y, int w, int h)protected intindexIntoGradientsArrays(float position)Helper function to index into the gradients array.private voidinterpolate(int rgb1, int rgb2, int[] output)Yet another helper function.private static floatmod1(float x)private static floatmod1Reflect(float x)private static voidputCachedRaster(java.awt.image.ColorModel cm, java.awt.image.Raster ras)Took this cacheRaster code from GradientPaint.
-
-
-
Field Detail
-
MIN_INTERVAL_LENGTH
private static final float MIN_INTERVAL_LENGTH
- See Also:
- Constant Field Values
-
model
protected java.awt.image.ColorModel model
The PaintContext's ColorModel. This is ARGB if colors are not all opaque, otherwise it is RGB.
-
XRGB_MODEL
private static final java.awt.image.ColorModel XRGB_MODEL
Color model used if gradient colors are all opaque.
-
cachedModel
protected static java.awt.image.ColorModel cachedModel
The cached ColorModel.
-
cached
protected static java.lang.ref.WeakReference<java.awt.image.Raster> cached
The cached raster, which is reusable among instances.
-
saved
protected java.awt.image.Raster saved
Raster is reused whenever possible.
-
cycleMethod
protected java.awt.MultipleGradientPaint.CycleMethod cycleMethod
The method to use when painting out of the gradient bounds.
-
colorSpace
protected java.awt.MultipleGradientPaint.ColorSpaceType colorSpace
The ColorSpace in which to perform the interpolation
-
a00
protected float a00
Elements of the inverse transform matrix.
-
a01
protected float a01
-
a10
protected float a10
-
a11
protected float a11
-
a02
protected float a02
-
a12
protected float a12
-
isSimpleLookup
protected boolean isSimpleLookup
This boolean specifies whether we are in simple lookup mode, where an input value between 0 and 1 may be used to directly index into a single array of gradient colors. If this boolean value is false, then we have to use a 2-step process where we have to determine which gradient array we fall into, then determine the index into that array.
-
fastGradientArraySize
protected int fastGradientArraySize
Size of gradients array for scaling the 0-1 index when looking up colors the fast way.
-
gradient
protected int[] gradient
Array which contains the interpolated color values for each interval, used by calculateSingleArrayGradient(). It is protected for possible direct access by subclasses.
-
gradients
private int[][] gradients
Array of gradient arrays, one array for each interval. Used by calculateMultipleArrayGradient().
-
normalizedIntervals
private float[] normalizedIntervals
Normalized intervals array.
-
fractions
private final float[] fractions
Fractions array.
-
transparencyTest
private int transparencyTest
Used to determine if gradient colors are all opaque.
-
GRADIENT_SIZE
protected static final int GRADIENT_SIZE
Constant number of max colors between any 2 arbitrary colors. Used for creating and indexing gradients arrays.- See Also:
- Constant Field Values
-
GRADIENT_SIZE_INDEX
protected static final int GRADIENT_SIZE_INDEX
- See Also:
- Constant Field Values
-
MAX_GRADIENT_ARRAY_SIZE
private static final int MAX_GRADIENT_ARRAY_SIZE
Maximum length of the fast single-array. If the estimated array size is greater than this, switch over to the slow lookup method. No particular reason for choosing this number, but it seems to provide satisfactory performance for the common case (fast lookup).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SVGMultipleGradientPaintContext
protected SVGMultipleGradientPaintContext(@NotNull @NotNull SVGMultipleGradientPaint mgp, @NotNull @NotNull java.awt.geom.AffineTransform t, float @NotNull [] fractions, @NotNull @NotNull java.awt.Color @NotNull [] colors, java.awt.MultipleGradientPaint.CycleMethod cycleMethod, java.awt.MultipleGradientPaint.ColorSpaceType colorSpace)Constructor for MultipleGradientPaintContext superclass.
-
-
Method Detail
-
calculateLookupData
private void calculateLookupData(java.awt.Color[] colors)
This function is the meat of this class. It calculates an array of gradient colors based on an array of fractions and color values at those fractions.
-
calculateSingleArrayGradient
private void calculateSingleArrayGradient(java.awt.Color[] colors, float minInterval)FAST LOOKUP METHODThis method calculates the gradient color values and places them in a single int array, gradient[]. It does this by allocating space for each interval based on its size relative to the smallest interval in the array. The smallest interval is allocated 255 interpolated values (the maximum number of unique in-between colors in a 24 bit color system), and all other intervals are allocated size = (255 * the ratio of their size to the smallest interval).
This scheme expedites a speedy retrieval because the colors are distributed along the array according to their user-specified distribution. All that is needed is a relative index from 0 to 1.
The only problem with this method is that the possibility exists for the array size to balloon in the case where there is a disproportionately small gradient interval. In this case the other intervals will be allocated huge space, but much of that data is redundant. We thus need to use the space conserving scheme below.
- Parameters:
minInterval- the size of the smallest interval
-
calculateMultipleArrayGradient
private void calculateMultipleArrayGradient(java.awt.Color[] colors)
SLOW LOOKUP METHODThis method calculates the gradient color values for each interval and places each into its own 255 size array. The arrays are stored in gradients[][]. (255 is used because this is the maximum number of unique colors between 2 arbitrary colors in a 24 bit color system.)
This method uses the minimum amount of space (only 255 * number of intervals), but it aggravates the lookup procedure, because now we have to find out which interval to select, then calculate the index within that interval. This causes a significant performance hit, because it requires this calculation be done for every point in the rendering loop.
For those of you who are interested, this is a classic example of the time-space tradeoff.
-
interpolate
private void interpolate(int rgb1, int rgb2, int[] output)Yet another helper function. This one linearly interpolates between 2 colors, filling up the output array.- Parameters:
rgb1- the start colorrgb2- the end coloroutput- the output array of colors; must not be null
-
mod1
private static float mod1(float x)
-
mod1Reflect
private static float mod1Reflect(float x)
-
indexIntoGradientsArrays
protected final int indexIntoGradientsArrays(float position)
Helper function to index into the gradients array. This is necessary because each interval has an array of colors with uniform size 255. However, the color intervals are not necessarily of uniform length, so a conversion is required.- Parameters:
position- the unmanipulated position, which will be mapped into the range 0 to 1- Returns:
- integer color to display
-
getRaster
public final java.awt.image.Raster getRaster(int x, int y, int w, int h)- Specified by:
getRasterin interfacejava.awt.PaintContext
-
fillRaster
protected abstract void fillRaster(int[] pixels, int off, int adjust, int x, int y, int w, int h)
-
getCachedRaster
private static java.awt.image.Raster getCachedRaster(java.awt.image.ColorModel cm, int w, int h)Took this cacheRaster code from GradientPaint. It appears to recycle rasters for use by any other instance, as long as they are sufficiently large.
-
putCachedRaster
private static void putCachedRaster(java.awt.image.ColorModel cm, java.awt.image.Raster ras)Took this cacheRaster code from GradientPaint. It appears to recycle rasters for use by any other instance, as long as they are sufficiently large.
-
dispose
public final void dispose()
- Specified by:
disposein interfacejava.awt.PaintContext
-
getColorModel
public final java.awt.image.ColorModel getColorModel()
- Specified by:
getColorModelin interfacejava.awt.PaintContext
-
-