1 /*
   2  * Copyright (c) 2006, 2014, 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 
  38 public class CMSManager {
  39     public static ColorSpace GRAYspace;       // These two fields allow access
  40     public static ColorSpace LINEAR_RGBspace; // to java.awt.color.ColorSpace
  41                                               // private fields from other
  42                                               // packages.  The fields are set
  43                                               // by java.awt.color.ColorSpace
  44                                               // and read by
  45                                               // java.awt.image.ColorModel.
  46 
  47     private static PCMM cmmImpl = null;
  48 
  49     public static synchronized PCMM getModule() {
  50         if (cmmImpl != null) {
  51             return cmmImpl;
  52         }
  53 
  54         GetPropertyAction gpa = new GetPropertyAction("sun.java2d.cmm");
  55         String cmmProviderClass = AccessController.doPrivileged(gpa);
  56         CMMServiceProvider provider = null;
  57         if (cmmProviderClass != null) {
  58             try {
  59                 @SuppressWarnings("deprecation")
  60                 Object o = Class.forName(cmmProviderClass).newInstance();
  61                 provider = (CMMServiceProvider)o;
  62             } catch (ReflectiveOperationException e) {
  63             }
  64         }
  65         if (provider == null) {
  66             provider = new sun.java2d.cmm.lcms.LcmsServiceProvider();
  67         }
  68 
  69         cmmImpl = provider.getColorManagementModule();
  70 
  71         if (cmmImpl == null) {
  72             throw new CMMException("Cannot initialize Color Management System."+
  73                                    "No CM module found");
  74         }
  75 
  76         gpa = new GetPropertyAction("sun.java2d.cmm.trace");
  77         String cmmTrace = AccessController.doPrivileged(gpa);
  78         if (cmmTrace != null) {
  79             cmmImpl = new CMMTracer(cmmImpl);
  80         }
  81 
  82         return cmmImpl;
  83     }
  84 
  85     static synchronized boolean canCreateModule() {
  86         return (cmmImpl == null);
  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 Profile loadProfile(byte[] data) {
 101             System.err.print(cName + ".loadProfile");
 102             Profile p = tcmm.loadProfile(data);
 103             System.err.printf("(ID=%s)\n", p.toString());
 104             return p;
 105         }
 106 
 107         public void freeProfile(Profile p) {
 108             System.err.printf(cName + ".freeProfile(ID=%s)\n", p.toString());
 109             tcmm.freeProfile(p);
 110         }
 111 
 112         public int getProfileSize(Profile p) {
 113             System.err.print(cName + ".getProfileSize(ID=" + p + ")");
 114             int size = tcmm.getProfileSize(p);
 115             System.err.println("=" + size);
 116             return size;
 117         }
 118 
 119         public void getProfileData(Profile p, byte[] data) {
 120             System.err.print(cName + ".getProfileData(ID=" + p + ") ");
 121             System.err.println("requested " + data.length + " byte(s)");
 122             tcmm.getProfileData(p, data);
 123         }
 124 
 125         public int getTagSize(Profile p, int tagSignature) {
 126             System.err.printf(cName + ".getTagSize(ID=%x, TagSig=%s)",
 127                               p, signatureToString(tagSignature));
 128             int size = tcmm.getTagSize(p, tagSignature);
 129             System.err.println("=" + size);
 130             return size;
 131         }
 132 
 133         public void getTagData(Profile p, int tagSignature,
 134                                byte[] data) {
 135             System.err.printf(cName + ".getTagData(ID=%x, TagSig=%s)",
 136                               p, signatureToString(tagSignature));
 137             System.err.println(" requested " + data.length + " byte(s)");
 138             tcmm.getTagData(p, tagSignature, data);
 139         }
 140 
 141         public void setTagData(Profile p, int tagSignature,
 142                                byte[] data) {
 143             System.err.print(cName + ".setTagData(ID=" + p +
 144                              ", TagSig=" + tagSignature + ")");
 145             System.err.println(" sending " + data.length + " byte(s)");
 146             tcmm.setTagData(p, 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 }