从BufferedImage有效地提取RGBA缓冲区 [英] Efficiently extracting RGBA buffer from BufferedImage

查看:94
本文介绍了从BufferedImage有效地提取RGBA缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将Java中的bufferedImages作为IntBuffers加载.但是,我遇到的一个问题是从具有半透明或完全透明的图像中获取像素数据.Java似乎只允许您获取RGB值,在我的情况下,这是一个问题,因为应该透明的所有像素都变得完全不透明.经过大约几个小时的搜索,我发现了这种获取RGBA值的方法...

I've been trying to load in bufferedImages in java as IntBuffers. However, one problem I've come across is getting the pixel data from an image with semi or complete transparency. Java only seems to allow you to get the RGB value, which in my case is a problem because any pixels that should be transparent are rendered completely opaque. After about a few hours of searching I came across this way of getting the RGBA values...

Color color = new Color(image.getRGB(x, y), true);

尽管它确实有效,但它可能不是最佳方法.有谁知道一种更有效的方法来完成相同的任务,即不需要每个像素都有一个颜色对象的实例.您可以看到如果尝试加载相当大的图像,这将是多么糟糕.这是我的代码,以防万一您需要参考...

Although it does work, it can't possibly be the best way of doing this. Does anyone know of a more efficient way to complete the same task, one that does not require an instance of a color object for EVERY pixel. You can see how this would be bad if you're trying to load in a fairly large image. Here is my code just in case you need a reference...

public static IntBuffer getImageBuffer(BufferedImage image) {

    int width = image.getWidth();
    int height = image.getHeight();

    int[] pixels = new int[width * height];     
    for (int i = 0; i < pixels.length; i++) {

        Color color = new Color(image.getRGB(i % width, i / width), true);

        int a = color.getAlpha();
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();

        pixels[i] = a << 24 | b << 16 | g << 8 | r;

    }

    return BufferUtils.toIntBuffer(pixels);

}
public static IntBuffer toIntBuffer(int[] elements) {

    IntBuffer buffer = ByteBuffer.allocateDirect(elements.length << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
    buffer.put(elements).flip();
    return buffer;

}

*传递到参数的bufferedImage是从磁盘加载的

* The bufferedImage passed into the parameter is loaded from the disk

推荐答案

这是我有的一些旧代码,可将图像转换为LWJGL的OpenGL.由于必须交换字节顺序,因此(例如,我认为)将图像加载为整数是没有用的.

Here's some old code I have that converts images to OpenGL for LWJGL. Since the byte order has to be swapped, it isn't useful (I think) to load the image as for example integers.

   public static ByteBuffer decodePng( BufferedImage image )
           throws IOException
   {

      int width = image.getWidth();
      int height = image.getHeight();

      // Load texture contents into a byte buffer
      ByteBuffer buf = ByteBuffer.allocateDirect( 4 * width * height );

      // decode image
      // ARGB format to -> RGBA
      for( int h = 0; h < height; h++ )
         for( int w = 0; w < width; w++ ) {
            int argb = image.getRGB( w, h );
            buf.put( (byte) ( 0xFF & ( argb >> 16 ) ) );
            buf.put( (byte) ( 0xFF & ( argb >> 8 ) ) );
            buf.put( (byte) ( 0xFF & ( argb ) ) );
            buf.put( (byte) ( 0xFF & ( argb >> 24 ) ) );
         }
      buf.flip();
      return buf;
   }

示例用法:

    BufferedImage image = ImageIO.read( getClass().getResourceAsStream(heightMapFile) );

    int height = image.getHeight();
    int width = image.getWidth();
    ByteBuffer buf = TextureUtils.decodePng(image);

这篇关于从BufferedImage有效地提取RGBA缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆