【问题标题】:Renderscript allocation from bitmap从位图分配渲染脚本
【发布时间】:2015-09-01 22:25:53
【问题描述】:

我正在尝试进入渲染脚本,但对分配的使用感到困惑。几乎所有的例子都展示了下一个算法:

  1. 创建进出位图
  2. 从位图进出相应地创建进出分配
  3. 配置脚本并执行 forEach 方法
  4. 使用 copyTo 方法将 Out 分配的结果复制到位图中

类似的东西:

    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
    Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());

    Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);
    Allocation allocationOut = Allocation.createFromBitmap(renderScript, dstBitmap);

    scriptColorMatrix.setGreyscale();

    scriptColorMatrix.forEach(allocationIn, allocationOut);

    //no difference after removing this line
    allocationOut.copyTo(dstBitmap);

    imagePreview.setImageBitmap(dstBitmap);

这有效,但即使我通过删除省略了第 4 步,它也有效:

allocationOut.copyTo(dstBitmap);

让我们在灰度之后进一步降低亮度:

    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);
    Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), srcBitmap.getConfig());

    Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);
    Allocation allocationOut = Allocation.createFromBitmap(renderScript, dstBitmap);

    scriptColorMatrix.setGreyscale();

    scriptColorMatrix.forEach(allocationIn, allocationOut);

    //reset color matrix
    scriptColorMatrix.setColorMatrix(new Matrix4f());

    //adjust brightness
    scriptColorMatrix.setAdd(-0.5f, -0.5f, -0.5f, 0f);

    //Performing forEach vise versa (from out to in)
    scriptColorMatrix.forEach(allocationOut, allocationIn);

    imagePreview.setImageBitmap(srcBitmap);

简单描述上面的代码,我们执行了从 In 分配到 Out 的灰度颜色矩阵,以及向后方向的亮度调整。我从未调用过 copyTo 方法,但最后我在 srcBitmap 中得到了结果,它是正确的。

这还没有结束。让我们更深入。我只留下一张位图和一张分配:

    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lena);

    Allocation allocationIn = Allocation.createFromBitmap(renderScript, srcBitmap);

    scriptColorMatrix.setGreyscale();

    scriptColorMatrix.forEach(allocationIn, allocationIn);

    //reset color matrix
    scriptColorMatrix.setColorMatrix(new Matrix4f());

    //adjust brightness
    scriptColorMatrix.setAdd(-0.5f, -0.5f, -0.5f, 0f);

    scriptColorMatrix.forEach(allocationIn, allocationIn);

    imagePreview.setImageBitmap(srcBitmap);

结果是一样的……



谁能解释为什么会发生这种情况以及在哪里使用 copyTo 以及在没有它的情况下我可以在哪里使用定位位图?

【问题讨论】:

    标签: android renderscript


    【解决方案1】:

    需要Allocation 对象,以便将Bitmap 正确映射到Renderscript 可以理解的内容。如果您的目标是 API 18 或更高版本,您正在使用的 Allocation.createFromBitmap() 方法会自动提供标志 USAGE_SHARED,它试图让 Allocation 使用与 Bitmap 对象相同的后备内存。所以两者是链接,但从技术上讲,copyTo() 方法仍然需要,因为 RS 实现可能需要将其同步回来。在某些平台上,这可能已经发生,而其他平台可能会导致暂停,因为 DMA 或其他机制正在使用 RS 代码所做的任何更改更新Bitmap 的后备内存。

    至于为什么在调用脚本时可以颠倒输入/输出Allocation 的顺序 - 它是有效的,由您决定是否正确获取参数和顺序。对于 RS 来说,它们只是指向要操作的某种类型的支持数据的对象。由于两者都是使用Allocation.createFromBitmap() 调用创建的,因此只要Bitmap 对象是可变的,它们就可以用作输入或输出。

    同样的,输入输出使用相同的Allocation是不正常的,但也不是无效的。这只是意味着您的输入正在动态变化。只要在为特定 Element 调用根函数时,您的脚本没有访问数据中的其他 Elements,那么它应该可以工作(如您所见。)

    【讨论】:

    • 不客气!我很高兴你发现它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多