Esempio n. 1
0
  /**
   * 将BufferedImage转化为LTextureData
   *
   * @param image
   */
  private void create(BufferedImage image) {

    int srcWidth = image.getWidth();
    int srcHeight = image.getHeight();

    this.hasAlpha = image.getColorModel().hasAlpha();

    if (GLEx.isPowerOfTwo(srcWidth) && GLEx.isPowerOfTwo(srcHeight)) {
      this.width = srcWidth;
      this.height = srcHeight;
      this.texHeight = srcHeight;
      this.texWidth = srcWidth;
      this.source =
          BufferUtils.createByteBuffer(
              (byte[])
                  image
                      .getRaster()
                      .getDataElements(0, 0, image.getWidth(), image.getHeight(), null));
      this.pixels = GraphicsUtils.getPixels(image);
      return;
    }

    int texWidth = GLEx.toPowerOfTwo(srcWidth);
    int texHeight = GLEx.toPowerOfTwo(srcHeight);

    this.width = srcWidth;
    this.height = srcHeight;
    this.texHeight = texHeight;
    this.texWidth = texWidth;

    BufferedImage texImage =
        new BufferedImage(
            texWidth,
            texHeight,
            hasAlpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR);

    Graphics2D g = texImage.createGraphics();

    g.drawImage(image, 0, 0, null);

    if (height < texHeight - 1) {
      copyArea(texImage, g, 0, 0, width, 1, 0, texHeight - 1);
      copyArea(texImage, g, 0, height - 1, width, 1, 0, 1);
    }
    if (width < texWidth - 1) {
      copyArea(texImage, g, 0, 0, 1, height, texWidth - 1, 0);
      copyArea(texImage, g, width - 1, 0, 1, height, 1, 0);
    }

    source =
        BufferUtils.createByteBuffer(
            (byte[])
                texImage
                    .getRaster()
                    .getDataElements(0, 0, texImage.getWidth(), texImage.getHeight(), null));
    this.pixels = GraphicsUtils.getPixels(texImage);

    if (texImage != null) {
      texImage.flush();
      texImage = null;
    }
  }
Esempio n. 2
0
 private void init(int width, int height, boolean alpha) {
   this.width = width;
   this.height = height;
   this.hasAlpha = alpha;
   this.buffer = BufferUtils.createByteBuffer(width * height * 4);
 }