Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/cmm/lcms/LCMS.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*/2425package sun.java2d.cmm.lcms;2627import java.awt.color.CMMException;28import java.awt.color.ICC_Profile;29import sun.java2d.cmm.ColorTransform;30import sun.java2d.cmm.PCMM;31import sun.java2d.cmm.Profile;32import sun.java2d.cmm.lcms.LCMSProfile.TagData;3334public class LCMS implements PCMM {3536/* methods invoked from ICC_Profile */37@Override38public Profile loadProfile(byte[] data) {39final Object disposerRef = new Object();4041final long ptr = loadProfileNative(data, disposerRef);4243if (ptr != 0L) {44return new LCMSProfile(ptr, disposerRef);45}46return null;47}4849private native long loadProfileNative(byte[] data, Object ref);5051private LCMSProfile getLcmsProfile(Profile p) {52if (p instanceof LCMSProfile) {53return (LCMSProfile)p;54}55throw new CMMException("Invalid profile: " + p);56}575859@Override60public void freeProfile(Profile p) {61// we use disposer, so this method does nothing62}6364@Override65public int getProfileSize(final Profile p) {66synchronized (p) {67return getProfileSizeNative(getLcmsProfile(p).getLcmsPtr());68}69}7071private native int getProfileSizeNative(long ptr);7273@Override74public void getProfileData(final Profile p, byte[] data) {75synchronized (p) {76getProfileDataNative(getLcmsProfile(p).getLcmsPtr(), data);77}78}7980private native void getProfileDataNative(long ptr, byte[] data);8182@Override83public int getTagSize(Profile p, int tagSignature) {84final LCMSProfile profile = getLcmsProfile(p);8586synchronized (profile) {87TagData t = profile.getTag(tagSignature);88return t == null ? 0 : t.getSize();89}90}9192static native byte[] getTagNative(long profileID, int signature);9394@Override95public void getTagData(Profile p, int tagSignature, byte[] data)96{97final LCMSProfile profile = getLcmsProfile(p);9899synchronized (profile) {100TagData t = profile.getTag(tagSignature);101if (t != null) {102t.copyDataTo(data);103}104}105}106107@Override108public synchronized void setTagData(Profile p, int tagSignature, byte[] data) {109final LCMSProfile profile = getLcmsProfile(p);110111synchronized (profile) {112profile.clearTagCache();113114// Now we are going to update the profile with new tag data115// In some cases, we may change the pointer to the native116// profile.117//118// If we fail to write tag data for any reason, the old pointer119// should be used.120setTagDataNative(profile.getLcmsPtr(),121tagSignature, data);122}123}124125/**126* Writes supplied data as a tag into the profile.127* Destroys old profile, if new one was successfully128* created.129*130* Returns valid pointer to new profile.131*132* Throws CMMException if operation fails, preserve old profile from133* destruction.134*/135private native void setTagDataNative(long ptr, int tagSignature,136byte[] data);137138public synchronized static native LCMSProfile getProfileID(ICC_Profile profile);139140/* Helper method used from LCMSColorTransfrom */141static long createTransform(142LCMSProfile[] profiles, int renderType,143int inFormatter, boolean isInIntPacked,144int outFormatter, boolean isOutIntPacked,145Object disposerRef)146{147long[] ptrs = new long[profiles.length];148149for (int i = 0; i < profiles.length; i++) {150if (profiles[i] == null) throw new CMMException("Unknown profile ID");151152ptrs[i] = profiles[i].getLcmsPtr();153}154155return createNativeTransform(ptrs, renderType, inFormatter,156isInIntPacked, outFormatter, isOutIntPacked, disposerRef);157}158159private static native long createNativeTransform(160long[] profileIDs, int renderType,161int inFormatter, boolean isInIntPacked,162int outFormatter, boolean isOutIntPacked,163Object disposerRef);164165/**166* Constructs ColorTransform object corresponding to an ICC_profile167*/168public ColorTransform createTransform(ICC_Profile profile,169int renderType,170int transformType)171{172return new LCMSTransform(profile, renderType, renderType);173}174175/**176* Constructs an ColorTransform object from a list of ColorTransform177* objects178*/179public synchronized ColorTransform createTransform(180ColorTransform[] transforms)181{182return new LCMSTransform(transforms);183}184185/* methods invoked from LCMSTransform */186public static native void colorConvert(LCMSTransform trans,187LCMSImageLayout src,188LCMSImageLayout dest);189public static native void freeTransform(long ID);190191public static native void initLCMS(Class Trans, Class IL, Class Pf);192193private LCMS() {};194195private static LCMS theLcms = null;196197static synchronized PCMM getModule() {198if (theLcms != null) {199return theLcms;200}201202java.security.AccessController.doPrivileged(203new java.security.PrivilegedAction() {204public Object run() {205/* We need to load awt here because of usage trace and206* disposer frameworks207*/208System.loadLibrary("awt");209System.loadLibrary("lcms");210return null;211}212});213214initLCMS(LCMSTransform.class, LCMSImageLayout.class, ICC_Profile.class);215216theLcms = new LCMS();217218return theLcms;219}220}221222223