0
我已經寫了下面的函數來改變一個位圖的伽馬值,但它有點慢,即使在小的(300乘300)位圖上。我怎樣才能讓這個功能運行得更快?例如,是否有更好的方法(即更快的方式)從位圖訪問各個像素值?如何讓Android中的此位圖伽瑪函數運行得更快?
public Bitmap apply(Bitmap bmp, float gamma) {
if (bmp == null)
return null;
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int[] powers = new int[256];
for (int i = 0; i < powers.length; i++)
powers[i] = (int)(Math.pow(i/255.0f, 1.0f/gamma) * 255);
for (int p = 0; p < pixels.length; p++) {
int r = Color.red(pixels[p]);
int g = Color.green(pixels[p]);
int b = Color.blue(pixels[p]);
int newR = powers[r];
int newG = powers[g];
int newB = powers[b];
pixels[p] = Color.rgb(newR, newG, newB);
}
Bitmap newBmp = Bitmap.createBitmap(pixels, 0, width, width, height, Config.ARGB_8888);
return newBmp;
}
作爲一種優化,我提前計算所有可能的像素值(0〜255),這有助於權力,但還不夠。另外,聲明所有的int在第二個for循環之外並沒有多大幫助,所以我把它們放在了前面。