Can I set input and output allocations on Renderscript to be of different sizes/dimensions?

假装没事ソ 提交于 2019-12-02 08:51:18

问题


Background

I'm trying to learn Renderscript, so I wish to try to do some simple operations that I think about.

The problem

I thought of rotating a bitmap, which is something that's simple enough to manage.

on C/C++, it's a simple thing to do (search for "jniRotateBitmapCw90") :

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

Thing is, when I try this on Renderscript, I get this error:

android.support.v8.renderscript.RSRuntimeException: Dimension mismatch between parameters ain and aout!

Here's what I do:

RS:

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

Java:

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

Even when I do set both allocations to be of the same size (squared bitmap), and I just set the output to be the input:

out[width * y + x] = in[width * y+x];
  • then what I get is a bitmap with holes... How come?

This is what I get:

The questions

  1. Does this mean I can't do this kind of operation?

  2. Does it mean that I can't use allocations of various sizes/dimensions?

  3. Is it possible to overcome this issue (and still use Renderscript, of course) ? If so, how? Maybe I could add an array variable inside the RS side, and set the allocation to it, instead?

  4. Why do I get holes in the bitmap, for the case of a square input&output?


EDIT:This is my current code:

RS

rs_allocation *in;  

uchar4 attribute((kernel)) rotate90CW(uint32_t x, uint32_t y){ // XY. ..X ... ... // ...>..Y>...>Y.. // ... ... .YX X.. uchar4 curIn =rsGetElementAt_uchar4(in, 0, 0); return curIn; //just for testing... }

Java:

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

回答1:


Here goes:

  1. Does this mean I can't do this kind of operation?

No, not really. You just have to craft things correctly.

  1. Does it mean that I can't use allocations of various sizes/dimensions?

No, but it does mean you can't use different size allocations in the way you currently are doing things. The default kernel in/out mechanism expects the input and output sizes to match so it can iterate over all of the elements correctly. If you need something different, it's up to you to manage it. More on that below.

  1. Is it possible to overcome this issues...how?

The easiest solution would be to create an Allocation for input and bind it to the renderscript instance rather than pass it as a parameter. Then your RS would only need an output allocation (and your kernel only take output, x and y). From there you can determine which coordinate within the input allocation you want and place it directly into the output location:

int inX = ...;
int inY = ...;
uchar4 curIn = rsGetElementAt_uchar4(inAlloc, inX, inY);
*out = curIn;
  1. Why do I get holes in the bitmap, for the case of a square input&output?

It's because you cannot use the x and y parameters to offset into the input and output allocation. Those in/out parameters are already pointing to the correct (same) location in both the input and output. The indexing you're doing is unnecessary and not really supported. Each time your kernel is called, it is being called for 1 element location within the allocation. This is why the input and output sizes must be the same when provided as parameters.



来源:https://stackoverflow.com/questions/33056776/can-i-set-input-and-output-allocations-on-renderscript-to-be-of-different-sizes

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