public static BufferedImage convertImageData(ImageData data) {
    BufferedImage bimg = null;
    int height = data.getHeight(), width = data.getWidth();
    Object pixels = data.getData();
    if (pixels instanceof int[] && data.getElementWidth() == 1) {
      int[] arr = (int[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        int l = arr[j];
        barray[k++] = (byte) (l & 0xFF);
        barray[k++] = (byte) ((l >>> 8) & 0xFF);
        barray[k++] = (byte) ((l >>> 16) & 0xFF);
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else if (pixels instanceof byte[]
        && data.getElementWidth() == 1) { // Assume gray scale model?
      byte[] arr = (byte[]) pixels;
      byte[] barray = new byte[arr.length * 3];
      int k = 0;
      for (int j = 0; j < arr.length; j++) {
        byte l = arr[j];
        barray[k++] = l;
        barray[k++] = l;
        barray[k++] = l;
      }
      ColorModel ccm =
          new ComponentColorModel(
              ICC_ColorSpace.getInstance(ICC_ColorSpace.CS_sRGB),
              new int[] {8, 8, 8},
              false,
              false,
              Transparency.OPAQUE,
              DataBuffer.TYPE_BYTE);
      DataBuffer bbuf = new DataBufferByte(barray, barray.length);
      SampleModel bmodel =
          new PixelInterleavedSampleModel(
              DataBuffer.TYPE_BYTE, width, height, 3, 3 * width, new int[] {2, 1, 0});

      WritableRaster raster = Raster.createWritableRaster(bmodel, bbuf, new Point(0, 0));
      bimg = new BufferedImage(ccm, raster, false, new Hashtable());
    } else {
      throw new RuntimeException("Unexpected data.");
    }
    return bimg;
  }
Esempio n. 2
0
  /**
   * Creates an immutable image from a source mutable image.
   *
   * <p>This method is useful for placing the contents of mutable images into <code>Choice</code>
   * objects. The application can create an off-screen image using the {@link #createImage(int, int)
   * createImage(w, h)} method, draw into it using a <code>Graphics</code> object obtained with the
   * {@link #getGraphics() getGraphics()} method, and then create an immutable copy of it with this
   * method. The immutable copy may then be placed into <code>Choice</code> objects.
   *
   * @param mutableSource the source mutable image to be copied
   * @return the new immutable image
   */
  public ImageData createImmutableCopy(ImageData mutableSource) {
    ImageData data = new ImageData(mutableSource.getWidth(), mutableSource.getHeight(), false);

    // Duplicate mutable image contents
    try {
      createImmutableImageDataCopy(data, mutableSource);
    } catch (OutOfMemoryError e) {
      garbageCollectImages(false);

      try {
        createImmutableImageDataCopy(data, mutableSource);
      } catch (OutOfMemoryError e2) {
        garbageCollectImages(true);

        createImmutableImageDataCopy(data, mutableSource);
      }
    }

    return data;
  }