From 1554ed0ef5a332084c9d5e97498180542e65f6a0 Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Sat, 17 May 2025 20:22:42 +0300 Subject: [PATCH] refactor + add colors --- lab_2/src/main/kotlin/Form1.kt | 7 ++--- lab_2/src/main/kotlin/Main.kt | 6 ++-- lab_2/src/main/kotlin/TransferHelper.kt | 39 ++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lab_2/src/main/kotlin/Form1.kt b/lab_2/src/main/kotlin/Form1.kt index 5bd6911..fa0dce6 100644 --- a/lab_2/src/main/kotlin/Form1.kt +++ b/lab_2/src/main/kotlin/Form1.kt @@ -11,13 +11,10 @@ class Form1 { private val bin = Bin val view = View() - private var minTF = -3000 - private var widthTF = 2000 - private var frameCount = 0 private var nextFPSUpdate = System.currentTimeMillis() + 1000 - fun displayFPS() { + private fun displayFPS() { if (System.currentTimeMillis() >= nextFPSUpdate) { println("CT Visualizer (fps = $frameCount)") nextFPSUpdate = System.currentTimeMillis() + 1000 @@ -26,7 +23,7 @@ class Form1 { frameCount++ } - fun openFileDialog() { + private fun openFileDialog() { val fileChooser = JFileChooser() fileChooser.fileFilter = FileNameExtensionFilter("BIN Files", "bin") val result = fileChooser.showOpenDialog(null) diff --git a/lab_2/src/main/kotlin/Main.kt b/lab_2/src/main/kotlin/Main.kt index 9d027c5..d0b07de 100644 --- a/lab_2/src/main/kotlin/Main.kt +++ b/lab_2/src/main/kotlin/Main.kt @@ -7,18 +7,16 @@ import org.lwjgl.system.MemoryUtil.NULL import kotlin.system.exitProcess fun main() { - if (!glfwInit()) { + if (!glfwInit()) throw IllegalStateException("Unable to initialize GLFW") - } glfwDefaultWindowHints() glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE) val window = glfwCreateWindow(800, 600, "CT Visualizer", NULL, NULL) - if (window == NULL) { + if (window == NULL) throw RuntimeException("Failed to create the GLFW window") - } glfwSetFramebufferSizeCallback(window) { _: Long, width: Int, height: Int -> glViewport(0, 0, width, height) diff --git a/lab_2/src/main/kotlin/TransferHelper.kt b/lab_2/src/main/kotlin/TransferHelper.kt index 46d872e..e474d2e 100644 --- a/lab_2/src/main/kotlin/TransferHelper.kt +++ b/lab_2/src/main/kotlin/TransferHelper.kt @@ -3,8 +3,8 @@ package org.exampl import java.awt.Color object TransferHelper { - private var min = -3000 - private var max = 16000 + private var min = -3100 + private var max = 5000 fun setTF(newMin: Int, newMax: Int) { min = newMin @@ -12,7 +12,38 @@ object TransferHelper { } fun transferFunction(value: Short): Color { - val newVal = Math.max(0, Math.min((value - min) * 255 / (max - min), 255)) - return Color(newVal, newVal, newVal) + val k = value.toFloat().map(min.toFloat()..max.toFloat(), 0f..1f) + + /* + 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, toRange: ClosedRange): 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 } } \ No newline at end of file