• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.testapp
18 
19 import android.graphics.Bitmap
20 import android.renderscript.Allocation
21 import android.renderscript.Element
22 import android.renderscript.RenderScript
23 import android.renderscript.Script
24 import android.renderscript.ScriptIntrinsic3DLUT
25 import android.renderscript.Type
26 import android.renderscript.toolkit.Range2d
27 
28 /**
29  * Does a 3D LookUpTable operation using the RenderScript Intrinsics.
30  */
intrinsicLut3dnull31 fun intrinsicLut3d(
32     context: RenderScript,
33     inputArray: ByteArray,
34     sizeX: Int,
35     sizeY: Int,
36     cubeArray: ByteArray,
37     cubeSize: Dimension,
38     restriction: Range2d?
39 ): ByteArray {
40     val scriptLut3d: ScriptIntrinsic3DLUT = ScriptIntrinsic3DLUT.create(
41         context, Element.U8_4(
42             context
43         )
44     )
45     val builder = Type.Builder(context, Element.U8_4(context))
46     builder.setX(sizeX)
47     builder.setY(sizeY)
48     val arrayType = builder.create()
49     val inputAllocation = Allocation.createTyped(context, arrayType)
50     val outAllocation = Allocation.createTyped(context, arrayType)
51     inputAllocation.copyFrom(inputArray)
52     val intrinsicOutArray = ByteArray(sizeX * sizeY * 4)
53 
54     val cubeTypeBuilder: Type.Builder =
55         Type.Builder(context, Element.U8_4(context))
56     cubeTypeBuilder.setX(cubeSize.sizeX)
57     cubeTypeBuilder.setY(cubeSize.sizeY)
58     cubeTypeBuilder.setZ(cubeSize.sizeZ)
59     val cubeType: Type = cubeTypeBuilder.create()
60     val cubeAllocation = Allocation.createTyped(context, cubeType)
61     cubeAllocation.copyFrom(cubeArray)
62     scriptLut3d.setLUT(cubeAllocation)
63     if (restriction != null) {
64         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
65         val options = Script.LaunchOptions()
66         options.setX(restriction.startX, restriction.endX)
67         options.setY(restriction.startY, restriction.endY)
68         scriptLut3d.forEach(inputAllocation, outAllocation, options)
69     } else {
70         scriptLut3d.forEach(inputAllocation, outAllocation)
71     }
72 
73     outAllocation.copyTo(intrinsicOutArray)
74     inputAllocation.destroy()
75     outAllocation.destroy()
76     cubeAllocation.destroy()
77     arrayType.destroy()
78     cubeType.destroy()
79     scriptLut3d.destroy()
80     return intrinsicOutArray
81 }
82 
intrinsicLut3dnull83 fun intrinsicLut3d(
84     context: RenderScript,
85     bitmap: Bitmap,
86     cubeArray: ByteArray,
87     cubeSize: Dimension,
88     restriction: Range2d?
89 ): ByteArray {
90     val baseElement = renderScriptElementForBitmap(context, bitmap)
91     val scriptLut3d: ScriptIntrinsic3DLUT = ScriptIntrinsic3DLUT.create(context, baseElement)
92     val inputAllocation = Allocation.createFromBitmap(context, bitmap)
93     inputAllocation.copyFrom(bitmap)
94     val outAllocation = Allocation.createTyped(context, inputAllocation.type)
95     val intrinsicOutArray = ByteArray(bitmap.byteCount)
96 
97     val cubeTypeBuilder: Type.Builder =
98         Type.Builder(context, Element.U8_4(context))
99     cubeTypeBuilder.setX(cubeSize.sizeX)
100     cubeTypeBuilder.setY(cubeSize.sizeY)
101     cubeTypeBuilder.setZ(cubeSize.sizeZ)
102     val cubeType: Type = cubeTypeBuilder.create()
103     val cubeAllocation = Allocation.createTyped(context, cubeType)
104     cubeAllocation.copyFrom(cubeArray)
105     scriptLut3d.setLUT(cubeAllocation)
106     if (restriction != null) {
107         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
108         val options = Script.LaunchOptions()
109         options.setX(restriction.startX, restriction.endX)
110         options.setY(restriction.startY, restriction.endY)
111         scriptLut3d.forEach(inputAllocation, outAllocation, options)
112     } else {
113         scriptLut3d.forEach(inputAllocation, outAllocation)
114     }
115 
116     outAllocation.copyTo(intrinsicOutArray)
117     inputAllocation.destroy()
118     outAllocation.destroy()
119     cubeAllocation.destroy()
120     cubeType.destroy()
121     scriptLut3d.destroy()
122     return intrinsicOutArray
123 }
124