Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMSImageLayout.java
38918 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.java2d.cmm.lcms;2526import java.awt.image.BufferedImage;27import java.awt.image.ComponentColorModel;28import java.awt.image.ComponentSampleModel;29import java.awt.image.DataBuffer;30import java.awt.image.ColorModel;31import java.awt.image.Raster;32import java.awt.image.SampleModel;33import sun.awt.image.ByteComponentRaster;34import sun.awt.image.ShortComponentRaster;35import sun.awt.image.IntegerComponentRaster;3637class LCMSImageLayout {3839public static int BYTES_SH(int x) {40return x;41}4243public static int EXTRA_SH(int x) {44return x << 7;45}4647public static int CHANNELS_SH(int x) {48return x << 3;49}50public static final int SWAPFIRST = 1 << 14;51public static final int DOSWAP = 1 << 10;52public static final int PT_RGB_8 =53CHANNELS_SH(3) | BYTES_SH(1);54public static final int PT_GRAY_8 =55CHANNELS_SH(1) | BYTES_SH(1);56public static final int PT_GRAY_16 =57CHANNELS_SH(1) | BYTES_SH(2);58public static final int PT_RGBA_8 =59EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);60public static final int PT_ARGB_8 =61EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1) | SWAPFIRST;62public static final int PT_BGR_8 =63DOSWAP | CHANNELS_SH(3) | BYTES_SH(1);64public static final int PT_ABGR_8 =65DOSWAP | EXTRA_SH(1) | CHANNELS_SH(3) | BYTES_SH(1);66public static final int PT_BGRA_8 = EXTRA_SH(1) | CHANNELS_SH(3)67| BYTES_SH(1) | DOSWAP | SWAPFIRST;68public static final int DT_BYTE = 0;69public static final int DT_SHORT = 1;70public static final int DT_INT = 2;71public static final int DT_DOUBLE = 3;72boolean isIntPacked = false;73int pixelType;74int dataType;75int width;76int height;77int nextRowOffset;78private int nextPixelOffset;79int offset;8081/* This flag indicates whether the image can be processed82* at once by doTransfrom() native call. Otherwise, the83* image is processed scan by scan.84*/85private boolean imageAtOnce = false;86Object dataArray;8788private int dataArrayLength; /* in bytes */8990private LCMSImageLayout(int np, int pixelType, int pixelSize)91throws ImageLayoutException92{93this.pixelType = pixelType;94width = np;95height = 1;96nextPixelOffset = pixelSize;97nextRowOffset = safeMult(pixelSize, np);98offset = 0;99}100101private LCMSImageLayout(int width, int height, int pixelType,102int pixelSize)103throws ImageLayoutException104{105this.pixelType = pixelType;106this.width = width;107this.height = height;108nextPixelOffset = pixelSize;109nextRowOffset = safeMult(pixelSize, width);110offset = 0;111}112113114public LCMSImageLayout(byte[] data, int np, int pixelType, int pixelSize)115throws ImageLayoutException116{117this(np, pixelType, pixelSize);118dataType = DT_BYTE;119dataArray = data;120dataArrayLength = data.length;121122verify();123}124125public LCMSImageLayout(short[] data, int np, int pixelType, int pixelSize)126throws ImageLayoutException127{128this(np, pixelType, pixelSize);129dataType = DT_SHORT;130dataArray = data;131dataArrayLength = 2 * data.length;132133verify();134}135136public LCMSImageLayout(int[] data, int np, int pixelType, int pixelSize)137throws ImageLayoutException138{139this(np, pixelType, pixelSize);140dataType = DT_INT;141dataArray = data;142dataArrayLength = 4 * data.length;143144verify();145}146147public LCMSImageLayout(double[] data, int np, int pixelType, int pixelSize)148throws ImageLayoutException149{150this(np, pixelType, pixelSize);151dataType = DT_DOUBLE;152dataArray = data;153dataArrayLength = 8 * data.length;154155verify();156}157158private LCMSImageLayout() {159}160161/* This method creates a layout object for given image.162* Returns null if the image is not supported by current implementation.163*/164public static LCMSImageLayout createImageLayout(BufferedImage image) throws ImageLayoutException {165LCMSImageLayout l = new LCMSImageLayout();166167switch (image.getType()) {168case BufferedImage.TYPE_INT_RGB:169l.pixelType = PT_ARGB_8;170l.isIntPacked = true;171break;172case BufferedImage.TYPE_INT_ARGB:173l.pixelType = PT_ARGB_8;174l.isIntPacked = true;175break;176case BufferedImage.TYPE_INT_BGR:177l.pixelType = PT_ABGR_8;178l.isIntPacked = true;179break;180case BufferedImage.TYPE_3BYTE_BGR:181l.pixelType = PT_BGR_8;182break;183case BufferedImage.TYPE_4BYTE_ABGR:184l.pixelType = PT_ABGR_8;185break;186case BufferedImage.TYPE_BYTE_GRAY:187l.pixelType = PT_GRAY_8;188break;189case BufferedImage.TYPE_USHORT_GRAY:190l.pixelType = PT_GRAY_16;191break;192default:193/* ColorConvertOp creates component images as194* default destination, so this kind of images195* has to be supported.196*/197ColorModel cm = image.getColorModel();198if (cm instanceof ComponentColorModel) {199ComponentColorModel ccm = (ComponentColorModel) cm;200201// verify whether the component size is fine202int[] cs = ccm.getComponentSize();203for (int s : cs) {204if (s != 8) {205return null;206}207}208209return createImageLayout(image.getRaster());210211}212return null;213}214215l.width = image.getWidth();216l.height = image.getHeight();217218switch (image.getType()) {219case BufferedImage.TYPE_INT_RGB:220case BufferedImage.TYPE_INT_ARGB:221case BufferedImage.TYPE_INT_BGR:222do {223IntegerComponentRaster intRaster = (IntegerComponentRaster)224image.getRaster();225l.nextRowOffset = safeMult(4, intRaster.getScanlineStride());226l.nextPixelOffset = safeMult(4, intRaster.getPixelStride());227l.offset = safeMult(4, intRaster.getDataOffset(0));228l.dataArray = intRaster.getDataStorage();229l.dataArrayLength = 4 * intRaster.getDataStorage().length;230l.dataType = DT_INT;231232if (l.nextRowOffset == l.width * 4 * intRaster.getPixelStride()) {233l.imageAtOnce = true;234}235} while (false);236break;237238case BufferedImage.TYPE_3BYTE_BGR:239case BufferedImage.TYPE_4BYTE_ABGR:240do {241ByteComponentRaster byteRaster = (ByteComponentRaster)242image.getRaster();243l.nextRowOffset = byteRaster.getScanlineStride();244l.nextPixelOffset = byteRaster.getPixelStride();245246int firstBand = image.getSampleModel().getNumBands() - 1;247l.offset = byteRaster.getDataOffset(firstBand);248l.dataArray = byteRaster.getDataStorage();249l.dataArrayLength = byteRaster.getDataStorage().length;250l.dataType = DT_BYTE;251if (l.nextRowOffset == l.width * byteRaster.getPixelStride()) {252l.imageAtOnce = true;253}254} while (false);255break;256257case BufferedImage.TYPE_BYTE_GRAY:258do {259ByteComponentRaster byteRaster = (ByteComponentRaster)260image.getRaster();261l.nextRowOffset = byteRaster.getScanlineStride();262l.nextPixelOffset = byteRaster.getPixelStride();263264l.dataArrayLength = byteRaster.getDataStorage().length;265l.offset = byteRaster.getDataOffset(0);266l.dataArray = byteRaster.getDataStorage();267l.dataType = DT_BYTE;268269if (l.nextRowOffset == l.width * byteRaster.getPixelStride()) {270l.imageAtOnce = true;271}272} while (false);273break;274275case BufferedImage.TYPE_USHORT_GRAY:276do {277ShortComponentRaster shortRaster = (ShortComponentRaster)278image.getRaster();279l.nextRowOffset = safeMult(2, shortRaster.getScanlineStride());280l.nextPixelOffset = safeMult(2, shortRaster.getPixelStride());281282l.offset = safeMult(2, shortRaster.getDataOffset(0));283l.dataArray = shortRaster.getDataStorage();284l.dataArrayLength = 2 * shortRaster.getDataStorage().length;285l.dataType = DT_SHORT;286287if (l.nextRowOffset == l.width * 2 * shortRaster.getPixelStride()) {288l.imageAtOnce = true;289}290} while (false);291break;292default:293return null;294}295l.verify();296return l;297}298299private static enum BandOrder {300DIRECT,301INVERTED,302ARBITRARY,303UNKNOWN;304305public static BandOrder getBandOrder(int[] bandOffsets) {306BandOrder order = UNKNOWN;307308int numBands = bandOffsets.length;309310for (int i = 0; (order != ARBITRARY) && (i < bandOffsets.length); i++) {311switch (order) {312case UNKNOWN:313if (bandOffsets[i] == i) {314order = DIRECT;315} else if (bandOffsets[i] == (numBands - 1 - i)) {316order = INVERTED;317} else {318order = ARBITRARY;319}320break;321case DIRECT:322if (bandOffsets[i] != i) {323order = ARBITRARY;324}325break;326case INVERTED:327if (bandOffsets[i] != (numBands - 1 - i)) {328order = ARBITRARY;329}330break;331}332}333return order;334}335}336337private void verify() throws ImageLayoutException {338339if (offset < 0 || offset >= dataArrayLength) {340throw new ImageLayoutException("Invalid image layout");341}342343if (nextPixelOffset != getBytesPerPixel(pixelType)) {344throw new ImageLayoutException("Invalid image layout");345}346347int lastScanOffset = safeMult(nextRowOffset, (height - 1));348349int lastPixelOffset = safeMult(nextPixelOffset, (width -1 ));350351lastPixelOffset = safeAdd(lastPixelOffset, lastScanOffset);352353int off = safeAdd(offset, lastPixelOffset);354355if (off < 0 || off >= dataArrayLength) {356throw new ImageLayoutException("Invalid image layout");357}358}359360static int safeAdd(int a, int b) throws ImageLayoutException {361long res = a;362res += b;363if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {364throw new ImageLayoutException("Invalid image layout");365}366return (int)res;367}368369static int safeMult(int a, int b) throws ImageLayoutException {370long res = a;371res *= b;372if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) {373throw new ImageLayoutException("Invalid image layout");374}375return (int)res;376}377378public static class ImageLayoutException extends Exception {379public ImageLayoutException(String message) {380super(message);381}382}383public static LCMSImageLayout createImageLayout(Raster r) {384LCMSImageLayout l = new LCMSImageLayout();385if (r instanceof ByteComponentRaster &&386r.getSampleModel() instanceof ComponentSampleModel) {387ByteComponentRaster br = (ByteComponentRaster)r;388389ComponentSampleModel csm = (ComponentSampleModel)r.getSampleModel();390391l.pixelType = CHANNELS_SH(br.getNumBands()) | BYTES_SH(1);392393int[] bandOffsets = csm.getBandOffsets();394BandOrder order = BandOrder.getBandOrder(bandOffsets);395396int firstBand = 0;397switch (order) {398case INVERTED:399l.pixelType |= DOSWAP;400firstBand = csm.getNumBands() - 1;401break;402case DIRECT:403// do nothing404break;405default:406// unable to create the image layout;407return null;408}409410l.nextRowOffset = br.getScanlineStride();411l.nextPixelOffset = br.getPixelStride();412413l.offset = br.getDataOffset(firstBand);414l.dataArray = br.getDataStorage();415l.dataType = DT_BYTE;416417l.width = br.getWidth();418l.height = br.getHeight();419420if (l.nextRowOffset == l.width * br.getPixelStride()) {421l.imageAtOnce = true;422}423return l;424}425return null;426}427428/**429* Derives number of bytes per pixel from the pixel format.430* Following bit fields are used here:431* [0..2] - bytes per sample432* [3..6] - number of color samples per pixel433* [7..9] - number of non-color samples per pixel434*435* A complete description of the pixel format can be found436* here: lcms2.h, lines 651 - 667.437*438* @param pixelType pixel format in lcms2 notation.439* @return number of bytes per pixel for given pixel format.440*/441private static int getBytesPerPixel(int pixelType) {442int bytesPerSample = (0x7 & pixelType);443int colorSamplesPerPixel = 0xF & (pixelType >> 3);444int extraSamplesPerPixel = 0x7 & (pixelType >> 7);445446return bytesPerSample * (colorSamplesPerPixel + extraSamplesPerPixel);447}448}449450451