Example usage for java.awt.image RenderedImage copyData

List of usage examples for java.awt.image RenderedImage copyData

Introduction

In this page you can find the example usage for java.awt.image RenderedImage copyData.

Prototype

WritableRaster copyData(WritableRaster raster);

Source Link

Document

Computes an arbitrary rectangular region of the RenderedImage and copies it into a caller-supplied WritableRaster.

Usage

From source file:org.apache.batik.transcoder.image.AbstractImageTranscoderTest.java

protected BufferedImage getImage(InputStream is) throws IOException {
    ImageTagRegistry reg = ImageTagRegistry.getRegistry();
    Filter filt = reg.readStream(is);
    if (filt == null)
        throw new IOException("Couldn't read Stream");

    RenderedImage red = filt.createDefaultRendering();
    if (red == null)
        throw new IOException("Couldn't render Stream");

    BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
    red.copyData(img.getRaster());
    return img;//  w ww .j a  v a 2  s  . c  om
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Loads an image from a URL/*from  w w  w .ja  va2 s . c o  m*/
 * @param url the URL to the image
 * @return the bitmap as BufferedImage
 * TODO This method doesn't close the InputStream opened by the URL.
 */
public static BufferedImage getImage(URL url) {
    ImageTagRegistry reg = ImageTagRegistry.getRegistry();
    Filter filt = reg.readURL(new ParsedURL(url));
    if (filt == null) {
        return null;
    }

    RenderedImage red = filt.createDefaultRendering();
    if (red == null) {
        return null;
    }

    BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
    red.copyData(img.getRaster());

    return img;
}

From source file:org.apache.fop.visual.BitmapComparator.java

/**
 * Loads an image from a URL/*from ww w. j a v  a2  s .com*/
 * @param bitmapFile the bitmap file
 * @return the bitmap as BufferedImage
 */
public static BufferedImage getImage(File bitmapFile) {
    try {
        InputStream in = new java.io.FileInputStream(bitmapFile);
        try {
            in = new java.io.BufferedInputStream(in);

            ImageTagRegistry reg = ImageTagRegistry.getRegistry();
            Filter filt = reg.readStream(in);
            if (filt == null) {
                return null;
            }

            RenderedImage red = filt.createDefaultRendering();
            if (red == null) {
                return null;
            }

            BufferedImage img = new BufferedImage(red.getWidth(), red.getHeight(), BufferedImage.TYPE_INT_ARGB);
            red.copyData(img.getRaster());
            return img;
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (IOException e) {
        return null;
    }
}

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

/** {@inheritDoc} */
public Image loadImage(ImageInfo info, Map hints, ImageSessionContext session)
        throws ImageException, IOException {
    RenderedImage imageData = null;
    IIOException firstException = null;

    IIOMetadata iiometa = (IIOMetadata) info.getCustomObjects().get(ImageIOUtil.IMAGEIO_METADATA);
    boolean ignoreMetadata = (iiometa != null);
    boolean providerIgnoresICC = false;

    Source src = session.needSource(info.getOriginalURI());
    ImageInputStream imgStream = ImageUtil.needImageInputStream(src);
    try {//  w w  w  . j a va  2 s  .c  o  m
        Iterator iter = ImageIO.getImageReaders(imgStream);
        while (iter.hasNext()) {
            ImageReader reader = (ImageReader) iter.next();
            try {
                imgStream.mark();
                ImageReadParam param = reader.getDefaultReadParam();
                reader.setInput(imgStream, false, ignoreMetadata);
                final int pageIndex = ImageUtil.needPageIndexFromURI(info.getOriginalURI());
                try {
                    if (ImageFlavor.BUFFERED_IMAGE.equals(this.targetFlavor)) {
                        imageData = reader.read(pageIndex, param);
                    } else {
                        imageData = reader.read(pageIndex, param);
                        //imageData = reader.readAsRenderedImage(pageIndex, param);
                        //TODO Reenable the above when proper listeners are implemented
                        //to react to late pixel population (so the stream can be closed
                        //properly).
                    }
                    if (iiometa == null) {
                        iiometa = reader.getImageMetadata(pageIndex);
                    }
                    providerIgnoresICC = checkProviderIgnoresICC(reader.getOriginatingProvider());
                    break; //Quit early, we have the image
                } catch (IndexOutOfBoundsException indexe) {
                    throw new ImageException("Page does not exist. Invalid image index: " + pageIndex);
                } catch (IllegalArgumentException iae) {
                    //Some codecs like com.sun.imageio.plugins.wbmp.WBMPImageReader throw
                    //IllegalArgumentExceptions when they have trouble parsing the image.
                    throw new ImageException("Error loading image using ImageIO codec", iae);
                } catch (IIOException iioe) {
                    if (firstException == null) {
                        firstException = iioe;
                    } else {
                        log.debug("non-first error loading image: " + iioe.getMessage());
                    }
                }
                try {
                    //Try fallback for CMYK images
                    BufferedImage bi = getFallbackBufferedImage(reader, pageIndex, param);
                    imageData = bi;
                    firstException = null; //Clear exception after successful fallback attempt
                    break;
                } catch (IIOException iioe) {
                    //ignore
                }
                imgStream.reset();
            } finally {
                reader.dispose();
            }
        }
    } finally {
        ImageUtil.closeQuietly(src);
        //TODO Some codecs may do late reading.
    }
    if (firstException != null) {
        throw new ImageException("Error while loading image: " + firstException.getMessage(), firstException);
    }
    if (imageData == null) {
        throw new ImageException("No ImageIO ImageReader found .");
    }

    ColorModel cm = imageData.getColorModel();

    Color transparentColor = null;
    if (cm instanceof IndexColorModel) {
        //transparent color will be extracted later from the image
    } else {
        if (providerIgnoresICC && cm instanceof ComponentColorModel) {
            // Apply ICC Profile to Image by creating a new image with a new
            // color model.
            ICC_Profile iccProf = tryToExctractICCProfile(iiometa);
            if (iccProf != null) {
                ColorModel cm2 = new ComponentColorModel(new ICC_ColorSpace(iccProf), cm.hasAlpha(),
                        cm.isAlphaPremultiplied(), cm.getTransparency(), cm.getTransferType());
                WritableRaster wr = Raster.createWritableRaster(imageData.getSampleModel(), null);
                imageData.copyData(wr);
                BufferedImage bi = new BufferedImage(cm2, wr, cm2.isAlphaPremultiplied(), null);
                imageData = bi;
                cm = cm2;
            }
        }
        // ImageIOUtil.dumpMetadataToSystemOut(iiometa);
        // Retrieve the transparent color from the metadata
        if (iiometa != null && iiometa.isStandardMetadataFormatSupported()) {
            Element metanode = (Element) iiometa.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
            Element dim = ImageIOUtil.getChild(metanode, "Transparency");
            if (dim != null) {
                Element child;
                child = ImageIOUtil.getChild(dim, "TransparentColor");
                if (child != null) {
                    String value = child.getAttribute("value");
                    if (value == null || value.length() == 0) {
                        //ignore
                    } else if (cm.getNumColorComponents() == 1) {
                        int gray = Integer.parseInt(value);
                        transparentColor = new Color(gray, gray, gray);
                    } else {
                        StringTokenizer st = new StringTokenizer(value);
                        transparentColor = new Color(Integer.parseInt(st.nextToken()),
                                Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                    }
                }
            }
        }
    }

    if (ImageFlavor.BUFFERED_IMAGE.equals(this.targetFlavor)) {
        return new ImageBuffered(info, (BufferedImage) imageData, transparentColor);
    } else {
        return new ImageRendered(info, imageData, transparentColor);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @noinspection UseOfObsoleteCollectionType
 * @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform)
 *//* w w w .  java2  s.c  om*/
@Override
public void drawRenderedImage(final RenderedImage img, final AffineTransform xform) {
    final BufferedImage image;
    if (img instanceof BufferedImage) {
        image = (BufferedImage) img;
    } else {
        final ColorModel cm = img.getColorModel();
        final int width = img.getWidth();
        final int height = img.getHeight();
        final WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
        final boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        final Hashtable properties = new Hashtable();
        final String[] keys = img.getPropertyNames();
        if (keys != null) {
            final int keyCount = keys.length;
            for (int i = 0; i < keyCount; i++) {
                properties.put(keys[i], img.getProperty(keys[i]));
            }
        }
        final BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
        img.copyData(raster);
        image = result;
    }
    drawImage(image, xform, null);
}