jdk/src/share/classes/sun/java2d/pipe/BufferedBufImgOps.java
author neugens
Fri, 30 Oct 2009 19:19:35 +0100
changeset 4250 e88776b21913
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6896068: SunGraphics2D exposes a reference to itself while non fully initialised. Summary: Introduce a new Interface to mark the Loops based pipes and initialise the loops accordingly. Reviewed-by: flar, rkennke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 sun.java2d.pipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.color.ColorSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.AffineTransformOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.BufferedImageOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.BufferedImageOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.ByteLookupTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.ConvolveOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.Kernel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.LookupOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.LookupTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.image.RescaleOp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.image.ShortLookupTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.java2d.SurfaceData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.java2d.loops.CompositeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static sun.java2d.pipe.BufferedOpCodes.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BufferedBufImgOps {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void enableBufImgOp(RenderQueue rq, SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                      BufferedImage srcImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                      BufferedImageOp biop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (biop instanceof ConvolveOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            enableConvolveOp(rq, srcData, (ConvolveOp)biop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        } else if (biop instanceof RescaleOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            enableRescaleOp(rq, srcData, srcImg, (RescaleOp)biop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } else if (biop instanceof LookupOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            enableLookupOp(rq, srcData, srcImg, (LookupOp)biop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            throw new InternalError("Unknown BufferedImageOp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static void disableBufImgOp(RenderQueue rq, BufferedImageOp biop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (biop instanceof ConvolveOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            disableConvolveOp(rq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else if (biop instanceof RescaleOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            disableRescaleOp(rq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } else if (biop instanceof LookupOp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            disableLookupOp(rq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new InternalError("Unknown BufferedImageOp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/**************************** ConvolveOp support ****************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static boolean isConvolveOpValid(ConvolveOp cop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Kernel kernel = cop.getKernel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int kw = kernel.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int kh = kernel.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // REMIND: we currently can only handle 3x3 and 5x5 kernels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        //         but hopefully this is just a temporary restriction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        //         see native shader comments for more details
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (!(kw == 3 && kh == 3) && !(kw == 5 && kh == 5)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static void enableConvolveOp(RenderQueue rq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                         SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                         ConvolveOp cop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        boolean edgeZero =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Kernel kernel = cop.getKernel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int kernelWidth = kernel.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        int kernelHeight = kernel.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int kernelSize = kernelWidth * kernelHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int sizeofFloat = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        buf.putInt(ENABLE_CONVOLVE_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        buf.putLong(srcData.getNativeOps());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        buf.putInt(edgeZero ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        buf.putInt(kernelWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        buf.putInt(kernelHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        buf.put(kernel.getKernelData(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static void disableConvolveOp(RenderQueue rq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        rq.ensureCapacity(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        buf.putInt(DISABLE_CONVOLVE_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
/**************************** RescaleOp support *****************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static boolean isRescaleOpValid(RescaleOp rop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                           BufferedImage srcImg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int numFactors = rop.getNumFactors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        ColorModel srcCM = srcImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (srcCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                IllegalArgumentException("Rescaling cannot be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                         "performed on an indexed image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (numFactors != 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            numFactors != srcCM.getNumColorComponents() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            numFactors != srcCM.getNumComponents())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new IllegalArgumentException("Number of scaling constants "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                               "does not equal the number of"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                               " of color or color/alpha "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                               " components");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int csType = srcCM.getColorSpace().getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (csType != ColorSpace.TYPE_RGB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            csType != ColorSpace.TYPE_GRAY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Not prepared to deal with other color spaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (numFactors == 2 || numFactors > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // Not really prepared to handle this at the native level, so...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static void enableRescaleOp(RenderQueue rq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                        SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                        BufferedImage srcImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                        RescaleOp rop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ColorModel srcCM = srcImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        boolean nonPremult =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            srcCM.hasAlpha() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            srcCM.isAlphaPremultiplied();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * Note: The user-provided scale factors and offsets are arranged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * in R/G/B/A order, regardless of the raw data order of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * underlying Raster/DataBuffer.  The source image data is ultimately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * converted into RGBA data when uploaded to an OpenGL texture
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * (even for TYPE_GRAY), so the scale factors and offsets are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * in the order expected by the native OpenGL code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * However, the offsets provided by the user are in a range dictated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * by the size of each color/alpha band in the source image.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * example, for 8/8/8 data each offset is in the range [0,255],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * for 5/5/5 data each offset is in the range [0,31], and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         * The OpenGL shader only thinks in terms of [0,1], so below we need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         * to normalize the user-provided offset values into the range [0,1].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int numFactors = rop.getNumFactors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        float[] origScaleFactors = rop.getScaleFactors(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        float[] origOffsets = rop.getOffsets(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // To make things easier, we will always pass all four bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // down to native code...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        float[] normScaleFactors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        float[] normOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (numFactors == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            normScaleFactors = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            normOffsets      = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                normScaleFactors[i] = origScaleFactors[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                normOffsets[i]      = origOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // Leave alpha untouched...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            normScaleFactors[3] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            normOffsets[3]      = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } else if (numFactors == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            normScaleFactors = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            normOffsets      = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                normScaleFactors[i] = origScaleFactors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                normOffsets[i]      = origOffsets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // Leave alpha untouched...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            normScaleFactors[3] = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            normOffsets[3]      = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } else { // (numFactors == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            normScaleFactors = origScaleFactors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            normOffsets      = origOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // The user-provided offsets are specified in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // of each source color band, but the OpenGL shader only wants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // to deal with data in the range [0,1], so we need to normalize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // each offset value to the range [0,1] here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (srcCM.getNumComponents() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Gray data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int nBits = srcCM.getComponentSize(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            int maxValue = (1 << nBits) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            for (int i = 0; i < 3; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                normOffsets[i] /= maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // RGB(A) data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            for (int i = 0; i < srcCM.getNumComponents(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                int nBits = srcCM.getComponentSize(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                int maxValue = (1 << nBits) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                normOffsets[i] /= maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int sizeofFloat = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int totalBytesRequired = 4 + 8 + 4 + (4 * sizeofFloat * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        buf.putInt(ENABLE_RESCALE_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        buf.putLong(srcData.getNativeOps());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        buf.putInt(nonPremult ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        buf.put(normScaleFactors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        buf.put(normOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static void disableRescaleOp(RenderQueue rq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        rq.ensureCapacity(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        buf.putInt(DISABLE_RESCALE_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
/**************************** LookupOp support ******************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static boolean isLookupOpValid(LookupOp lop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                          BufferedImage srcImg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        LookupTable table = lop.getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int numComps = table.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ColorModel srcCM = srcImg.getColorModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (srcCM instanceof IndexColorModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                IllegalArgumentException("LookupOp cannot be "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                         "performed on an indexed image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (numComps != 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            numComps != srcCM.getNumComponents() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            numComps != srcCM.getNumColorComponents())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new IllegalArgumentException("Number of arrays in the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                               " lookup table ("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                               numComps+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                               ") is not compatible with"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                               " the src image: "+srcImg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int csType = srcCM.getColorSpace().getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (csType != ColorSpace.TYPE_RGB &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            csType != ColorSpace.TYPE_GRAY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // Not prepared to deal with other color spaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (numComps == 2 || numComps > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // Not really prepared to handle this at the native level, so...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // The LookupTable spec says that "all arrays must be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // same size" but unfortunately the constructors do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // enforce that.  Also, our native code only works with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        // arrays no larger than 256 elements, so check both of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // these restrictions here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (table instanceof ByteLookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            byte[][] data = ((ByteLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            for (int i = 1; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (data[i].length > 256 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    data[i].length != data[i-1].length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        } else if (table instanceof ShortLookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            short[][] data = ((ShortLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            for (int i = 1; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                if (data[i].length > 256 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    data[i].length != data[i-1].length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private static void enableLookupOp(RenderQueue rq,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                       SurfaceData srcData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                       BufferedImage srcImg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                       LookupOp lop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        boolean nonPremult =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            srcImg.getColorModel().hasAlpha() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            srcImg.isAlphaPremultiplied();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        LookupTable table = lop.getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int numBands = table.getNumComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int offset = table.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        int bandLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int bytesPerElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        boolean shortData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (table instanceof ShortLookupTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            short[][] data = ((ShortLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            bandLength = data[0].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            bytesPerElem = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            shortData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } else { // (table instanceof ByteLookupTable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            byte[][] data = ((ByteLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            bandLength = data[0].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            bytesPerElem = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            shortData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // Adjust the LUT length so that it ends on a 4-byte boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int totalLutBytes = numBands * bandLength * bytesPerElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        int paddedLutBytes = (totalLutBytes + 3) & (~3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        int padding = paddedLutBytes - totalLutBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int totalBytesRequired = 4 + 8 + 20 + paddedLutBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        buf.putInt(ENABLE_LOOKUP_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        buf.putLong(srcData.getNativeOps());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        buf.putInt(nonPremult ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        buf.putInt(shortData ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        buf.putInt(numBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        buf.putInt(bandLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        buf.putInt(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (shortData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            short[][] data = ((ShortLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                buf.put(data[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            byte[][] data = ((ByteLookupTable)table).getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                buf.put(data[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (padding != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            buf.position(buf.position() + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private static void disableLookupOp(RenderQueue rq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // assert rq.lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        RenderBuffer buf = rq.getBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        rq.ensureCapacity(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        buf.putInt(DISABLE_LOOKUP_OP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
}