我有以下JNI
方法,我想将其转换为 Android RenderScript
,因为这会使我的项目更易于管理。
这是JNI
方法:
static void applyRGBCurve(int width, int height, int *pixels, int *rgb) {
int R[256];
int G[256];
int B[256];
// It seems that they extract the RGB components here
// to build up a lookup table
// so, we end up with lookup table for the RED component, GREEN component, BLUE component of each pixel
for (int i = 0; i < 256; i++) {
R[i] = (rgb[i] << 16) & 0x00FF0000;
G[i] = (rgb[i] << 8) & 0x0000FF00;
B[i] = rgb[i] & 0x000000FF;
}
for (int i = 0; i < width * height; i++) {
// using the lookup tables, they construct each pixel
pixels[i] =
(0xFF000000 & pixels[i]) | (R[(pixels[i] >> 16) & 0xFF]) | (G[(pixels[i] >> 8) & 0xFF]) | (B[pixels[i] & 0xFF]);
}
}
我是新手RenderScript
。所以我很感谢任何帮助。
这是我尝试过的:
#pragma version(1)
#pragma rs java_package_name(com.example.imageprocessinginkotlin)
#pragma rs_fp_relaxed
int rgb[256];
uint32_t R[256];
uint32_t G[256];
uint32_t B[256];
void prepareChannels(){
for (int i = 0; i < 256; i++) {
// HERE I did not know how to apply shift operations in Renderscript
R[i] = rgb[i] & 0xFF;
G[i] = rgb[i] & 0xFF;
B[i] = rgb[i] & 0xFF;
}
}
uchar4 RS_KERNEL applyRGBCurve(uchar4 in){
uchar4 out;
out.a = in.a;
out.r = R[in.r];
out.g = G[in.g];
out.b = B[in.b];
return out;
}
从 Java 端看,它看起来像这样:
val rs = RenderScript.create(activity)
allocationIn = Allocation.createFromBitmap(rs, bitmap)
allocationOut = Allocation.createFromBitmap(rs, bitmap)
script = ScriptC_tonecurve(rs)
script.set_rgb(item.rgb.toIntArray())
script.invoke_prepareChannels()
script.forEach_applyRGBCurve(allocationIn, allocationOut)
allocationOut.copyTo(bitmap)
binding.imageView.setImageBitmap(bitmap)
我的Renderscript
代码是否等同于 JNI 方法?我会说不是因为缺少移位操作(与 JNI 方法相比)。