1 /*
<lambda>null2  * 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.renderscript.toolkit.Range2d
20 import android.renderscript.toolkit.Rgba3dArray
21 
22 /**
23  * Reference implementation of a 3D LookUpTable operation.
24  */
25 @ExperimentalUnsignedTypes
26 fun referenceLut3d(
27     inputArray: ByteArray,
28     sizeX: Int,
29     sizeY: Int,
30     cube: Rgba3dArray,
31     restriction: Range2d?
32 ): ByteArray {
33     val input = Vector2dArray(inputArray.asUByteArray(), 4, sizeX, sizeY)
34     val output = input.createSameSized()
35     input.forEach(restriction) { x, y ->
36         output[x, y] = lookup(input[x, y], cube)
37     }
38     return output.values.asByteArray()
39 }
40 
41 @ExperimentalUnsignedTypes
lookupnull42 private fun lookup(input: UByteArray, cube: Rgba3dArray): UByteArray {
43     // Calculate the two points at opposite edges of the size 1
44     // cube that contains our point.
45     val maxIndex = Int4(cube.sizeX - 1, cube.sizeY - 1, cube.sizeZ - 1, 0)
46     val baseCoordinate: Float4 = input.toFloat4() * maxIndex.toFloat4() / 255f
47     val point1: Int4 = baseCoordinate.intFloor()
48     val point2: Int4 = min(point1 + 1, maxIndex)
49     val fractionAwayFromPoint1: Float4 = baseCoordinate - point1.toFloat4()
50 
51     // Get the RGBA values at each of the four corners of the size 1 cube.
52     val v000 = cube[point1.x, point1.y, point1.z].toFloat4()
53     val v100 = cube[point2.x, point1.y, point1.z].toFloat4()
54     val v010 = cube[point1.x, point2.y, point1.z].toFloat4()
55     val v110 = cube[point2.x, point2.y, point1.z].toFloat4()
56     val v001 = cube[point1.x, point1.y, point2.z].toFloat4()
57     val v101 = cube[point2.x, point1.y, point2.z].toFloat4()
58     val v011 = cube[point1.x, point2.y, point2.z].toFloat4()
59     val v111 = cube[point2.x, point2.y, point2.z].toFloat4()
60 
61     // Do the linear mixing of these eight values.
62     val yz00 = mix(v000, v100, fractionAwayFromPoint1.x)
63     val yz10 = mix(v010, v110, fractionAwayFromPoint1.x)
64     val yz01 = mix(v001, v101, fractionAwayFromPoint1.x)
65     val yz11 = mix(v011, v111, fractionAwayFromPoint1.x)
66 
67     val z0 = mix(yz00, yz10, fractionAwayFromPoint1.y)
68     val z1 = mix(yz01, yz11, fractionAwayFromPoint1.y)
69 
70     val v = mix(z0, z1, fractionAwayFromPoint1.z)
71 
72     // Preserve the alpha of the original value
73     return ubyteArrayOf(v.x.clampToUByte(), v.y.clampToUByte(), v.z.clampToUByte(), input[3])
74 }
75