[英]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.