0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.cmm.lcms;
0N/A
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.awt.image.ComponentColorModel;
0N/Aimport java.awt.image.Raster;
0N/Aimport java.awt.image.WritableRaster;
0N/Aimport java.awt.image.SinglePixelPackedSampleModel;
0N/Aimport java.awt.image.ComponentSampleModel;
0N/Aimport java.awt.image.DataBuffer;
0N/Aimport java.awt.image.DataBufferByte;
0N/Aimport java.awt.image.DataBufferUShort;
0N/Aimport java.awt.image.DataBufferInt;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport sun.awt.image.ByteComponentRaster;
0N/Aimport sun.awt.image.ShortComponentRaster;
0N/Aimport sun.awt.image.IntegerComponentRaster;
0N/A
0N/A
0N/Aclass LCMSImageLayout {
0N/A
0N/A public static int BYTES_SH(int x) {
0N/A return x;
0N/A }
0N/A
0N/A public static int EXTRA_SH(int x) {
0N/A return x<<7;
0N/A }
0N/A
0N/A public static int CHANNELS_SH(int x) {
0N/A return x<<3;
0N/A }
0N/A
0N/A public static final int SWAPFIRST = 1<<14;
0N/A
0N/A public static final int DOSWAP = 1<<10;
0N/A
0N/A public static final int PT_RGB_8 =
0N/A CHANNELS_SH(3) | BYTES_SH(1);
0N/A
0N/A public static final int PT_GRAY_8 =
0N/A CHANNELS_SH(1) | BYTES_SH(1);
0N/A
0N/A public static final int PT_GRAY_16 =
0N/A CHANNELS_SH(1) | BYTES_SH(2);
0N/A
0N/A public static final int PT_RGBA_8 =
0N/A EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
0N/A
0N/A public static final int PT_ARGB_8 =
0N/A EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1) | SWAPFIRST;
0N/A
0N/A public static final int PT_BGR_8 =
0N/A DOSWAP | CHANNELS_SH(3) | BYTES_SH(1);
0N/A
0N/A public static final int PT_ABGR_8 =
0N/A DOSWAP | EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);
0N/A
0N/A public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3) |
0N/A BYTES_SH(1) | DOSWAP | SWAPFIRST;
0N/A
0N/A public static final int DT_BYTE = 0;
0N/A public static final int DT_SHORT = 1;
0N/A public static final int DT_INT = 2;
0N/A public static final int DT_DOUBLE = 3;
0N/A
0N/A
0N/A boolean isIntPacked = false;
0N/A int pixelType;
0N/A int dataType;
0N/A int width;
0N/A int height;
0N/A int nextRowOffset;
6372N/A private int nextPixelOffset;
0N/A int offset;
0N/A
0N/A Object dataArray;
5818N/A private int dataArrayLength; /* in bytes */
5818N/A
5818N/A private LCMSImageLayout(int np, int pixelType, int pixelSize)
5818N/A throws ImageLayoutException
5818N/A {
0N/A this.pixelType = pixelType;
0N/A width = np;
0N/A height = 1;
6372N/A nextPixelOffset = pixelSize;
5818N/A nextRowOffset = safeMult(pixelSize, np);
0N/A offset = 0;
0N/A }
0N/A
0N/A private LCMSImageLayout(int width, int height, int pixelType,
5818N/A int pixelSize)
5818N/A throws ImageLayoutException
5818N/A {
0N/A this.pixelType = pixelType;
0N/A this.width = width;
0N/A this.height = height;
6372N/A nextPixelOffset = pixelSize;
5818N/A nextRowOffset = safeMult(pixelSize, width);
0N/A offset = 0;
0N/A }
0N/A
0N/A
5818N/A public LCMSImageLayout(byte[] data, int np, int pixelType, int pixelSize)
5818N/A throws ImageLayoutException
5818N/A {
0N/A this(np, pixelType, pixelSize);
0N/A dataType = DT_BYTE;
0N/A dataArray = data;
5818N/A dataArrayLength = data.length;
5818N/A
5818N/A verify();
0N/A }
0N/A
5818N/A public LCMSImageLayout(short[] data, int np, int pixelType, int pixelSize)
5818N/A throws ImageLayoutException
5818N/A {
0N/A this(np, pixelType, pixelSize);
0N/A dataType = DT_SHORT;
0N/A dataArray = data;
5818N/A dataArrayLength = 2 * data.length;
5818N/A
5818N/A verify();
0N/A }
0N/A
5818N/A public LCMSImageLayout(int[] data, int np, int pixelType, int pixelSize)
5818N/A throws ImageLayoutException
5818N/A {
0N/A this(np, pixelType, pixelSize);
0N/A dataType = DT_INT;
0N/A dataArray = data;
5818N/A dataArrayLength = 4 * data.length;
5818N/A
5818N/A verify();
0N/A }
0N/A
0N/A public LCMSImageLayout(double[] data, int np, int pixelType, int pixelSize)
5818N/A throws ImageLayoutException
0N/A {
0N/A this(np, pixelType, pixelSize);
0N/A dataType = DT_DOUBLE;
0N/A dataArray = data;
5818N/A dataArrayLength = 8 * data.length;
5818N/A
5818N/A verify();
0N/A }
0N/A
5818N/A public LCMSImageLayout(BufferedImage image) throws ImageLayoutException {
0N/A ShortComponentRaster shortRaster;
0N/A IntegerComponentRaster intRaster;
0N/A ByteComponentRaster byteRaster;
0N/A switch (image.getType()) {
0N/A case BufferedImage.TYPE_INT_RGB:
0N/A pixelType = PT_ARGB_8;
0N/A isIntPacked = true;
0N/A break;
0N/A case BufferedImage.TYPE_INT_ARGB:
0N/A pixelType = PT_ARGB_8;
0N/A isIntPacked = true;
0N/A break;
0N/A case BufferedImage.TYPE_INT_BGR:
0N/A pixelType = PT_ABGR_8;
0N/A isIntPacked = true;
0N/A break;
0N/A case BufferedImage.TYPE_3BYTE_BGR:
0N/A pixelType = PT_BGR_8;
0N/A break;
0N/A case BufferedImage.TYPE_4BYTE_ABGR:
0N/A pixelType = PT_ABGR_8;
0N/A break;
0N/A case BufferedImage.TYPE_BYTE_GRAY:
0N/A pixelType = PT_GRAY_8;
0N/A break;
0N/A case BufferedImage.TYPE_USHORT_GRAY:
0N/A pixelType = PT_GRAY_16;
0N/A break;
0N/A default:
0N/A // TODO: Add support for some images having
0N/A // SinglePixelPackedModel and ComponentSampleModel
0N/A throw new IllegalArgumentException(
0N/A "CMMImageLayout - bad image type passed to constructor");
0N/A }
0N/A
0N/A width = image.getWidth();
0N/A height = image.getHeight();
0N/A
0N/A switch (image.getType()) {
0N/A case BufferedImage.TYPE_INT_RGB:
0N/A case BufferedImage.TYPE_INT_ARGB:
0N/A case BufferedImage.TYPE_INT_BGR:
0N/A intRaster = (IntegerComponentRaster)image.getRaster();
5818N/A
5818N/A nextRowOffset = safeMult(4, intRaster.getScanlineStride());
6372N/A nextPixelOffset = safeMult(4, intRaster.getPixelStride());
5818N/A
5818N/A offset = safeMult(4, intRaster.getDataOffset(0));
5818N/A
0N/A dataArray = intRaster.getDataStorage();
5818N/A dataArrayLength = 4 * intRaster.getDataStorage().length;
0N/A dataType = DT_INT;
0N/A break;
0N/A
0N/A case BufferedImage.TYPE_3BYTE_BGR:
0N/A case BufferedImage.TYPE_4BYTE_ABGR:
0N/A byteRaster = (ByteComponentRaster)image.getRaster();
0N/A nextRowOffset = byteRaster.getScanlineStride();
6372N/A nextPixelOffset = byteRaster.getPixelStride();
6372N/A
5818N/A int firstBand = image.getSampleModel().getNumBands() - 1;
5818N/A offset = byteRaster.getDataOffset(firstBand);
0N/A dataArray = byteRaster.getDataStorage();
5818N/A dataArrayLength = byteRaster.getDataStorage().length;
0N/A dataType = DT_BYTE;
0N/A break;
0N/A
0N/A case BufferedImage.TYPE_BYTE_GRAY:
0N/A byteRaster = (ByteComponentRaster)image.getRaster();
0N/A nextRowOffset = byteRaster.getScanlineStride();
6372N/A nextPixelOffset = byteRaster.getPixelStride();
6372N/A
0N/A offset = byteRaster.getDataOffset(0);
0N/A dataArray = byteRaster.getDataStorage();
5818N/A dataArrayLength = byteRaster.getDataStorage().length;
0N/A dataType = DT_BYTE;
0N/A break;
0N/A
0N/A case BufferedImage.TYPE_USHORT_GRAY:
0N/A shortRaster = (ShortComponentRaster)image.getRaster();
5818N/A nextRowOffset = safeMult(2, shortRaster.getScanlineStride());
6372N/A nextPixelOffset = safeMult(2, shortRaster.getPixelStride());
6372N/A
5818N/A offset = safeMult(2, shortRaster.getDataOffset(0));
0N/A dataArray = shortRaster.getDataStorage();
5818N/A dataArrayLength = 2 * shortRaster.getDataStorage().length;
0N/A dataType = DT_SHORT;
0N/A break;
0N/A }
5818N/A verify();
0N/A }
0N/A
0N/A public static boolean isSupported(BufferedImage image) {
0N/A switch (image.getType()) {
0N/A case BufferedImage.TYPE_INT_RGB:
0N/A case BufferedImage.TYPE_INT_ARGB:
0N/A case BufferedImage.TYPE_INT_BGR:
0N/A case BufferedImage.TYPE_3BYTE_BGR:
0N/A case BufferedImage.TYPE_4BYTE_ABGR:
0N/A case BufferedImage.TYPE_BYTE_GRAY:
0N/A case BufferedImage.TYPE_USHORT_GRAY:
0N/A return true;
0N/A }
0N/A return false;
0N/A }
5818N/A
5818N/A private void verify() throws ImageLayoutException {
5818N/A
5818N/A if (offset < 0 || offset >= dataArrayLength) {
5818N/A throw new ImageLayoutException("Invalid image layout");
5818N/A }
5818N/A
6372N/A if (nextPixelOffset != getBytesPerPixel(pixelType)) {
6372N/A throw new ImageLayoutException("Invalid image layout");
6372N/A }
5818N/A
6372N/A int lastScanOffset = safeMult(nextRowOffset, (height - 1));
6372N/A
6372N/A int lastPixelOffset = safeMult(nextPixelOffset, (width -1 ));
6372N/A
6372N/A lastPixelOffset = safeAdd(lastPixelOffset, lastScanOffset);
5818N/A
5818N/A int off = safeAdd(offset, lastPixelOffset);
5818N/A
5818N/A if (off < 0 || off >= dataArrayLength) {
5818N/A throw new ImageLayoutException("Invalid image layout");
5818N/A }
5818N/A }
5818N/A
5818N/A static int safeAdd(int a, int b) throws ImageLayoutException {
5818N/A long res = a;
5818N/A res += b;
5818N/A if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
5818N/A throw new ImageLayoutException("Invalid image layout");
5818N/A }
5818N/A return (int)res;
5818N/A }
5818N/A
5818N/A static int safeMult(int a, int b) throws ImageLayoutException {
5818N/A long res = a;
5818N/A res *= b;
5818N/A if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {
5818N/A throw new ImageLayoutException("Invalid image layout");
5818N/A }
5818N/A return (int)res;
5818N/A }
5818N/A
5818N/A public static class ImageLayoutException extends Exception {
5818N/A public ImageLayoutException(String message) {
5818N/A super(message);
5818N/A }
5818N/A }
6372N/A
6372N/A /**
6372N/A * Derives number of bytes per pixel from the pixel format.
6372N/A * Following bit fields are used here:
6372N/A * [0..2] - bytes per sample
6372N/A * [3..6] - number of color samples per pixel
6372N/A * [7..9] - number of non-color samples per pixel
6372N/A *
6372N/A * A complete description of the pixel format can be found
6372N/A * here: lcms2.h, lines 651 - 667.
6372N/A *
6372N/A * @param pixelType pixel format in lcms2 notation.
6372N/A * @return number of bytes per pixel for given pixel format.
6372N/A */
6372N/A private static int getBytesPerPixel(int pixelType) {
6372N/A int bytesPerSample = (0x7 & pixelType);
6372N/A int colorSamplesPerPixel = 0xF & (pixelType >> 3);
6372N/A int extraSamplesPerPixel = 0x7 & (pixelType >> 7);
6372N/A
6372N/A return bytesPerSample * (colorSamplesPerPixel + extraSamplesPerPixel);
6372N/A }
0N/A}