Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RenderScript with multiple input allocations?

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?

like image 657
James Zhao Avatar asked Mar 22 '23 05:03

James Zhao


2 Answers

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);
like image 117
Stanislav Vitvitskyy Avatar answered Mar 23 '23 23:03

Stanislav Vitvitskyy


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.

like image 26
Tim Murray Avatar answered Mar 23 '23 21:03

Tim Murray