2
我有以下問題。我開始使用一個位圖轉換例程,它可以完美地運行我可以拋出的任何類型的轉換。內存中的位圖轉換
Bitmap transform(Bitmap src) {
// ... any kind of transformation , for example GAMMA
double gama = 0.8;
int[] tR = new int[256];
int[] gG = new int[256];
int[] tB = new int[256];
for(int i = 0; i < 256; ++i) {
tR[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5));
tG[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5));
tB[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5));
}
// apply transformation to the old bitmap -> bmOut
int wid = src.getWidth(), hei = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig());
int A, R, G, B;
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hei; y++) {
int pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
R = tR[Color.red(pixel)];
G = tG[Color.green(pixel)];
B = tB[Color.blue(pixel)];
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
return bmOut;
}
但它是PAINFULLY慢 - 由getPixel()/ setPixel()兄弟姐妹引起的。 沒問題,我說,我只是使用一個內存緩衝區(就像在舊的StretchBlt()天)。所以,我做了重大修改,創建軟件工程的下列寶石:)
Bitmap transform(Bitmap src) {
// ... transformation array are built here
// apply transformation
int wid = src.getWidth(), hei = src.getHeight();
Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig());
int[] pixs = new int[wid*hei]; // changed
src.getPixels(pixs, 0, wid, 0, 0, wid, hei); // changed
int A, R, G, B;
for(int x = 0; x < wid; x++) {
for(int y = 0; y < hei; y++) {
int off = (x * y) + y; // changed
int pixel = pixs[off]; // changed
A = Color.alpha(pixel);
R = tR[Color.red(pixel)];
G = tG[Color.green(pixel)];
B = tB[Color.blue(pixel)];
pixs[off] = Color.argb(A, R, G, B); // changed
}
}
bmOut.setPixels(pixs, 0, wid, 0, 0, wid, hei); // changed
return bmOut;
}
跑得快,甚至得到一個正確的結果,如果沒有轉型。但是,如果我嘗試按摩像素(應用轉換),它會分解 。所以我比較了getPixel()中的ARGB像素與getPixels(...)中的像素數組的數值,並且它們是不同的(前兩個是相同的,這讓我不知道大概是那麼多)。
array getPixel
a r g b a r g b
------------------
ff65340b ff65340b
ff64330a ff64330a
ff66320b ff63320a
ff65310a ff613008
ff66300c ff62300d
ff67310d ff62300d
ff68300d ff622d0d
ff69310e ff5f2a0a
....
有人知道我這次做錯了嗎?我不願意放棄mem-array解決方案的速度 。 謝謝,肖恩
感謝一大堆。它從來沒有抓住讓我驚訝的是,在20小時的編碼和10杯咖啡讓我保持清醒之後,我可以獲得多少愚蠢。我從不猶豫向世界展示。無論如何,將代碼縮減爲一個循環,會進一步加快速度。它工作得很好。肖恩 – seanpj 2013-03-16 10:37:51
不客氣肖恩。享受編碼:) – 2013-03-17 00:36:30