• 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.Matrix4f
22 import android.renderscript.RenderScript
23 import android.renderscript.Script
24 import android.renderscript.ScriptIntrinsicColorMatrix
25 import android.renderscript.Type
26 import android.renderscript.Float4
27 import android.renderscript.toolkit.Range2d
28 
29 /**
30  * Does a ColorMatrix operation using the RenderScript Intrinsics.
31  */
intrinsicColorMatrixnull32 fun intrinsicColorMatrix(
33     context: RenderScript,
34     conversion: Tester.ColorMatrixConversionType,
35     inputArray: ByteArray,
36     inputVectorSize: Int,
37     sizeX: Int,
38     sizeY: Int,
39     outputVectorSize: Int,
40     matrix: FloatArray,
41     addVector: FloatArray,
42     restriction: Range2d?
43 ): ByteArray {
44     val scriptColorMatrix = ScriptIntrinsicColorMatrix.create(context)
45     val inputBuilder = Type.Builder(
46         context, renderScriptVectorElementForU8(
47             context,
48             inputVectorSize
49         )
50     )
51     inputBuilder.setX(sizeX)
52     inputBuilder.setY(sizeY)
53     val inputArrayType = inputBuilder.create()
54     val inputAllocation = Allocation.createTyped(context, inputArrayType)
55     val outputBuilder = Type.Builder(
56         context, renderScriptVectorElementForU8(
57             context,
58             outputVectorSize
59         )
60     )
61     outputBuilder.setX(sizeX)
62     outputBuilder.setY(sizeY)
63     val outputArrayType = outputBuilder.create()
64     val outAllocation = Allocation.createTyped(context, outputArrayType)
65 
66     inputAllocation.copyFrom(inputArray)
67     val intrinsicOutArray = ByteArray(sizeX * sizeY * paddedSize(outputVectorSize))
68     when (conversion) {
69         Tester.ColorMatrixConversionType.RGB_TO_YUV -> scriptColorMatrix.setRGBtoYUV()
70         Tester.ColorMatrixConversionType.YUV_TO_RGB -> scriptColorMatrix.setYUVtoRGB()
71         Tester.ColorMatrixConversionType.GREYSCALE -> scriptColorMatrix.setGreyscale()
72         Tester.ColorMatrixConversionType.RANDOM -> {
73             val m = Matrix4f()
74             var index = 0
75             // RS is column major
76             for (x in 0..3) {
77                 for (y in 0..3) {
78                     m.set(x, y, matrix[index++])
79                 }
80             }
81             scriptColorMatrix.setColorMatrix(m)
82         }
83     }
84     val vector = Float4(
85         addVector[0],
86         addVector[1],
87         addVector[2],
88         addVector[3]
89     )
90     scriptColorMatrix.setAdd(vector)
91     if (restriction != null) {
92         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
93         val options = Script.LaunchOptions()
94         options.setX(restriction.startX, restriction.endX)
95         options.setY(restriction.startY, restriction.endY)
96         scriptColorMatrix.forEach(inputAllocation, outAllocation, options)
97     } else {
98         scriptColorMatrix.forEach(inputAllocation, outAllocation)
99     }
100     outAllocation.copyTo(intrinsicOutArray)
101 
102     inputAllocation.destroy()
103     outAllocation.destroy()
104     inputArrayType.destroy()
105     outputArrayType.destroy()
106     scriptColorMatrix.destroy()
107     return intrinsicOutArray
108 }
109 
intrinsicColorMatrixnull110 fun intrinsicColorMatrix(
111     context: RenderScript,
112     conversion: Tester.ColorMatrixConversionType,
113     bitmap: Bitmap,
114     matrix: FloatArray,
115     addVector: FloatArray,
116     restriction: Range2d?
117 ): ByteArray {
118     val scriptColorMatrix = ScriptIntrinsicColorMatrix.create(context)
119     val inputAllocation = Allocation.createFromBitmap(context, bitmap)
120     inputAllocation.copyFrom(bitmap)
121     val outAllocation = Allocation.createTyped(context, inputAllocation.type)
122     val intrinsicOutArray = ByteArray(bitmap.byteCount)
123 
124     when (conversion) {
125         Tester.ColorMatrixConversionType.RGB_TO_YUV -> scriptColorMatrix.setRGBtoYUV()
126         Tester.ColorMatrixConversionType.YUV_TO_RGB -> scriptColorMatrix.setYUVtoRGB()
127         Tester.ColorMatrixConversionType.GREYSCALE -> scriptColorMatrix.setGreyscale()
128         Tester.ColorMatrixConversionType.RANDOM -> {
129             val m = Matrix4f()
130             var index = 0
131             // RS is column major
132             for (x in 0..3) {
133                 for (y in 0..3) {
134                     m.set(x, y, matrix[index++])
135                 }
136             }
137             scriptColorMatrix.setColorMatrix(m)
138         }
139     }
140     val vector = Float4(
141         addVector[0],
142         addVector[1],
143         addVector[2],
144         addVector[3]
145     )
146     scriptColorMatrix.setAdd(vector)
147     if (restriction != null) {
148         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
149         val options = Script.LaunchOptions()
150         options.setX(restriction.startX, restriction.endX)
151         options.setY(restriction.startY, restriction.endY)
152         scriptColorMatrix.forEach(inputAllocation, outAllocation, options)
153     } else {
154         scriptColorMatrix.forEach(inputAllocation, outAllocation)
155     }
156     outAllocation.copyTo(intrinsicOutArray)
157 
158     inputAllocation.destroy()
159     outAllocation.destroy()
160     scriptColorMatrix.destroy()
161     return intrinsicOutArray
162 }
163