0N/A/*
2362N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.MultipleGradientPaint.CycleMethod;
0N/Aimport java.awt.MultipleGradientPaint.ColorSpaceType;
0N/Aimport java.awt.color.ColorSpace;
0N/Aimport java.awt.geom.AffineTransform;
0N/Aimport java.awt.geom.NoninvertibleTransformException;
0N/Aimport java.awt.geom.Rectangle2D;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport java.awt.image.DataBuffer;
0N/Aimport java.awt.image.DataBufferInt;
0N/Aimport java.awt.image.DirectColorModel;
0N/Aimport java.awt.image.Raster;
0N/Aimport java.awt.image.SinglePixelPackedSampleModel;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport java.lang.ref.SoftReference;
0N/Aimport java.lang.ref.WeakReference;
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * This is the superclass for all PaintContexts which use a multiple color
0N/A * gradient to fill in their raster. It provides the actual color
0N/A * interpolation functionality. Subclasses only have to deal with using
0N/A * the gradient to fill pixels in a raster.
0N/A *
0N/A * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
0N/A */
0N/Aabstract class MultipleGradientPaintContext implements PaintContext {
0N/A
0N/A /**
0N/A * The PaintContext's ColorModel. This is ARGB if colors are not all
0N/A * opaque, otherwise it is RGB.
0N/A */
0N/A protected ColorModel model;
0N/A
0N/A /** Color model used if gradient colors are all opaque. */
0N/A private static ColorModel xrgbmodel =
0N/A new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
0N/A
0N/A /** The cached ColorModel. */
0N/A protected static ColorModel cachedModel;
0N/A
0N/A /** The cached raster, which is reusable among instances. */
0N/A protected static WeakReference<Raster> cached;
0N/A
0N/A /** Raster is reused whenever possible. */
0N/A protected Raster saved;
0N/A
0N/A /** The method to use when painting out of the gradient bounds. */
0N/A protected CycleMethod cycleMethod;
0N/A
0N/A /** The ColorSpace in which to perform the interpolation */
0N/A protected ColorSpaceType colorSpace;
0N/A
0N/A /** Elements of the inverse transform matrix. */
0N/A protected float a00, a01, a10, a11, a02, a12;
0N/A
0N/A /**
0N/A * This boolean specifies wether we are in simple lookup mode, where an
0N/A * input value between 0 and 1 may be used to directly index into a single
0N/A * array of gradient colors. If this boolean value is false, then we have
0N/A * to use a 2-step process where we have to determine which gradient array
0N/A * we fall into, then determine the index into that array.
0N/A */
0N/A protected boolean isSimpleLookup;
0N/A
0N/A /**
0N/A * Size of gradients array for scaling the 0-1 index when looking up
0N/A * colors the fast way.
0N/A */
0N/A protected int fastGradientArraySize;
0N/A
0N/A /**
0N/A * Array which contains the interpolated color values for each interval,
0N/A * used by calculateSingleArrayGradient(). It is protected for possible
0N/A * direct access by subclasses.
0N/A */
0N/A protected int[] gradient;
0N/A
0N/A /**
0N/A * Array of gradient arrays, one array for each interval. Used by
0N/A * calculateMultipleArrayGradient().
0N/A */
0N/A private int[][] gradients;
0N/A
0N/A /** Normalized intervals array. */
0N/A private float[] normalizedIntervals;
0N/A
0N/A /** Fractions array. */
0N/A private float[] fractions;
0N/A
0N/A /** Used to determine if gradient colors are all opaque. */
0N/A private int transparencyTest;
0N/A
0N/A /** Color space conversion lookup tables. */
0N/A private static final int SRGBtoLinearRGB[] = new int[256];
0N/A private static final int LinearRGBtoSRGB[] = new int[256];
0N/A
0N/A static {
0N/A // build the tables
0N/A for (int k = 0; k < 256; k++) {
0N/A SRGBtoLinearRGB[k] = convertSRGBtoLinearRGB(k);
0N/A LinearRGBtoSRGB[k] = convertLinearRGBtoSRGB(k);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constant number of max colors between any 2 arbitrary colors.
0N/A * Used for creating and indexing gradients arrays.
0N/A */
0N/A protected static final int GRADIENT_SIZE = 256;
0N/A protected static final int GRADIENT_SIZE_INDEX = GRADIENT_SIZE -1;
0N/A
0N/A /**
0N/A * Maximum length of the fast single-array. If the estimated array size
0N/A * is greater than this, switch over to the slow lookup method.
0N/A * No particular reason for choosing this number, but it seems to provide
0N/A * satisfactory performance for the common case (fast lookup).
0N/A */
0N/A private static final int MAX_GRADIENT_ARRAY_SIZE = 5000;
0N/A
0N/A /**
0N/A * Constructor for MultipleGradientPaintContext superclass.
0N/A */
0N/A protected MultipleGradientPaintContext(MultipleGradientPaint mgp,
0N/A ColorModel cm,
0N/A Rectangle deviceBounds,
0N/A Rectangle2D userBounds,
0N/A AffineTransform t,
0N/A RenderingHints hints,
0N/A float[] fractions,
0N/A Color[] colors,
0N/A CycleMethod cycleMethod,
0N/A ColorSpaceType colorSpace)
0N/A {
0N/A if (deviceBounds == null) {
0N/A throw new NullPointerException("Device bounds cannot be null");
0N/A }
0N/A
0N/A if (userBounds == null) {
0N/A throw new NullPointerException("User bounds cannot be null");
0N/A }
0N/A
0N/A if (t == null) {
0N/A throw new NullPointerException("Transform cannot be null");
0N/A }
0N/A
0N/A if (hints == null) {
0N/A throw new NullPointerException("RenderingHints cannot be null");
0N/A }
0N/A
0N/A // The inverse transform is needed to go from device to user space.
0N/A // Get all the components of the inverse transform matrix.
0N/A AffineTransform tInv;
0N/A try {
0N/A // the following assumes that the caller has copied the incoming
0N/A // transform and is not concerned about it being modified
0N/A t.invert();
0N/A tInv = t;
0N/A } catch (NoninvertibleTransformException e) {
0N/A // just use identity transform in this case; better to show
0N/A // (incorrect) results than to throw an exception and/or no-op
0N/A tInv = new AffineTransform();
0N/A }
0N/A double m[] = new double[6];
0N/A tInv.getMatrix(m);
0N/A a00 = (float)m[0];
0N/A a10 = (float)m[1];
0N/A a01 = (float)m[2];
0N/A a11 = (float)m[3];
0N/A a02 = (float)m[4];
0N/A a12 = (float)m[5];
0N/A
0N/A // copy some flags
0N/A this.cycleMethod = cycleMethod;
0N/A this.colorSpace = colorSpace;
0N/A
0N/A // we can avoid copying this array since we do not modify its values
0N/A this.fractions = fractions;
0N/A
0N/A // note that only one of these values can ever be non-null (we either
0N/A // store the fast gradient array or the slow one, but never both
0N/A // at the same time)
0N/A int[] gradient =
0N/A (mgp.gradient != null) ? mgp.gradient.get() : null;
0N/A int[][] gradients =
0N/A (mgp.gradients != null) ? mgp.gradients.get() : null;
0N/A
0N/A if (gradient == null && gradients == null) {
0N/A // we need to (re)create the appropriate values
0N/A calculateLookupData(colors);
0N/A
0N/A // now cache the calculated values in the
0N/A // MultipleGradientPaint instance for future use
0N/A mgp.model = this.model;
0N/A mgp.normalizedIntervals = this.normalizedIntervals;
0N/A mgp.isSimpleLookup = this.isSimpleLookup;
0N/A if (isSimpleLookup) {
0N/A // only cache the fast array
0N/A mgp.fastGradientArraySize = this.fastGradientArraySize;
0N/A mgp.gradient = new SoftReference<int[]>(this.gradient);
0N/A } else {
0N/A // only cache the slow array
0N/A mgp.gradients = new SoftReference<int[][]>(this.gradients);
0N/A }
0N/A } else {
0N/A // use the values cached in the MultipleGradientPaint instance
0N/A this.model = mgp.model;
0N/A this.normalizedIntervals = mgp.normalizedIntervals;
0N/A this.isSimpleLookup = mgp.isSimpleLookup;
0N/A this.gradient = gradient;
0N/A this.fastGradientArraySize = mgp.fastGradientArraySize;
0N/A this.gradients = gradients;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This function is the meat of this class. It calculates an array of
0N/A * gradient colors based on an array of fractions and color values at
0N/A * those fractions.
0N/A */
0N/A private void calculateLookupData(Color[] colors) {
0N/A Color[] normalizedColors;
0N/A if (colorSpace == ColorSpaceType.LINEAR_RGB) {
0N/A // create a new colors array
0N/A normalizedColors = new Color[colors.length];
0N/A // convert the colors using the lookup table
0N/A for (int i = 0; i < colors.length; i++) {
0N/A int argb = colors[i].getRGB();
0N/A int a = argb >>> 24;
0N/A int r = SRGBtoLinearRGB[(argb >> 16) & 0xff];
0N/A int g = SRGBtoLinearRGB[(argb >> 8) & 0xff];
0N/A int b = SRGBtoLinearRGB[(argb ) & 0xff];
0N/A normalizedColors[i] = new Color(r, g, b, a);
0N/A }
0N/A } else {
0N/A // we can just use this array by reference since we do not
0N/A // modify its values in the case of SRGB
0N/A normalizedColors = colors;
0N/A }
0N/A
0N/A // this will store the intervals (distances) between gradient stops
0N/A normalizedIntervals = new float[fractions.length-1];
0N/A
0N/A // convert from fractions into intervals
0N/A for (int i = 0; i < normalizedIntervals.length; i++) {
0N/A // interval distance is equal to the difference in positions
0N/A normalizedIntervals[i] = this.fractions[i+1] - this.fractions[i];
0N/A }
0N/A
0N/A // initialize to be fully opaque for ANDing with colors
0N/A transparencyTest = 0xff000000;
0N/A
0N/A // array of interpolation arrays
0N/A gradients = new int[normalizedIntervals.length][];
0N/A
0N/A // find smallest interval
0N/A float Imin = 1;
0N/A for (int i = 0; i < normalizedIntervals.length; i++) {
0N/A Imin = (Imin > normalizedIntervals[i]) ?
0N/A normalizedIntervals[i] : Imin;
0N/A }
0N/A
0N/A // Estimate the size of the entire gradients array.
0N/A // This is to prevent a tiny interval from causing the size of array
0N/A // to explode. If the estimated size is too large, break to using
0N/A // separate arrays for each interval, and using an indexing scheme at
0N/A // look-up time.
0N/A int estimatedSize = 0;
0N/A for (int i = 0; i < normalizedIntervals.length; i++) {
0N/A estimatedSize += (normalizedIntervals[i]/Imin) * GRADIENT_SIZE;
0N/A }
0N/A
0N/A if (estimatedSize > MAX_GRADIENT_ARRAY_SIZE) {
0N/A // slow method
0N/A calculateMultipleArrayGradient(normalizedColors);
0N/A } else {
0N/A // fast method
0N/A calculateSingleArrayGradient(normalizedColors, Imin);
0N/A }
0N/A
0N/A // use the most "economical" model
0N/A if ((transparencyTest >>> 24) == 0xff) {
0N/A model = xrgbmodel;
0N/A } else {
0N/A model = ColorModel.getRGBdefault();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * FAST LOOKUP METHOD
0N/A *
0N/A * This method calculates the gradient color values and places them in a
0N/A * single int array, gradient[]. It does this by allocating space for
0N/A * each interval based on its size relative to the smallest interval in
0N/A * the array. The smallest interval is allocated 255 interpolated values
0N/A * (the maximum number of unique in-between colors in a 24 bit color
0N/A * system), and all other intervals are allocated
0N/A * size = (255 * the ratio of their size to the smallest interval).
0N/A *
0N/A * This scheme expedites a speedy retrieval because the colors are
0N/A * distributed along the array according to their user-specified
0N/A * distribution. All that is needed is a relative index from 0 to 1.
0N/A *
0N/A * The only problem with this method is that the possibility exists for
0N/A * the array size to balloon in the case where there is a
0N/A * disproportionately small gradient interval. In this case the other
0N/A * intervals will be allocated huge space, but much of that data is
0N/A * redundant. We thus need to use the space conserving scheme below.
0N/A *
0N/A * @param Imin the size of the smallest interval
0N/A */
0N/A private void calculateSingleArrayGradient(Color[] colors, float Imin) {
0N/A // set the flag so we know later it is a simple (fast) lookup
0N/A isSimpleLookup = true;
0N/A
0N/A // 2 colors to interpolate
0N/A int rgb1, rgb2;
0N/A
0N/A //the eventual size of the single array
0N/A int gradientsTot = 1;
0N/A
0N/A // for every interval (transition between 2 colors)
0N/A for (int i = 0; i < gradients.length; i++) {
0N/A // create an array whose size is based on the ratio to the
0N/A // smallest interval
0N/A int nGradients = (int)((normalizedIntervals[i]/Imin)*255f);
0N/A gradientsTot += nGradients;
0N/A gradients[i] = new int[nGradients];
0N/A
0N/A // the 2 colors (keyframes) to interpolate between
0N/A rgb1 = colors[i].getRGB();
0N/A rgb2 = colors[i+1].getRGB();
0N/A
0N/A // fill this array with the colors in between rgb1 and rgb2
0N/A interpolate(rgb1, rgb2, gradients[i]);
0N/A
0N/A // if the colors are opaque, transparency should still
0N/A // be 0xff000000
0N/A transparencyTest &= rgb1;
0N/A transparencyTest &= rgb2;
0N/A }
0N/A
0N/A // put all gradients in a single array
0N/A gradient = new int[gradientsTot];
0N/A int curOffset = 0;
0N/A for (int i = 0; i < gradients.length; i++){
0N/A System.arraycopy(gradients[i], 0, gradient,
0N/A curOffset, gradients[i].length);
0N/A curOffset += gradients[i].length;
0N/A }
0N/A gradient[gradient.length-1] = colors[colors.length-1].getRGB();
0N/A
0N/A // if interpolation occurred in Linear RGB space, convert the
0N/A // gradients back to sRGB using the lookup table
0N/A if (colorSpace == ColorSpaceType.LINEAR_RGB) {
0N/A for (int i = 0; i < gradient.length; i++) {
0N/A gradient[i] = convertEntireColorLinearRGBtoSRGB(gradient[i]);
0N/A }
0N/A }
0N/A
0N/A fastGradientArraySize = gradient.length - 1;
0N/A }
0N/A
0N/A /**
0N/A * SLOW LOOKUP METHOD
0N/A *
0N/A * This method calculates the gradient color values for each interval and
0N/A * places each into its own 255 size array. The arrays are stored in
0N/A * gradients[][]. (255 is used because this is the maximum number of
0N/A * unique colors between 2 arbitrary colors in a 24 bit color system.)
0N/A *
0N/A * This method uses the minimum amount of space (only 255 * number of
0N/A * intervals), but it aggravates the lookup procedure, because now we
0N/A * have to find out which interval to select, then calculate the index
0N/A * within that interval. This causes a significant performance hit,
0N/A * because it requires this calculation be done for every point in
0N/A * the rendering loop.
0N/A *
0N/A * For those of you who are interested, this is a classic example of the
0N/A * time-space tradeoff.
0N/A */
0N/A private void calculateMultipleArrayGradient(Color[] colors) {
0N/A // set the flag so we know later it is a non-simple lookup
0N/A isSimpleLookup = false;
0N/A
0N/A // 2 colors to interpolate
0N/A int rgb1, rgb2;
0N/A
0N/A // for every interval (transition between 2 colors)
0N/A for (int i = 0; i < gradients.length; i++){
0N/A // create an array of the maximum theoretical size for
0N/A // each interval
0N/A gradients[i] = new int[GRADIENT_SIZE];
0N/A
0N/A // get the the 2 colors
0N/A rgb1 = colors[i].getRGB();
0N/A rgb2 = colors[i+1].getRGB();
0N/A
0N/A // fill this array with the colors in between rgb1 and rgb2
0N/A interpolate(rgb1, rgb2, gradients[i]);
0N/A
0N/A // if the colors are opaque, transparency should still
0N/A // be 0xff000000
0N/A transparencyTest &= rgb1;
0N/A transparencyTest &= rgb2;
0N/A }
0N/A
0N/A // if interpolation occurred in Linear RGB space, convert the
0N/A // gradients back to SRGB using the lookup table
0N/A if (colorSpace == ColorSpaceType.LINEAR_RGB) {
0N/A for (int j = 0; j < gradients.length; j++) {
0N/A for (int i = 0; i < gradients[j].length; i++) {
0N/A gradients[j][i] =
0N/A convertEntireColorLinearRGBtoSRGB(gradients[j][i]);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Yet another helper function. This one linearly interpolates between
0N/A * 2 colors, filling up the output array.
0N/A *
0N/A * @param rgb1 the start color
0N/A * @param rgb2 the end color
0N/A * @param output the output array of colors; must not be null
0N/A */
0N/A private void interpolate(int rgb1, int rgb2, int[] output) {
0N/A // color components
0N/A int a1, r1, g1, b1, da, dr, dg, db;
0N/A
0N/A // step between interpolated values
0N/A float stepSize = 1.0f / output.length;
0N/A
0N/A // extract color components from packed integer
0N/A a1 = (rgb1 >> 24) & 0xff;
0N/A r1 = (rgb1 >> 16) & 0xff;
0N/A g1 = (rgb1 >> 8) & 0xff;
0N/A b1 = (rgb1 ) & 0xff;
0N/A
0N/A // calculate the total change in alpha, red, green, blue
0N/A da = ((rgb2 >> 24) & 0xff) - a1;
0N/A dr = ((rgb2 >> 16) & 0xff) - r1;
0N/A dg = ((rgb2 >> 8) & 0xff) - g1;
0N/A db = ((rgb2 ) & 0xff) - b1;
0N/A
0N/A // for each step in the interval calculate the in-between color by
0N/A // multiplying the normalized current position by the total color
0N/A // change (0.5 is added to prevent truncation round-off error)
0N/A for (int i = 0; i < output.length; i++) {
0N/A output[i] =
0N/A (((int) ((a1 + i * da * stepSize) + 0.5) << 24)) |
0N/A (((int) ((r1 + i * dr * stepSize) + 0.5) << 16)) |
0N/A (((int) ((g1 + i * dg * stepSize) + 0.5) << 8)) |
0N/A (((int) ((b1 + i * db * stepSize) + 0.5) ));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Yet another helper function. This one extracts the color components
0N/A * of an integer RGB triple, converts them from LinearRGB to SRGB, then
0N/A * recompacts them into an int.
0N/A */
0N/A private int convertEntireColorLinearRGBtoSRGB(int rgb) {
0N/A // color components
0N/A int a1, r1, g1, b1;
0N/A
0N/A // extract red, green, blue components
0N/A a1 = (rgb >> 24) & 0xff;
0N/A r1 = (rgb >> 16) & 0xff;
0N/A g1 = (rgb >> 8) & 0xff;
0N/A b1 = (rgb ) & 0xff;
0N/A
0N/A // use the lookup table
0N/A r1 = LinearRGBtoSRGB[r1];
0N/A g1 = LinearRGBtoSRGB[g1];
0N/A b1 = LinearRGBtoSRGB[b1];
0N/A
0N/A // re-compact the components
0N/A return ((a1 << 24) |
0N/A (r1 << 16) |
0N/A (g1 << 8) |
0N/A (b1 ));
0N/A }
0N/A
0N/A /**
0N/A * Helper function to index into the gradients array. This is necessary
0N/A * because each interval has an array of colors with uniform size 255.
0N/A * However, the color intervals are not necessarily of uniform length, so
0N/A * a conversion is required.
0N/A *
0N/A * @param position the unmanipulated position, which will be mapped
0N/A * into the range 0 to 1
0N/A * @returns integer color to display
0N/A */
0N/A protected final int indexIntoGradientsArrays(float position) {
0N/A // first, manipulate position value depending on the cycle method
0N/A if (cycleMethod == CycleMethod.NO_CYCLE) {
0N/A if (position > 1) {
0N/A // upper bound is 1
0N/A position = 1;
0N/A } else if (position < 0) {
0N/A // lower bound is 0
0N/A position = 0;
0N/A }
0N/A } else if (cycleMethod == CycleMethod.REPEAT) {
0N/A // get the fractional part
0N/A // (modulo behavior discards integer component)
0N/A position = position - (int)position;
0N/A
0N/A //position should now be between -1 and 1
0N/A if (position < 0) {
0N/A // force it to be in the range 0-1
0N/A position = position + 1;
0N/A }
0N/A } else { // cycleMethod == CycleMethod.REFLECT
0N/A if (position < 0) {
0N/A // take absolute value
0N/A position = -position;
0N/A }
0N/A
0N/A // get the integer part
0N/A int part = (int)position;
0N/A
0N/A // get the fractional part
0N/A position = position - part;
0N/A
0N/A if ((part & 1) == 1) {
0N/A // integer part is odd, get reflected color instead
0N/A position = 1 - position;
0N/A }
0N/A }
0N/A
0N/A // now, get the color based on this 0-1 position...
0N/A
0N/A if (isSimpleLookup) {
0N/A // easy to compute: just scale index by array size
0N/A return gradient[(int)(position * fastGradientArraySize)];
0N/A } else {
0N/A // more complicated computation, to save space
0N/A
0N/A // for all the gradient interval arrays
0N/A for (int i = 0; i < gradients.length; i++) {
0N/A if (position < fractions[i+1]) {
0N/A // this is the array we want
0N/A float delta = position - fractions[i];
0N/A
0N/A // this is the interval we want
0N/A int index = (int)((delta / normalizedIntervals[i])
0N/A * (GRADIENT_SIZE_INDEX));
0N/A
0N/A return gradients[i][index];
0N/A }
0N/A }
0N/A }
0N/A
0N/A return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
0N/A }
0N/A
0N/A /**
0N/A * Helper function to convert a color component in sRGB space to linear
0N/A * RGB space. Used to build a static lookup table.
0N/A */
0N/A private static int convertSRGBtoLinearRGB(int color) {
0N/A float input, output;
0N/A
0N/A input = color / 255.0f;
0N/A if (input <= 0.04045f) {
0N/A output = input / 12.92f;
0N/A } else {
0N/A output = (float)Math.pow((input + 0.055) / 1.055, 2.4);
0N/A }
0N/A
0N/A return Math.round(output * 255.0f);
0N/A }
0N/A
0N/A /**
0N/A * Helper function to convert a color component in linear RGB space to
0N/A * SRGB space. Used to build a static lookup table.
0N/A */
0N/A private static int convertLinearRGBtoSRGB(int color) {
0N/A float input, output;
0N/A
0N/A input = color/255.0f;
0N/A if (input <= 0.0031308) {
0N/A output = input * 12.92f;
0N/A } else {
0N/A output = (1.055f *
0N/A ((float) Math.pow(input, (1.0 / 2.4)))) - 0.055f;
0N/A }
0N/A
0N/A return Math.round(output * 255.0f);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public final Raster getRaster(int x, int y, int w, int h) {
0N/A // If working raster is big enough, reuse it. Otherwise,
0N/A // build a large enough new one.
0N/A Raster raster = saved;
0N/A if (raster == null ||
0N/A raster.getWidth() < w || raster.getHeight() < h)
0N/A {
0N/A raster = getCachedRaster(model, w, h);
0N/A saved = raster;
0N/A }
0N/A
0N/A // Access raster internal int array. Because we use a DirectColorModel,
0N/A // we know the DataBuffer is of type DataBufferInt and the SampleModel
0N/A // is SinglePixelPackedSampleModel.
0N/A // Adjust for initial offset in DataBuffer and also for the scanline
0N/A // stride.
0N/A // These calls make the DataBuffer non-acceleratable, but the
0N/A // Raster is never Stable long enough to accelerate anyway...
0N/A DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
0N/A int[] pixels = rasterDB.getData(0);
0N/A int off = rasterDB.getOffset();
0N/A int scanlineStride = ((SinglePixelPackedSampleModel)
0N/A raster.getSampleModel()).getScanlineStride();
0N/A int adjust = scanlineStride - w;
0N/A
0N/A fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass
0N/A
0N/A return raster;
0N/A }
0N/A
0N/A protected abstract void fillRaster(int pixels[], int off, int adjust,
0N/A int x, int y, int w, int h);
0N/A
0N/A
0N/A /**
0N/A * Took this cacheRaster code from GradientPaint. It appears to recycle
0N/A * rasters for use by any other instance, as long as they are sufficiently
0N/A * large.
0N/A */
0N/A private static synchronized Raster getCachedRaster(ColorModel cm,
0N/A int w, int h)
0N/A {
0N/A if (cm == cachedModel) {
0N/A if (cached != null) {
0N/A Raster ras = (Raster) cached.get();
0N/A if (ras != null &&
0N/A ras.getWidth() >= w &&
0N/A ras.getHeight() >= h)
0N/A {
0N/A cached = null;
0N/A return ras;
0N/A }
0N/A }
0N/A }
0N/A return cm.createCompatibleWritableRaster(w, h);
0N/A }
0N/A
0N/A /**
0N/A * Took this cacheRaster code from GradientPaint. It appears to recycle
0N/A * rasters for use by any other instance, as long as they are sufficiently
0N/A * large.
0N/A */
0N/A private static synchronized void putCachedRaster(ColorModel cm,
0N/A Raster ras)
0N/A {
0N/A if (cached != null) {
0N/A Raster cras = (Raster) cached.get();
0N/A if (cras != null) {
0N/A int cw = cras.getWidth();
0N/A int ch = cras.getHeight();
0N/A int iw = ras.getWidth();
0N/A int ih = ras.getHeight();
0N/A if (cw >= iw && ch >= ih) {
0N/A return;
0N/A }
0N/A if (cw * ch >= iw * ih) {
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A cachedModel = cm;
0N/A cached = new WeakReference<Raster>(ras);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public final void dispose() {
0N/A if (saved != null) {
0N/A putCachedRaster(model, saved);
0N/A saved = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public final ColorModel getColorModel() {
0N/A return model;
0N/A }
0N/A}