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 * 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 * 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 * 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. 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 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 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans 0N/A * The PaintContext's ColorModel. This is ARGB if colors are not all 0N/A * opaque, otherwise it is RGB. 0N/A /** Color model used if gradient colors are all opaque. */ 0N/A /** The cached ColorModel. */ 0N/A /** The cached raster, which is reusable among instances. */ 0N/A /** Raster is reused whenever possible. */ 0N/A /** The method to use when painting out of the gradient bounds. */ 0N/A /** The ColorSpace in which to perform the interpolation */ 0N/A /** Elements of the inverse transform matrix. */ 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 * Size of gradients array for scaling the 0-1 index when looking up 0N/A * colors the fast way. 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 * Array of gradient arrays, one array for each interval. Used by 0N/A * calculateMultipleArrayGradient(). 0N/A /** Normalized intervals array. */ 0N/A /** Fractions array. */ 0N/A /** Used to determine if gradient colors are all opaque. */ 0N/A /** Color space conversion lookup tables. */ 0N/A for (
int k =
0; k <
256; k++) {
0N/A * Constant number of max colors between any 2 arbitrary colors. 0N/A * Used for creating and indexing gradients arrays. 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 * Constructor for MultipleGradientPaintContext superclass. 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 // the following assumes that the caller has copied the incoming 0N/A // transform and is not concerned about it being modified 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 double m[] =
new double[
6];
0N/A // we can avoid copying this array since we do not modify its values 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 // we need to (re)create the appropriate values 0N/A // now cache the calculated values in the 0N/A // MultipleGradientPaint instance for future use 0N/A // only cache the fast array 0N/A // only cache the slow array 0N/A // use the values cached in the MultipleGradientPaint instance 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 // create a new colors array 0N/A // convert the colors using the lookup table 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 // this will store the intervals (distances) between gradient stops 0N/A // convert from fractions into intervals 0N/A // interval distance is equal to the difference in positions 0N/A // initialize to be fully opaque for ANDing with colors 0N/A // array of interpolation arrays 0N/A // find smallest interval 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 // use the most "economical" model 0N/A * FAST LOOKUP METHOD 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 * 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 * 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 * @param Imin the size of the smallest interval 0N/A // set the flag so we know later it is a simple (fast) lookup 0N/A // 2 colors to interpolate 0N/A //the eventual size of the single array 0N/A // for every interval (transition between 2 colors) 0N/A // create an array whose size is based on the ratio to the 0N/A // smallest interval 0N/A // the 2 colors (keyframes) to interpolate between 0N/A // fill this array with the colors in between rgb1 and rgb2 0N/A // if the colors are opaque, transparency should still 0N/A // put all gradients in a single array 0N/A // if interpolation occurred in Linear RGB space, convert the 0N/A // gradients back to sRGB using the lookup table 0N/A * SLOW LOOKUP METHOD 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 * 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 * For those of you who are interested, this is a classic example of the 0N/A * time-space tradeoff. 0N/A // set the flag so we know later it is a non-simple lookup 0N/A // 2 colors to interpolate 0N/A // for every interval (transition between 2 colors) 0N/A // create an array of the maximum theoretical size for 0N/A // get the the 2 colors 0N/A // fill this array with the colors in between rgb1 and rgb2 0N/A // if the colors are opaque, transparency should still 0N/A // if interpolation occurred in Linear RGB space, convert the 0N/A // gradients back to SRGB using the lookup table 0N/A * Yet another helper function. This one linearly interpolates between 0N/A * 2 colors, filling up the output array. 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 // step between interpolated values 0N/A // extract color components from packed integer 0N/A // calculate the total change in alpha, red, green, blue 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 * 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 // extract red, green, blue components 0N/A // use the lookup table 0N/A // re-compact the components 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 * @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 // first, manipulate position value depending on the cycle method 0N/A // get the fractional part 0N/A // (modulo behavior discards integer component) 0N/A //position should now be between -1 and 1 0N/A // force it to be in the range 0-1 0N/A }
else {
// cycleMethod == CycleMethod.REFLECT 0N/A // take absolute value 0N/A // get the integer part 0N/A // get the fractional part 0N/A // integer part is odd, get reflected color instead 0N/A // now, get the color based on this 0-1 position... 0N/A // easy to compute: just scale index by array size 0N/A // more complicated computation, to save space 0N/A // for all the gradient interval arrays 0N/A // this is the array we want 0N/A // this is the interval we want 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 * 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 // If working raster is big enough, reuse it. Otherwise, 0N/A // build a large enough new one. 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 // These calls make the DataBuffer non-acceleratable, but the 0N/A // Raster is never Stable long enough to accelerate anyway... 0N/A int x,
int y,
int w,
int h);
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 * 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