Example usage for java.awt.color ColorSpace getNumComponents

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

Introduction

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

Prototype

public int getNumComponents() 

Source Link

Document

Returns the number of components of this ColorSpace.

Usage

From source file:org.apache.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory.java

/**
 * This will create the correct color space from a java colorspace.
 *
 * @param doc The doc to potentiall write information to.
 * @param cs The awt colorspace.//from  w w  w.j  a  v a 2  s  . co  m
 *
 * @return The color space.
 *
 * @throws IOException If the color space name is unknown.
 */
public static PDColorSpace createColorSpace(PDDocument doc, ColorSpace cs) throws IOException {
    PDColorSpace retval = null;
    if (cs.isCS_sRGB()) {
        retval = PDDeviceRGB.INSTANCE;
    } else if (cs instanceof ICC_ColorSpace) {
        ICC_ColorSpace ics = (ICC_ColorSpace) cs;
        PDICCBased pdCS = new PDICCBased(doc);
        retval = pdCS;
        COSArray ranges = new COSArray();
        for (int i = 0; i < cs.getNumComponents(); i++) {
            ranges.add(new COSFloat(ics.getMinValue(i)));
            ranges.add(new COSFloat(ics.getMaxValue(i)));
        }
        PDStream iccData = pdCS.getPDStream();
        OutputStream output = null;
        try {
            output = iccData.createOutputStream();
            output.write(ics.getProfile().getData());
        } finally {
            if (output != null) {
                output.close();
            }
        }
        pdCS.setNumberOfComponents(cs.getNumComponents());
    } else {
        throw new IOException("Not yet implemented:" + cs);
    }
    return retval;
}

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   w w w. j  av a 2s.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
    }
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

static void writeImageCommand(PSDictionary imageDict, Dimension imgDim, ColorSpace colorSpace,
        boolean invertImage, PSGenerator gen) throws IOException {
    imageDict.put("/ImageType", "1");
    imageDict.put("/Width", Integer.toString(imgDim.width));
    imageDict.put("/Height", Integer.toString(imgDim.height));
    String decodeArray = getDecodeArray(colorSpace.getNumComponents(), invertImage);
    imageDict.put("/Decode", decodeArray);
    // Setup scanning for left-to-right and top-to-bottom
    imageDict.put("/ImageMatrix", "[" + imgDim.width + " 0 0 " + imgDim.height + " 0 0]");

    prepareColorspace(gen, colorSpace);//from  www.j  a va 2s . c o  m
    gen.write(imageDict.toString());
    gen.writeln(" image");
}