如何使用具有多个输入分配的 RenderScript?

Jam*_*hao 5 android renderscript

最近,我发现渲染脚本是 Android 上图像处理的更好选择。表演很精彩。但关于它的文献并不多。我想知道是否可以通过渲染脚本将多张照片合并为结果照片。

http://developer.android.com/guide/topics/renderscript/compute.html说:

内核可能有输入Allocation、输出Allocation或两者都有。一个内核不能有超过一个输入或一个输出Allocation。如果需要多个输入或输出,这些对象应绑定到脚本全局变量,并通过或rs_allocation从内核或可调用函数访问。rsGetElementAt_type()rsSetElementAt_type()

这个问题有代码示例吗?

Sta*_*kyy 5

对于具有多个输入的内核,您必须手动处理额外的输入。

假设您需要 2 个输入。

示例.rs:

rs_allocation extra_alloc;

uchar4 __attribute__((kernel)) kernel(uchar4 i1, uint32_t x, uint32_t y)
{
    // Manually getting current element from the extra input
    uchar4 i2 = rsGetElementAt_uchar4(extra_alloc, x, y);
    // Now process i1 and i2 and generate out
    uchar4 out = ...;
    return out;
}
Run Code Online (Sandbox Code Playgroud)

爪哇:

Bitmap bitmapIn = ...;
Bitmap bitmapInExtra = ...;
Bitmap bitmapOut = Bitmap.createBitmap(bitmapIn.getWidth(),
                    bitmapIn.getHeight(), bitmapIn.getConfig());

RenderScript rs = RenderScript.create(this);
ScriptC_example script = new ScriptC_example(rs);

Allocation inAllocation = Allocation.createFromBitmap(rs, bitmapIn);
Allocation inAllocationExtra = Allocation.createFromBitmap(rs, bitmapInExtra);
Allocation outAllocation = Allocation.createFromBitmap(rs, bitmapOut);

// Execute this kernel on two inputs
script.set_extra_alloc(inAllocationExtra);
script.forEach_kernel(inAllocation, outAllocation);

// Get the data back into bitmap
outAllocation.copyTo(bitmapOut);
Run Code Online (Sandbox Code Playgroud)


Tim*_*ray 3

你想做类似的事情

rs_allocation input1;
rs_allocation input2;

uchar4 __attribute__((kernel)) kernel() {
  ... // body of kernel goes here
  uchar4 out = ...;
  return out;
}
Run Code Online (Sandbox Code Playgroud)

从您的 Java 代码中调用set_input1set_input2将它们设置为适当的分配,然后forEach_kernel使用您的输出分配进行调用。