예제 #1
0
  public static void store(
      String codecName,
      java.io.OutputStream outputStream,
      java.awt.Image image,
      edu.cmu.cs.stage3.image.codec.ImageEncodeParam imageEncodeParam)
      throws InterruptedException, java.io.IOException {
    int width = ImageUtilities.getWidth(image);
    int height = ImageUtilities.getHeight(image);

    java.awt.image.RenderedImage renderedImage;

    if (codecName.equals("jpeg")) {
      java.awt.Image originalImage = image;
      image =
          new java.awt.image.BufferedImage(
              width, height, java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
      java.awt.Graphics g = image.getGraphics();
      g.drawImage(
          originalImage,
          0,
          0,
          new java.awt.image.ImageObserver() {
            @Override
            public boolean imageUpdate(
                java.awt.Image image, int infoflags, int x, int y, int width, int height) {
              return true;
            }
          });
      // todo: does dispose ensure the image is finished drawing?
      g.dispose();
    }
    if (image instanceof java.awt.image.RenderedImage) {
      renderedImage = (java.awt.image.RenderedImage) image;
    } else {
      int[] pixels = ImageUtilities.getPixels(image, width, height);
      java.awt.image.BufferedImage bufferedImage =
          new java.awt.image.BufferedImage(
              width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
      bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);
      renderedImage = bufferedImage;
    }
    if (imageEncodeParam == null) {
      if (codecName.equals("png")) {
        imageEncodeParam =
            edu.cmu.cs.stage3.image.codec.PNGEncodeParam.getDefaultEncodeParam(renderedImage);
      }
    }
    java.io.BufferedOutputStream bufferedOutputStream;
    if (outputStream instanceof java.io.BufferedOutputStream) {
      bufferedOutputStream = (java.io.BufferedOutputStream) outputStream;
    } else {
      bufferedOutputStream = new java.io.BufferedOutputStream(outputStream);
    }

    edu.cmu.cs.stage3.image.codec.ImageEncoder imageEncoder =
        edu.cmu.cs.stage3.image.codec.ImageCodec.createImageEncoder(
            codecName, bufferedOutputStream, imageEncodeParam);
    imageEncoder.encode(renderedImage);
    bufferedOutputStream.flush();
  }
예제 #2
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;
    }
  }