加载,操作更新图片/像素的最佳方法是什么? [英] What is the best way to load-manipulate-update picture/pixels?

查看:74
本文介绍了加载,操作更新图片/像素的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有BufferedImage(顺便说一句,这是存储和编辑框架图形的最佳方法吗?)和对该图像进行一些编辑的功能.

There's BufferedImage (btw, is it the best way to store and edit frame's graphics?) and function that does some editing to this image.

我目前的做法:

 //usage: 
image.setData(update(image.getRaster()));


public Raster update(WritableRaster raster) {
    int[] pixels = new int[raster.getWidth() * raster.getHeight()];
    raster.getDataElements(0, 0, w, h, pixels); // raster to int[]
  //pixels array operations      
    raster.setDataElements(0, 0, w, h, pixels); // int[] to raster
    return raster;
}

发送栅格似乎是这种方法的瓶颈,但是还有其他选择吗?

Sending raster seems to be a bottleneck of this approach but what are the other options?

推荐答案

通过将颜色值直接存储在栅格中,可以有效地写入图像中的任何像素.我发现以这种方式动态操作图像的最高效的方式是这样的:

You can write efficiently into any pixel in the image by storing color values directly in the raster. The most performant way I've found to dynamically manipulate images this way is like this:

BufferedImage image = new BufferedImage(inputWidth, inputHeight, BufferedImage.TYPE_INT_ARGB);
        int[] rgbRaster = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

        for(int i = 0; i < inputHeight; i++) {
            for(int j = 0; j < inputWidth; j++) {
                int index = (inputWidth  * i) + j;
                rgbRaster[index] = SOME_ARG_VALUE;
                }
            }
        }

当然,您不必一遍又一遍地重新创建图像或栅格阵列,只需创建一次即可,并在需要时写入阵列.相当快,也很容易.

Of course you don't have to recreate the image or raster array over and over, just create them once, and write into the array when you want to. Pretty fast, and pretty easy.

这篇关于加载,操作更新图片/像素的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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