Example usage for java.awt.color ColorSpace TYPE_CMYK

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

Introduction

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

Prototype

int TYPE_CMYK

To view the source code for java.awt.color ColorSpace TYPE_CMYK.

Click Source Link

Document

Any of the family of CMYK color spaces.

Usage

From source file:org.apache.pdfbox.pdmodel.edit.PDPageContentStream.java

/**
 * Set the stroking color, specified as RGB.
 *
 * @param color The color to set.//from  w w w  .j a v a 2s .c  om
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setStrokingColor(Color color) throws IOException {
    ColorSpace colorSpace = color.getColorSpace();
    if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
        setStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        color.getColorComponents(colorComponents);
        setStrokingColor(colorComponents[0]);
    } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        color.getColorComponents(colorComponents);
        setStrokingColor(colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

From source file:org.apache.pdfbox.pdmodel.edit.PDPageContentStream.java

/**
 * Set the non stroking color, specified as RGB.
 *
 * @param color The color to set./* www . j  a  v  a2  s.c om*/
 * @throws IOException If an IO error occurs while writing to the stream.
 */
public void setNonStrokingColor(Color color) throws IOException {
    ColorSpace colorSpace = color.getColorSpace();
    if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
        setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0]);
    } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        color.getColorComponents(colorComponents);
        setNonStrokingColor(colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

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

/** {@inheritDoc} */
public Image loadImage(ImageInfo info, Map hints, ImageSessionContext session)
        throws ImageException, IOException {
    if (!MimeConstants.MIME_JPEG.equals(info.getMimeType())) {
        throw new IllegalArgumentException(
                "ImageInfo must be from a image with MIME type: " + MimeConstants.MIME_JPEG);
    }/*from  w ww  .  jav a  2 s. c  om*/

    ColorSpace colorSpace = null;
    boolean appeFound = false;
    int sofType = 0;
    ByteArrayOutputStream iccStream = null;

    Source src = session.needSource(info.getOriginalURI());
    ImageInputStream in = ImageUtil.needImageInputStream(src);
    JPEGFile jpeg = new JPEGFile(in);
    in.mark();
    try {
        outer: while (true) {
            int reclen;
            int segID = jpeg.readMarkerSegment();
            if (log.isTraceEnabled()) {
                log.trace("Seg Marker: " + Integer.toHexString(segID));
            }
            switch (segID) {
            case EOI:
                log.trace("EOI found. Stopping.");
                break outer;
            case SOS:
                log.trace("SOS found. Stopping early."); //TODO Not sure if this is safe
                break outer;
            case SOI:
            case NULL:
                break;
            case SOF0: //baseline
            case SOF1: //extended sequential DCT
            case SOF2: //progressive (since PDF 1.3)
            case SOFA: //progressive (since PDF 1.3)
                sofType = segID;
                if (log.isTraceEnabled()) {
                    log.trace("SOF: " + Integer.toHexString(sofType));
                }
                in.mark();
                try {
                    reclen = jpeg.readSegmentLength();
                    in.skipBytes(1); //data precision
                    in.skipBytes(2); //height
                    in.skipBytes(2); //width
                    int numComponents = in.readUnsignedByte();
                    if (numComponents == 1) {
                        colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
                    } else if (numComponents == 3) {
                        colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
                    } else if (numComponents == 4) {
                        colorSpace = DeviceCMYKColorSpace.getInstance();
                    } else {
                        throw new ImageException("Unsupported ColorSpace for image " + info
                                + ". The number of components supported are 1, 3 and 4.");
                    }
                } finally {
                    in.reset();
                }
                in.skipBytes(reclen);
                break;
            case APP2: //ICC (see ICC1V42.pdf)
                in.mark();
                try {
                    reclen = jpeg.readSegmentLength();
                    // Check for ICC profile
                    byte[] iccString = new byte[11];
                    in.readFully(iccString);
                    in.skipBytes(1); //string terminator (null byte)

                    if ("ICC_PROFILE".equals(new String(iccString, "US-ASCII"))) {
                        in.skipBytes(2); //chunk sequence number and total number of chunks
                        int payloadSize = reclen - 2 - 12 - 2;
                        if (ignoreColorProfile(hints)) {
                            log.debug("Ignoring ICC profile data in JPEG");
                            in.skipBytes(payloadSize);
                        } else {
                            byte[] buf = new byte[payloadSize];
                            in.readFully(buf);
                            if (iccStream == null) {
                                if (log.isDebugEnabled()) {
                                    log.debug("JPEG has an ICC profile");
                                    DataInputStream din = new DataInputStream(new ByteArrayInputStream(buf));
                                    log.debug("Declared ICC profile size: " + din.readInt());
                                }
                                //ICC profiles can be split into several chunks
                                //so collect in a byte array output stream
                                iccStream = new ByteArrayOutputStream();
                            }
                            iccStream.write(buf);
                        }
                    }
                } finally {
                    in.reset();
                }
                in.skipBytes(reclen);
                break;
            case APPE: //Adobe-specific (see 5116.DCT_Filter.pdf)
                in.mark();
                try {
                    reclen = jpeg.readSegmentLength();
                    // Check for Adobe header
                    byte[] adobeHeader = new byte[5];
                    in.readFully(adobeHeader);

                    if ("Adobe".equals(new String(adobeHeader, "US-ASCII"))) {
                        // The reason for reading the APPE marker is that Adobe Photoshop
                        // generates CMYK JPEGs with inverted values. The correct thing
                        // to do would be to interpret the values in the marker, but for now
                        // only assume that if APPE marker is present and colorspace is CMYK,
                        // the image is inverted.
                        appeFound = true;
                    }
                } finally {
                    in.reset();
                }
                in.skipBytes(reclen);
                break;
            default:
                jpeg.skipCurrentMarkerSegment();
            }
        }
    } finally {
        in.reset();
    }

    ICC_Profile iccProfile = buildICCProfile(info, colorSpace, iccStream);
    if (iccProfile == null && colorSpace == null) {
        throw new ImageException("ColorSpace could not be identified for JPEG image " + info);
    }

    boolean invertImage = false;
    if (appeFound && colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        if (log.isDebugEnabled()) {
            log.debug("JPEG has an Adobe APPE marker. Note: CMYK Image will be inverted. ("
                    + info.getOriginalURI() + ")");
        }
        invertImage = true;
    }

    ImageRawJPEG rawImage = new ImageRawJPEG(info, ImageUtil.needInputStream(src), sofType, colorSpace,
            iccProfile, invertImage);
    return rawImage;
}

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

private boolean establishColorFromColor(StringBuffer codeBuffer, Color color) {
    //Important: see above note about color handling!
    float[] comps = color.getColorComponents(null);
    if (color.getColorSpace().getType() == ColorSpace.TYPE_CMYK) {
        // colorspace is CMYK
        writeSetColor(codeBuffer, comps, "setcmykcolor");
        return true;
    }/*from w ww  .  ja  v  a2s . co  m*/
    return false;
}

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

private static String getColorSpaceName(ColorSpace colorSpace) {
    if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
        return ("/DeviceCMYK");
    } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
        return ("/DeviceGray");
    } else {//  w w w  .  j  a va  2 s .  c  om
        return ("/DeviceRGB");
    }
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

public static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage) {
    if (awtImage.getColorModel().getNumComponents() == 1) {
        // 256 color (gray) JPEG
        return PDDeviceGray.INSTANCE;
    }/*from w  ww. j a  v a  2 s .c o  m*/

    ColorSpace awtColorSpace = awtImage.getColorModel().getColorSpace();
    if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB()) {
        throw new UnsupportedOperationException("ICC color spaces not implemented");
    }

    switch (awtColorSpace.getType()) {
    case ColorSpace.TYPE_RGB:
        return PDDeviceRGB.INSTANCE;
    case ColorSpace.TYPE_GRAY:
        return PDDeviceGray.INSTANCE;
    case ColorSpace.TYPE_CMYK:
        return PDDeviceCMYK.INSTANCE;
    default:
        throw new UnsupportedOperationException("color space not implemented: " + awtColorSpace.getType());
    }
}