LCMS.java 6.9 KB
Newer Older
D
duke 已提交
1
/*
O
ohair 已提交
2
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28
 */

package sun.java2d.cmm.lcms;

import java.awt.color.ICC_Profile;
29 30
import java.util.Arrays;
import java.util.HashMap;
D
duke 已提交
31 32 33 34 35 36
import sun.java2d.cmm.ColorTransform;
import sun.java2d.cmm.PCMM;

public class LCMS implements PCMM {

    /* methods invoked from ICC_Profile */
37 38 39
    @Override
    public long loadProfile(byte[] data) {
        long id = loadProfileNative(data);
D
duke 已提交
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        if (id != 0L) {
            if (profiles == null) {
                profiles = new HashMap<>();
            }
            profiles.put(id, new TagCache(id));
        }
        return id;
    }

    private native long loadProfileNative(byte[] data);

    @Override
    public void freeProfile(long profileID) {
        TagCache c = profiles.remove(profileID);
        if (c != null) {
            c.clear();
        }
        if (profiles.isEmpty()) {
            profiles = null;
        }
        freeProfileNative(profileID);
    }

    private native void freeProfileNative(long profileID);
D
duke 已提交
65 66 67 68 69

    public native synchronized int getProfileSize(long profileID);

    public native synchronized void getProfileData(long profileID, byte[] data);

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    @Override
    public synchronized int getTagSize(long profileID, int tagSignature) {
        TagCache cache = profiles.get(profileID);

        if (cache ==  null) {
            cache = new TagCache(profileID);
            profiles.put(profileID, cache);
        }

        TagData t = cache.getTag(tagSignature);
        return t == null ? 0 : t.getSize();
    }

    private static native byte[] getTagNative(long profileID, int signature);

    @Override
    public synchronized void getTagData(long profileID, int tagSignature,
                                               byte[] data)
    {
        TagCache cache = profiles.get(profileID);

        if (cache ==  null) {
            cache = new TagCache(profileID);
            profiles.put(profileID, cache);
        }

        TagData t = cache.getTag(tagSignature);
        if (t != null) {
            t.copyDataTo(data);
        }
    }

    @Override
    public synchronized void setTagData(long profileID, int tagSignature, byte[] data) {
        TagCache cache = profiles.get(profileID);

        if (cache != null) {
            cache.clear();
        }
        setTagDataNative(profileID, tagSignature, data);
    }

    private native synchronized void setTagDataNative(long profileID, int tagSignature,
D
duke 已提交
113 114 115 116 117
                                               byte[] data);

    public static native long getProfileID(ICC_Profile profile);

    public static native long createNativeTransform(
118 119 120
        long[] profileIDs, int renderType,
        int inFormatter, boolean isInIntPacked,
        int outFormatter, boolean isOutIntPacked,
121
        Object disposerRef);
D
duke 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

   /**
     * Constructs ColorTransform object corresponding to an ICC_profile
     */
    public ColorTransform createTransform(ICC_Profile profile,
                                                       int renderType,
                                                       int transformType)
    {
        return new LCMSTransform(profile, renderType, renderType);
    }

    /**
     * Constructs an ColorTransform object from a list of ColorTransform
     * objects
     */
    public synchronized ColorTransform createTransform(
        ColorTransform[] transforms)
    {
        return new LCMSTransform(transforms);
    }

    /* methods invoked from LCMSTransform */
    public static native void colorConvert(LCMSTransform trans,
                                           LCMSImageLayout src,
                                           LCMSImageLayout dest);
    public static native void freeTransform(long ID);

    public static native void initLCMS(Class Trans, Class IL, Class Pf);

151 152 153 154 155 156 157 158 159
    private LCMS() {};

    private static LCMS theLcms = null;

    static synchronized PCMM getModule() {
        if (theLcms != null) {
            return theLcms;
        }

D
duke 已提交
160
        java.security.AccessController.doPrivileged(
161 162 163 164 165 166 167 168 169 170
                new java.security.PrivilegedAction() {
                    public Object run() {
                        /* We need to load awt here because of usage trace and
                         * disposer frameworks
                         */
                        System.loadLibrary("awt");
                        System.loadLibrary("lcms");
                        return null;
                    }
                });
D
duke 已提交
171 172

        initLCMS(LCMSTransform.class, LCMSImageLayout.class, ICC_Profile.class);
173 174 175 176

        theLcms = new LCMS();

        return theLcms;
D
duke 已提交
177
    }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

    private static class TagData {
        private int signature;
        private byte[] data;

        TagData(int sig, byte[] data) {
            this.signature = sig;
            this.data = data;
        }

        int getSize() {
            return data.length;
        }

        byte[] getData() {
            return Arrays.copyOf(data, data.length);
        }

        void copyDataTo(byte[] dst) {
            System.arraycopy(data, 0, dst, 0, data.length);
        }

        int getSignature() {
            return signature;
        }
    }

    private static class TagCache  {
        private long profileID;
        private HashMap<Integer, TagData> tags;

        TagCache(long id) {
            profileID = id;

            tags = new HashMap<>();
        }

        TagData getTag(int sig) {
            TagData t = tags.get(sig);
            if (t == null) {
                byte[] tagData = getTagNative(profileID, sig);
                if (tagData != null) {
                    t = new TagData(sig, tagData);
                    tags.put(sig, t);
                }
            }
            return t;
        }

        void clear() {
            tags.clear();
        }
    }

    private static HashMap<Long, TagCache> profiles;
D
duke 已提交
233
}