| 13 | import java.util.concurrent.TimeUnit |
| 14 | import java.util.stream.Collectors |
| 15 | |
| 16 | class SlitScanner(val source: String) { |
| 17 | |
| 18 | |
| 19 | var data: MutableList<ByteBuffer> |
| 20 | var dim: IntArray = intArrayOf(0, 0) |
| 21 | |
| 22 | init { |
| 23 | var files = Files.list(File(source).toPath()).filter { it.toString().endsWith(".jpg") && !it.fileName.startsWith(".") }.sorted().collect( |
| 24 | Collectors.toList()) |
| 25 | |
| 26 | while (files.size > 500) |
| 27 | files.toList().filterIndexed { i, b -> i % 2 == 0 }.toList() |
| 28 | data = files.map { |
| 29 | dim = FastJPEG.j.dimensions(it.toString()) |
| 30 | val dat = ByteBuffer.allocateDirect(3 * dim[0] * dim[1]).order(ByteOrder.nativeOrder()) |
| 31 | FastJPEG.j.decompress(it.toString(), dat, dim[0], dim[1]) |
| 32 | |
| 33 | dat |
| 34 | }.toMutableList() |
| 35 | } |
| 36 | |
| 37 | fun access(x: Int, y: Int, n: Int, out: FloatArray, offset: Int) { |
| 38 | val o = 3 * (y * dim[0] + x) |
| 39 | out[0 + offset] = (data[n].get(o + 0).toInt() and 255) / 255f |
| 40 | out[1 + offset] = (data[n].get(o + 1).toInt() and 255) / 255f |
| 41 | out[2 + offset] = (data[n].get(o + 2).toInt() and 255) / 255f |
| 42 | } |
| 43 | |
| 44 | fun buildImage(fn: String, w: Int, h: Int, func: (Float, Float, Float, Vec3) -> Unit) { |
| 45 | |
| 46 | val o = ByteBuffer.allocateDirect(3 * w * h).order(ByteOrder.nativeOrder()) |
| 47 | |
| 48 | val b = object : ThreadLocal<FloatArray>() { |
| 49 | override fun initialValue(): FloatArray { |
| 50 | return FloatArray(24) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | val pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 2) |
| 55 | |
| 56 | for (y in 0 until h) { |
| 57 | pool.submit { |
| 58 | print("$y / $h ") |
| 59 | val oo = b.get() |
| 60 | val a = Vec3() |
| 61 | try { |
| 62 | for (x in 0 until w) { |
| 63 | |
| 64 | func(x / w.toFloat(), y / h.toFloat(), 0.5f, a) |
| 65 | accessI(a.x.toFloat(), a.y.toFloat(), a.z.toFloat(), oo) |
| 66 | |
| 67 | o.put(3 * y * w + 3 * x + 0, (Math.max(0f, Math.min(1f, oo[0])) * 255).toInt().toByte()) |
| 68 | o.put(3 * y * w + 3 * x + 1, (Math.max(0f, Math.min(1f, oo[1])) * 255).toInt().toByte()) |
| 69 | o.put(3 * y * w + 3 * x + 2, (Math.max(0f, Math.min(1f, oo[2])) * 255).toInt().toByte()) |
| 70 | |
| 71 | } |
| 72 | } catch (e: Exception) { |
nothing calls this directly
no test coverage detected