Skip to content

Commit

Permalink
Fixed the sample method. It used to go out of bounds and didn't work
Browse files Browse the repository at this point in the history
with odd h values. Issue #750

Made a hot fix for generating an image form the ColormapEditor the image
was only halfdrawn. Issue #757
  • Loading branch information
Matt authored and skalarproduktraum committed Aug 7, 2024
1 parent d61772d commit 01bd87b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
48 changes: 27 additions & 21 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 globalOffset = width * 4 * height / 2
val previousColor = globalOffset + previous * 4
val nextColor = globalOffset + next * 4

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()

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)

return c1.lerp(c2, bufferPosition - previous.toFloat())
val bufferPosition: Float = position.coerceIn(0.0f, 1.0f) * (width - 1)
val previous = bufferPosition.toInt()

val band = height/2

//The number of bytes per pixel are fixed at 4.
val globalOffset = width * band * 4

val b = buffer.slice()
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 ){
//interpolate fraction part.
b.get(color);
val c2 = Vector4f( color[0].toUByte().toFloat() / 255f, color[1].toUByte().toFloat() / 255f, color[2].toUByte().toFloat() / 255f, color[3].toUByte().toFloat() / 255f)
return c1.lerp(c2, bufferPosition - previous.toFloat())
} else{
return c1
}

}

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)
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)
val img = toImage()
g2d.drawImage(img, 0, 0, this)

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

0 comments on commit 01bd87b

Please sign in to comment.