Example usage for javax.imageio IIOImage getRenderedImage

List of usage examples for javax.imageio IIOImage getRenderedImage

Introduction

In this page you can find the example usage for javax.imageio IIOImage getRenderedImage.

Prototype

public RenderedImage getRenderedImage() 

Source Link

Document

Returns the currently set RenderedImage , or null if only a Raster is available.

Usage

From source file:ch5ImageWriter.java

/**
 * write out the output image specified by index imageIndex using the
 * parameters specified by the ImageWriteParam object param
 *///from   w  ww  . j  a  v  a 2s . com
public void write(IIOMetadata metadata, IIOImage iioimage, ImageWriteParam param) {
    Node root = null;
    Node dimensionsElementNode = null;

    if (iioimage.getRenderedImage() != null)
        raster = iioimage.getRenderedImage().getData();
    else
        raster = iioimage.getRaster();

    /*
     * since this format allows you to write multiple images, the
     * streamMetadataWritten variable makes sure the stream metadata is
     * written only once
     */
    if (streamMetadataWritten == false) {
        if (metadata == null)
            metadata = getDefaultStreamMetadata(param);
        root = metadata.getAsTree("ch5.imageio.ch5stream_1.00");
        dimensionsElementNode = root.getFirstChild();
        Node numberImagesAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("numberImages");
        String numberImages = numberImagesAttributeNode.getNodeValue();
        try {
            ios.writeBytes("5\n");
            ios.writeBytes(numberImages);
            ios.flush();
        } catch (IOException ioe) {
            System.err.println("IOException " + ioe.getMessage());
        }
        streamMetadataWritten = true;
    }

    String widthString;
    String heightString;
    IIOMetadata imageMetadata = (ch5ImageMetadata) iioimage.getMetadata();
    /*
     * don't really need image metadata object here since raster knows
     * necessary information
     */
    if (imageMetadata == null)
        imageMetadata = getDefaultImageMetadata(null, param);

    root = imageMetadata.getAsTree("ch5.imageio.ch5image_1.00");
    dimensionsElementNode = root.getFirstChild();

    Node widthAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("imageWidth");
    widthString = widthAttributeNode.getNodeValue();

    Node heightAttributeNode = dimensionsElementNode.getAttributes().getNamedItem("imageHeight");
    heightString = heightAttributeNode.getNodeValue();

    int sourceWidth = Integer.parseInt(widthString);
    int sourceHeight = Integer.parseInt(heightString);
    int destinationWidth = -1;
    int destinationHeight = -1;
    int sourceRegionWidth = -1;
    int sourceRegionHeight = -1;
    int sourceRegionXOffset = -1;
    int sourceRegionYOffset = -1;
    int xSubsamplingFactor = -1;
    int ySubsamplingFactor = -1;

    if (param == null)
        param = getDefaultWriteParam();

    /*
     * get Rectangle object which will be used to clip the source image's
     * dimensions.
     */
    Rectangle sourceRegion = param.getSourceRegion();
    if (sourceRegion != null) {
        sourceRegionWidth = (int) sourceRegion.getWidth();
        sourceRegionHeight = (int) sourceRegion.getHeight();
        sourceRegionXOffset = (int) sourceRegion.getX();
        sourceRegionYOffset = (int) sourceRegion.getY();

        /*
         * correct for overextended source regions
         */
        if (sourceRegionXOffset + sourceRegionWidth > sourceWidth)
            destinationWidth = sourceWidth - sourceRegionXOffset;
        else
            destinationWidth = sourceRegionWidth;

        if (sourceRegionYOffset + sourceRegionHeight > sourceHeight)
            destinationHeight = sourceHeight - sourceRegionYOffset;
        else
            destinationHeight = sourceRegionHeight;
    } else {
        destinationWidth = sourceWidth;
        destinationHeight = sourceHeight;
        sourceRegionXOffset = sourceRegionYOffset = 0;
    }
    /*
     * get subsampling factors
     */
    xSubsamplingFactor = param.getSourceXSubsampling();
    ySubsamplingFactor = param.getSourceYSubsampling();

    destinationWidth = (destinationWidth - 1) / xSubsamplingFactor + 1;
    destinationHeight = (destinationHeight - 1) / ySubsamplingFactor + 1;

    byte[] sourceBuffer;
    byte[] destinationBuffer = new byte[destinationWidth];

    try {
        ios.writeBytes(new String("\n"));
        ios.writeBytes(new String(destinationWidth + "\n"));
        ios.writeBytes(new String(destinationHeight + "\n"));

        int jj;
        int index;
        for (int j = 0; j < sourceWidth; j++) {
            sourceBuffer = (byte[]) raster.getDataElements(0, j, sourceWidth, 1, null);
            jj = j - sourceRegionYOffset;
            if (jj % ySubsamplingFactor == 0) {
                jj /= ySubsamplingFactor;
                if ((jj >= 0) && (jj < destinationHeight)) {
                    for (int i = 0; i < destinationWidth; i++) {
                        index = sourceRegionXOffset + i * xSubsamplingFactor;
                        destinationBuffer[i] = sourceBuffer[index];
                    }
                    ios.write(destinationBuffer, 0, destinationWidth);
                    ios.flush();
                }
            }
        }
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
    }
}

From source file:de.unigoettingen.sub.commons.contentlib.imagelib.JpegInterpreter.java

/************************************************************************************
 * Constructor for {@link JpegInterpreter} to read an jpeg image from given {@link InputStream}
 * /*from   www .  ja v a2  s.  c  o m*/
 * @param inStream {@link InputStream}
 * @throws ImageInterpreterException
 ************************************************************************************/
public JpegInterpreter(InputStream inStream) throws ImageInterpreterException {
    InputStream inputStream = null;
    byte imagebytes[] = null;

    // read the stream and store it in a byte array
    try {
        this.readImageStream(inStream);
        imagebytes = this.getImageByteStream();
        if (inStream != null) {
            inStream.close();
        }
    } catch (IOException e) {
        LOGGER.error("Failed to close input stream", e);
    }
    //

    // Read image bytes into new stream
    try {
        inputStream = new ByteArraySeekableStream(imagebytes);
    } catch (IOException e1) {
        LOGGER.error("Can't transform the image's byte array to stream");
        ImageInterpreterException iie = new ImageInterpreterException(
                "Can't transform the image's byte array to stream");
        throw iie;
    }
    //

    // read the stream
    Node domNode = null;
    try {
        IIOImage image = createImage(inputStream, 0);
        if ((image == null) || (image.getRenderedImage() == null)) {
            LOGGER.error("Failed to read image from input stream. Aborting!");
            ImageInterpreterException iie = new ImageInterpreterException(
                    "Failed to read image from input stream. Aborting!");
            throw iie;
        }
        this.renderedimage = image.getRenderedImage();
        IIOMetadata metadata = image.getMetadata();
        if (metadata != null) {
            String formatName = metadata.getNativeMetadataFormatName();
            domNode = metadata.getAsTree(formatName);
            if ((domNode == null) || (domNode.getChildNodes() == null)) {
                metadata = null;
            }
        }
        if (metadata == null) {
            LOGGER.error("Failed to read metadata from input stream. Using default values");
            xResolution = defaultXResolution;
            yResolution = defaultYResolution;
            width = this.renderedimage.getWidth();
            height = this.renderedimage.getHeight();
            samplesPerPixel = 1;
            return;
        }
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.error("Failed to close input stream");
            }
        }
    }
    //

    // get new metadata - this is not very sophisticated parsing the DOM
    // tree - needs to be replaced by
    // XPATH expressions - see above
    String height_str = this.getNumLines(domNode);
    if (height_str != null) {
        this.height = Integer.parseInt(height_str);
    }

    String width_str = this.getSamplesPerLine(domNode);
    if (width_str != null) {
        this.width = Integer.parseInt(width_str);
    }

    String resunits = this.getResUnits(domNode);
    int resunits_int = 1; // default is DPI
    // if resunits==1 than it is dpi,
    // if resunits==2 than it is dpcm
    if (resunits != null) {
        resunits_int = Integer.parseInt(resunits);
    }

    String xres_str = this.getXdensity(domNode);
    if (xres_str != null) {
        this.xResolution = Integer.parseInt(xres_str);
        if (resunits_int == 2) {
            this.xResolution = this.xResolution * 2.54f; // d/inch = d/cm * cm/inch
            // this.xResolution = this.xResolution / 2.54f;
        }
    }

    String yres_str = this.getYdensity(domNode);
    if (yres_str != null) {
        this.yResolution = Integer.parseInt(yres_str);
        if (resunits_int == 2) {
            this.yResolution = this.yResolution * 2.54f; // d/inch = d/cm * cm/inch
            // this.yResolution = this.yResolution / 2.54f;
        }
    }

    if ((resunits == null) || (resunits_int == 0) || (xResolution <= 1.0) || (yResolution <= 1.0)) {
        xResolution = defaultXResolution;
        yResolution = defaultYResolution;
    }

    String colordepth_str = this.getSamplePrecision(domNode);
    if (colordepth_str != null) {
        this.colorDepth = Integer.parseInt(colordepth_str);
    }

    String samplesperpixel_str = this.getNumFrames(domNode);
    if (samplesperpixel_str != null) {
        this.samplesPerPixel = Integer.parseInt(samplesperpixel_str);
    }
}