Example #1
0
  public static java.awt.Image load(
      String codecName,
      java.io.InputStream inputStream,
      edu.cmu.cs.stage3.image.codec.ImageDecodeParam imageDecodeParam)
      throws java.io.IOException {
    java.io.BufferedInputStream bufferedInputStream;
    if (inputStream instanceof java.io.BufferedInputStream) {
      bufferedInputStream = (java.io.BufferedInputStream) inputStream;
    } else {
      bufferedInputStream = new java.io.BufferedInputStream(inputStream);
    }
    edu.cmu.cs.stage3.image.codec.ImageDecoder imageDecoder =
        edu.cmu.cs.stage3.image.codec.ImageCodec.createImageDecoder(
            codecName, bufferedInputStream, imageDecodeParam);
    java.awt.image.RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage();

    if (renderedImage instanceof java.awt.Image) {
      return (java.awt.Image) renderedImage;
    } else {
      java.awt.image.Raster raster = renderedImage.getData();
      java.awt.image.ColorModel colorModel = renderedImage.getColorModel();
      java.util.Hashtable properties = null;
      String[] propertyNames = renderedImage.getPropertyNames();
      if (propertyNames != null) {
        properties = new java.util.Hashtable();
        for (String propertyName : propertyNames) {
          properties.put(propertyName, renderedImage.getProperty(propertyName));
        }
      }
      java.awt.image.WritableRaster writableRaster;
      if (raster instanceof java.awt.image.WritableRaster) {
        writableRaster = (java.awt.image.WritableRaster) raster;
      } else {
        writableRaster = raster.createCompatibleWritableRaster();
      }
      java.awt.image.BufferedImage bufferedImage =
          new java.awt.image.BufferedImage(
              renderedImage.getColorModel(),
              writableRaster,
              colorModel.isAlphaPremultiplied(),
              properties);
      return bufferedImage;
    }
  }
Example #2
0
 /**
  * Converts a rendered image to a {@code BufferedImage}. This utility method has come from a forum
  * post by Jim Moore at:
  *
  * <p><a href="http://www.jguru.com/faq/view.jsp?EID=114602">
  * http://www.jguru.com/faq/view.jsp?EID=114602</a>
  *
  * @param img the rendered image.
  * @return A buffered image.
  */
 private static BufferedImage convertRenderedImage(RenderedImage img) {
   if (img instanceof BufferedImage) {
     return (BufferedImage) img;
   }
   ColorModel cm = img.getColorModel();
   int width = img.getWidth();
   int height = img.getHeight();
   WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
   boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
   Hashtable properties = new Hashtable();
   String[] keys = img.getPropertyNames();
   if (keys != null) {
     for (int i = 0; i < keys.length; i++) {
       properties.put(keys[i], img.getProperty(keys[i]));
     }
   }
   BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
   img.copyData(raster);
   return result;
 }
Example #3
0
 /** @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform) */
 public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
   BufferedImage image = null;
   if (img instanceof BufferedImage) {
     image = (BufferedImage) img;
   } else {
     ColorModel cm = img.getColorModel();
     int width = img.getWidth();
     int height = img.getHeight();
     WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
     boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
     Hashtable properties = new Hashtable();
     String[] keys = img.getPropertyNames();
     if (keys != null) {
       for (int i = 0; i < keys.length; i++) {
         properties.put(keys[i], img.getProperty(keys[i]));
       }
     }
     BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
     img.copyData(raster);
     image = result;
   }
   drawImage(image, xform, null);
 }