Example usage for java.awt.color ColorSpace TYPE_GRAY

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

Introduction

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

Prototype

int TYPE_GRAY

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

Click Source Link

Document

Any of the family of GRAY color spaces.

Usage

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

/**
 * Set the 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 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.
 * /*from  w w w  .  ja  v  a  2 s . c o 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:lucee.runtime.img.Image.java

public void resize(int width, int height, int interpolation, double blurFactor) throws ExpressionException {

    ColorModel cm = image().getColorModel();

    if (interpolation == IP_HIGHESTPERFORMANCE) {
        interpolation = IPC_BICUBIC;/*from   w ww.  j a  va  2 s . c om*/
    }

    if (cm.getColorSpace().getType() == ColorSpace.TYPE_GRAY && cm.getComponentSize()[0] == 8) {
        if (interpolation == IP_HIGHESTQUALITY || interpolation == IP_HIGHPERFORMANCE
                || interpolation == IP_HIGHQUALITY || interpolation == IP_MEDIUMPERFORMANCE
                || interpolation == IP_MEDIUMQUALITY) {
            interpolation = IPC_BICUBIC;
        }
        if (interpolation != IPC_BICUBIC && interpolation != IPC_BILINEAR && interpolation != IPC_NEAREST) {
            throw new ExpressionException("invalid grayscale interpolation");
        }
    }

    if (interpolation <= IPC_MAX) {
        resizeImage(width, height, interpolation);
    } else {
        image(ImageResizer.resize(image(), width, height, interpolation, blurFactor));

    }
}

From source file:org.apache.carbondata.sdk.file.ImageTest.java

public void binaryToCarbonWithHWD(String sourceImageFolder, String outputPath, String preDestPath,
        String sufAnnotation, final String sufImage, int numToWrite) throws Exception {
    int num = 1;// ww w.  j av a  2  s  . co m
    Field[] fields = new Field[7];
    fields[0] = new Field("height", DataTypes.INT);
    fields[1] = new Field("width", DataTypes.INT);
    fields[2] = new Field("depth", DataTypes.INT);
    fields[3] = new Field("binaryName", DataTypes.STRING);
    fields[4] = new Field("binary", DataTypes.BINARY);
    fields[5] = new Field("labelName", DataTypes.STRING);
    fields[6] = new Field("labelContent", DataTypes.STRING);

    byte[] originBinary = null;

    // read and write image data
    for (int j = 0; j < num; j++) {

        Object[] files = listFiles(sourceImageFolder, sufImage).toArray();

        int index = 0;

        if (null != files) {
            CarbonWriter writer = CarbonWriter.builder().outputPath(outputPath).withCsvInput(new Schema(fields))
                    .withBlockSize(256).writtenBy("SDKS3Example").withPageSizeInMb(1).build();

            for (int i = 0; i < files.length; i++) {
                if (0 == index % numToWrite) {
                    writer.close();
                    writer = CarbonWriter.builder().outputPath(outputPath).withCsvInput(new Schema(fields))
                            .withBlockSize(256).writtenBy("SDKS3Example").withPageSizeInMb(1).build();
                }
                index++;

                // read image and encode to Hex
                File file = new File((String) files[i]);
                System.out.println(file.getCanonicalPath());
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                int depth = 0;
                boolean isGray;
                boolean hasAlpha;
                BufferedImage bufferedImage = null;
                try {
                    bufferedImage = ImageIO.read(file);
                    isGray = bufferedImage.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_GRAY;
                    hasAlpha = bufferedImage.getColorModel().hasAlpha();

                    if (isGray) {
                        depth = 1;
                    } else if (hasAlpha) {
                        depth = 4;
                    } else {
                        depth = 3;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(i);
                    ImageInputStream stream = new FileImageInputStream(new File(file.getCanonicalPath()));
                    Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

                    Exception lastException = null;
                    while (iter.hasNext()) {
                        ImageReader reader = null;
                        try {
                            reader = (ImageReader) iter.next();
                            ImageReadParam param = reader.getDefaultReadParam();
                            reader.setInput(stream, true, true);
                            Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);

                            while (imageTypes.hasNext()) {
                                ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                System.out
                                        .println(imageTypeSpecifier.getColorModel().getColorSpace().getType());
                                int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                                if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                                    param.setDestinationType(imageTypeSpecifier);
                                    break;
                                }
                            }
                            bufferedImage = reader.read(0, param);
                            isGray = bufferedImage.getColorModel().getColorSpace()
                                    .getType() == ColorSpace.TYPE_GRAY;
                            hasAlpha = bufferedImage.getColorModel().hasAlpha();

                            if (isGray) {
                                depth = 1;
                            } else if (hasAlpha) {
                                depth = 4;
                            } else {
                                depth = 3;
                            }
                            if (null != bufferedImage)
                                break;
                        } catch (Exception e2) {
                            lastException = e2;
                        } finally {
                            if (null != reader)
                                reader.dispose();
                        }
                    }
                    // If you don't have an image at the end of all readers
                    if (null == bufferedImage) {
                        if (null != lastException) {
                            throw lastException;
                        }
                    }
                } finally {
                    originBinary = new byte[bis.available()];
                    while ((bis.read(originBinary)) != -1) {
                    }

                    String txtFileName = file.getCanonicalPath().split(sufImage)[0] + sufAnnotation;
                    BufferedInputStream txtBis = new BufferedInputStream(new FileInputStream(txtFileName));
                    String txtValue = null;
                    byte[] txtBinary = null;
                    txtBinary = new byte[txtBis.available()];
                    while ((txtBis.read(txtBinary)) != -1) {
                        txtValue = new String(txtBinary, "UTF-8");
                    }
                    // write data
                    writer.write(new Object[] { bufferedImage.getHeight(), bufferedImage.getWidth(), depth,
                            file.getCanonicalPath(), originBinary, txtFileName, txtValue.replace("\n", "") });
                    bis.close();
                }
            }
            writer.close();
        }
    }

    CarbonReader reader = CarbonReader.builder(outputPath).build();

    System.out.println("\nData:");
    int i = 0;
    while (i < 20 && reader.hasNext()) {
        Object[] row = (Object[]) reader.readNextRow();

        byte[] outputBinary = (byte[]) row[1];
        System.out.println(row[2] + " " + row[3] + " " + row[4] + " " + row[5] + " image size:"
                + outputBinary.length + " " + row[0]);

        // save image, user can compare the save image and original image
        String destString = preDestPath + i + sufImage;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
        bos.write(outputBinary);
        bos.close();
        i++;
    }
    System.out.println("\nFinished");
    reader.close();
}

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;// w w w  .  j  a v a 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  om
    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

/**
 * Converts a ColorSpace object to a PDFColorSpace object.
 * @param cs ColorSpace instance//  w w  w . j ava 2 s .  co m
 * @return PDFColorSpace new converted object
 */
public static PDFDeviceColorSpace toPDFColorSpace(ColorSpace cs) {
    if (cs == null) {
        return null;
    }

    PDFDeviceColorSpace pdfCS = new PDFDeviceColorSpace(0);
    switch (cs.getType()) {
    case ColorSpace.TYPE_CMYK:
        pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
        break;
    case ColorSpace.TYPE_GRAY:
        pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_GRAY);
        break;
    default:
        pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
    }
    return pdfCS;
}

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

/**
 * Set the stroking color, specified as RGB.
 *
 * @param color The color to set.//w ww  .ja  v  a 2s  .  co  m
 * @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.  ja v a  2  s .  c o  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.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  .jav  a 2 s. c  o m
        return ("/DeviceRGB");
    }
}