1 /*
   2  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.cmm;
  27 
  28 import java.awt.color.ColorSpace;
  29 import java.awt.color.ICC_Profile;
  30 import java.awt.color.CMMException;
  31 import java.awt.image.BufferedImage;
  32 import java.awt.image.Raster;
  33 import java.awt.image.WritableRaster;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;
  36 import sun.security.action.GetPropertyAction;
  37 import java.util.ServiceLoader;
  38 
  39 public class CMSManager {
  40     public static ColorSpace GRAYspace;       // These two fields allow access
  41     public static ColorSpace LINEAR_RGBspace; // to java.awt.color.ColorSpace
  42                                               // private fields from other
  43                                               // packages.  The fields are set
  44                                               // by java.awt.color.ColorSpace
  45                                               // and read by
  46                                               // java.awt.image.ColorModel.
  47 
  48     private static PCMM cmmImpl = null;
  49 
  50     public static synchronized PCMM getModule() {
  51         if (cmmImpl != null) {
  52             return cmmImpl;
  53         }
  54 
  55         cmmImpl = (PCMM)AccessController.doPrivileged(new PrivilegedAction() {
  56             public Object run() {
  57                 String cmmClass = System.getProperty(
  58                     "sun.java2d.cmm", "sun.java2d.cmm.kcms.CMM");
  59 
  60                 ServiceLoader<PCMM> cmmLoader
  61                     = ServiceLoader.loadInstalled(PCMM.class);
  62 
  63                 PCMM service = null;
  64 
  65                 for (PCMM cmm : cmmLoader) {
  66                     service = cmm;
  67                     if (cmm.getClass().getName().equals(cmmClass)) {
  68                         break;
  69                     }
  70                 }
  71                 return service;
  72             }
  73         });
  74 
  75         if (cmmImpl == null) {
  76             throw new CMMException("Cannot initialize Color Management System."+
  77                                    "No CM module found");
  78         }
  79 
  80         GetPropertyAction gpa = new GetPropertyAction("sun.java2d.cmm.trace");
  81         String cmmTrace = (String)AccessController.doPrivileged(gpa);
  82         if (cmmTrace != null) {
  83             cmmImpl = new CMMTracer(cmmImpl);
  84         }
  85 
  86         return cmmImpl;
  87     }
  88 
  89     /* CMM trace routines */
  90 
  91     public static class CMMTracer implements PCMM {
  92         PCMM tcmm;
  93         String cName ;
  94 
  95         public CMMTracer(PCMM tcmm) {
  96             this.tcmm = tcmm;
  97             cName = tcmm.getClass().getName();
  98         }
  99 
 100         public long loadProfile(byte[] data) {
 101             System.err.print(cName + ".loadProfile");
 102             long profileID = tcmm.loadProfile(data);
 103             System.err.printf("(ID=%x)\n", profileID);
 104             return profileID;
 105         }
 106 
 107         public void freeProfile(long profileID) {
 108             System.err.printf(cName + ".freeProfile(ID=%x)\n", profileID);
 109             tcmm.freeProfile(profileID);
 110         }
 111 
 112         public int getProfileSize(long profileID) {
 113             System.err.print(cName + ".getProfileSize(ID=" + profileID + ")");
 114             int size = tcmm.getProfileSize(profileID);
 115             System.err.println("=" + size);
 116             return size;
 117         }
 118 
 119         public void getProfileData(long profileID, byte[] data) {
 120             System.err.print(cName + ".getProfileData(ID=" + profileID + ") ");
 121             System.err.println("requested " + data.length + " byte(s)");
 122             tcmm.getProfileData(profileID, data);
 123         }
 124 
 125         public int getTagSize(long profileID, int tagSignature) {
 126             System.err.printf(cName + ".getTagSize(ID=%x, TagSig=%s)",
 127                               profileID, signatureToString(tagSignature));
 128             int size = tcmm.getTagSize(profileID, tagSignature);
 129             System.err.println("=" + size);
 130             return size;
 131         }
 132 
 133         public void getTagData(long profileID, int tagSignature,
 134                                byte[] data) {
 135             System.err.printf(cName + ".getTagData(ID=%x, TagSig=%s)",
 136                               profileID, signatureToString(tagSignature));
 137             System.err.println(" requested " + data.length + " byte(s)");
 138             tcmm.getTagData(profileID, tagSignature, data);
 139         }
 140 
 141         public void setTagData(long profileID, int tagSignature,
 142                                byte[] data) {
 143             System.err.print(cName + ".setTagData(ID=" + profileID +
 144                              ", TagSig=" + tagSignature + ")");
 145             System.err.println(" sending " + data.length + " byte(s)");
 146             tcmm.setTagData(profileID, tagSignature, data);
 147         }
 148 
 149         /* methods for creating ColorTransforms */
 150         public ColorTransform createTransform(ICC_Profile profile,
 151                                               int renderType,
 152                                               int transformType) {
 153             System.err.println(cName + ".createTransform(ICC_Profile,int,int)");
 154             return tcmm.createTransform(profile, renderType, transformType);
 155         }
 156 
 157         public ColorTransform createTransform(ColorTransform[] transforms) {
 158             System.err.println(cName + ".createTransform(ColorTransform[])");
 159             return tcmm.createTransform(transforms);
 160         }
 161 
 162         private static String signatureToString(int sig) {
 163             return String.format("%c%c%c%c",
 164                                  (char)(0xff & (sig >> 24)),
 165                                  (char)(0xff & (sig >> 16)),
 166                                  (char)(0xff & (sig >>  8)),
 167                                  (char)(0xff & (sig      )));
 168         }
 169     }
 170 }