Example usage for java.awt.color ICC_Profile getNumComponents

List of usage examples for java.awt.color ICC_Profile getNumComponents

Introduction

In this page you can find the example usage for java.awt.color ICC_Profile getNumComponents.

Prototype

public int getNumComponents() 

Source Link

Document

Returns the number of color components in the "input" color space of this profile.

Usage

From source file:org.apache.xmlgraphics.image.loader.impl.ImageLoaderRawJPEG.java

private ICC_Profile buildICCProfile(ImageInfo info, ColorSpace colorSpace, ByteArrayOutputStream iccStream)
        throws IOException, ImageException {
    if (iccStream != null && iccStream.size() > 0) {
        if (log.isDebugEnabled()) {
            log.debug("Effective ICC profile size: " + iccStream.size());
        }/*from   ww  w .jav  a2  s.c om*/
        final int alignment = 4;
        int padding = (alignment - (iccStream.size() % alignment)) % alignment;
        if (padding != 0) {
            try {
                iccStream.write(new byte[padding]);
            } catch (IOException ioe) {
                throw new IOException("Error while aligning ICC stream: " + ioe.getMessage());
            }
        }

        ICC_Profile iccProfile = null;
        try {
            iccProfile = ICC_Profile.getInstance(iccStream.toByteArray());
            if (log.isDebugEnabled()) {
                log.debug("JPEG has an ICC profile: " + iccProfile.toString());
            }
        } catch (IllegalArgumentException iae) {
            log.warn("An ICC profile is present in the JPEG file but it is invalid (" + iae.getMessage()
                    + "). The color profile will be ignored. (" + info.getOriginalURI() + ")");
            return null;
        }
        if (iccProfile.getNumComponents() != colorSpace.getNumComponents()) {
            log.warn("The number of components of the ICC profile (" + iccProfile.getNumComponents()
                    + ") doesn't match the image (" + colorSpace.getNumComponents()
                    + "). Ignoring the ICC color profile.");
            return null;
        } else {
            return iccProfile;
        }
    } else {
        return null; //no ICC profile available
    }
}