jdk/src/share/classes/java/awt/TexturePaintContext.java
author art
Mon, 02 Sep 2013 16:48:51 +0400
changeset 20107 18e644411f0b
parent 5506 202f599c92aa
child 23010 6dadb192ad81
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.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.geom.NoninvertibleTransformException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.image.SunWritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.awt.image.IntegerInterleavedRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.awt.image.ByteInterleavedRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
abstract class TexturePaintContext implements PaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static ColorModel xrgbmodel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static ColorModel argbmodel = ColorModel.getRGBdefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    ColorModel colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    int bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    WritableRaster outRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    double xOrg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    double yOrg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    double incXAcross;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    double incYAcross;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    double incXDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    double incYDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int colincx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int colincy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int colincxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int colincyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int rowincx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int rowincy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    int rowincxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int rowincyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static PaintContext getContext(BufferedImage bufImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                          AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                          RenderingHints hints,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                          Rectangle devBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        WritableRaster raster = bufImg.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ColorModel cm = bufImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        int maxw = devBounds.width;
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    76
        Object val = hints.get(RenderingHints.KEY_INTERPOLATION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean filter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            (val == null
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    79
             ? (hints.get(RenderingHints.KEY_RENDERING) == RenderingHints.VALUE_RENDER_QUALITY)
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
    80
             : (val != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (raster instanceof IntegerInterleavedRaster &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            (!filter || isFilterableDCM(cm)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return new Int(iir, cm, xform, maxw, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else if (raster instanceof ByteInterleavedRaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    if (isFilterableICM(cm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        return new ByteFilter(bir, cm, xform, maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    return new Byte(bir, cm, xform, maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return new Any(raster, cm, xform, maxw, filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static boolean isFilterableICM(ColorModel cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (cm instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            IndexColorModel icm = (IndexColorModel) cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (icm.getMapSize() <= 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static boolean isFilterableDCM(ColorModel cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (cm instanceof DirectColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            DirectColorModel dcm = (DirectColorModel) cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return (isMaskOK(dcm.getAlphaMask(), true) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    isMaskOK(dcm.getRedMask(), false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    isMaskOK(dcm.getGreenMask(), false) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    isMaskOK(dcm.getBlueMask(), false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static boolean isMaskOK(int mask, boolean canbezero) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (canbezero && mask == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return (mask == 0xff ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                mask == 0xff00 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                mask == 0xff0000 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                mask == 0xff000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static ColorModel getInternedColorModel(ColorModel cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (xrgbmodel == cm || xrgbmodel.equals(cm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return xrgbmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (argbmodel == cm || argbmodel.equals(cm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return argbmodel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    TexturePaintContext(ColorModel cm, AffineTransform xform,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        int bWidth, int bHeight, int maxw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.colorModel = getInternedColorModel(cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.bWidth = bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.bHeight = bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.maxWidth = maxw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            xform = xform.createInverse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } catch (NoninvertibleTransformException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            xform.setToScale(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.incXAcross = mod(xform.getScaleX(), bWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.incYAcross = mod(xform.getShearY(), bHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.incXDown = mod(xform.getShearX(), bWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.incYDown = mod(xform.getScaleY(), bHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.xOrg = xform.getTranslateX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.yOrg = xform.getTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.colincx = (int) incXAcross;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        this.colincy = (int) incYAcross;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.colincxerr = fractAsInt(incXAcross);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.colincyerr = fractAsInt(incYAcross);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.rowincx = (int) incXDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.rowincy = (int) incYDown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this.rowincxerr = fractAsInt(incXDown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        this.rowincyerr = fractAsInt(incYDown);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static int fractAsInt(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return (int) ((d % 1.0) * Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static double mod(double num, double den) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        num = num % den;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (num < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            num += den;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (num >= den) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // For very small negative numerators, the answer might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // be such a tiny bit less than den that the difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                // is smaller than the mantissa of a double allows and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // the result would then be rounded to den.  If that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // the case then we map that number to 0 as the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                // modulus representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Release the resources allocated for the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        dropRaster(colorModel, outRas);
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
     * Return the ColorModel of the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public ColorModel getColorModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Return a Raster containing the colors generated for the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param x,y,w,h The area in device space for which colors are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public Raster getRaster(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (outRas == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            outRas.getWidth() < w ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            outRas.getHeight() < h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // If h==1, we will probably get lots of "scanline" rects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            outRas = makeRaster((h == 1 ? Math.max(w, maxWidth) : w), h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        double X = mod(xOrg + x * incXAcross + y * incXDown, bWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        double Y = mod(yOrg + x * incYAcross + y * incYDown, bHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        setRaster((int) X, (int) Y, fractAsInt(X), fractAsInt(Y),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                  w, h, bWidth, bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                  colincx, colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                  colincy, colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                  rowincx, rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                  rowincy, rowincyerr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        SunWritableRaster.markDirty(outRas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return outRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   237
    private static WeakReference<Raster> xrgbRasRef;
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   238
    private static WeakReference<Raster> argbRasRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    synchronized static WritableRaster makeRaster(ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                                  Raster srcRas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                                  int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (xrgbmodel == cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (xrgbRasRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                WritableRaster wr = (WritableRaster) xrgbRasRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    xrgbRasRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    return wr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // If we are going to cache this Raster, make it non-tiny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (w <= 32 && h <= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                w = h = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else if (argbmodel == cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (argbRasRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                WritableRaster wr = (WritableRaster) argbRasRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    argbRasRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    return wr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // If we are going to cache this Raster, make it non-tiny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (w <= 32 && h <= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                w = h = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (srcRas != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return srcRas.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return cm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    synchronized static void dropRaster(ColorModel cm, Raster outRas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (outRas == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (xrgbmodel == cm) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   281
            xrgbRasRef = new WeakReference<>(outRas);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } else if (argbmodel == cm) {
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   283
            argbRasRef = new WeakReference<>(outRas);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   287
    private static WeakReference<Raster> byteRasRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    synchronized static WritableRaster makeByteRaster(Raster srcRas,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                      int w, int h)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (byteRasRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            WritableRaster wr = (WritableRaster) byteRasRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (wr != null && wr.getWidth() >= w && wr.getHeight() >= h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                byteRasRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return wr;
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 we are going to cache this Raster, make it non-tiny
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (w <= 32 && h <= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            w = h = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return srcRas.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    synchronized static void dropByteRaster(Raster outRas) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (outRas == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
20107
18e644411f0b 8022184: Fix static , Raw warnings in classes belonging to java.awt
art
parents: 5506
diff changeset
   310
        byteRasRef = new WeakReference<>(outRas);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public abstract WritableRaster makeRaster(int w, int h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public abstract void setRaster(int x, int y, int xerr, int yerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                   int w, int h, int bWidth, int bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                   int colincx, int colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                   int colincy, int colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                   int rowincx, int rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                   int rowincy, int rowincyerr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Blends the four ARGB values in the rgbs array using the factors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * described by xmul and ymul in the following ratio:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *     rgbs[0] * (1-xmul) * (1-ymul) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *     rgbs[1] * (  xmul) * (1-ymul) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *     rgbs[2] * (1-xmul) * (  ymul) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *     rgbs[3] * (  xmul) * (  ymul)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * xmul and ymul are integer values in the half-open range [0, 2^31)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * where 0 == 0.0 and 2^31 == 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Note that since the range is half-open, the values are always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * logically less than 1.0.  This makes sense because while choosing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * pixels to blend, when the error values reach 1.0 we move to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * next pixel and reset them to 0.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static int blend(int rgbs[], int xmul, int ymul) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // xmul/ymul are 31 bits wide, (0 => 2^31-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // shift them to 12 bits wide, (0 => 2^12-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        xmul = (xmul >>> 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        ymul = (ymul >>> 19);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int accumA, accumR, accumG, accumB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        accumA = accumR = accumG = accumB = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            int rgb = rgbs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // The complement of the [xy]mul values (1-[xy]mul) can result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // in new values in the range (1 => 2^12).  Thus for any given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // loop iteration, the values could be anywhere in (0 => 2^12).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            xmul = (1<<12) - xmul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if ((i & 1) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                ymul = (1<<12) - ymul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // xmul and ymul are each 12 bits (0 => 2^12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // factor is thus 24 bits (0 => 2^24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int factor = xmul * ymul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (factor != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                // accum variables will accumulate 32 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                // bytes extracted from rgb fit in 8 bits (0 => 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                // byte * factor thus fits in 32 bits (0 => 255 * 2^24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                accumA += (((rgb >>> 24)       ) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                accumR += (((rgb >>> 16) & 0xff) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                accumG += (((rgb >>>  8) & 0xff) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                accumB += (((rgb       ) & 0xff) * factor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return ((((accumA + (1<<23)) >>> 24) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                (((accumR + (1<<23)) >>> 24) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                (((accumG + (1<<23)) >>> 24) <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                (((accumB + (1<<23)) >>> 24)      ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    static class Int extends TexturePaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        IntegerInterleavedRaster srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int inData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        int outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        int outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        boolean filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        public Int(IntegerInterleavedRaster srcRas, ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                   AffineTransform xform, int maxw, boolean filter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            this.srcRas = srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            this.inData = srcRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            this.inSpan = srcRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            this.inOff = srcRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        public WritableRaster makeRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            WritableRaster ras = makeRaster(colorModel, srcRas, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            outData = iiRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            outSpan = iiRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            outOff = iiRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        public void setRaster(int x, int y, int xerr, int yerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                              int w, int h, int bWidth, int bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                              int colincx, int colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                              int colincy, int colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                              int rowincx, int rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                              int rowincy, int rowincyerr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            int[] inData = this.inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            int[] outData = this.outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            int out = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            int inSpan = this.inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            int inOff = this.inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            int outSpan = this.outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            boolean filter = this.filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            boolean normalx = (colincx == 1 && colincxerr == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                               colincy == 0 && colincyerr == 0) && !filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            int rowx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            int rowy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            int rowxerr = xerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            int rowyerr = yerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (normalx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                outSpan -= w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            int rgbs[] = filter ? new int[4] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if (normalx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    int in = inOff + rowy * inSpan + bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    x = bWidth - rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    out += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    if (bWidth >= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        int i = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        while (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            int copyw = (i < x) ? i : x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            System.arraycopy(inData, in - x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                             outData, out - i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                             copyw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            i -= copyw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            if ((x -= copyw) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                x = bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        for (int i = w; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            outData[out - i] = inData[in - x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                            if (--x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                                x = bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    x = rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    y = rowy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    xerr = rowxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    yerr = rowyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        if (filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            int nextx, nexty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            if ((nextx = x + 1) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                nextx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                            if ((nexty = y + 1) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                                nexty = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                            rgbs[0] = inData[inOff + y * inSpan + x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                            rgbs[1] = inData[inOff + y * inSpan + nextx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            rgbs[2] = inData[inOff + nexty * inSpan + x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                            rgbs[3] = inData[inOff + nexty * inSpan + nextx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            outData[out + i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                TexturePaintContext.blend(rgbs, xerr, yerr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                            outData[out + i] = inData[inOff + y * inSpan + x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        if ((xerr += colincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            xerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                            x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        if ((x += colincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            x -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        if ((yerr += colincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                            yerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            y++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        if ((y += colincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                            y -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                if ((rowxerr += rowincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    rowxerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    rowx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if ((rowx += rowincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    rowx -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if ((rowyerr += rowincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    rowyerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    rowy++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if ((rowy += rowincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    rowy -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                out += outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    static class Byte extends TexturePaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        ByteInterleavedRaster srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        byte inData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        int inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        byte outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        int outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        public Byte(ByteInterleavedRaster srcRas, ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    AffineTransform xform, int maxw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            this.srcRas = srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            this.inData = srcRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            this.inSpan = srcRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            this.inOff = srcRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public WritableRaster makeRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            WritableRaster ras = makeByteRaster(srcRas, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            ByteInterleavedRaster biRas = (ByteInterleavedRaster) ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            outData = biRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            outSpan = biRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            outOff = biRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            dropByteRaster(outRas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        public void setRaster(int x, int y, int xerr, int yerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                              int w, int h, int bWidth, int bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                              int colincx, int colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                              int colincy, int colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                              int rowincx, int rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                              int rowincy, int rowincyerr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            byte[] inData = this.inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            byte[] outData = this.outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            int out = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            int inSpan = this.inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            int inOff = this.inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            int outSpan = this.outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            boolean normalx = (colincx == 1 && colincxerr == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                               colincy == 0 && colincyerr == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            int rowx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            int rowy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            int rowxerr = xerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            int rowyerr = yerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (normalx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                outSpan -= w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                if (normalx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    int in = inOff + rowy * inSpan + bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    x = bWidth - rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    out += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (bWidth >= 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        int i = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        while (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                            int copyw = (i < x) ? i : x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                            System.arraycopy(inData, in - x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                             outData, out - i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                             copyw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                            i -= copyw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                            if ((x -= copyw) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                x = bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        for (int i = w; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            outData[out - i] = inData[in - x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            if (--x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                x = bWidth;
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
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    x = rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    y = rowy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    xerr = rowxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    yerr = rowyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        outData[out + i] = inData[inOff + y * inSpan + x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        if ((xerr += colincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            xerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        if ((x += colincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            x -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        if ((yerr += colincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                            yerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                            y++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        if ((y += colincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                            y -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                if ((rowxerr += rowincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    rowxerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    rowx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                if ((rowx += rowincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    rowx -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if ((rowyerr += rowincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    rowyerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    rowy++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                if ((rowy += rowincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    rowy -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                out += outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    static class ByteFilter extends TexturePaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        ByteInterleavedRaster srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        int inPalette[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        byte inData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        int inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        int inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int outData[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        int outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        public ByteFilter(ByteInterleavedRaster srcRas, ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                          AffineTransform xform, int maxw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            super((cm.getTransparency() == Transparency.OPAQUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                   ? xrgbmodel : argbmodel),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                  xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            this.inPalette = new int[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            ((IndexColorModel) cm).getRGBs(this.inPalette);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            this.srcRas = srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            this.inData = srcRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            this.inSpan = srcRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            this.inOff = srcRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        public WritableRaster makeRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // Note that we do not pass srcRas to makeRaster since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // is a Byte Raster and this colorModel needs an Int Raster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            WritableRaster ras = makeRaster(colorModel, null, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            IntegerInterleavedRaster iiRas = (IntegerInterleavedRaster) ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            outData = iiRas.getDataStorage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            outSpan = iiRas.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            outOff = iiRas.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return ras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        public void setRaster(int x, int y, int xerr, int yerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                              int w, int h, int bWidth, int bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                              int colincx, int colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                              int colincy, int colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                              int rowincx, int rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                              int rowincy, int rowincyerr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            byte[] inData = this.inData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            int[] outData = this.outData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            int out = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            int inSpan = this.inSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            int inOff = this.inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            int outSpan = this.outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            int rowx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            int rowy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            int rowxerr = xerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            int rowyerr = yerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            int rgbs[] = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                x = rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                y = rowy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                xerr = rowxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                yerr = rowyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    int nextx, nexty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    if ((nextx = x + 1) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        nextx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    if ((nexty = y + 1) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        nexty = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    rgbs[0] = inPalette[0xff & inData[inOff + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                                      inSpan * y]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    rgbs[1] = inPalette[0xff & inData[inOff + nextx +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                                                      inSpan * y]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    rgbs[2] = inPalette[0xff & inData[inOff + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                                      inSpan * nexty]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    rgbs[3] = inPalette[0xff & inData[inOff + nextx +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                                      inSpan * nexty]];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    outData[out + i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        TexturePaintContext.blend(rgbs, xerr, yerr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    if ((xerr += colincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                        xerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    if ((x += colincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        x -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    if ((yerr += colincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        yerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        y++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    if ((y += colincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        y -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                if ((rowxerr += rowincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    rowxerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    rowx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                if ((rowx += rowincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    rowx -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if ((rowyerr += rowincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    rowyerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    rowy++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                if ((rowy += rowincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    rowy -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                out += outSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    static class Any extends TexturePaintContext {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        WritableRaster srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        boolean filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        public Any(WritableRaster srcRas, ColorModel cm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                   AffineTransform xform, int maxw, boolean filter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            super(cm, xform, srcRas.getWidth(), srcRas.getHeight(), maxw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            this.srcRas = srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        public WritableRaster makeRaster(int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return makeRaster(colorModel, srcRas, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        public void setRaster(int x, int y, int xerr, int yerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                              int w, int h, int bWidth, int bHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                              int colincx, int colincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                              int colincy, int colincyerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                              int rowincx, int rowincxerr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                              int rowincy, int rowincyerr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            Object data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            int rowx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int rowy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            int rowxerr = xerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            int rowyerr = yerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            WritableRaster srcRas = this.srcRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            WritableRaster outRas = this.outRas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            int rgbs[] = filter ? new int[4] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            for (int j = 0; j < h; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                x = rowx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                y = rowy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                xerr = rowxerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                yerr = rowyerr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                for (int i = 0; i < w; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    data = srcRas.getDataElements(x, y, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    if (filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        int nextx, nexty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        if ((nextx = x + 1) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                            nextx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        if ((nexty = y + 1) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            nexty = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        rgbs[0] = colorModel.getRGB(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        data = srcRas.getDataElements(nextx, y, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        rgbs[1] = colorModel.getRGB(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                        data = srcRas.getDataElements(x, nexty, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        rgbs[2] = colorModel.getRGB(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                        data = srcRas.getDataElements(nextx, nexty, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        rgbs[3] = colorModel.getRGB(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                        int rgb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                            TexturePaintContext.blend(rgbs, xerr, yerr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                        data = colorModel.getDataElements(rgb, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    outRas.setDataElements(i, j, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    if ((xerr += colincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                        xerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    if ((x += colincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        x -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    if ((yerr += colincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        yerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        y++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    if ((y += colincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                        y -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                if ((rowxerr += rowincxerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    rowxerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    rowx++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if ((rowx += rowincx) >= bWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    rowx -= bWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                if ((rowyerr += rowincyerr) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    rowyerr &= Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    rowy++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                if ((rowy += rowincy) >= bHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    rowy -= bHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
}