Example usage for java.awt.image AffineTransformOp createCompatibleDestRaster

List of usage examples for java.awt.image AffineTransformOp createCompatibleDestRaster

Introduction

In this page you can find the example usage for java.awt.image AffineTransformOp createCompatibleDestRaster.

Prototype

public WritableRaster createCompatibleDestRaster(Raster src) 

Source Link

Document

Creates a zeroed destination Raster with the correct size and number of bands.

Usage

From source file:org.apache.cocoon.reading.ImageReader.java

protected void processStream(InputStream inputStream) throws IOException, ProcessingException {
    if (hasTransform()) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("image " + ((width == 0) ? "?" : Integer.toString(width)) + "x"
                    + ((height == 0) ? "?" : Integer.toString(height)) + " expires: " + expires);
        }/*w  w w . j av a2 s  .  c  o  m*/

        /*
         * NOTE (SM):
         * Due to Bug Id 4502892 (which is found in *all* JVM implementations from
         * 1.2.x and 1.3.x on all OS!), we must buffer the JPEG generation to avoid
         * that connection resetting by the peer (user pressing the stop button,
         * for example) crashes the entire JVM (yes, dude, the bug is *that* nasty
         * since it happens in JPEG routines which are native!)
         * I'm perfectly aware of the huge memory problems that this causes (almost
         * doubling memory consuption for each image and making the GC work twice
         * as hard) but it's *far* better than restarting the JVM every 2 minutes
         * (since this is the average experience for image-intensive web application
         * such as an image gallery).
         * Please, go to the <a href="http://developer.java.sun.com/developer/bugParade/bugs/4502892.html">Sun Developers Connection</a>
         * and vote this BUG as the one you would like fixed sooner rather than
         * later and all this hack will automagically go away.
         * Many deep thanks to Michael Hartle <mhartle@hartle-klug.com> for tracking
         * this down and suggesting the workaround.
         *
         * UPDATE (SM):
         * This appears to be fixed on JDK 1.4
         */

        try {
            byte content[] = readFully(inputStream);
            ImageIcon icon = new ImageIcon(content);
            BufferedImage original = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                    BufferedImage.TYPE_INT_RGB);
            BufferedImage currentImage = original;
            currentImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);

            if (width > 0 || height > 0) {
                double ow = icon.getImage().getWidth(null);
                double oh = icon.getImage().getHeight(null);

                if (usePercent) {
                    if (width > 0) {
                        width = Math.round((int) (ow * width) / 100);
                    }
                    if (height > 0) {
                        height = Math.round((int) (oh * height) / 100);
                    }
                }

                AffineTransformOp filter = new AffineTransformOp(getTransform(ow, oh, width, height),
                        AffineTransformOp.TYPE_BILINEAR);
                WritableRaster scaledRaster = filter.createCompatibleDestRaster(currentImage.getRaster());

                filter.filter(currentImage.getRaster(), scaledRaster);

                currentImage = new BufferedImage(original.getColorModel(), scaledRaster, true, null);
            }

            if (null != grayscaleFilter) {
                grayscaleFilter.filter(currentImage, currentImage);
            }

            if (null != colorFilter) {
                colorFilter.filter(currentImage, currentImage);
            }

            // JVM Bug handling
            if (JVMBugFixed) {
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
                p.setQuality(this.quality[0], true);
                encoder.setJPEGEncodeParam(p);
                encoder.encode(currentImage);
            } else {
                ByteArrayOutputStream bstream = new ByteArrayOutputStream();
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bstream);
                JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
                p.setQuality(this.quality[0], true);
                encoder.setJPEGEncodeParam(p);
                encoder.encode(currentImage);
                out.write(bstream.toByteArray());
            }

            out.flush();
        } catch (ImageFormatException e) {
            throw new ProcessingException(
                    "Error reading the image. " + "Note that only JPEG images are currently supported.");
        } finally {
            // Bugzilla Bug 25069, close inputStream in finally block
            // this will close inputStream even if processStream throws
            // an exception
            inputStream.close();
        }
    } else {
        // only read the resource - no modifications requested
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("passing original resource");
        }
        super.processStream(inputStream);
    }
}

From source file:org.apache.cocoon.reading.RepoImageReader.java

/**
 * @return the time the read source was last modified or 0 if it is not
 *         possible to detect/*from  w ww. j  a  va  2 s.co m*/
 */
/*
public long getLastModified() {
if (hasRanges()) {
    // This is a byte range request so we can't use the cache, return null.
    return 0;
}
        
if (quickTest) {
    return inputSource.getLastModified();
}
        
final String systemId = (String) documents.get(request.getRequestURI());
// Note: getURI() might be null in some incomplete implementations
final String sourceURI = inputSource.getURI();
if (systemId == null || (sourceURI != null && sourceURI.equals(systemId))) {
    return inputSource.getLastModified();
}
        
documents.remove(request.getRequestURI());
return 0;
}*/

protected void processStream(InputStream inputStream) throws IOException, ProcessingException {

    if (inputStream != null) {
        if (hasTransform()) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("image " + ((width == 0) ? "?" : Integer.toString(width)) + "x"
                        + ((height == 0) ? "?" : Integer.toString(height)) + " expires: " + expires);
            }

            try {
                byte content[] = readFully(inputStream);
                ImageIcon icon = new ImageIcon(content);
                BufferedImage original = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                        BufferedImage.TYPE_INT_RGB);
                BufferedImage currentImage = original;
                currentImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);

                if (width > 0 || height > 0) {
                    double ow = icon.getImage().getWidth(null);
                    double oh = icon.getImage().getHeight(null);

                    if (usePercent) {
                        if (width > 0) {
                            width = Math.round((int) (ow * width) / 100);
                        }
                        if (height > 0) {
                            height = Math.round((int) (oh * height) / 100);
                        }
                    }

                    AffineTransformOp filter = new AffineTransformOp(getTransform(ow, oh, width, height),
                            AffineTransformOp.TYPE_BILINEAR);
                    WritableRaster scaledRaster = filter.createCompatibleDestRaster(currentImage.getRaster());

                    filter.filter(currentImage.getRaster(), scaledRaster);

                    currentImage = new BufferedImage(original.getColorModel(), scaledRaster, true, null);
                }

                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(currentImage);
                p.setQuality(this.quality[0], true);
                encoder.setJPEGEncodeParam(p);
                encoder.encode(currentImage);

                out.flush();
            } catch (ImageFormatException e) {
                throw new ProcessingException(
                        "Error reading the image. " + "Note that only JPEG images are currently supported.");
            } finally {
                // Bugzilla Bug 25069, close inputStream in finally block
                // this will close inputStream even if processStream throws
                // an exception
                inputStream.close();
            }
        } else {
            try {
                InputStream is = inputStream;

                long expires = parameters.getParameterAsInteger("expires", -1);
                if (expires > 0) {
                    response.setDateHeader("Expires", System.currentTimeMillis() + expires);
                }
                response.setHeader("Accept-Ranges", "bytes");

                byte[] buffer = new byte[8192];
                int length;
                while ((length = is.read(buffer)) > -1) {
                    out.write(buffer, 0, length);
                }
                is.close();
                out.flush();
            } catch (RuntimeException e) {
                throw e;
            } finally {
                // Bugzilla Bug 25069, close inputStream in finally block
                // this will close inputStream even if processStream throws
                // an exception
                inputStream.close();
            }

        }
    } else {
        throw new IOException("Deals: Problem, resource not found or Repository not working correctly");
    }

}