我可以在Renderscript上设置不同大小/尺寸的输入和输出分配吗?

时间:2015-10-10 17:20:36

标签: android rotation renderscript

背景

我正在尝试学习Renderscript,所以我希望尝试做一些我想到的简单操作。

问题

我想过旋转一个位图,这个东西很容易管理。

在C / C ++上,这很简单(搜索“jniRotateBitmapCw90”):

https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations/blob/master/JniBitmapOperationsLibrary/jni/JniBitmapOperationsLibrary.cpp

事情是,当我在Renderscript上尝试这个时,我收到了这个错误:

  

android.support.v8.renderscript.RSRuntimeException:维度不匹配   参数之间ain和aout!

这就是我的所作所为:

RS:

void rotate90CW(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
    // XY. ..X ... ... 
    // ...>..Y>...>Y..
    // ... ... .YX X.. 
    out[...]=in[...] ...
}

爪哇:

    mRenderScript = RenderScript.create(this);
    mInBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_photo);
    mOutBitmap = Bitmap.createBitmap(mInBitmap.getHeight(), mInBitmap.getWidth(), mInBitmap.getConfig());
    final Allocation input = Allocation.createFromBitmap(mRenderScript, mInBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createFromBitmap(mRenderScript, mOutBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    ScriptC_test script = new ScriptC_test(mRenderScript, getResources(), R.raw.test);
    ...
    script.forEach_rotate90CW(input, output);
    output.copyTo(mOutBitmap);

即使我将两个分配设置为相同的大小(平方位图),我只是将输出设置为输入:

out[width * y + x] = in[width * y+x];
  • 然后我得到的是带洞的位图......怎么样?

这就是我得到的:

enter image description here

问题

  1. 这是否意味着我不能做这种操作?

  2. 这是否意味着我无法使用各种尺寸/尺寸的分配?

  3. 是否有可能克服此问题(当然仍然使用Renderscript)?如果是这样,怎么样? 也许我可以在RS端添加一个数组变量,然后设置它的分配,而不是?

  4. 对于方形输入和输出的情况,为什么我会在位图中出现漏洞?

  5. 编辑:这是我目前的代码:

    RS

    rs_allocation *in;  
    

    uchar4 属性((内核))rotate90CW(uint32_t x,uint32_t y){         // XY。 ..X ... ...         // ...> .. Y> ...> Y ..         // ...... ... .YX X ..         uchar4 curIn = rsGetElementAt_uchar4(in,0,0);         返回curIn; //只是为了测试......     }

    爪哇:

        mRenderScript = RenderScript.create(this);
        mInBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_photo);
        mOutBitmap = Bitmap.createBitmap(mInBitmap.getHeight(), mInBitmap.getWidth(), mInBitmap.getConfig());
        final Allocation input = Allocation.createFromBitmap(mRenderScript, mInBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        final Allocation output = Allocation.createFromBitmap(mRenderScript, mOutBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        ScriptC_test script = new ScriptC_test(mRenderScript, getResources(), R.raw.test);
    
        script.bind_in(input);
        script.forEach_rotate90CW(output);
        output.copyTo(mOutBitmap);
        mImageView.setImageBitmap(mOutBitmap);
    

2 个答案:

答案 0 :(得分:2)

这里是:

  
      
  1. 这是否意味着我无法进行此类操作?
  2.   

不,不是真的。你只需要正确地制作东西。

  
      
  1. 这是否意味着我无法使用各种尺寸/尺寸的分配?
  2.   

不,但它确实意味着您不能以当前的方式使用不同的大小分配。默认的内核输入/输出机制要求输入和输出大小匹配,以便它可以正确地迭代所有元素。如果您需要不同的东西,可以由您来管理它。更多关于以下内容。

  
      
  1. 是否有可能克服这个问题......怎么样?
  2.   

最简单的解决方案是为输入创建一个Allocation并将其绑定到renderscript实例,而不是将其作为参数传递。然后你的RS只需要一个输出分配(你的内核只需要输出,x和y)。从那里,您可以确定所需输入分配中的哪个坐标,并将其直接放入输出位置:

int inX = ...;
int inY = ...;
uchar4 curIn = rsGetElementAt_uchar4(inAlloc, inX, inY);
*out = curIn;
  
      
  1. 为什么我会在位图中出现漏洞,对于方形输入和输出?
  2.   

这是因为你不能使用x和y参数来偏移到输入和输出分配中。那些输入/输出参数已经指向输入和输出中的正确(相同)位置。您正在执行的索引是不必要的,并且不受支持。每次调用内核时,都会在分配中调用1个元素位置。这就是输入和输出大小在作为参数提供时必须相同的原因。

答案 1 :(得分:0)

这应该可以解决您的问题

RS

rs_allocation *in;  
uchar4 attribute((kernel)) rotate90CW(uint32_t x, uint32_t y){
...
uchar4 curIn =rsGetElementAt_uchar4(in, x, y); 

return curIn; 
 }