jdk/src/share/classes/java/awt/GradientPaintContext.java
author art
Mon, 02 Sep 2013 16:48:51 +0400
changeset 20107 18e644411f0b
parent 5506 202f599c92aa
child 22584 eed64ee05369
permissions -rw-r--r--
8022184: Fix static , Raw warnings in classes belonging to java.awt Reviewed-by: art, anthony Contributed-by: Srikalyan Chandrashekar <srikalyan.chandrashekar@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, 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.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.awt.image.IntegerComponentRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class GradientPaintContext implements PaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static ColorModel xrgbmodel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static ColorModel xbgrmodel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        new DirectColorModel(24, 0x000000ff, 0x0000ff00, 0x00ff0000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static ColorModel cachedModel;
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    44
    static WeakReference<Raster> cached;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (cm == cachedModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (cached != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                Raster ras = (Raster) cached.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                if (ras != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                    ras.getWidth() >= w &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    ras.getHeight() >= h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    cached = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    return ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return cm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (cached != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            Raster cras = (Raster) cached.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (cras != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                int cw = cras.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                int ch = cras.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                int iw = ras.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                int ih = ras.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (cw >= iw && ch >= ih) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                if (cw * ch >= iw * ih) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        cachedModel = cm;
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    79
        cached = new WeakReference<>(ras);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    double x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    double y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    double dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    double dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    boolean cyclic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    int interp[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    Raster saved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    ColorModel model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public GradientPaintContext(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                Point2D p1, Point2D p2, AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                Color c1, Color c2, boolean cyclic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // First calculate the distance moved in user space when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // we move a single unit along the X & Y axes in device space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Point2D xvec = new Point2D.Double(1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Point2D yvec = new Point2D.Double(0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            AffineTransform inverse = xform.createInverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            inverse.deltaTransform(xvec, xvec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            inverse.deltaTransform(yvec, yvec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (NoninvertibleTransformException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            xvec.setLocation(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            yvec.setLocation(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Now calculate the (square of the) user space distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // between the anchor points. This value equals:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        //     (UserVec . UserVec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        double udx = p2.getX() - p1.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        double udy = p2.getY() - p1.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        double ulenSq = udx * udx + udy * udy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (ulenSq <= Double.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            dy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // Now calculate the proportional distance moved along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // vector from p1 to p2 when we move a unit along X & Y in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // device space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // The length of the projection of the Device Axis Vector is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // its dot product with the Unit User Vector:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            //     (DevAxisVec . (UserVec / Len(UserVec))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            // The "proportional" length is that length divided again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // by the length of the User Vector:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            //     (DevAxisVec . (UserVec / Len(UserVec))) / Len(UserVec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // which simplifies to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            //     ((DevAxisVec . UserVec) / Len(UserVec)) / Len(UserVec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // which simplifies to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            //     (DevAxisVec . UserVec) / LenSquared(UserVec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            dx = (xvec.getX() * udx + xvec.getY() * udy) / ulenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            dy = (yvec.getX() * udx + yvec.getY() * udy) / ulenSq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (cyclic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                dx = dx % 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                dy = dy % 1.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // We are acyclic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (dx < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    // If we are using the acyclic form below, we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    // dx to be non-negative for simplicity of scanning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    // across the scan lines for the transition points.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    // To ensure that constraint, we negate the dx/dy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    // values and swap the points and colors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    Point2D p = p1; p1 = p2; p2 = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    Color c = c1; c1 = c2; c2 = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    dx = -dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    dy = -dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Point2D dp1 = xform.transform(p1, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.x1 = dp1.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.y1 = dp1.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.cyclic = cyclic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int rgb1 = c1.getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int rgb2 = c2.getRGB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        int a1 = (rgb1 >> 24) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int r1 = (rgb1 >> 16) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int g1 = (rgb1 >>  8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int b1 = (rgb1      ) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int da = ((rgb2 >> 24) & 0xff) - a1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int dr = ((rgb2 >> 16) & 0xff) - r1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        int dg = ((rgb2 >>  8) & 0xff) - g1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int db = ((rgb2      ) & 0xff) - b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (a1 == 0xff && da == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            model = xrgbmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (cm instanceof DirectColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                DirectColorModel dcm = (DirectColorModel) cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                int tmp = dcm.getAlphaMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if ((tmp == 0 || tmp == 0xff) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    dcm.getRedMask() == 0xff &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    dcm.getGreenMask() == 0xff00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    dcm.getBlueMask() == 0xff0000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    model = xbgrmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    tmp = r1; r1 = b1; b1 = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    tmp = dr; dr = db; db = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            model = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        interp = new int[cyclic ? 513 : 257];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        for (int i = 0; i <= 256; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            float rel = i / 256.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int rgb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                (((int) (a1 + da * rel)) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                (((int) (r1 + dr * rel)) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                (((int) (g1 + dg * rel)) <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                (((int) (b1 + db * rel))      );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            interp[i] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (cyclic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                interp[512 - i] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Release the resources allocated for the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (saved != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            putCachedRaster(model, saved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            saved = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Return the ColorModel of the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Return a Raster containing the colors generated for the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param x,y,w,h The area in device space for which colors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public Raster getRaster(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        double rowrel = (x - x1) * dx + (y - y1) * dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Raster rast = saved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (rast == null || rast.getWidth() < w || rast.getHeight() < h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            rast = getCachedRaster(model, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            saved = rast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        IntegerComponentRaster irast = (IntegerComponentRaster) rast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int off = irast.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        int adjust = irast.getScanlineStride() - w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int[] pixels = irast.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (cyclic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            cycleFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            clipFillRaster(pixels, off, adjust, w, h, rowrel, dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        irast.markDirty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return rast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    void cycleFillRaster(int[] pixels, int off, int adjust, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         double rowrel, double dx, double dy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        rowrel = rowrel % 2.0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int irowrel = ((int) (rowrel * (1 << 30))) << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int idx = (int) (-dx * (1 << 31));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int idy = (int) (-dy * (1 << 31));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        while (--h >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int icolrel = irowrel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            for (int j = w; j > 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                pixels[off++] = interp[icolrel >>> 23];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                icolrel += idx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            off += adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            irowrel += idy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void clipFillRaster(int[] pixels, int off, int adjust, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        double rowrel, double dx, double dy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        while (--h >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            double colrel = rowrel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            int j = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (colrel <= 0.0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                int rgb = interp[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    pixels[off++] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    colrel += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                } while (--j > 0 && colrel <= 0.0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            while (colrel < 1.0 && --j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                pixels[off++] = interp[(int) (colrel * 256)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                colrel += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (j > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                int rgb = interp[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    pixels[off++] = rgb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                } while (--j > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            off += adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            rowrel += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
}