jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/common/ImageUtil.java
author bpb
Mon, 23 Nov 2015 12:26:12 -0800
changeset 34416 68c0d866db5d
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8143342: Integrate Java Image I/O support for TIFF per JEP 262 Summary: Port TIFF reader and writer plugins from JAI Image I/O Tools to JDK 9 Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34416
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, 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 com.sun.imageio.plugins.common;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.color.ColorSpace;
34416
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
    32
import java.awt.color.ICC_ColorSpace;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.image.BufferedImage;
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.ComponentColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.image.ComponentSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.image.DataBufferByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.image.DataBufferInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.image.DataBufferShort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.image.DataBufferUShort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.image.DirectColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.MultiPixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.Raster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.RenderedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.image.SampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.image.SinglePixelPackedSampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Arrays;
34416
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
    51
import java.util.Iterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.imageio.IIOImage;
34416
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
    54
import javax.imageio.ImageReadParam;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.imageio.ImageTypeSpecifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.imageio.spi.ImageWriterSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class ImageUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates a <code>ColorModel</code> that may be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * specified <code>SampleModel</code>.  If a suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * <code>ColorModel</code> cannot be found, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <p> Suitable <code>ColorModel</code>s are guaranteed to exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * for all instances of <code>ComponentSampleModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * For 1- and 3- banded <code>SampleModel</code>s, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <code>ColorModel</code> will be opaque.  For 2- and 4-banded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>SampleModel</code>s, the output will use alpha transparency
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * which is not premultiplied.  1- and 2-banded data will use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * grayscale <code>ColorSpace</code>, and 3- and 4-banded data a sRGB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <code>ColorSpace</code>. Data with 5 or more bands will have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>BogusColorSpace</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p>An instance of <code>DirectColorModel</code> will be created for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * instances of <code>SinglePixelPackedSampleModel</code> with no more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * than 4 bands.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>An instance of <code>IndexColorModel</code> will be created for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * instances of <code>MultiPixelPackedSampleModel</code>. The colormap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * will be a grayscale ramp with <code>1&nbsp;<<&nbsp;numberOfBits</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * entries ranging from zero to at most 255.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return An instance of <code>ColorModel</code> that is suitable for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *         the supplied <code>SampleModel</code>, or <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @throws IllegalArgumentException  If <code>sampleModel</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *         <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final ColorModel createColorModel(SampleModel sampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // Check the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if(sampleModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException("sampleModel == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Get the data type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int dataType = sampleModel.getDataType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // Check the data type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        switch(dataType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        case DataBuffer.TYPE_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        case DataBuffer.TYPE_USHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        case DataBuffer.TYPE_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        case DataBuffer.TYPE_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        case DataBuffer.TYPE_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        case DataBuffer.TYPE_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            // Return null for other types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return 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
        // The return variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ColorModel colorModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // Get the sample size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int[] sampleSize = sampleModel.getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // Create a Component ColorModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if(sampleModel instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // Get the number of bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int numBands = sampleModel.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // Determine the color space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ColorSpace colorSpace = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if(numBands <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if(numBands <= 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                colorSpace = new BogusColorSpace(numBands);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            boolean hasAlpha = (numBands == 2) || (numBands == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            boolean isAlphaPremultiplied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int transparency = hasAlpha ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Transparency.TRANSLUCENT : Transparency.OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            colorModel = new ComponentColorModel(colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                 sampleSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                 hasAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                 isAlphaPremultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                 transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                 dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (sampleModel.getNumBands() <= 4 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                   sampleModel instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                (SinglePixelPackedSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            int[] bitMasks = sppsm.getBitMasks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            int rmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int gmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            int bmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            int amask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            int numBands = bitMasks.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (numBands <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                rmask = gmask = bmask = bitMasks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (numBands == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    amask = bitMasks[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                rmask = bitMasks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                gmask = bitMasks[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                bmask = bitMasks[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (numBands == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    amask = bitMasks[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (int i = 0; i < sampleSize.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                bits += sampleSize[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return new DirectColorModel(bits, rmask, gmask, bmask, amask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } else if(sampleModel instanceof MultiPixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // Load the colormap with a ramp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int bitsPerSample = sampleSize[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int numEntries = 1 << bitsPerSample;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            byte[] map = new byte[numEntries];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            for (int i = 0; i < numEntries; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                map[i] = (byte)(i*255/(numEntries - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            colorModel = new IndexColorModel(bitsPerSample, numEntries,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                             map, map, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * For the case of binary data (<code>isBinary()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>true</code>), return the binary data as a packed byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * The data will be packed as eight bits per byte with no bit offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * i.e., the first bit in each image line will be the left-most of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * first byte of the line.  The line stride in bytes will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <code>(int)((getWidth()+7)/8)</code>.  The length of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * array will be the line stride multiplied by <code>getHeight()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the binary data as a packed array of bytes with zero offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * of <code>null</code> if the data are not binary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <code>false</code> with the <code>SampleModel</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * supplied <code>Raster</code> as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public static byte[] getPackedBinaryData(Raster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                             Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if(!isBinary(sm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int rectX = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        int rectY = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int rectWidth = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int rectHeight = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        DataBuffer dataBuffer = raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int dx = rectX - raster.getSampleModelTranslateX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int dy = rectY - raster.getSampleModelTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int lineStride = mpp.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int bitOffset = mpp.getBitOffset(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int numBytesPerRow = (rectWidth + 7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if(dataBuffer instanceof DataBufferByte &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
           eltOffset == 0 && bitOffset == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
           numBytesPerRow == lineStride &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
           ((DataBufferByte)dataBuffer).getData().length ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
           numBytesPerRow*rectHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        byte[] binaryDataArray = new byte[numBytesPerRow*rectHeight];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if(bitOffset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                int stride = numBytesPerRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    System.arraycopy(data, eltOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                     binaryDataArray, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                     stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    offset += stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                      dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    while(xRemaining > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        short datum = data[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        binaryDataArray[b++] = (byte)((datum >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        binaryDataArray[b++] = (byte)(datum & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        xRemaining -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    if(xRemaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        binaryDataArray[b++] = (byte)((data[i] >>> 8) & 0XFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    while(xRemaining > 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        int datum = data[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        binaryDataArray[b++] = (byte)((datum >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        binaryDataArray[b++] = (byte)((datum >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        binaryDataArray[b++] = (byte)((datum >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        binaryDataArray[b++] = (byte)(datum & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        xRemaining -= 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    int shift = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    while(xRemaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            (byte)((data[i] >>> shift) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        shift -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        xRemaining -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else { // bitOffset != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if((bitOffset & 7) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    int stride = numBytesPerRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        System.arraycopy(data, eltOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                         binaryDataArray, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                         stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        offset += stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                } else { // bitOffset % 8 != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    int leftShift = bitOffset & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    int rightShift = 8 - leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        while(xRemaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                            if(xRemaining > rightShift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                    (byte)(((data[i++]&0xFF) << leftShift) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                           ((data[i]&0xFF) >>> rightShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                    (byte)((data[i]&0xFF) << leftShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            xRemaining -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                      dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    int bOffset = bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    for(int x = 0; x < rectWidth; x += 8, bOffset += 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        int i = eltOffset + bOffset/16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        int mod = bOffset % 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        int left = data[i] & 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        if(mod <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            binaryDataArray[b++] = (byte)(left >>> (8 - mod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            int delta = mod - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                            int right = data[i+1] & 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                            binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                (byte)((left << delta) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                       (right >>> (16 - delta)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    int bOffset = bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    for(int x = 0; x < rectWidth; x += 8, bOffset += 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                        int i = eltOffset + bOffset/32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        int mod = bOffset % 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        int left = data[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        if(mod <= 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                (byte)(left >>> (24 - mod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                            int delta = mod - 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            int right = data[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                            binaryDataArray[b++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                (byte)((left << delta) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                       (right >>> (32 - delta)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return binaryDataArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Returns the binary data unpacked into an array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * The line stride will be the width of the <code>Raster</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>false</code> with the <code>SampleModel</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * supplied <code>Raster</code> as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public static byte[] getUnpackedBinaryData(Raster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                               Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if(!isBinary(sm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        int rectX = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        int rectY = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int rectWidth = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        int rectHeight = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        DataBuffer dataBuffer = raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int dx = rectX - raster.getSampleModelTranslateX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int dy = rectY - raster.getSampleModelTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int lineStride = mpp.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int bitOffset = mpp.getBitOffset(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        byte[] bdata = new byte[rectWidth*rectHeight];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        int maxY = rectY + rectHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int maxX = rectX + rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            for(int y = rectY; y < maxY; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                int bOffset = eltOffset*8 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                for(int x = rectX; x < maxX; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    byte b = data[bOffset/8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    bdata[k++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        (byte)((b >>> (7 - bOffset & 7)) & 0x0000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                  dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            for(int y = rectY; y < maxY; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                int bOffset = eltOffset*16 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                for(int x = rectX; x < maxX; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    short s = data[bOffset/16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    bdata[k++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        (byte)((s >>> (15 - bOffset % 16)) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                               0x0000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            for(int y = rectY; y < maxY; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                int bOffset = eltOffset*32 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                for(int x = rectX; x < maxX; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    int i = data[bOffset/32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    bdata[k++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        (byte)((i >>> (31 - bOffset % 32)) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                               0x0000001);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return bdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Sets the supplied <code>Raster</code>'s data from an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * of packed binary data of the form returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <code>getPackedBinaryData()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <code>false</code> with the <code>SampleModel</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * supplied <code>Raster</code> as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public static void setPackedBinaryData(byte[] binaryDataArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                           WritableRaster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                           Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if(!isBinary(sm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int rectX = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        int rectY = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        int rectWidth = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        int rectHeight = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        DataBuffer dataBuffer = raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        int dx = rectX - raster.getSampleModelTranslateX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        int dy = rectY - raster.getSampleModelTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        int lineStride = mpp.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int bitOffset = mpp.getBitOffset(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        int b = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if(bitOffset == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if(data == binaryDataArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    // Optimal case: simply return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                int stride = (rectWidth + 7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    System.arraycopy(binaryDataArray, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                     data, eltOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                     stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    offset += stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                      dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    while(xRemaining > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                        data[i++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            (short)(((binaryDataArray[b++] & 0xFF) << 8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                                    (binaryDataArray[b++] & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        xRemaining -= 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    if(xRemaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        data[i++] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                            (short)((binaryDataArray[b++] & 0xFF) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    while(xRemaining > 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        data[i++] =
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   548
                            (((binaryDataArray[b++] & 0xFF) << 24) |
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   549
                             ((binaryDataArray[b++] & 0xFF) << 16) |
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   550
                             ((binaryDataArray[b++] & 0xFF) << 8) |
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   551
                             (binaryDataArray[b++] & 0xFF));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        xRemaining -= 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    int shift = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    while(xRemaining > 0) {
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   556
                        data[i] |= ((binaryDataArray[b++] & 0xFF) << shift);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        shift -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                        xRemaining -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } else { // bitOffset != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            int stride = (rectWidth + 7)/8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if((bitOffset & 7) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        System.arraycopy(binaryDataArray, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                         data, eltOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                         stride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        offset += stride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                } else { // bitOffset % 8 != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    int rightShift = bitOffset & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    int leftShift = 8 - rightShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    int leftShift8 = 8 + leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    int mask = (byte)(255<<leftShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    int mask1 = (byte)~mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        int i = eltOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        while(xRemaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                            byte datum = binaryDataArray[b++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                            if (xRemaining > leftShift8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                // when all the bits in this BYTE will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                // into the data buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                                data[i] = (byte)((data[i] & mask ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                    ((datum&0xFF) >>> rightShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                data[++i] = (byte)((datum & 0xFF) << leftShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            } else if (xRemaining > leftShift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                // All the "leftShift" high bits will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                                // into the data buffer.  But not all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                // "rightShift" low bits will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                data[i] = (byte)((data[i] & mask ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                    ((datum&0xFF) >>> rightShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                data[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                    (byte)((data[i] & mask1) | ((datum & 0xFF) << leftShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                // Less than "leftShift" high bits will be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                int remainMask = (1 << leftShift - xRemaining) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                data[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                    (byte)((data[i] & (mask | remainMask)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                                    (datum&0xFF) >>> rightShift & ~remainMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                            xRemaining -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                      dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                int rightShift = bitOffset & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                int leftShift = 8 - rightShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                int leftShift16 = 16 + leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                int mask = (short)(~(255 << leftShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                int mask1 = (short)(65535 << leftShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                int mask2 = (short)~mask1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    int bOffset = bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    for(int x = 0; x < rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        x += 8, bOffset += 8, xRemaining -= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        int i = eltOffset + (bOffset >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        int mod = bOffset & 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        int datum = binaryDataArray[b++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                        if(mod <= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                            // This BYTE is set into one SHORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                            if (xRemaining < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                // Mask the bits to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                datum &= 255 << 8 - xRemaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                            data[i] = (short)((data[i] & mask) | (datum << leftShift));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        } else if (xRemaining > leftShift16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                            // This BYTE will be set into two SHORTs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            data[i] = (short)((data[i] & mask1) | ((datum >>> rightShift)&0xFFFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                            data[++i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                (short)((datum << leftShift)&0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        } else if (xRemaining > leftShift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            // This BYTE will be set into two SHORTs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            // But not all the low bits will be set into SHORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                            data[i] = (short)((data[i] & mask1) | ((datum >>> rightShift)&0xFFFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                            data[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                (short)((data[i] & mask2) | ((datum << leftShift)&0xFFFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                            // Only some of the high bits will be set into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                            // SHORTs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                            int remainMask = (1 << leftShift - xRemaining) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            data[i] = (short)((data[i] & (mask1 | remainMask)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                      ((datum >>> rightShift)&0xFFFF & ~remainMask));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                int rightShift = bitOffset & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                int leftShift = 8 - rightShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                int leftShift32 = 32 + leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                int mask = 0xFFFFFFFF << leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                int mask1 = ~mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    int bOffset = bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    int xRemaining = rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    for(int x = 0; x < rectWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        x += 8, bOffset += 8, xRemaining -= 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        int i = eltOffset + (bOffset >> 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        int mod = bOffset & 31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        int datum = binaryDataArray[b++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        if(mod <= 24) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            // This BYTE is set into one INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                            int shift = 24 - mod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            if (xRemaining < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                // Mask the bits to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                datum &= 255 << 8 - xRemaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                            data[i] = (data[i] & (~(255 << shift))) | (datum << shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        } else if (xRemaining > leftShift32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                            // All the bits of this BYTE will be set into two INTs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                            data[i] = (data[i] & mask) | (datum >>> rightShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                            data[++i] = datum << leftShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        } else if (xRemaining > leftShift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            // This BYTE will be set into two INTs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            // But not all the low bits will be set into INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                            data[i] = (data[i] & mask) | (datum >>> rightShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            data[i] = (data[i] & mask1) | (datum << leftShift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            // Only some of the high bits will be set into INT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            int remainMask = (1 << leftShift - xRemaining) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            data[i] = (data[i] & (mask | remainMask)) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                      (datum >>> rightShift & ~remainMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Copies data into the packed array of the <code>Raster</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * from an array of unpacked data of the form returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>getUnpackedBinaryData()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p> If the data are binary, then the target bit will be set if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * and only if the corresponding byte is non-zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @throws IllegalArgumentException if <code>isBinary()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <code>false</code> with the <code>SampleModel</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * supplied <code>Raster</code> as argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public static void setUnpackedBinaryData(byte[] bdata,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                             WritableRaster raster,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                             Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        SampleModel sm = raster.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if(!isBinary(sm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw new IllegalArgumentException(I18N.getString("ImageUtil0"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        int rectX = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        int rectY = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        int rectWidth = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        int rectHeight = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        DataBuffer dataBuffer = raster.getDataBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        int dx = rectX - raster.getSampleModelTranslateX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        int dy = rectY - raster.getSampleModelTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        MultiPixelPackedSampleModel mpp = (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        int lineStride = mpp.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        int bitOffset = mpp.getBitOffset(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if(dataBuffer instanceof DataBufferByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            byte[] data = ((DataBufferByte)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                int bOffset = eltOffset*8 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                for(int x = 0; x < rectWidth; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    if(bdata[k++] != (byte)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        data[bOffset/8] |=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                            (byte)(0x00000001 << (7 - bOffset & 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        } else if(dataBuffer instanceof DataBufferShort ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                  dataBuffer instanceof DataBufferUShort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            short[] data = dataBuffer instanceof DataBufferShort ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                ((DataBufferShort)dataBuffer).getData() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                ((DataBufferUShort)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                int bOffset = eltOffset*16 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                for(int x = 0; x < rectWidth; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    if(bdata[k++] != (byte)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        data[bOffset/16] |=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                            (short)(0x00000001 <<
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                    (15 - bOffset % 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        } else if(dataBuffer instanceof DataBufferInt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            int[] data = ((DataBufferInt)dataBuffer).getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            for(int y = 0; y < rectHeight; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                int bOffset = eltOffset*32 + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                for(int x = 0; x < rectWidth; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    if(bdata[k++] != (byte)0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                        data[bOffset/32] |=
22584
eed64ee05369 8032733: Fix cast lint warnings in client libraries
darcy
parents: 5506
diff changeset
   789
                            (0x00000001 << (31 - bOffset % 32));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    bOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                eltOffset += lineStride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public static boolean isBinary(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        return sm instanceof MultiPixelPackedSampleModel &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            ((MultiPixelPackedSampleModel)sm).getPixelBitStride() == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            sm.getNumBands() == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    public static ColorModel createColorModel(ColorSpace colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                                              SampleModel sampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        ColorModel colorModel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if(sampleModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            throw new IllegalArgumentException(I18N.getString("ImageUtil1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int numBands = sampleModel.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (numBands < 1 || numBands > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        int dataType = sampleModel.getDataType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (sampleModel instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            if (dataType < DataBuffer.TYPE_BYTE ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                //dataType == DataBuffer.TYPE_SHORT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                dataType > DataBuffer.TYPE_DOUBLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (colorSpace == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                colorSpace =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    numBands <= 2 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    ColorSpace.getInstance(ColorSpace.CS_GRAY) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            boolean useAlpha = (numBands == 2) || (numBands == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            int transparency = useAlpha ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                               Transparency.TRANSLUCENT : Transparency.OPAQUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            boolean premultiplied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            int dataTypeSize = DataBuffer.getDataTypeSize(dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            int[] bits = new int[numBands];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            for (int i = 0; i < numBands; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                bits[i] = dataTypeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            colorModel = new ComponentColorModel(colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                                 bits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                                 useAlpha,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                                 premultiplied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                                 transparency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                                 dataType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                (SinglePixelPackedSampleModel)sampleModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            int[] bitMasks = sppsm.getBitMasks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            int rmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            int gmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            int bmask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            int amask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            numBands = bitMasks.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            if (numBands <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                rmask = gmask = bmask = bitMasks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                if (numBands == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    amask = bitMasks[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                rmask = bitMasks[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                gmask = bitMasks[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                bmask = bitMasks[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                if (numBands == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                    amask = bitMasks[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            int[] sampleSize = sppsm.getSampleSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            int bits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            for (int i = 0; i < sampleSize.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                bits += sampleSize[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if (colorSpace == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            colorModel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                new DirectColorModel(colorSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                     bits, rmask, gmask, bmask, amask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                                     false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                     sampleModel.getDataType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            int bits =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                ((MultiPixelPackedSampleModel)sampleModel).getPixelBitStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            int size = 1 << bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            byte[] comp = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            for (int i = 0; i < size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                comp[i] = (byte)(255 * i / (size - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            colorModel = new IndexColorModel(bits, size, comp, comp, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        return colorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public static int getElementSize(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (sm instanceof MultiPixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            MultiPixelPackedSampleModel mppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return mppsm.getSampleSize(0) * mppsm.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        } else if (sm instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            return sm.getNumBands() * elementSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        } else if (sm instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            return elementSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return elementSize * sm.getNumBands();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public static long getTileSize(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (sm instanceof MultiPixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            MultiPixelPackedSampleModel mppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                (MultiPixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return (mppsm.getScanlineStride() * mppsm.getHeight() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                   (mppsm.getDataBitOffset() + elementSize -1) / elementSize) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                   ((elementSize + 7) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        } else if (sm instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            ComponentSampleModel csm = (ComponentSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            int[] bandOffsets = csm.getBandOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            int maxBandOff = bandOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            for (int i=1; i<bandOffsets.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                maxBandOff = Math.max(maxBandOff, bandOffsets[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            long size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            int pixelStride = csm.getPixelStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            int scanlineStride = csm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            if (maxBandOff >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                size += maxBandOff + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            if (pixelStride > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                size += pixelStride * (sm.getWidth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (scanlineStride > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                size += scanlineStride * (sm.getHeight() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            int[] bankIndices = csm.getBankIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            maxBandOff = bankIndices[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            for (int i=1; i<bankIndices.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                maxBandOff = Math.max(maxBandOff, bankIndices[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            return size * (maxBandOff + 1) * ((elementSize + 7) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        } else if (sm instanceof SinglePixelPackedSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            SinglePixelPackedSampleModel sppsm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                (SinglePixelPackedSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            long size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                        sppsm.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            return size * ((elementSize + 7) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public static long getBandSize(SampleModel sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        int elementSize = DataBuffer.getDataTypeSize(sm.getDataType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (sm instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            ComponentSampleModel csm = (ComponentSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            int pixelStride = csm.getPixelStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            int scanlineStride = csm.getScanlineStride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            long size = Math.min(pixelStride, scanlineStride);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            if (pixelStride > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                size += pixelStride * (sm.getWidth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            if (scanlineStride > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                size += scanlineStride * (sm.getHeight() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return size * ((elementSize + 7) / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            return getTileSize(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Tests whether the color indices represent a gray-scale image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @param r The red channel color indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @param g The green channel color indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @param b The blue channel color indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @return If all the indices have 256 entries, and are identical mappings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *         return <code>true</code>; otherwise, return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    public static boolean isIndicesForGrayscale(byte[] r, byte[] g, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (r.length != g.length || r.length != b.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        int size = r.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        if (size != 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            byte temp = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (r[i] != temp || g[i] != temp || b[i] != temp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /** Converts the provided object to <code>String</code> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public static String convertObjectToString(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        String s = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (obj instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            byte[] bArray = (byte[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            for (int i = 0; i < bArray.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                s += bArray[i] + " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (obj instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            int[] iArray = (int[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            for (int i = 0; i < iArray.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                s += iArray[i] + " " ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if (obj instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            short[] sArray = (short[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            for (int i = 0; i < sArray.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                s += sArray[i] + " " ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return obj.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /** Checks that the provided <code>ImageWriter</code> can encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * the provided <code>ImageTypeSpecifier</code> or not.  If not, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <code>IIOException</code> will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * @param writer The provided <code>ImageWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @param type The image to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @throws IIOException If the writer cannot encoded the provided image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    public static final void canEncodeImage(ImageWriter writer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                                            ImageTypeSpecifier type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        ImageWriterSpi spi = writer.getOriginatingProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if(type != null && spi != null && !spi.canEncodeImage(type))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            throw new IIOException(I18N.getString("ImageUtil2")+" "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                   writer.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    /** Checks that the provided <code>ImageWriter</code> can encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * the provided <code>ColorModel</code> and <code>SampleModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * If not, an <code>IIOException</code> will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param writer The provided <code>ImageWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @param colorModel The provided <code>ColorModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @param sampleModel The provided <code>SampleModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @throws IIOException If the writer cannot encoded the provided image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    public static final void canEncodeImage(ImageWriter writer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                                            ColorModel colorModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                                            SampleModel sampleModel)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        throws IIOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        ImageTypeSpecifier type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (colorModel != null && sampleModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            type = new ImageTypeSpecifier(colorModel, sampleModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        canEncodeImage(writer, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * Returns whether the image has contiguous data across rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public static final boolean imageIsContiguous(RenderedImage image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        SampleModel sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if(image instanceof BufferedImage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            WritableRaster ras = ((BufferedImage)image).getRaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            sm = ras.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            sm = image.getSampleModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (sm instanceof ComponentSampleModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            // Ensure image rows samples are stored contiguously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            // in a single bank.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            ComponentSampleModel csm = (ComponentSampleModel)sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            if (csm.getPixelStride() != csm.getNumBands()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            int[] bandOffsets = csm.getBandOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            for (int i = 0; i < bandOffsets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                if (bandOffsets[i] != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            int[] bankIndices = csm.getBankIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            for (int i = 0; i < bandOffsets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                if (bankIndices[i] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // Otherwise true if and only if it's a bilevel image with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // a MultiPixelPackedSampleModel, 1 bit per pixel, and 1 bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        // pixel stride.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        return ImageUtil.isBinary(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
34416
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1117
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1118
    /**
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1119
     * Gets the destination image type.
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1120
     */
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1121
    public static final ImageTypeSpecifier
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1122
        getDestinationType(ImageReadParam param,
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1123
                           Iterator<ImageTypeSpecifier> imageTypes) throws IIOException {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1124
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1125
        if (imageTypes == null || !imageTypes.hasNext()) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1126
            throw new IllegalArgumentException("imageTypes null or empty!");
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1127
        }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1128
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1129
        ImageTypeSpecifier imageType = null;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1130
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1131
        // If param is non-null, use it
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1132
        if (param != null) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1133
            imageType = param.getDestinationType();
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1134
        }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1135
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1136
        // No info from param, use fallback image type
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1137
        if (imageType == null) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1138
            Object o = imageTypes.next();
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1139
            if (!(o instanceof ImageTypeSpecifier)) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1140
                throw new IllegalArgumentException
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1141
                    ("Non-ImageTypeSpecifier retrieved from imageTypes!");
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1142
            }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1143
            imageType = (ImageTypeSpecifier)o;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1144
        } else {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1145
            boolean foundIt = false;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1146
            while (imageTypes.hasNext()) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1147
                ImageTypeSpecifier type =
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1148
                    imageTypes.next();
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1149
                if (type.equals(imageType)) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1150
                    foundIt = true;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1151
                    break;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1152
                }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1153
            }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1154
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1155
            if (!foundIt) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1156
                throw new IIOException
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1157
                    ("Destination type from ImageReadParam does not match!");
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1158
            }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1159
        }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1160
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1161
        return imageType;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1162
    }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1163
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1164
    /**
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1165
     * Returns <code>true</code> if the given <code>ColorSpace</code> object is
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1166
     * an instance of <code>ICC_ColorSpace</code> but is not one of the standard
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1167
     * <code>ColorSpace</code>s returned by <code>ColorSpace.getInstance()</code>.
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1168
     *
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1169
     * @param cs The <code>ColorSpace</code> to test.
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1170
     */
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1171
    public static boolean isNonStandardICCColorSpace(ColorSpace cs) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1172
        boolean retval = false;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1173
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1174
        try {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1175
            // Check the standard ColorSpaces in decreasing order of
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1176
            // likelihood except check CS_PYCC last as in some JREs
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1177
            // PYCC.pf used not to be installed.
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1178
            retval =
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1179
                (cs instanceof ICC_ColorSpace) &&
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1180
                !(cs.isCS_sRGB() ||
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1181
                  cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) ||
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1182
                  cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)) ||
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1183
                  cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)) ||
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1184
                  cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)));
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1185
        } catch(IllegalArgumentException e) {
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1186
            // PYCC.pf not installed: ignore it - 'retval' is still 'false'.
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1187
        }
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1188
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1189
        return retval;
68c0d866db5d 8143342: Integrate Java Image I/O support for TIFF per JEP 262
bpb
parents: 25859
diff changeset
  1190
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
}