简体   繁体   English

从位图分配Renderscript

[英]Renderscript allocation from bitmap

I'm trying to get into render script and was confused regarding allocations usage. 我正试图进入渲染脚本,并对分配使用感到困惑。 Almost all examples show next algorithm: 几乎所有示例都显示了下一个算法:

  1. Create In and Out Bitmap 创建进出位图
  2. Create In and Out Allocation from Bitmaps In and Out correspondingly 相应地从位图输入和输出创建输入和输出分配
  3. Configure script and perform forEach method 配置脚本并执行forEach方法
  4. copy result from Out allocation into bitmap using copyTo method 使用copyTo方法将Out out的结果复制到位图中

Something like that: 像这样的东西:

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

This works, but it also works even if I omit step 4 by removing: 这有效,但即使我通过删除省略步骤4也可以工作:

allocationOut.copyTo(dstBitmap);

Lets go further and lower brightness after grayscale: 让我们进一步降低灰度后的亮度:

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

Shortly describing the code above, we performed grayscale collor matrix from In allocation into Out one, and brightness adjustment in backward direction. 简单描述上面的代码,我们执行了从In allocation到Out one的灰度collor矩阵,以及向后方向的亮度调整。 I never called copyTo method, but at the end I've got result in srcBitmap and it was correct. 我从来没有调用copyTo方法,但最后我得到了srcBitmap的结果并且它是正确的。

That's not the end. 那不是结束。 Lets go deeper. 让我们更深入。 I'll leave only one bitmap and one Allocation: 我只留下一个位图和一个分配:

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

The result is the same... 结果是一样的...



Can anybody explain why does it happen and where to use copyTo and where I can use targeting Bitmap without it? 任何人都可以解释为什么会发生这种情况以及在哪里使用copyTo以及我可以在没有它的情况下使用目标Bitmap?

The Allocation objects are needed in order to provide the proper mapping of Bitmap to something Renderscript understands. 需要Allocation对象才能提供Bitmap到Renderscript理解的正确映射。 If you are targeting API 18 or higher, the Allocation.createFromBitmap() methods you are using are automatically giving in the flag USAGE_SHARED , which tries to have the Allocation use the same backing memory as the Bitmap object. 如果您的目标是API 18或更高版本,则您使用的Allocation.createFromBitmap()方法会自动提供USAGE_SHARED标志,该标志会尝试让Allocation使用与Bitmap对象相同的后备内存。 So the two are linked but technically the copyTo() method is still needed as the RS implementation may need to synchronize it back. 所以两者是相互关联的,但从技术上讲,仍然需要copyTo()方法,因为RS实现可能需要将其同步回来。 On some platforms this may have already happened where others may cause this to pause as DMA or other mechanisms are updating the Bitmap 's backing memory with whatever changes were made by the RS code. 在某些平台上,这可能已经发生,其他人可能会因为DMA或其他机制正在使用RS代码所做的任何更改来更新Bitmap的后备内存而暂停。

As for why you can reverse the in/out Allocation order when calling scripts - it's valid and up to you to get the arguments and order correct. 至于为什么你可以在调用脚本时反转输入/输出Allocation顺序 - 它是有效的,由你来获取参数和顺序正确。 To RS they are just objects which point to some type of backing data to be manipulated. 对于RS,它们只是指向要操纵的某种类型的后备数据的对象。 Since both were created with the Allocation.createFromBitmap() call, they can be used as either input or output as long as the Bitmap objects are mutable. 由于两者都是使用Allocation.createFromBitmap()调用创建的,因此只要Bitmap对象是可变的,它们就可以用作输入或输出。

Similarly, using the same Allocation for the input and output is not normal, but also not invalid. 同样,对输入和输出使用相同的Allocation是不正常的,但也不是无效的。 It just means your input is changing on the fly. 它只是意味着您的输入正在快速变化。 As long as your script is not accessing other Element s in the data when the root function is called for a specific Element , then it should work (as you are seeing.) 只要在为特定Element调用根函数时,您的脚本没有访问数据中的其他Element ,那么它应该可以正常工作(如您所见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM