Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

improving speed of getpixel() and setpixel() on Android Bitmap

All,

After I noticed how slow getPixel and setPixel are (not sure which one, guess both are not turbocharged) I quickly coded a container for Bitmap that uses int[] array to handle bitmap operations.

Already - its noticeably faster, but this is not enough. Please could you advice how to speed it up further?

My idea is to keep track of what is made "dirty" by the setPixel functions and update only this part of Bitmap when getBitmap() is called ... not clear on how to set the setPixels parameters though (something with offset and stride I guess).

Also - any faster recipe?

Thanks for all help in advance!

import android.graphics.Bitmap;

public class DrawableBitmapContainer {
private Bitmap image;
private int width, height;
private int[]  pixels;
public DrawableBitmapContainer(Bitmap _source ){
    image = _source;
    width = image.getWidth();
    height = image.getHeight();
    pixels = new int[width*height];
    image.getPixels(pixels,0,width,0,0,width,height);
}
public int getPixel(int x,int y){
    return pixels[x+y*width];
}
public void setPixel(int x,int y, int color){
    pixels[x+y*width]=color;
}
public Bitmap getBimap(){
    image.setPixels(pixels,0,width,0,0,width,height);
    return image;
}
public int getWidth(){
    return image.getWidth();
}
public int getHeight(){
    return image.getHeight();
}
}
like image 898
Piotr Avatar asked Jan 17 '11 17:01

Piotr


1 Answers

For functions as simple as setPixel / getPixel, the function call overhead is relatively large.

It would be a lot faster to access the pixels array directly instead of through these functions. Of course that means you have to make pixels public, which isn't very nice from a design point of view, but if you absolutely need all the performance you can get, this is the way to go.

See also Designing for performance in the Android docs.

If this is still not enough, consider coding your bitmap operations in C++ using the NDK.

like image 162
svdree Avatar answered Sep 28 '22 08:09

svdree