Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the sample method. #759

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/main/kotlin/graphics/scenery/volumes/Colormap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,36 @@ class Colormap(val buffer: ByteBuffer, val width: Int, val height: Int) {
private constructor() : this(ByteBuffer.allocate(0), 0, 0)

/**
* Returns the value of the colormap, sampled at [position].
* Returns the value of the color map sampled at the normalized position.
*
* position: A floating point value between 0 and 1.
*/
@OptIn(ExperimentalUnsignedTypes::class)
fun sample(position: Float): Vector4f {
val bufferPosition: Float = position.coerceIn(0.0f, 1.0f) * width
val previous = floor(bufferPosition).roundToInt()
val next = ceil(bufferPosition).roundToInt()
val bufferPosition: Float = position.coerceIn(0.0f, 1.0f) * (width - 1)
val previous = bufferPosition.toInt()

val globalOffset = width * 4 * height / 2
val previousColor = globalOffset + previous * 4
val nextColor = globalOffset + next * 4
val band = height/2

val b = buffer.duplicate()
val color = ByteArray(8)
@Suppress("USELESS_CAST")
(b.position(previousColor) as? ByteBuffer)?.get(color, 0, 4)
@Suppress("USELESS_CAST")
(b.position(nextColor) as? ByteBuffer)?.get(color, 4, 4)
val ub = color.toUByteArray()
//The number of bytes per pixel are fixed at 4.
val globalOffset = width * band * 4

val c1 = Vector4f(ub[0].toFloat() / 255.0f, ub[1].toFloat() / 255.0f, ub[2].toFloat() / 255.0f, ub[3].toFloat() / 255.0f)
val c2 = Vector4f(ub[4].toFloat() / 255.0f, ub[5].toFloat() / 255.0f, ub[6].toFloat() / 255.0f, ub[7].toFloat() / 255.0f)
val b = buffer.duplicate()
b.position(globalOffset + previous * 4);
val color = ByteArray(4)
b.get(color);
//Add to "Image" utility class?
val c1 = Vector4f( color[0].toUByte().toFloat() / 255f, color[1].toUByte().toFloat() / 255f, color[2].toUByte().toFloat() / 255f, color[3].toUByte().toFloat() / 255f)

if( bufferPosition > previous ){
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
//interpolate fraction part.
b.get(color);
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
val c2 = Vector4f( color[0].toUByte().toFloat() / 255f, color[1].toUByte().toFloat() / 255f, color[2].toUByte().toFloat() / 255f, color[3].toUByte().toFloat() / 255f)
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
return c1.lerp(c2, bufferPosition - previous.toFloat())
} else{
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
return c1
}

return c1.lerp(c2, bufferPosition - previous.toFloat())
}

companion object {
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/graphics/scenery/volumes/ColormapPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ class ColormapPanel(val target:Volume?): JPanel() {

internal fun toImage(): BufferedImage {
val rec: Rectangle = this.bounds
val bufferedImage = BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB)
paintBackgroundGradient(colorPoints.sortedBy { it.position }, bufferedImage.graphics as Graphics2D)
val bufferedImage = BufferedImage(rec.width, rec.height - 10, BufferedImage.TYPE_INT_ARGB)
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
paintBackgroundGradient(colorPoints.sortedBy { it.position }, bufferedImage.createGraphics())
return bufferedImage
}

Expand All @@ -324,8 +324,11 @@ class ColormapPanel(val target:Volume?): JPanel() {
val h = height
val pointList = colorPoints.sortedBy { it.position }


// background Gradient
paintBackgroundGradient(pointList, g2d)
//paintBackgroundGradient(pointList, g2d)
skalarproduktraum marked this conversation as resolved.
Show resolved Hide resolved
val img = toImage()
g2d.drawImage(img, 0, 0, this)

// color point markers
val relativeSize = 0.25f //relative to height
Expand Down
Loading