gpt4 book ai didi

android - 内存位图转换

转载 作者:行者123 更新时间:2023-11-29 01:55:28 24 4
gpt4 key购买 nike

我有以下问题。我从一个有效的位图转换例程开始完美地进行任何类型的转换。

  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;
}

但它非常缓慢 - 由 getPixel()/setPixel() sibling 造成。没问题,我说,我将只使用内存缓冲区(就像在旧的 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
....

有人知道我这次做错了什么吗?我不愿意放弃的速度内存数组解决方案。谢谢,肖恩

最佳答案

应该是

int off = ( y * wid ) + x;

顺便说一句,我认为这两个循环是不必要的,你可以简单地做:

for (int off = pixs.length - 1; off >= 0; off--) 

关于android - 内存位图转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com