• 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.RenderScript
22 import android.renderscript.Script
23 import android.renderscript.ScriptIntrinsicResize
24 import android.renderscript.Type
25 import android.renderscript.toolkit.Range2d
26 
27 /**
28  * Does a Resize operation using the RenderScript Intrinsics.
29  */
intrinsicResizenull30 fun intrinsicResize(
31     context: RenderScript,
32     inputArray: ByteArray,
33     vectorSize: Int,
34     inSizeX: Int,
35     inSizeY: Int,
36     outSizeX: Int,
37     outSizeY: Int,
38     restriction: Range2d?
39 ): ByteArray {
40     val scriptResize = ScriptIntrinsicResize.create(context)
41     val builder = Type.Builder(
42         context,
43         renderScriptVectorElementForU8(context, vectorSize)
44     )
45     builder.setX(inSizeX)
46     builder.setY(inSizeY)
47     val inputArrayType = builder.create()
48     val inputAllocation = Allocation.createTyped(context, inputArrayType)
49     builder.setX(outSizeX)
50     builder.setY(outSizeY)
51     val outputArrayType = builder.create()
52     val outAllocation = Allocation.createTyped(context, outputArrayType)
53     val intrinsicOutArray = ByteArray(outSizeX * outSizeY * paddedSize(vectorSize))
54 
55     inputAllocation.copyFrom(inputArray)
56     scriptResize.setInput(inputAllocation)
57     if (restriction != null) {
58         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
59         val options = Script.LaunchOptions()
60         options.setX(restriction.startX, restriction.endX)
61         options.setY(restriction.startY, restriction.endY)
62         scriptResize.forEach_bicubic(outAllocation, options)
63     } else {
64         scriptResize.forEach_bicubic(outAllocation)
65     }
66     outAllocation.copyTo(intrinsicOutArray)
67 
68     inputAllocation.destroy()
69     outAllocation.destroy()
70     scriptResize.destroy()
71     inputArrayType.destroy()
72     outputArrayType.destroy()
73     return intrinsicOutArray
74 }
75 
intrinsicResizenull76 fun intrinsicResize(
77     context: RenderScript,
78     bitmap: Bitmap,
79     outSizeX: Int,
80     outSizeY: Int,
81     restriction: Range2d?
82 ): ByteArray {
83     val scriptResize = ScriptIntrinsicResize.create(context)
84     val inputAllocation = Allocation.createFromBitmap(context, bitmap)
85     inputAllocation.copyFrom(bitmap)
86 
87     val vectorSize = when (bitmap.config) {
88         Bitmap.Config.ARGB_8888 -> 4
89         Bitmap.Config.ALPHA_8 -> 1
90         else -> error("Unrecognized bitmap config $bitmap.config")
91     }
92     val builder = Type.Builder(
93         context,
94         renderScriptVectorElementForU8(context, vectorSize)
95     )
96     builder.setX(outSizeX)
97     builder.setY(outSizeY)
98     val outputArrayType = builder.create()
99     val outAllocation = Allocation.createTyped(context, outputArrayType)
100     val intrinsicOutArray = ByteArray(outSizeX * outSizeY * vectorSize)
101 
102     scriptResize.setInput(inputAllocation)
103     if (restriction != null) {
104         outAllocation.copyFrom(intrinsicOutArray) // To initialize to zero
105         val options = Script.LaunchOptions()
106         options.setX(restriction.startX, restriction.endX)
107         options.setY(restriction.startY, restriction.endY)
108         scriptResize.forEach_bicubic(outAllocation, options)
109     } else {
110         scriptResize.forEach_bicubic(outAllocation)
111     }
112     outAllocation.copyTo(intrinsicOutArray)
113 
114     inputAllocation.destroy()
115     outAllocation.destroy()
116     outputArrayType.destroy()
117     scriptResize.destroy()
118     return intrinsicOutArray
119 }
120