我可以设置输入和输出的分配上Renderscript是不同的尺寸/尺寸? [英] Can I set input and output allocations on Renderscript to be of different sizes/dimensions?

查看:268
本文介绍了我可以设置输入和输出的分配上Renderscript是不同的尺寸/尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我想学习Renderscript,所以我想尝试这样做,我想一些简单的操作。

问题

我想旋转一个位图,这恐怕是相当简单来管理。

在C / C ++,它是一个简单的事情(搜索jniRotateBitmapCw90):

<一个href="https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations/blob/master/JniBitmapOperationsLibrary/jni/JniBitmapOperationsLibrary.cpp" rel="nofollow">https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations/blob/master/JniBitmapOperationsLibrary/jni/JniBitmapOperationsLibrary.cpp

事情是,当我尝试这样的Renderscript,我得到这个错误:

  

android.support.v8.renderscript.RSRuntimeException:尺寸不匹配   艾因参数之间AOUT!

下面是我做的:

RS:

 无效rotate90CW(常量在uchar4 *,uchar4 *出来,uint32_t的X,uint32_t的Y){
    // XY。 ..X ......
    // ...&GT; .. Y'GT; ...&GT; Y ..
    // ...... .YX X ..
    出[...] =在[...] ...
}
 

Java的:

  mRenderScript = RenderScript.create(本);
    mInBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.sample_photo);
    mOutBitmap = Bitmap.createBitmap(mInBitmap.getHeight(),mInBitmap.getWidth(),mInBitmap.getConfig());
    最终分配的输入= Allocation.createFromBitmap(mRenderScript,mInBitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
    最终分配输出= Allocation.createFromBitmap(mRenderScript,mOutBitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
    ScriptC_test脚本=新ScriptC_test(mRenderScript,getResources(),R.raw.test);
    ...
    script.forEach_rotate90CW(输入,输出);
    output.copyTo(mOutBitmap);
 

甚至当我设置这两个分配到相同的大小(平方位)的,我只是设置输出为输入:

 出[宽* Y + X] = [宽度* Y + X]
 

  • 那么我得到的是与孔位图......怎么会这样?

这是我所得到的:

输入图像的描述在这里

的问题

  1. 这是否意味着我不能做这种手术?

  2. 这是否意味着我不能使用各种大小的分配/尺寸?

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

  4. 为什么我得到孔位图,对于一个方形输入和放大器的情况下;输出


编辑:这是我目前的code:

RS

  rs_allocation *的;
 

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

Java的:

  mRenderScript = RenderScript.create(本);
    mInBitmap = BitmapFactory.de codeResource(getResources(),R.drawable.sample_photo);
    mOutBitmap = Bitmap.createBitmap(mInBitmap.getHeight(),mInBitmap.getWidth(),mInBitmap.getConfig());
    最终分配的输入= Allocation.createFromBitmap(mRenderScript,mInBitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
    最终分配输出= Allocation.createFromBitmap(mRenderScript,mOutBitmap,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
    ScriptC_test脚本=新ScriptC_test(mRenderScript,getResources(),R.raw.test);

    script.bind_in(输入);
    script.forEach_rotate90CW(输出);
    output.copyTo(mOutBitmap);
    mImageView.setImageBitmap(mOutBitmap);
 

解决方案

这里所说:

  
      
  1. 这是否意味着我不能做这种手术?
  2.   

没有,真的没有。你只需要正确手艺的事情。

  <醇开始=2>   
  • 这是否意味着我不能使用各种大小/尺寸?
  • 的分配   

    没有,但它确实意味着你不能在你现在正在做的事情的方式使用不同大小的分配。输入/输出机制的默认内核期望的输入和输出的大小来匹配,因此它可以遍历所有元素的正确。如果你需要不同的东西,它是由你来管理它。低于更多。

      <醇开始=3>   
  • 是否有可能解决这个问题......怎么样?
  •   

    最简单的解决方案是创建一个配置为输入,并将其绑定到renderscript实例,而不是把它作为一个参数。那么你的RS将只需要一个输出分配(和你的内核只需要输出,X和Y)。从那里,你可以决定你想要的输入分配在其中协调,并直接将其放置到输出位置:

      INT INX = ...;
    INT INY = ...;
    uchar4 curIn = rsGetElementAt_uchar4(inAlloc,INX,INY);
    * OUT = curIn;
     

      <醇开始=4>   
  • 为什么我得到孔位图,对于一个方形输入和放大器的情况下;输出
  •   

    这是因为你不能使用x和y参数,以抵消到输入和输出分配。那些输入/输出参数已经指向在输入和输出的正确的(相同)的位置。你在做索引是不必要的,而不是真正的支持。每当你的内核被调用时,它被要求分配在1元位置。这就是为什么当作为参数所提供的输入和输出大小必须是相同的。

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

    解决方案

    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.

    这篇关于我可以设置输入和输出的分配上Renderscript是不同的尺寸/尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆