Esempio n. 1
0
  /**
   * Creates an immutable image using pixel data from the specified region of a source image,
   * transformed as specified.
   *
   * <p>The source image may be mutable or immutable. For immutable source images, transparency
   * information, if any, is copied to the new image unchanged.
   *
   * <p>On some devices, pre-transformed images may render more quickly than images that are
   * transformed on the fly using <code>drawRegion</code>. However, creating such images does
   * consume additional heap space, so this technique should be applied only to images whose
   * rendering speed is critical.
   *
   * <p>The transform function used must be one of the following, as defined in the {@link
   * javax.microedition.lcdui.game.Sprite Sprite} class:<br>
   * <code>Sprite.TRANS_NONE</code> - causes the specified image region to be copied unchanged<br>
   * <code>Sprite.TRANS_ROT90</code> - causes the specified image region to be rotated clockwise by
   * 90 degrees.<br>
   * <code>Sprite.TRANS_ROT180</code> - causes the specified image region to be rotated clockwise by
   * 180 degrees.<br>
   * <code>Sprite.TRANS_ROT270</code> - causes the specified image region to be rotated clockwise by
   * 270 degrees.<br>
   * <code>Sprite.TRANS_MIRROR</code> - causes the specified image region to be reflected about its
   * vertical center.<br>
   * <code>Sprite.TRANS_MIRROR_ROT90</code> - causes the specified image region to be reflected
   * about its vertical center and then rotated clockwise by 90 degrees.<br>
   * <code>Sprite.TRANS_MIRROR_ROT180</code> - causes the specified image region to be reflected
   * about its vertical center and then rotated clockwise by 180 degrees.<br>
   * <code>Sprite.TRANS_MIRROR_ROT270</code> - causes the specified image region to be reflected
   * about its vertical center and then rotated clockwise by 270 degrees.<br>
   *
   * <p>The size of the returned image will be the size of the specified region with the transform
   * applied. For example, if the region is <code>100&nbsp;x&nbsp;50</code> pixels and the transform
   * is <code>TRANS_ROT90</code>, the returned image will be <code>50&nbsp;x&nbsp;100</code> pixels.
   *
   * <p><strong>Note:</strong> If all of the following conditions are met, this method may simply
   * return the source <code>Image</code> without creating a new one:
   *
   * <ul>
   *   <li>the source image is immutable;
   *   <li>the region represents the entire source image; and
   *   <li>the transform is <code>TRANS_NONE</code>.
   * </ul>
   *
   * @param dataSource the source image data to be copied from
   * @param x the horizontal location of the region to be copied
   * @param y the vertical location of the region to be copied
   * @param width the width of the region to be copied
   * @param height the height of the region to be copied
   * @param transform the transform to be applied to the region
   * @return the new immutable image data
   */
  public ImageData createImmutableImageData(
      ImageData dataSource, int x, int y, int width, int height, int transform) {
    ImageData dataDest;

    if ((transform & Image.TRANSFORM_SWAP_AXIS) != 0x0) {
      dataDest = new ImageData(height, width, false);
    } else {
      dataDest = new ImageData(width, height, false);
    }

    // Copy native data from the source region
    try {
      createImmutableImageDataRegion(
          dataDest, dataSource, x, y, width, height, transform, dataSource.isMutable());
    } catch (OutOfMemoryError e) {
      garbageCollectImages(false);

      try {
        createImmutableImageDataRegion(
            dataDest, dataSource, x, y, width, height, transform, dataSource.isMutable());
      } catch (OutOfMemoryError e2) {
        garbageCollectImages(true);

        createImmutableImageDataRegion(
            dataDest, dataSource, x, y, width, height, transform, dataSource.isMutable());
      }
    }

    return dataDest;
  }