示例#1
0
  public static BufferedImage convertToImage(TextureRegion textureRegion) {
    final int width = textureRegion.getWidth();
    final int height = textureRegion.getHeight();

    final Rect2i pixelRegion = textureRegion.getPixelRegion();
    final Texture texture = textureRegion.getTexture();
    ByteBuffer textureBytes = texture.getData().getBuffers()[0];
    int stride = texture.getWidth() * 4;
    int posX = pixelRegion.minX();
    int posY = pixelRegion.minY();

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4));
        int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1));
        int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2));
        int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3));

        int argb = (a << 24) + (r << 16) + (g << 8) + b;
        image.setRGB(x, y, argb);
      }
    }
    return image;
  }