jdk/src/share/classes/java/awt/MultipleGradientPaintContext.java
author henryjen
Tue, 10 Jun 2014 16:18:54 -0700
changeset 24865 09b1d992ca72
parent 22584 eed64ee05369
permissions -rw-r--r--
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo Reviewed-by: mduigou, lancea, alanb, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.MultipleGradientPaint.CycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.MultipleGradientPaint.ColorSpaceType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.DataBufferInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This is the superclass for all PaintContexts which use a multiple color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * gradient to fill in their raster.  It provides the actual color
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * interpolation functionality.  Subclasses only have to deal with using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the gradient to fill pixels in a raster.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
abstract class MultipleGradientPaintContext implements PaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * The PaintContext's ColorModel.  This is ARGB if colors are not all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * opaque, otherwise it is RGB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected ColorModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** Color model used if gradient colors are all opaque. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static ColorModel xrgbmodel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** The cached ColorModel. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected static ColorModel cachedModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** The cached raster, which is reusable among instances. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected static WeakReference<Raster> cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** Raster is reused whenever possible. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected Raster saved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** The method to use when painting out of the gradient bounds. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected CycleMethod cycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /** The ColorSpace in which to perform the interpolation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected ColorSpaceType colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /** Elements of the inverse transform matrix. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected float a00, a01, a10, a11, a02, a12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    84
     * This boolean specifies whether we are in simple lookup mode, where an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * input value between 0 and 1 may be used to directly index into a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * array of gradient colors.  If this boolean value is false, then we have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * to use a 2-step process where we have to determine which gradient array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * we fall into, then determine the index into that array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected boolean isSimpleLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Size of gradients array for scaling the 0-1 index when looking up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * colors the fast way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected int fastGradientArraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Array which contains the interpolated color values for each interval,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * used by calculateSingleArrayGradient().  It is protected for possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * direct access by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected int[] gradient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Array of gradient arrays, one array for each interval.  Used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * calculateMultipleArrayGradient().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int[][] gradients;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /** Normalized intervals array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private float[] normalizedIntervals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /** Fractions array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private float[] fractions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** Used to determine if gradient colors are all opaque. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private int transparencyTest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** Color space conversion lookup tables. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int SRGBtoLinearRGB[] = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final int LinearRGBtoSRGB[] = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // build the tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int k = 0; k < 256; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            SRGBtoLinearRGB[k] = convertSRGBtoLinearRGB(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            LinearRGBtoSRGB[k] = convertLinearRGBtoSRGB(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constant number of max colors between any 2 arbitrary colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Used for creating and indexing gradients arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected static final int GRADIENT_SIZE = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected static final int GRADIENT_SIZE_INDEX = GRADIENT_SIZE -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Maximum length of the fast single-array.  If the estimated array size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * is greater than this, switch over to the slow lookup method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * No particular reason for choosing this number, but it seems to provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * satisfactory performance for the common case (fast lookup).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final int MAX_GRADIENT_ARRAY_SIZE = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Constructor for MultipleGradientPaintContext superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected MultipleGradientPaintContext(MultipleGradientPaint mgp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                           ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                           Rectangle deviceBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                           Rectangle2D userBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                           AffineTransform t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                           RenderingHints hints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                           float[] fractions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                           Color[] colors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                           CycleMethod cycleMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                           ColorSpaceType colorSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (deviceBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new NullPointerException("Device bounds cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (userBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new NullPointerException("User bounds cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new NullPointerException("Transform cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (hints == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new NullPointerException("RenderingHints cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // The inverse transform is needed to go from device to user space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Get all the components of the inverse transform matrix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        AffineTransform tInv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // the following assumes that the caller has copied the incoming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // transform and is not concerned about it being modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            t.invert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            tInv = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (NoninvertibleTransformException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // just use identity transform in this case; better to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // (incorrect) results than to throw an exception and/or no-op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            tInv = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        double m[] = new double[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        tInv.getMatrix(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        a00 = (float)m[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        a10 = (float)m[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        a01 = (float)m[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        a11 = (float)m[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        a02 = (float)m[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        a12 = (float)m[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // copy some flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        this.cycleMethod = cycleMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.colorSpace = colorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // we can avoid copying this array since we do not modify its values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.fractions = fractions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // note that only one of these values can ever be non-null (we either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // store the fast gradient array or the slow one, but never both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // at the same time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int[] gradient =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            (mgp.gradient != null) ? mgp.gradient.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int[][] gradients =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            (mgp.gradients != null) ? mgp.gradients.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (gradient == null && gradients == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // we need to (re)create the appropriate values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            calculateLookupData(colors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            // now cache the calculated values in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // MultipleGradientPaint instance for future use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            mgp.model               = this.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            mgp.normalizedIntervals = this.normalizedIntervals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            mgp.isSimpleLookup      = this.isSimpleLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (isSimpleLookup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                // only cache the fast array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                mgp.fastGradientArraySize = this.fastGradientArraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                mgp.gradient = new SoftReference<int[]>(this.gradient);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                // only cache the slow array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                mgp.gradients = new SoftReference<int[][]>(this.gradients);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // use the values cached in the MultipleGradientPaint instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            this.model                 = mgp.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.normalizedIntervals   = mgp.normalizedIntervals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.isSimpleLookup        = mgp.isSimpleLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.gradient              = gradient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            this.fastGradientArraySize = mgp.fastGradientArraySize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            this.gradients             = gradients;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * This function is the meat of this class.  It calculates an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * gradient colors based on an array of fractions and color values at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * those fractions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private void calculateLookupData(Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Color[] normalizedColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (colorSpace == ColorSpaceType.LINEAR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // create a new colors array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            normalizedColors = new Color[colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // convert the colors using the lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (int i = 0; i < colors.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                int argb = colors[i].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                int a = argb >>> 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                int r = SRGBtoLinearRGB[(argb >> 16) & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                int g = SRGBtoLinearRGB[(argb >>  8) & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                int b = SRGBtoLinearRGB[(argb      ) & 0xff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                normalizedColors[i] = new Color(r, g, b, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // we can just use this array by reference since we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // modify its values in the case of SRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            normalizedColors = colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        // this will store the intervals (distances) between gradient stops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        normalizedIntervals = new float[fractions.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // convert from fractions into intervals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (int i = 0; i < normalizedIntervals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // interval distance is equal to the difference in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            normalizedIntervals[i] = this.fractions[i+1] - this.fractions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // initialize to be fully opaque for ANDing with colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        transparencyTest = 0xff000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // array of interpolation arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        gradients = new int[normalizedIntervals.length][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // find smallest interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        float Imin = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (int i = 0; i < normalizedIntervals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Imin = (Imin > normalizedIntervals[i]) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                normalizedIntervals[i] : Imin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // Estimate the size of the entire gradients array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // This is to prevent a tiny interval from causing the size of array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // to explode.  If the estimated size is too large, break to using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // separate arrays for each interval, and using an indexing scheme at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        // look-up time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int estimatedSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (int i = 0; i < normalizedIntervals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            estimatedSize += (normalizedIntervals[i]/Imin) * GRADIENT_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (estimatedSize > MAX_GRADIENT_ARRAY_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // slow method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            calculateMultipleArrayGradient(normalizedColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // fast method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            calculateSingleArrayGradient(normalizedColors, Imin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // use the most "economical" model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if ((transparencyTest >>> 24) == 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            model = xrgbmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * FAST LOOKUP METHOD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * This method calculates the gradient color values and places them in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * single int array, gradient[].  It does this by allocating space for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * each interval based on its size relative to the smallest interval in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the array.  The smallest interval is allocated 255 interpolated values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * (the maximum number of unique in-between colors in a 24 bit color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * system), and all other intervals are allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * size = (255 * the ratio of their size to the smallest interval).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * This scheme expedites a speedy retrieval because the colors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * distributed along the array according to their user-specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * distribution.  All that is needed is a relative index from 0 to 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * The only problem with this method is that the possibility exists for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the array size to balloon in the case where there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * disproportionately small gradient interval.  In this case the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * intervals will be allocated huge space, but much of that data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * redundant.  We thus need to use the space conserving scheme below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param Imin the size of the smallest interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private void calculateSingleArrayGradient(Color[] colors, float Imin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // set the flag so we know later it is a simple (fast) lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        isSimpleLookup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // 2 colors to interpolate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int rgb1, rgb2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        //the eventual size of the single array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int gradientsTot = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // for every interval (transition between 2 colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        for (int i = 0; i < gradients.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // create an array whose size is based on the ratio to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            // smallest interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            int nGradients = (int)((normalizedIntervals[i]/Imin)*255f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            gradientsTot += nGradients;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            gradients[i] = new int[nGradients];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            // the 2 colors (keyframes) to interpolate between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            rgb1 = colors[i].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            rgb2 = colors[i+1].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            // fill this array with the colors in between rgb1 and rgb2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            interpolate(rgb1, rgb2, gradients[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // if the colors are opaque, transparency should still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // be 0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            transparencyTest &= rgb1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            transparencyTest &= rgb2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        // put all gradients in a single array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        gradient = new int[gradientsTot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int curOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        for (int i = 0; i < gradients.length; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            System.arraycopy(gradients[i], 0, gradient,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                             curOffset, gradients[i].length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            curOffset += gradients[i].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        gradient[gradient.length-1] = colors[colors.length-1].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // if interpolation occurred in Linear RGB space, convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // gradients back to sRGB using the lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (colorSpace == ColorSpaceType.LINEAR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            for (int i = 0; i < gradient.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                gradient[i] = convertEntireColorLinearRGBtoSRGB(gradient[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        fastGradientArraySize = gradient.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * SLOW LOOKUP METHOD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * This method calculates the gradient color values for each interval and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * places each into its own 255 size array.  The arrays are stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * gradients[][].  (255 is used because this is the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * unique colors between 2 arbitrary colors in a 24 bit color system.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This method uses the minimum amount of space (only 255 * number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * intervals), but it aggravates the lookup procedure, because now we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * have to find out which interval to select, then calculate the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * within that interval.  This causes a significant performance hit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * because it requires this calculation be done for every point in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * the rendering loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * For those of you who are interested, this is a classic example of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * time-space tradeoff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    private void calculateMultipleArrayGradient(Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // set the flag so we know later it is a non-simple lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        isSimpleLookup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // 2 colors to interpolate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int rgb1, rgb2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // for every interval (transition between 2 colors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        for (int i = 0; i < gradients.length; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // create an array of the maximum theoretical size for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // each interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            gradients[i] = new int[GRADIENT_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // get the the 2 colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            rgb1 = colors[i].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            rgb2 = colors[i+1].getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // fill this array with the colors in between rgb1 and rgb2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            interpolate(rgb1, rgb2, gradients[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // if the colors are opaque, transparency should still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // be 0xff000000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            transparencyTest &= rgb1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            transparencyTest &= rgb2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        // if interpolation occurred in Linear RGB space, convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // gradients back to SRGB using the lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (colorSpace == ColorSpaceType.LINEAR_RGB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            for (int j = 0; j < gradients.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                for (int i = 0; i < gradients[j].length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    gradients[j][i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        convertEntireColorLinearRGBtoSRGB(gradients[j][i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Yet another helper function.  This one linearly interpolates between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * 2 colors, filling up the output array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param rgb1 the start color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param rgb2 the end color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param output the output array of colors; must not be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private void interpolate(int rgb1, int rgb2, int[] output) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int a1, r1, g1, b1, da, dr, dg, db;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        // step between interpolated values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        float stepSize = 1.0f / output.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // extract color components from packed integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        a1 = (rgb1 >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        r1 = (rgb1 >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        g1 = (rgb1 >>  8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        b1 = (rgb1      ) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // calculate the total change in alpha, red, green, blue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        da = ((rgb2 >> 24) & 0xff) - a1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        dr = ((rgb2 >> 16) & 0xff) - r1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        dg = ((rgb2 >>  8) & 0xff) - g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        db = ((rgb2      ) & 0xff) - b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // for each step in the interval calculate the in-between color by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // multiplying the normalized current position by the total color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // change (0.5 is added to prevent truncation round-off error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        for (int i = 0; i < output.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            output[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                (((int) ((a1 + i * da * stepSize) + 0.5) << 24)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                (((int) ((r1 + i * dr * stepSize) + 0.5) << 16)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                (((int) ((g1 + i * dg * stepSize) + 0.5) <<  8)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                (((int) ((b1 + i * db * stepSize) + 0.5)      ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Yet another helper function.  This one extracts the color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * of an integer RGB triple, converts them from LinearRGB to SRGB, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * recompacts them into an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    private int convertEntireColorLinearRGBtoSRGB(int rgb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // color components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int a1, r1, g1, b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // extract red, green, blue components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        a1 = (rgb >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        r1 = (rgb >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        g1 = (rgb >>  8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        b1 = (rgb      ) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // use the lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        r1 = LinearRGBtoSRGB[r1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        g1 = LinearRGBtoSRGB[g1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        b1 = LinearRGBtoSRGB[b1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // re-compact the components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return ((a1 << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                (r1 << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                (g1 <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                (b1      ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Helper function to index into the gradients array.  This is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * because each interval has an array of colors with uniform size 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * However, the color intervals are not necessarily of uniform length, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * a conversion is required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param position the unmanipulated position, which will be mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *                 into the range 0 to 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @returns integer color to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    protected final int indexIntoGradientsArrays(float position) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        // first, manipulate position value depending on the cycle method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (cycleMethod == CycleMethod.NO_CYCLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (position > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // upper bound is 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                position = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else if (position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                // lower bound is 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        } else if (cycleMethod == CycleMethod.REPEAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // get the fractional part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            // (modulo behavior discards integer component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            position = position - (int)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            //position should now be between -1 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                // force it to be in the range 0-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                position = position + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        } else { // cycleMethod == CycleMethod.REFLECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (position < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // take absolute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                position = -position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            // get the integer part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            int part = (int)position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            // get the fractional part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            position = position - part;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if ((part & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // integer part is odd, get reflected color instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                position = 1 - position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // now, get the color based on this 0-1 position...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (isSimpleLookup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            // easy to compute: just scale index by array size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return gradient[(int)(position * fastGradientArraySize)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            // more complicated computation, to save space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            // for all the gradient interval arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            for (int i = 0; i < gradients.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (position < fractions[i+1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    // this is the array we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    float delta = position - fractions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    // this is the interval we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    int index = (int)((delta / normalizedIntervals[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                      * (GRADIENT_SIZE_INDEX));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    return gradients[i][index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return gradients[gradients.length - 1][GRADIENT_SIZE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Helper function to convert a color component in sRGB space to linear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * RGB space.  Used to build a static lookup table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private static int convertSRGBtoLinearRGB(int color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        float input, output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        input = color / 255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (input <= 0.04045f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            output = input / 12.92f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            output = (float)Math.pow((input + 0.055) / 1.055, 2.4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return Math.round(output * 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Helper function to convert a color component in linear RGB space to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * SRGB space.  Used to build a static lookup table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private static int convertLinearRGBtoSRGB(int color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        float input, output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        input = color/255.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (input <= 0.0031308) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            output = input * 12.92f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            output = (1.055f *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                ((float) Math.pow(input, (1.0 / 2.4)))) - 0.055f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return Math.round(output * 255.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public final Raster getRaster(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // If working raster is big enough, reuse it. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        // build a large enough new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        Raster raster = saved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (raster == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            raster.getWidth() < w || raster.getHeight() < h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            raster = getCachedRaster(model, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            saved = raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // Access raster internal int array. Because we use a DirectColorModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // we know the DataBuffer is of type DataBufferInt and the SampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // is SinglePixelPackedSampleModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        // Adjust for initial offset in DataBuffer and also for the scanline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        // stride.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        // These calls make the DataBuffer non-acceleratable, but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // Raster is never Stable long enough to accelerate anyway...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        int[] pixels = rasterDB.getData(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int off = rasterDB.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int scanlineStride = ((SinglePixelPackedSampleModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                              raster.getSampleModel()).getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        int adjust = scanlineStride - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    protected abstract void fillRaster(int pixels[], int off, int adjust,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                       int x, int y, int w, int h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Took this cacheRaster code from GradientPaint. It appears to recycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * rasters for use by any other instance, as long as they are sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * large.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    private static synchronized Raster getCachedRaster(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                                                       int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (cm == cachedModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (cached != null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   668
                Raster ras = cached.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (ras != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    ras.getWidth() >= w &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    ras.getHeight() >= h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    cached = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    return ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return cm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Took this cacheRaster code from GradientPaint. It appears to recycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * rasters for use by any other instance, as long as they are sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * large.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    private static synchronized void putCachedRaster(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                                                     Raster ras)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (cached != null) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 21278
diff changeset
   690
            Raster cras = cached.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (cras != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                int cw = cras.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                int ch = cras.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                int iw = ras.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                int ih = ras.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                if (cw >= iw && ch >= ih) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                if (cw * ch >= iw * ih) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        cachedModel = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        cached = new WeakReference<Raster>(ras);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public final void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (saved != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            putCachedRaster(model, saved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            saved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public final ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}