refactor + add colors

This commit is contained in:
Sweetbread 2025-05-17 20:22:42 +03:00
parent 7c27a20994
commit 1554ed0ef5
3 changed files with 39 additions and 13 deletions

View File

@ -11,13 +11,10 @@ class Form1 {
private val bin = Bin private val bin = Bin
val view = View() val view = View()
private var minTF = -3000
private var widthTF = 2000
private var frameCount = 0 private var frameCount = 0
private var nextFPSUpdate = System.currentTimeMillis() + 1000 private var nextFPSUpdate = System.currentTimeMillis() + 1000
fun displayFPS() { private fun displayFPS() {
if (System.currentTimeMillis() >= nextFPSUpdate) { if (System.currentTimeMillis() >= nextFPSUpdate) {
println("CT Visualizer (fps = $frameCount)") println("CT Visualizer (fps = $frameCount)")
nextFPSUpdate = System.currentTimeMillis() + 1000 nextFPSUpdate = System.currentTimeMillis() + 1000
@ -26,7 +23,7 @@ class Form1 {
frameCount++ frameCount++
} }
fun openFileDialog() { private fun openFileDialog() {
val fileChooser = JFileChooser() val fileChooser = JFileChooser()
fileChooser.fileFilter = FileNameExtensionFilter("BIN Files", "bin") fileChooser.fileFilter = FileNameExtensionFilter("BIN Files", "bin")
val result = fileChooser.showOpenDialog(null) val result = fileChooser.showOpenDialog(null)

View File

@ -7,18 +7,16 @@ import org.lwjgl.system.MemoryUtil.NULL
import kotlin.system.exitProcess import kotlin.system.exitProcess
fun main() { fun main() {
if (!glfwInit()) { if (!glfwInit())
throw IllegalStateException("Unable to initialize GLFW") throw IllegalStateException("Unable to initialize GLFW")
}
glfwDefaultWindowHints() glfwDefaultWindowHints()
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE) glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
val window = glfwCreateWindow(800, 600, "CT Visualizer", NULL, NULL) val window = glfwCreateWindow(800, 600, "CT Visualizer", NULL, NULL)
if (window == NULL) { if (window == NULL)
throw RuntimeException("Failed to create the GLFW window") throw RuntimeException("Failed to create the GLFW window")
}
glfwSetFramebufferSizeCallback(window) { _: Long, width: Int, height: Int -> glfwSetFramebufferSizeCallback(window) { _: Long, width: Int, height: Int ->
glViewport(0, 0, width, height) glViewport(0, 0, width, height)

View File

@ -3,8 +3,8 @@ package org.exampl
import java.awt.Color import java.awt.Color
object TransferHelper { object TransferHelper {
private var min = -3000 private var min = -3100
private var max = 16000 private var max = 5000
fun setTF(newMin: Int, newMax: Int) { fun setTF(newMin: Int, newMax: Int) {
min = newMin min = newMin
@ -12,7 +12,38 @@ object TransferHelper {
} }
fun transferFunction(value: Short): Color { fun transferFunction(value: Short): Color {
val newVal = Math.max(0, Math.min((value - min) * 255 / (max - min), 255)) val k = value.toFloat().map(min.toFloat()..max.toFloat(), 0f..1f)
return Color(newVal, newVal, newVal)
/*
0 -> black (0, 0, 0)
.25 -> blue (0, 0, f)
.5 -> green (0, f, 0)
.75 -> yellow (f, f, 0)
1 -> white (f, f, f)
*/
return when (k) {
in 0f..0.25f -> Color(0, 0, (k * 4 * 255).toInt())
in 0.25f..0.5f -> {
val g = k.map(0.25f..0.5f, 0f..255f).toInt()
Color(0, g, 255 - g)
}
in 0.5f..0.75f -> {
val r = k.map(0.5f..0.75f, 0f..255f).toInt()
Color(r, 255, 0)
}
in 0.75f..1f -> {
val b = k.map(0.75f..1f, 0f..255f).toInt()
Color(255, 255, b)
}
else -> throw IllegalArgumentException("$value not in range [$min..$max]")
}
}
fun Float.map(fromRange: ClosedRange<Float>, toRange: ClosedRange<Float>): Float {
if (fromRange.start == fromRange.endInclusive)
return (toRange.start + toRange.endInclusive) / 2
return (this - fromRange.start) * (toRange.endInclusive - toRange.start) /
(fromRange.endInclusive - fromRange.start) + toRange.start
} }
} }