public static ColorModel getCompatibleColorModel(SampleModel sm,
                                                     Map config) {
        ColorModel cm = null;

        if(config == null ||
           !Boolean.FALSE.equals(
               config.get(JAI.KEY_DEFAULT_COLOR_MODEL_ENABLED))) {

            // Set the default ColorModel

            if(config != null &&
               config.containsKey(JAI.KEY_DEFAULT_COLOR_MODEL_METHOD)) {
                // Attempt to retrieve the default CM Method.
                Method cmMethod =
                    (Method)config.get(JAI.KEY_DEFAULT_COLOR_MODEL_METHOD);

                // Check method compatibility.
                Class[] paramTypes = cmMethod.getParameterTypes();
                if((cmMethod.getModifiers() & Modifier.STATIC) !=
                   Modifier.STATIC) {
                    // Method must be static.
                    throw new RuntimeException(JaiI18N.getString("ImageUtil1"));
                } else if(cmMethod.getReturnType() != ColorModel.class) {
                    // Method must return a ColorModel.
                    throw new RuntimeException(JaiI18N.getString("ImageUtil2"));
                } else if(paramTypes.length != 1 ||
                          !paramTypes[0].equals(SampleModel.class)) {
                    // Unique Method parameter must be a SampleModel.
                    throw new RuntimeException(JaiI18N.getString("ImageUtil3"));
                }

                // Set the default ColorModel.
                try {
                    // Attempt to use the supplied Method.
                    Object[] args = new Object[] {sm};
                    cm = (ColorModel)cmMethod.invoke(null, args);
                } catch(Exception e) {
                    String message =
                        JaiI18N.getString("ImageUtil4") + cmMethod.getName();
                    sendExceptionToListener(message ,
                                            new ImagingException(message, e));
/*
                    // XXX Is this a reasonable Exception to throw?
                    throw new RuntimeException(cmMethod.getName()+" "+
                                               e.getMessage());
*/
                }
            } else { // No default method hint set.
                // Use PlanarImage method.
                cm = PlanarImage.createColorModel(sm);
            }
        }

        return cm;
    }