【发布时间】:2015-09-01 22:25:53
【问题描述】:
我正在尝试进入渲染脚本,但对分配的使用感到困惑。几乎所有的例子都展示了下一个算法:
- 创建进出位图
- 从位图进出相应地创建进出分配
- 配置脚本并执行 forEach 方法
- 使用 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