1
+ package pl.pk.binarizer.rs
2
+
3
+ import android.renderscript.Allocation
4
+ import android.renderscript.Element
5
+ import android.renderscript.RenderScript
6
+ import android.util.Size
7
+ import pl.pk.binarizer.Processor
8
+
9
+ class IntegralBradleyBinarizationFS (private val rs : RenderScript , private val dimensions : Size ) : Processor {
10
+
11
+ private val kernel = ScriptC_IntegralBradleyBinarizationFS (rs)
12
+
13
+ override fun process (input : Allocation , output : Allocation ) {
14
+ val monochromeBytesSize = (input.bytesSize / 1.5 ).toInt()
15
+ val originalBytes = ByteArray (monochromeBytesSize)
16
+ val integral = IntArray (monochromeBytesSize)
17
+
18
+ input.copy1DRangeTo(0 , monochromeBytesSize, originalBytes)
19
+ calculateIntegral(originalBytes, integral)
20
+
21
+ val integralAllocation = Allocation .createSized(rs, Element .I32 (rs), monochromeBytesSize, Allocation .USAGE_SCRIPT )
22
+ integralAllocation.copy1DRangeFrom(0 , monochromeBytesSize, integral)
23
+
24
+ kernel._currentFrame = input
25
+ kernel._integral = integralAllocation
26
+ kernel._width = dimensions.width.toLong()
27
+ kernel._height = dimensions.height.toLong()
28
+ kernel.forEach_process(output)
29
+ rs.finish()
30
+ }
31
+
32
+ private fun calculateIntegral (originalBytes : ByteArray , integral : IntArray ) {
33
+ for (row in 0 until dimensions.height) {
34
+ var sum = 0
35
+ for (column in 0 until dimensions.width) {
36
+ sum + = originalBytes[column + dimensions.width * row].toInt() and 0xFF
37
+ if (row == 0 ) {
38
+ integral[column + dimensions.width * row] = sum
39
+ } else {
40
+ integral[column + dimensions.width * row] = integral[column + dimensions.width * (row - 1 )] + sum
41
+ }
42
+ }
43
+ }
44
+ }
45
+
46
+ }
0 commit comments