| 7 | import java.util.* |
| 8 | import kotlin.experimental.and |
| 9 | |
| 10 | class Red : Analysis.Fast { |
| 11 | |
| 12 | val j = FastJPEG() |
| 13 | override fun perform(data: List<File>): List<List<Float>> { |
| 14 | var b: ByteBuffer? = null |
| 15 | |
| 16 | return data.map { |
| 17 | |
| 18 | val dim = j.dimensions(it.absolutePath) |
| 19 | |
| 20 | if (b == null || b!!.capacity() != 3 * dim[0] * dim[1]) { |
| 21 | b = ByteBuffer.allocateDirect(3 * dim[0] * dim[1]) |
| 22 | } |
| 23 | j.decompress(it.absolutePath, b, dim[0], dim[1]) |
| 24 | |
| 25 | val f = process(b!!) |
| 26 | Collections.singletonList(f) |
| 27 | }.toList() |
| 28 | } |
| 29 | |
| 30 | fun process(b: ByteBuffer): Float { |
| 31 | |
| 32 | var tot = 0 |
| 33 | |
| 34 | for (x in 0 until b.capacity() / 3) { |
| 35 | val r = b.get(x * 3 + 0).toInt() and 0xff |
| 36 | val g = b.get(x * 3 + 1).toInt() and 0xff |
| 37 | val bl = b.get(x * 3 + 2).toInt() and 0xff |
| 38 | |
| 39 | tot += r |
| 40 | } |
| 41 | return tot / (255f * b.capacity()) |
| 42 | |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | |