Java. What is the fastest way to run and process every pixel on bitmap?

被刻印的时光 ゝ 提交于 2019-12-11 05:25:35

问题


I have the image_source that is the source bitmap with some picture and image_new — temporary bitmap

I do this code, that makes image_source be anaglyph background layer:

int [] pixel = {0x0, 0x0, 0x0};
int [] image_params = {image_source.getWidth() - 2 * anaglyph_amplitude, image_source.getHeight()};
Bitmap image_new = Bitmap.createScaledBitmap(image_source, image_params[0], image_params[1], false);
for(int i = 0; i < image_params[0]; ++i)
    for(int j = 0; j < image_params[1]; ++j) {
        pixel[0] = image_source.getPixel(i, j);
        pixel[1] = image_source.getPixel(i + 2 * anaglyph_amplitude, j);
        pixel[2] = pixel[0] + pixel[1] % 0x10000 - pixel[0] % 0x10000;
        image_new.setPixel(i, j, pixel[2]);
    }
image_source = Bitmap.createBitmap(image_new);
image_new = null;

Then image_source is drawn to canvas (drawing to canvas at ones is not available).

The problem is that this programm takes about 5 seconds to process image with 1000x1000 size on smart Android device.

Are there any other ways to run bitmap pixels?


回答1:


There is some performance enhancement for snippet code. I am not sure it is enough for you.

First change

pixel[2] = pixel[0] + (pixel[1] & 0xFFFF) - (pixel[0] & 0xFFFF);

instead of

pixel[2] = pixel[0] + pixel[1] % 0x10000 - pixel[0] % 0x10000;

-

pixel[1] = image_source.getPixel(i + (anaglyph_amplitude<<1), j);

instead of

pixel[1] = image_source.getPixel(i + 2 * anaglyph_amplitude, j);



回答2:


Another minor tweak. In the inner loop, no reason to use an array for pixel[1] etc. Have three ints, p0, p1 and p2.

EDIT ADDED

I'm less familiar with Android than Swing, but I was hoping that Android's Bitmap has a "get me a bunch of pixels at a time" method similar to a Raster. It does.

I think you should be using the Bitmap method javadocs link here

public void getPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)

If you have the memory available, get all 1,000,000 at once.




回答3:


You could cache the calculation of 2 * anaglyph_amplitude so it has not to be calculated on each iteration.




回答4:


Here is solution: I get array of pixels and worked with it.

int [] pixel = {0x0, 0x0, 0x0};
int [] pixels_old, pixels_new;
int [] params = {image_source.getWidth(), image_source.getHeight()};
int [] image_params = {image_source.getWidth() - 2 * anaglyph_amplitude, image_source.getHeight()};
pixels_old = new int[params[2] * params[3]];
pixels_new = new int[image_params[0] * image_params[1]];
image_source.getPixels(pixels_old, 0, params[2], 0, 0, params[2], params[3]); 
image_source = null;
for(int i = 0; i < image_params[0]; ++i)
    for(int j = 0; j < image_params[1]; ++j) {
        pixel[0] = pixels_old[i + j * params[2]];
        pixel[1] = pixels_old[i + (anaglyph_amplitude<<1) + j * params[2]];
        pixel[2] = pixel[0] + (pixel[1] & 0xFFFF) - (pixel[0] & 0xFFFF);
        pixels_new[i + j * image_params[0]] = pixel[2];
    }
pixels_old = null;
image_source = Bitmap.createBitmap(pixels_new, image_params[0], image_params[1], Bitmap.Config.ARGB_8888);
pixels_new = null;


来源:https://stackoverflow.com/questions/8722643/java-what-is-the-fastest-way-to-run-and-process-every-pixel-on-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!