jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSImageLayout.java
author bae
Tue, 19 Feb 2013 12:06:28 +0400
changeset 16899 666a37b19844
parent 14884 74d1acdb7ee4
child 16900 5e0400ee2917
permissions -rw-r--r--
8007675: Improve color conversion Reviewed-by: prr, mschoene, jgodinez
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) 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 sun.java2d.cmm.lcms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.image.ComponentColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.image.ComponentSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.DataBufferUShort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.DataBufferInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.ColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.awt.image.ByteComponentRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.image.ShortComponentRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.image.IntegerComponentRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class LCMSImageLayout {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static int BYTES_SH(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static int EXTRA_SH(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return x<<7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static int CHANNELS_SH(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return x<<3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int SWAPFIRST   = 1<<14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int DOSWAP      = 1<<10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int PT_RGB_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        CHANNELS_SH(3) | BYTES_SH(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final int PT_GRAY_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        CHANNELS_SH(1) | BYTES_SH(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int PT_GRAY_16 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        CHANNELS_SH(1) | BYTES_SH(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int PT_RGBA_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int PT_ARGB_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1) | SWAPFIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int PT_BGR_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        DOSWAP | CHANNELS_SH(3) | BYTES_SH(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int PT_ABGR_8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        DOSWAP | EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        BYTES_SH(1) | DOSWAP | SWAPFIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final int DT_BYTE     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static final int DT_SHORT    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final int DT_INT      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final int DT_DOUBLE   = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    boolean isIntPacked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    int pixelType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    int dataType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int nextRowOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    Object dataArray;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   102
    private int dataArrayLength; /* in bytes */
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   103
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   104
    private LCMSImageLayout(int np, int pixelType, int pixelSize)
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   105
            throws ImageLayoutException
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   106
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.pixelType = pixelType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        width = np;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        height = 1;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   110
        nextRowOffset = safeMult(pixelSize, np);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        offset = 0;
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 LCMSImageLayout(int width, int height, int pixelType,
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   115
                            int pixelSize)
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   116
            throws ImageLayoutException
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   117
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.pixelType = pixelType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.width = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.height = height;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   121
        nextRowOffset = safeMult(pixelSize, width);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   126
    public LCMSImageLayout(byte[] data, int np, int pixelType, int pixelSize)
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   127
            throws ImageLayoutException
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   128
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this(np, pixelType, pixelSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        dataType = DT_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        dataArray = data;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   132
        dataArrayLength = data.length;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   133
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   134
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   137
    public LCMSImageLayout(short[] data, int np, int pixelType, int pixelSize)
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   138
            throws ImageLayoutException
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   139
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this(np, pixelType, pixelSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        dataType = DT_SHORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        dataArray = data;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   143
        dataArrayLength = 2 * data.length;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   144
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   145
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   148
    public LCMSImageLayout(int[] data, int np, int pixelType, int pixelSize)
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   149
            throws ImageLayoutException
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   150
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(np, pixelType, pixelSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        dataType = DT_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        dataArray = data;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   154
        dataArrayLength = 4 * data.length;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   155
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   156
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public LCMSImageLayout(double[] data, int np, int pixelType, int pixelSize)
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   160
            throws ImageLayoutException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this(np, pixelType, pixelSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        dataType = DT_DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        dataArray = data;
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   165
        dataArrayLength = 8 * data.length;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   166
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   167
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   170
    public LCMSImageLayout(BufferedImage image) throws ImageLayoutException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        ShortComponentRaster shortRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        IntegerComponentRaster intRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        ByteComponentRaster byteRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        switch (image.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            case BufferedImage.TYPE_INT_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                pixelType = PT_ARGB_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                isIntPacked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            case BufferedImage.TYPE_INT_ARGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                pixelType = PT_ARGB_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                isIntPacked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            case BufferedImage.TYPE_INT_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                pixelType = PT_ABGR_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                isIntPacked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            case BufferedImage.TYPE_3BYTE_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                pixelType = PT_BGR_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            case BufferedImage.TYPE_4BYTE_ABGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                pixelType = PT_ABGR_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            case BufferedImage.TYPE_BYTE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                pixelType = PT_GRAY_8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            case BufferedImage.TYPE_USHORT_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                pixelType = PT_GRAY_16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // TODO: Add support for some images having
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // SinglePixelPackedModel and ComponentSampleModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    "CMMImageLayout - bad image type passed to constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        width = image.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        height = image.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        switch (image.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            case BufferedImage.TYPE_INT_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            case BufferedImage.TYPE_INT_ARGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            case BufferedImage.TYPE_INT_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                intRaster = (IntegerComponentRaster)image.getRaster();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   214
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   215
                nextRowOffset = safeMult(4, intRaster.getScanlineStride());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   216
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   217
                offset = safeMult(4, intRaster.getDataOffset(0));
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   218
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                dataArray = intRaster.getDataStorage();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   220
                dataArrayLength = 4 * intRaster.getDataStorage().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                dataType = DT_INT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            case BufferedImage.TYPE_3BYTE_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            case BufferedImage.TYPE_4BYTE_ABGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                byteRaster = (ByteComponentRaster)image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                nextRowOffset = byteRaster.getScanlineStride();
14884
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 5506
diff changeset
   228
                int firstBand = image.getSampleModel().getNumBands() - 1;
74d1acdb7ee4 7124245: [lcms] ColorConvertOp to color space CS_GRAY apparently converts orange to 244,244,0
bae
parents: 5506
diff changeset
   229
                offset = byteRaster.getDataOffset(firstBand);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                dataArray = byteRaster.getDataStorage();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   231
                dataArrayLength = byteRaster.getDataStorage().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                dataType = DT_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            case BufferedImage.TYPE_BYTE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                byteRaster = (ByteComponentRaster)image.getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                nextRowOffset = byteRaster.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                offset = byteRaster.getDataOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                dataArray = byteRaster.getDataStorage();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   240
                dataArrayLength = byteRaster.getDataStorage().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                dataType = DT_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            case BufferedImage.TYPE_USHORT_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                shortRaster = (ShortComponentRaster)image.getRaster();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   246
                nextRowOffset = safeMult(2, shortRaster.getScanlineStride());
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   247
                offset = safeMult(2, shortRaster.getDataOffset(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                dataArray = shortRaster.getDataStorage();
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   249
                dataArrayLength = 2 * shortRaster.getDataStorage().length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                dataType = DT_SHORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   253
        verify();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public static boolean isSupported(BufferedImage image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        switch (image.getType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            case BufferedImage.TYPE_INT_RGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            case BufferedImage.TYPE_INT_ARGB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            case BufferedImage.TYPE_INT_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            case BufferedImage.TYPE_3BYTE_BGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            case BufferedImage.TYPE_4BYTE_ABGR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            case BufferedImage.TYPE_BYTE_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            case BufferedImage.TYPE_USHORT_GRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
16899
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   269
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   270
    private void verify() throws ImageLayoutException {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   271
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   272
        if (offset < 0 || offset >= dataArrayLength) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   273
            throw new ImageLayoutException("Invalid image layout");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   274
        }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   275
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   276
        int lastPixelOffset = safeMult(nextRowOffset, (height - 1));
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   277
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   278
        lastPixelOffset = safeAdd(lastPixelOffset, (width - 1));
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   279
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   280
        int off = safeAdd(offset, lastPixelOffset);
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   281
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   282
        if (off < 0 || off >= dataArrayLength) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   283
            throw new ImageLayoutException("Invalid image layout");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   284
        }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   285
    }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   286
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   287
    static int safeAdd(int a, int b) throws ImageLayoutException {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   288
        long res = a;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   289
        res += b;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   290
        if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   291
            throw new ImageLayoutException("Invalid image layout");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   292
        }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   293
        return (int)res;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   294
    }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   295
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   296
    static int safeMult(int a, int b) throws ImageLayoutException {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   297
        long res = a;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   298
        res *= b;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   299
        if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   300
            throw new ImageLayoutException("Invalid image layout");
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   301
        }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   302
        return (int)res;
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   303
    }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   304
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   305
    public static class ImageLayoutException extends Exception {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   306
        public ImageLayoutException(String message) {
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   307
            super(message);
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   308
        }
666a37b19844 8007675: Improve color conversion
bae
parents: 14884
diff changeset
   309
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}