[英]Android: Renderscript error - Cannot update allocation from bitmap, sizes mismatch
[英]Renderscript allocation from bitmap
我正试图进入渲染脚本,并对分配使用感到困惑。 几乎所有示例都显示了下一个算法:
像这样的东西:
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 allocation到Out one的灰度collor矩阵,以及向后方向的亮度调整。 我从来没有调用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以及我可以在没有它的情况下使用目标Bitmap?
需要Allocation
对象才能提供Bitmap
到Renderscript理解的正确映射。 如果您的目标是API 18或更高版本,则您使用的Allocation.createFromBitmap()
方法会自动提供USAGE_SHARED
标志,该标志会尝试让Allocation
使用与Bitmap
对象相同的后备内存。 所以两者是相互关联的,但从技术上讲,仍然需要copyTo()
方法,因为RS实现可能需要将其同步回来。 在某些平台上,这可能已经发生,其他人可能会因为DMA或其他机制正在使用RS代码所做的任何更改来更新Bitmap
的后备内存而暂停。
至于为什么你可以在调用脚本时反转输入/输出Allocation
顺序 - 它是有效的,由你来获取参数和顺序正确。 对于RS,它们只是指向要操纵的某种类型的后备数据的对象。 由于两者都是使用Allocation.createFromBitmap()
调用创建的,因此只要Bitmap
对象是可变的,它们就可以用作输入或输出。
同样,对输入和输出使用相同的Allocation
是不正常的,但也不是无效的。 它只是意味着您的输入正在快速变化。 只要在为特定Element
调用根函数时,您的脚本没有访问数据中的其他Element
,那么它应该可以正常工作(如您所见)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.