Search in sources :

Example 1 with DeferRable

use of org.apache.batik.ext.awt.image.renderable.DeferRable in project quick-media by liuyueyi.

the class PNGRegistryEntry method handleStream.

/**
 * Decode the Stream into a RenderableImage
 *
 * @param inIS The input stream that contains the image.
 * @param origURL The original URL, if any, for documentation
 *                purposes only.  This may be null.
 * @param needRawData If true the image returned should not have
 *                    any default color correction the file may
 *                    specify applied.
 */
public Filter handleStream(InputStream inIS, ParsedURL origURL, boolean needRawData) {
    final DeferRable dr = new DeferRable();
    final InputStream is = inIS;
    final boolean raw = needRawData;
    final String errCode;
    final Object[] errParam;
    if (origURL != null) {
        errCode = ERR_URL_FORMAT_UNREADABLE;
        errParam = new Object[] { "PNG", origURL };
    } else {
        errCode = ERR_STREAM_FORMAT_UNREADABLE;
        errParam = new Object[] { "PNG" };
    }
    Thread t = new Thread() {

        public void run() {
            Filter filt;
            try {
                PNGDecodeParam param = new PNGDecodeParam();
                param.setExpandPalette(true);
                if (raw)
                    param.setPerformGammaCorrection(false);
                else {
                    param.setPerformGammaCorrection(true);
                    // sRGB gamma
                    param.setDisplayExponent(2.2f);
                }
                CachableRed cr = new PNGRed(is, param);
                dr.setBounds(new Rectangle2D.Double(0, 0, cr.getWidth(), cr.getHeight()));
                cr = new Any2sRGBRed(cr);
                cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
                WritableRaster wr = (WritableRaster) cr.getData();
                ColorModel cm = cr.getColorModel();
                BufferedImage image;
                image = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
                cr = GraphicsUtil.wrap(image);
                filt = new RedRable(cr);
            } catch (IOException ioe) {
                filt = ImageTagRegistry.getBrokenLinkImage(PNGRegistryEntry.this, errCode, errParam);
            } catch (ThreadDeath td) {
                filt = ImageTagRegistry.getBrokenLinkImage(PNGRegistryEntry.this, errCode, errParam);
                dr.setSource(filt);
                throw td;
            } catch (Throwable t) {
                filt = ImageTagRegistry.getBrokenLinkImage(PNGRegistryEntry.this, errCode, errParam);
            }
            dr.setSource(filt);
        }
    };
    t.start();
    return dr;
}
Also used : Any2sRGBRed(org.apache.batik.ext.awt.image.rendered.Any2sRGBRed) InputStream(java.io.InputStream) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) CachableRed(org.apache.batik.ext.awt.image.rendered.CachableRed) BufferedImage(java.awt.image.BufferedImage) RedRable(org.apache.batik.ext.awt.image.renderable.RedRable) DeferRable(org.apache.batik.ext.awt.image.renderable.DeferRable) Filter(org.apache.batik.ext.awt.image.renderable.Filter) FormatRed(org.apache.batik.ext.awt.image.rendered.FormatRed) WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel)

Example 2 with DeferRable

use of org.apache.batik.ext.awt.image.renderable.DeferRable in project quick-media by liuyueyi.

the class AbstractImageIORegistryEntry method handleStream.

/**
 * Decode the Stream into a RenderableImage
 *
 * @param inIS The input stream that contains the image.
 * @param origURL The original URL, if any, for documentation
 *                purposes only.  This may be null.
 * @param needRawData If true the image returned should not have
 *                    any default color correction the file may
 *                    specify applied.
 */
public Filter handleStream(InputStream inIS, ParsedURL origURL, boolean needRawData) {
    final DeferRable dr = new DeferRable();
    final InputStream is = inIS;
    final String errCode;
    final Object[] errParam;
    if (origURL != null) {
        errCode = ERR_URL_FORMAT_UNREADABLE;
        errParam = new Object[] { getFormatName(), origURL };
    } else {
        errCode = ERR_STREAM_FORMAT_UNREADABLE;
        errParam = new Object[] { getFormatName() };
    }
    Thread t = new Thread() {

        @Override
        public void run() {
            Filter filt;
            try {
                Iterator<ImageReader> iter = ImageIO.getImageReadersByMIMEType(getMimeTypes().get(0).toString());
                if (!iter.hasNext()) {
                    throw new UnsupportedOperationException("No image reader for " + getFormatName() + " available!");
                }
                ImageReader reader = iter.next();
                ImageInputStream imageIn = ImageIO.createImageInputStream(is);
                reader.setInput(imageIn, true);
                int imageIndex = 0;
                dr.setBounds(new Rectangle2D.Double(0, 0, reader.getWidth(imageIndex), reader.getHeight(imageIndex)));
                CachableRed cr;
                // Naive approach possibly wasting lots of memory
                // and ignoring the gamma correction done by PNGRed :-(
                // Matches the code used by the former JPEGRegistryEntry, though.
                BufferedImage bi = reader.read(imageIndex);
                if (bi.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
                    BufferedImage image = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2d = image.createGraphics();
                    g2d.drawImage(bi, 0, 0, null);
                    g2d.dispose();
                    cr = GraphicsUtil.wrap(image);
                } else {
                    cr = GraphicsUtil.wrap(bi);
                    cr = new Any2sRGBRed(cr);
                    cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
                    WritableRaster wr = (WritableRaster) cr.getData();
                    ColorModel cm = cr.getColorModel();
                    BufferedImage image = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
                    cr = GraphicsUtil.wrap(image);
                }
                filt = new RedRable(cr);
            } catch (IOException ioe) {
                // Something bad happened here...
                filt = ImageTagRegistry.getBrokenLinkImage(AbstractImageIORegistryEntry.this, errCode, errParam);
            } catch (ThreadDeath td) {
                filt = ImageTagRegistry.getBrokenLinkImage(AbstractImageIORegistryEntry.this, errCode, errParam);
                dr.setSource(filt);
                throw td;
            } catch (Throwable t) {
                filt = ImageTagRegistry.getBrokenLinkImage(AbstractImageIORegistryEntry.this, errCode, errParam);
            }
            dr.setSource(filt);
        }
    };
    t.start();
    return dr;
}
Also used : BufferedImage(java.awt.image.BufferedImage) RedRable(org.apache.batik.ext.awt.image.renderable.RedRable) DeferRable(org.apache.batik.ext.awt.image.renderable.DeferRable) FormatRed(org.apache.batik.ext.awt.image.rendered.FormatRed) WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel) ImageReader(javax.imageio.ImageReader) Any2sRGBRed(org.apache.batik.ext.awt.image.rendered.Any2sRGBRed) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) CachableRed(org.apache.batik.ext.awt.image.rendered.CachableRed) Filter(org.apache.batik.ext.awt.image.renderable.Filter)

Example 3 with DeferRable

use of org.apache.batik.ext.awt.image.renderable.DeferRable in project opencast by opencast.

the class CoverImageServiceOsgiImpl method activate.

/**
 * OSGi activation callback
 *
 * @param cc
 *          the OSGi component context
 */
@Override
public void activate(ComponentContext cc) {
    super.activate(cc);
    // See
    // http://www.stichlberger.com/software/workaround-for-batiks-noclassdeffounderrorclassnotfoundexception-truncatedfileexception/
    // ---------------
    // add this code before you use batik (make sure is runs only once)
    // via the lower priority this subclass is registered before JPEGRegistryEntry
    // and prevents JPEGRegistryEntry.handleStream from breaking when used on a non Sun/Oracle JDK
    JPEGRegistryEntry entry = new JPEGRegistryEntry() {

        @Override
        public float getPriority() {
            // higher than that of JPEGRegistryEntry (which is 1000)
            return 500;
        }

        /**
         * Decode the Stream into a RenderableImage
         *
         * @param inIS
         *          The input stream that contains the image.
         * @param origURL
         *          The original URL, if any, for documentation purposes only. This may be null.
         * @param needRawData
         *          If true the image returned should not have any default color correction the file may specify applied.
         */
        @Override
        public Filter handleStream(InputStream inIS, ParsedURL origURL, boolean needRawData) {
            // Code from org.apache.batik.ext.awt.image.codec.jpeg.JPEGRegistryEntry#handleStream
            // Reading image with ImageIO to prevent NoClassDefFoundError on OpenJDK
            final DeferRable dr = new DeferRable();
            final InputStream is = inIS;
            final String errCode;
            final Object[] errParam;
            if (origURL != null) {
                errCode = ERR_URL_FORMAT_UNREADABLE;
                errParam = new Object[] { "JPEG", origURL };
            } else {
                errCode = ERR_STREAM_FORMAT_UNREADABLE;
                errParam = new Object[] { "JPEG" };
            }
            Thread t = new Thread() {

                @Override
                public void run() {
                    Filter filt;
                    try {
                        BufferedImage image;
                        image = ImageIO.read(is);
                        dr.setBounds(new Rectangle2D.Double(0, 0, image.getWidth(), image.getHeight()));
                        CachableRed cr;
                        cr = GraphicsUtil.wrap(image);
                        cr = new Any2sRGBRed(cr);
                        cr = new FormatRed(cr, GraphicsUtil.sRGB_Unpre);
                        WritableRaster wr = (WritableRaster) cr.getData();
                        ColorModel cm = cr.getColorModel();
                        image = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null);
                        cr = GraphicsUtil.wrap(image);
                        filt = new RedRable(cr);
                    } catch (IOException ioe) {
                        // Something bad happened here...
                        filt = ImageTagRegistry.getBrokenLinkImage(this, errCode, errParam);
                    } catch (ThreadDeath td) {
                        filt = ImageTagRegistry.getBrokenLinkImage(this, errCode, errParam);
                        dr.setSource(filt);
                        throw td;
                    } catch (Throwable t) {
                        filt = ImageTagRegistry.getBrokenLinkImage(this, errCode, errParam);
                    }
                    dr.setSource(filt);
                }
            };
            t.start();
            return dr;
        }
    };
    ImageTagRegistry.getRegistry().register(entry);
    logger.info("Cover image service activated");
}
Also used : JPEGRegistryEntry(org.apache.batik.ext.awt.image.codec.jpeg.JPEGRegistryEntry) Any2sRGBRed(org.apache.batik.ext.awt.image.rendered.Any2sRGBRed) InputStream(java.io.InputStream) ParsedURL(org.apache.batik.util.ParsedURL) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) CachableRed(org.apache.batik.ext.awt.image.rendered.CachableRed) BufferedImage(java.awt.image.BufferedImage) RedRable(org.apache.batik.ext.awt.image.renderable.RedRable) DeferRable(org.apache.batik.ext.awt.image.renderable.DeferRable) Filter(org.apache.batik.ext.awt.image.renderable.Filter) FormatRed(org.apache.batik.ext.awt.image.rendered.FormatRed) WritableRaster(java.awt.image.WritableRaster) ColorModel(java.awt.image.ColorModel)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)3 BufferedImage (java.awt.image.BufferedImage)3 ColorModel (java.awt.image.ColorModel)3 WritableRaster (java.awt.image.WritableRaster)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 DeferRable (org.apache.batik.ext.awt.image.renderable.DeferRable)3 Filter (org.apache.batik.ext.awt.image.renderable.Filter)3 RedRable (org.apache.batik.ext.awt.image.renderable.RedRable)3 Any2sRGBRed (org.apache.batik.ext.awt.image.rendered.Any2sRGBRed)3 CachableRed (org.apache.batik.ext.awt.image.rendered.CachableRed)3 FormatRed (org.apache.batik.ext.awt.image.rendered.FormatRed)3 ImageReader (javax.imageio.ImageReader)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 JPEGRegistryEntry (org.apache.batik.ext.awt.image.codec.jpeg.JPEGRegistryEntry)1 ParsedURL (org.apache.batik.util.ParsedURL)1