Example usage for java.awt.color ColorSpace TYPE_RGB

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

Introduction

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

Prototype

int TYPE_RGB

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

Click Source Link

Document

Any of the family of RGB color spaces.

Usage

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

/**
 * Set the stroking color, specified as RGB.
 * /*from   www  . j  a v a  2 s. c  om*/
 * @param color
 *            The color to set.
 * @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[1], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

/**
 * Set the non stroking color, specified as RGB.
 * /* w  ww  .  ja  v a 2  s .  co m*/
 * @param color
 *            The color to set.
 * @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[1], colorComponents[2], colorComponents[3]);
    } else {
        throw new IOException("Error: unknown colorspace:" + colorSpace);
    }
}

From source file:org.apache.fop.afp.goca.GraphicsSetProcessColor.java

/** {@inheritDoc} */
public void writeToStream(OutputStream os) throws IOException {
    float[] colorComponents = color.getColorComponents(null);

    // COLSPCE/*from  ww  w  .j  a  v a2  s  .  c  o  m*/
    byte colspace;
    ColorSpace cs = color.getColorSpace();
    int colSpaceType = cs.getType();
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    byte[] colsizes;
    if (colSpaceType == ColorSpace.TYPE_CMYK) {
        colspace = CMYK;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x08 };
        for (int i = 0; i < colorComponents.length; i++) {
            baout.write(Math.round(colorComponents[i] * 255));
        }
    } else if (colSpaceType == ColorSpace.TYPE_RGB) {
        colspace = RGB;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x00 };
        for (int i = 0; i < colorComponents.length; i++) {
            baout.write(Math.round(colorComponents[i] * 255));
        }
    } else if (cs instanceof CIELabColorSpace) {
        colspace = CIELAB;
        colsizes = new byte[] { 0x08, 0x08, 0x08, 0x00 };
        DataOutput dout = new java.io.DataOutputStream(baout);
        //According to GOCA, I'd expect the multiplicator below to be 255f, not 100f
        //But only IBM AFP Workbench seems to support Lab colors and it requires "c * 100f"
        int l = Math.round(colorComponents[0] * 100f);
        int a = Math.round(colorComponents[1] * 255f) - 128;
        int b = Math.round(colorComponents[2] * 255f) - 128;
        dout.writeByte(l);
        dout.writeByte(a);
        dout.writeByte(b);
    } else {
        throw new IllegalStateException();
    }

    int len = getDataLength();
    byte[] data = new byte[12];
    data[0] = getOrderCode(); // GSPCOL order code
    data[1] = (byte) (len - 2); // LEN
    data[2] = 0x00; // reserved; must be zero
    data[3] = colspace; // COLSPCE
    data[4] = 0x00; // reserved; must be zero
    data[5] = 0x00; // reserved; must be zero
    data[6] = 0x00; // reserved; must be zero
    data[7] = 0x00; // reserved; must be zero
    data[8] = colsizes[0]; // COLSIZE(S)
    data[9] = colsizes[1];
    data[10] = colsizes[2];
    data[11] = colsizes[3];

    os.write(data);
    baout.writeTo(os);
}

From source file:org.apache.fop.render.afp.AFPImageHandlerRawJPEG.java

/** {@inheritDoc} */
public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException {
    AFPRenderingContext afpContext = (AFPRenderingContext) context;

    AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo) createDataObjectInfo();
    AFPPaintingState paintingState = afpContext.getPaintingState();

    // set resource information
    setResourceInformation(imageObjectInfo, image.getInfo().getOriginalURI(),
            afpContext.getForeignAttributes());
    setDefaultResourceLevel(imageObjectInfo, afpContext.getResourceManager());

    // Positioning
    imageObjectInfo.setObjectAreaInfo(createObjectAreaInfo(paintingState, pos));
    updateIntrinsicSize(imageObjectInfo, paintingState, image.getSize());

    // Image content
    ImageRawJPEG jpeg = (ImageRawJPEG) image;
    imageObjectInfo.setCompression(ImageContent.COMPID_JPEG);
    ColorSpace cs = jpeg.getColorSpace();
    switch (cs.getType()) {
    case ColorSpace.TYPE_GRAY:
        imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
        imageObjectInfo.setColor(false);
        imageObjectInfo.setBitsPerPixel(8);
        break;/*ww w  .j  a va  2 s  .c  om*/
    case ColorSpace.TYPE_RGB:
        imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS11);
        imageObjectInfo.setColor(true);
        imageObjectInfo.setBitsPerPixel(24);
        break;
    case ColorSpace.TYPE_CMYK:
        imageObjectInfo.setMimeType(MimeConstants.MIME_AFP_IOCA_FS45);
        imageObjectInfo.setColor(true);
        imageObjectInfo.setBitsPerPixel(32);
        break;
    default:
        throw new IllegalStateException("Color space of JPEG image not supported: " + cs);
    }

    boolean included = afpContext.getResourceManager().tryIncludeObject(imageObjectInfo);
    if (!included) {
        log.debug("Embedding undecoded JPEG as IOCA image...");
        InputStream inputStream = jpeg.createInputStream();
        try {
            imageObjectInfo.setData(IOUtils.toByteArray(inputStream));
        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        // Create image
        afpContext.getResourceManager().createObject(imageObjectInfo);
    }
}

From source file:org.apache.fop.render.afp.AFPImageHandlerRawJPEG.java

/** {@inheritDoc} */
public boolean isCompatible(RenderingContext targetContext, Image image) {
    if (!(targetContext instanceof AFPRenderingContext)) {
        return false; //AFP-specific image handler
    }//from   w  w  w .j  a v a  2s.c  o m
    AFPRenderingContext context = (AFPRenderingContext) targetContext;
    AFPPaintingState paintingState = context.getPaintingState();
    if (!paintingState.canEmbedJpeg()) {
        return false;
    }
    if (paintingState.getBitsPerPixel() < 8) {
        return false; //This would stand in the way of dithering and cause exceptions
    }
    if (image == null) {
        return true; //Don't know the image format, yet
    }
    if (image instanceof ImageRawJPEG) {
        ImageRawJPEG jpeg = (ImageRawJPEG) image;
        ColorSpace cs = jpeg.getColorSpace();
        switch (cs.getType()) {
        case ColorSpace.TYPE_GRAY:
        case ColorSpace.TYPE_RGB:
            //ok
            break;
        case ColorSpace.TYPE_CMYK:
            if (!paintingState.isCMYKImagesSupported()) {
                return false; //CMYK is disabled
                //Note: you may need to disable this image handler through configuration
                //if you want to paint a CMYK JPEG on 24bit and less configurations.
            }
            break;
        default:
            return false; //not supported
        }

        if (jpeg.getSOFType() != JPEGConstants.SOF0) {
            return false; //We'll let only baseline DCT through.
        }
        return true;
    }
    return false;
}

From source file:org.apache.fop.render.pdf.AbstractImageAdapter.java

/**
 * This is to be used by populateXObjectDictionary() when the image is palette based.
 * @param dict the dictionary to fill in
 * @param icm the image color model// w ww .j  av a 2 s. c  o  m
 */
protected void populateXObjectDictionaryForIndexColorModel(PDFDictionary dict, IndexColorModel icm) {
    PDFArray indexed = new PDFArray(dict);
    indexed.add(new PDFName("Indexed"));
    if (icm.getColorSpace().getType() != ColorSpace.TYPE_RGB) {
        log.warn("Indexed color space is not using RGB as base color space."
                + " The image may not be handled correctly." + " Base color space: " + icm.getColorSpace()
                + " Image: " + image.getInfo());
    }
    indexed.add(new PDFName(toPDFColorSpace(icm.getColorSpace()).getName()));
    int c = icm.getMapSize();
    int hival = c - 1;
    if (hival > MAX_HIVAL) {
        throw new UnsupportedOperationException("hival must not go beyond " + MAX_HIVAL);
    }
    indexed.add(Integer.valueOf(hival));
    int[] palette = new int[c];
    icm.getRGBs(palette);
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    for (int i = 0; i < c; i++) {
        // TODO Probably doesn't work for non RGB based color spaces
        // See log warning above
        int entry = palette[i];
        baout.write((entry & 0xFF0000) >> 16);
        baout.write((entry & 0xFF00) >> 8);
        baout.write(entry & 0xFF);
    }
    indexed.add(baout.toByteArray());

    dict.put("ColorSpace", indexed);
    dict.put("BitsPerComponent", icm.getPixelSize());

    Integer index = getIndexOfFirstTransparentColorInPalette(icm);
    if (index != null) {
        PDFArray mask = new PDFArray(dict);
        mask.add(index);
        mask.add(index);
        dict.put("Mask", mask);
    }
}

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

/**
 * Set the stroking color, specified as RGB.
 *
 * @param color The color to set./*from   ww  w .j a v  a2  s .  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./*  w  ww  .jav a2s  . co  m*/
 * @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.photovault.image.PhotovaultImage.java

/**
 Create a color mapping LUT based on current color channel mapping.
 @return the created LUT. If not channel mapping is specified, returns an
 identity mapping.//from   w  ww  . j av  a 2s  .  co  m
 */
private LookupTableJAI createColorMappingLUT() {
    ColorModel colorModel = this.getCorrectedImageColorModel();
    ColorSpace colorSpace = colorModel.getColorSpace();

    int[] componentSizes = colorModel.getComponentSize();
    ColorCurve valueCurve = null;
    ColorCurve[] componentCurves = new ColorCurve[componentSizes.length];
    boolean[] applyValueCurve = new boolean[componentSizes.length];
    for (int n = 0; n < componentSizes.length; n++) {
        applyValueCurve[n] = false;
    }

    if (channelMap != null) {
        valueCurve = channelMap.getChannelCurve("value");
        switch (colorSpace.getType()) {
        case ColorSpace.TYPE_GRAY:
            // Gray scale image - just apply value curve to 1st channel
            componentCurves[0] = valueCurve;
            break;
        case ColorSpace.TYPE_RGB:
            componentCurves[0] = channelMap.getChannelCurve("red");
            componentCurves[1] = channelMap.getChannelCurve("green");
            componentCurves[2] = channelMap.getChannelCurve("blue");
            applyValueCurve[0] = true;
            applyValueCurve[1] = true;
            applyValueCurve[2] = true;
            break;
        default:
            // Unsupported color format. Just return identity transform
            break;
        }
    }
    if (valueCurve == null) {
        // No color mapping found, use identity mapping
        valueCurve = new ColorCurve();
    }

    LookupTableJAI jailut = null;
    if (componentSizes[0] == 8) {
        byte[][] lut = new byte[componentSizes.length][256];
        double dx = 1.0 / 256.0;
        for (int band = 0; band < colorModel.getNumComponents(); band++) {
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band < componentCurves.length && componentCurves[band] != null) {
                    val = componentCurves[band].getValue(val);
                }
                if (band < applyValueCurve.length && applyValueCurve[band]) {
                    val = valueCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (byte) ((lut[band].length - 1) * val);
            }
        }
        jailut = new LookupTableJAI(lut);
    } else if (componentSizes[0] == 16) {
        short[][] lut = new short[componentSizes.length][0x10000];
        double dx = 1.0 / 65536.0;
        for (int band = 0; band < colorModel.getNumComponents(); band++) {
            for (int n = 0; n < lut[band].length; n++) {
                double x = dx * n;
                double val = x;
                if (band < componentCurves.length && componentCurves[band] != null) {
                    val = componentCurves[band].getValue(val);
                }
                if (band < applyValueCurve.length && applyValueCurve[band]) {
                    val = valueCurve.getValue(val);
                }
                val = Math.max(0.0, Math.min(val, 1.0));
                lut[band][n] = (short) ((lut[band].length - 1) * val);
            }
        }
        jailut = new LookupTableJAI(lut, true);
    } else {
        log.error("Unsupported data type with with = " + componentSizes[0]);
    }
    return jailut;
}

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 a2s .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());
    }
}