Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. class RsYuvToRgb(private val rs: RenderScript) {
  2.  
  3. private var mOutputAllocation: Allocation? = null
  4. private var mInputAllocation: Allocation? = null
  5.  
  6. private val rsYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs))
  7.  
  8. private var yArray: ByteArray? = null
  9. private var uArray: ByteArray? = null
  10. private var vArray: ByteArray? = null
  11.  
  12. private fun ByteBuffer.initByteArray(): ByteArray {
  13. return ByteArray(capacity())
  14. }
  15.  
  16. fun yuvToRgb(yBuffer: ByteBuffer, uBuffer: ByteBuffer, vBuffer: ByteBuffer, width: Int, height: Int): ByteBuffer {
  17. if (mInputAllocation == null){
  18. val totalSize = yBuffer.capacity() + uBuffer.capacity() + vBuffer.capacity()
  19. val yuvType = Type.Builder(rs, Element.U8(rs)).apply {
  20. setX(totalSize)
  21. }
  22. mInputAllocation = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT)
  23. }
  24. if (mOutputAllocation == null){
  25. val rgbType = Type.createXY(rs, Element.RGBA_8888(rs), width, height)
  26. mOutputAllocation = Allocation.createTyped(rs, rgbType, Allocation.USAGE_SCRIPT)
  27. }
  28.  
  29. if (yArray == null) {
  30. yArray = yBuffer.initByteArray()
  31. }
  32. if (uArray == null) {
  33. uArray = uBuffer.initByteArray()
  34. }
  35. if (vArray == null) {
  36. vArray = vBuffer.initByteArray()
  37. }
  38.  
  39. yBuffer.get(yArray)
  40. uBuffer.get(uArray)
  41. vBuffer.get(vArray)
  42.  
  43. mInputAllocation!!.copy1DRangeFrom(0, yArray!!.size, yArray)
  44. mInputAllocation!!.copy1DRangeFrom(yArray!!.size, uArray!!.size, uArray)
  45. mInputAllocation!!.copy1DRangeFrom(yArray!!.size + uArray!!.size, vArray!!.size, vArray)
  46.  
  47. rsYuvToRgb.setInput(mInputAllocation)
  48. rsYuvToRgb.forEach(mOutputAllocation)
  49. return mOutputAllocation!!.byteBuffer
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement