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 kotlin.math.floor
21 import kotlin.math.max
22
23 var trace = false
24
25 /**
26 * Reference implementation of a Resize operation.
27 */
28 @ExperimentalUnsignedTypes
29 fun referenceResize(inputArray: ByteArray,
30 vectorSize: Int,
31 inSizeX: Int,
32 inSizeY: Int,
33 outSizeX: Int, outSizeY: Int,
34 restriction: Range2d?): ByteArray {
35 val input = Vector2dArray(inputArray.asUByteArray(), vectorSize, inSizeX, inSizeY)
36 val scaleX: Float = input.sizeX.toFloat() / outSizeX.toFloat()
37 val scaleY: Float = input.sizeY.toFloat() / outSizeY.toFloat()
38 val outArray = UByteArray(outSizeX * outSizeY * paddedSize(input.vectorSize))
39 val out = Vector2dArray(outArray, input.vectorSize, outSizeX, outSizeY)
40 out.forEach (restriction) { x, y ->
41 if (x == 1827 && y == 46) {
42 println("Found it")
43 trace = true
44 }
45 val o = bicubicU4(x, y, input, scaleX, scaleY)
46 out[x, y] = o.clampToUByte()
47 }
48 return out.values.asByteArray()
49 }
50
cubicInterpolateFnull51 private fun cubicInterpolateF(p0: FloatArray, p1: FloatArray, p2: FloatArray, p3: FloatArray,
52 x: Float): FloatArray {
53 return p1 + (p2 - p0 + (p0 * 2f - p1 * 5f + p2 * 4f - p3
54 + ((p1 - p2) * 3f + p3 - p0) * x) * x) * x * 0.5f
55 }
56
57 @ExperimentalUnsignedTypes
bicubicU4null58 private fun bicubicU4(x: Int, y: Int, gIn: Vector2dArray, scaleX: Float, scaleY: Float): FloatArray {
59 var xf: Float = (x + 0.5f) * scaleX - 0.5f
60 var yf: Float = (y + 0.5f) * scaleY - 0.5f
61
62 val startX: Int = floor(xf - 1).toInt()
63 val startY: Int = floor(yf - 1).toInt()
64 xf -= floor(xf)
65 yf -= floor(yf)
66 val maxX: Int = gIn.sizeX - 1
67 val maxY: Int = gIn.sizeY - 1
68
69 val xs0: Int = max(0, startX + 0)
70 val xs1: Int = max(0, startX + 1)
71 val xs2: Int = kotlin.math.min(maxX, startX + 2)
72 val xs3: Int = kotlin.math.min(maxX, startX + 3)
73
74 val ys0: Int = max(0, startY + 0)
75 val ys1: Int = max(0, startY + 1)
76 val ys2: Int = kotlin.math.min(maxY, startY + 2)
77 val ys3: Int = kotlin.math.min(maxY, startY + 3)
78
79 val p00 = gIn[xs0, ys0].toFloatArray()
80 val p01 = gIn[xs1, ys0].toFloatArray()
81 val p02 = gIn[xs2, ys0].toFloatArray()
82 val p03 = gIn[xs3, ys0].toFloatArray()
83 val p0 = cubicInterpolateF(p00, p01, p02, p03, xf)
84
85 val p10 = gIn[xs0, ys1].toFloatArray()
86 val p11 = gIn[xs1, ys1].toFloatArray()
87 val p12 = gIn[xs2, ys1].toFloatArray()
88 val p13 = gIn[xs3, ys1].toFloatArray()
89 val p1 = cubicInterpolateF(p10, p11, p12, p13, xf)
90
91 val p20 = gIn[xs0, ys2].toFloatArray()
92 val p21 = gIn[xs1, ys2].toFloatArray()
93 val p22 = gIn[xs2, ys2].toFloatArray()
94 val p23 = gIn[xs3, ys2].toFloatArray()
95 val p2 = cubicInterpolateF(p20, p21, p22, p23, xf)
96
97 val p30 = gIn[xs0, ys3].toFloatArray()
98 val p31 = gIn[xs1, ys3].toFloatArray()
99 val p32 = gIn[xs2, ys3].toFloatArray()
100 val p33 = gIn[xs3, ys3].toFloatArray()
101 val p3 = cubicInterpolateF(p30, p31, p32, p33, xf)
102
103 return cubicInterpolateF(p0, p1, p2, p3, yf)
104 }
105
106
107 /* To be used if we implement Floats
bicubic_F4null108 private fun bicubic_F4(x: Int, y: Int, gin: ByteArray, sizeX: Int, sizeY: Int, scaleX: Float, scaleY: Float): Float4 {
109 var xf: Float = (x + 0.5f) * scaleX - 0.5f
110 var yf: Float = (y + 0.5f) * scaleY - 0.5f
111
112 val startX: Int = floor(xf - 1).toInt()
113 val startY: Int = floor(yf - 1).toInt()
114 xf = xf - floor(xf)
115 yf = yf - floor(yf)
116 val maxX: Int = sizeX - 1
117 val maxY: Int = sizeY - 1
118
119 val xs0: Int = max(0, startX + 0)
120 val xs1: Int = max(0, startX + 1)
121 val xs2: Int = min(maxX, startX + 2)
122 val xs3: Int = min(maxX, startX + 3)
123
124 val ys0: Int = max(0, startY + 0)
125 val ys1: Int = max(0, startY + 1)
126 val ys2: Int = min(maxY, startY + 2)
127 val ys3: Int = min(maxY, startY + 3)
128
129 val p00: Float4 = rsGetElementAt_Float4(gIn, xs0, ys0)
130 val p01: Float4 = rsGetElementAt_Float4(gIn, xs1, ys0)
131 val p02: Float4 = rsGetElementAt_Float4(gIn, xs2, ys0)
132 val p03: Float4 = rsGetElementAt_Float4(gIn, xs3, ys0)
133 val p0: Float4 = cubicInterpolate_F4(p00, p01, p02, p03, xf)
134
135 val p10: Float4 = rsGetElementAt_Float4(gIn, xs0, ys1)
136 val p11: Float4 = rsGetElementAt_Float4(gIn, xs1, ys1)
137 val p12: Float4 = rsGetElementAt_Float4(gIn, xs2, ys1)
138 val p13: Float4 = rsGetElementAt_Float4(gIn, xs3, ys1)
139 val p1: Float4 = cubicInterpolate_F4(p10, p11, p12, p13, xf)
140
141 val p20: Float4 = rsGetElementAt_Float4(gIn, xs0, ys2)
142 val p21: Float4 = rsGetElementAt_Float4(gIn, xs1, ys2)
143 val p22: Float4 = rsGetElementAt_Float4(gIn, xs2, ys2)
144 val p23: Float4 = rsGetElementAt_Float4(gIn, xs3, ys2)
145 val p2: Float4 = cubicInterpolate_F4(p20, p21, p22, p23, xf)
146
147 val p30: Float4 = rsGetElementAt_Float4(gIn, xs0, ys3)
148 val p31: Float4 = rsGetElementAt_Float4(gIn, xs1, ys3)
149 val p32: Float4 = rsGetElementAt_Float4(gIn, xs2, ys3)
150 val p33: Float4 = rsGetElementAt_Float4(gIn, xs3, ys3)
151 val p3: Float4 = cubicInterpolate_F4(p30, p31, p32, p33, xf)
152
153 val p: Float4 = cubicInterpolate_F4(p0, p1, p2, p3, yf)
154
155 return p
156 }
157 */
158