How to use RenderScript with multiple input allocations?

拈花ヽ惹草 提交于 2019-12-23 15:05:01

问题


Recently, I found render script is a better choice for image processing on Android. The performance is wonderful. But there are not many documents on it. I am wondering if I can merge multiple photos into a result photo by render script.

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

A kernel may have an input Allocation, an output Allocation, or both. A kernel may not have more than one input or one output Allocation. If more than one input or output is required, those objects should be bound to rs_allocation script globals and accessed from a kernel or invokable function via rsGetElementAt_type() or rsSetElementAt_type().

Is there any code example for this issue?


回答1:


For the kernel with multiple inputs you would have to manually handle additional inputs.

Let's say you want 2 inputs.

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

Java:

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



回答2:


you want to do something like

rs_allocation input1;
rs_allocation input2;

uchar4 __attribute__((kernel)) kernel() {
  ... // body of kernel goes here
  uchar4 out = ...;
  return out;
}

Call set_input1 and set_input2 from your Java code to set those to the appropriate Allocations, then call forEach_kernel with your output Allocation.




回答3:


This is how you do it :

in the .rs file :

uchar4 RS_KERNEL myKernel(float4 in1, int in2, uint32_t x, uint32_t y)
{
    //My code
}

in java :

myScript.forEach_myKernel(allocationInput1, allocationInput2, allocationOutput);

uchar4, float4, and int are used as example. It works for me, you can add more than 2 inputs.



来源:https://stackoverflow.com/questions/20783830/how-to-use-renderscript-with-multiple-input-allocations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!