1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Oleg V. Khaschansky
19 * @version $Revision$
20 */
21package org.apache.harmony.awt.gl.color;
22
23import java.awt.color.ICC_Profile;
24import java.security.AccessController;
25import java.security.PrivilegedAction;
26import java.util.HashMap;
27
28/**
29 * This class is a wrapper for the native CMM library
30 */
31public class NativeCMM {
32
33    /**
34     * Storage for profile handles, since they are private
35     * in ICC_Profile, but we need access to them.
36     */
37    private static HashMap<ICC_Profile, Long> profileHandles = new HashMap<ICC_Profile, Long>();
38
39    private static boolean isCMMLoaded;
40
41    public static void addHandle(ICC_Profile key, long handle) {
42        profileHandles.put(key, new Long(handle));
43    }
44
45    public static void removeHandle(ICC_Profile key) {
46        profileHandles.remove(key);
47    }
48
49    public static long getHandle(ICC_Profile key) {
50        return profileHandles.get(key).longValue();
51    }
52
53    /* ICC profile management */
54    public static native long cmmOpenProfile(byte[] data);
55    public static native void cmmCloseProfile(long profileID);
56    public static native int cmmGetProfileSize(long profileID);
57    public static native void cmmGetProfile(long profileID, byte[] data);
58    public static native int cmmGetProfileElementSize(long profileID, int signature);
59    public static native void cmmGetProfileElement(long profileID, int signature,
60                                           byte[] data);
61    public static native void cmmSetProfileElement(long profileID, int tagSignature,
62                                           byte[] data);
63
64
65    /* ICC transforms */
66    public static native long cmmCreateMultiprofileTransform(
67            long[] profileHandles,
68            int[] renderingIntents
69        );
70    public static native void cmmDeleteTransform(long transformHandle);
71    public static native void cmmTranslateColors(long transformHandle,
72            NativeImageFormat src,
73            NativeImageFormat dest);
74
75    static void loadCMM() {
76        if (!isCMMLoaded) {
77            AccessController.doPrivileged(
78                  new PrivilegedAction<Void>() {
79                    public Void run() {
80                        System.loadLibrary("lcmm"); //$NON-NLS-1$
81                        return null;
82                    }
83            } );
84            isCMMLoaded = true;
85        }
86    }
87
88    /* load native CMM library */
89    static {
90        loadCMM();
91    }
92}
93