package com.twelvemonkeys.imageio.color;

import com.twelvemonkeys.lang.SystemUtil;

import java.awt.color.ICC_Profile;

/**
 * ProfileCleaner.
 *
 * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
 * @author last modified by $Author: harald.kuhr$
 * @version $Id: ProfileCleaner.java,v 1.0 06/01/15 harald.kuhr Exp$
 */
interface ICCProfileSanitizer {
    void fixProfile(ICC_Profile profile, byte[] profileHeader);

    static class Factory {
        static ICCProfileSanitizer get() {
            // Strategy pattern:
            //  - KCMSSanitizerStrategy - Current behaviour, default for Java 1.6 and Oracle JRE 1.7
            //  - LCMSSanitizerStrategy - New behaviour, default for OpenJDK 1.7 and all java 1.8
            //              (unless KCMS switch -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider present)
            // TODO: Allow user-specific strategy selection, should heuristics not work..?
            // -Dcom.twelvemonkeys.imageio.color.ICCProfileSanitizer=com.foo.bar.FooCMSSanitizer

            ICCProfileSanitizer instance;

            // Explicit System properties
            if ("sun.java2d.cmm.kcms.KcmsServiceProvider".equals(System.getProperty("sun.java2d.cmm")) && SystemUtil.isClassAvailable("sun.java2d.cmm.kcms.CMM")) {
                instance = new KCMSSanitizerStrategy();
            }
            else if ("sun.java2d.cmm.lcms.LcmsServiceProvider".equals(System.getProperty("sun.java2d.cmm")) && SystemUtil.isClassAvailable("sun.java2d.cmm.lcms.LCMS")) {
                instance = new LCMSSanitizerStrategy();
            }
            // Default for Java 1.8+ or OpenJDK 1.7+ (no KCMS available)
            else if (SystemUtil.isClassAvailable("java.util.stream.Stream")
                    || SystemUtil.isClassAvailable("java.lang.invoke.CallSite") && !SystemUtil.isClassAvailable("sun.java2d.cmm.kcms.CMM")) {
                instance = new LCMSSanitizerStrategy();
            }
            // Default for all Java versions <= 1.7
            else {
                instance = new KCMSSanitizerStrategy();
            }

            if (ColorSpaces.DEBUG) {
                System.out.println("ICC ProfileCleaner instance: " + instance.getClass().getName());
            }

            return instance;
        }
    }
}

@@ -121,28 +121,33 @@
 %0A/**%0A * 
+ICC
 Profile
-Clean
+Sanitiz
 er.%0A *%0A 
@@ -1052,182 +1052,1620 @@
     
-ICCProfileSanitizer instance;%0A%0A            // Explicit System properties%0A            if (%22sun.java2d.cmm.kcms.KcmsServiceProvider%22.equals(System.getProperty(%22sun.java2d.cmm%22)
+// TODO: Support for explicit Java 7 settings: sun.java2d.cmm.kcms.CMM%0A            // (the CMSManager was changed from using direct class to ServiceProvider in Java 8,%0A            // so the class names/interfaces are different).%0A%0A            // Here's the evolution of Java Color Management:%0A%0A            // Java 6:%0A            // sun.awt.color.CMM (as the one and only)%0A%0A            // Java 7:%0A            // sun.java2d.cmm.CMSManager (using default sun.java2d.cmm=sun.java2d.cmm.kcms.CMM)%0A            // sun.java2d.cmm.PCMM%0A            // sun.java2d.cmm.kcms.CMM implements PCMM (similar to Java 6 CMM)%0A            // sun.javard.cmm.lcms.LCMS implements PCMM%0A%0A            // Java 8:%0A            // sun.java2d.cmm.CMSManager (using default sun.java2d.cmm=sun.java2d.cmm.lcms.LcmsServiceProvider)%0A            // sun.java2d.cmm.CMMServiceProvider (getModule() method, that returns PCMM)%0A            // sun.java2d.cmm.PCMM%0A            // sun.java2d.cmm.kcms.KcmsServiceProvider%0A            // sun.java2d.cmm.kcms.CMM implements PCMM (similar to Java 6 CMM)%0A            // sun.java2d.cmm.lcms.LcmsServiceProvider%0A            // sun.java2d.cmm.lcms.LCMS implements PCMM%0A%0A            // TODO: Consider a different option, invoking CMSManager.getModule() through reflection to get actual used instance%0A            // Default to using a NullSanitizerStrategy on non-Sun/Oracle systems?%0A%0A            ICCProfileSanitizer instance;%0A%0A            // Explicit System properties%0A            String cmmProperty = System.getProperty(%22sun.java2d.cmm%22);%0A            if (%22sun.java2d.cmm.kcms.KcmsServiceProvider%22.equals(cmmProperty
 ) &&
@@ -2867,44 +2867,19 @@
 als(
-System.getProperty(%22sun.java2d.cmm%22)
+cmmProperty
 ) &&

Clean up + notes for ICC profile sanitizing.