Skip to content

Commit

Permalink
[api]: Take account of DATAMIN and DATAMAX FITS keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Apr 5, 2024
1 parent 8fb505f commit 8437bec
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion nebulosa-fits/src/main/kotlin/nebulosa/fits/FitsFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ data object FitsFormat : ImageFormat {
val numberOfChannels = header.numberOfChannels
val bitpix = header.bitpix
val position = source.position
val rangeMin = header.getFloat(FitsKeyword.DATAMIN, 0f)
val rangeMax = header.getFloat(FitsKeyword.DATAMAX, 1f)
val range = rangeMin..rangeMax

val data = SeekableSourceImageData(source, position, width, height, numberOfChannels, bitpix)
val data = SeekableSourceImageData(source, position, width, height, numberOfChannels, bitpix, range)
val skipBytes = computeRemainingBytesToSkip(data.totalSizeInBytes)
if (skipBytes > 0L) source.seek(position + data.totalSizeInBytes + skipBytes)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nebulosa.fits
import nebulosa.fits.FitsFormat.readPixel
import nebulosa.image.format.ImageChannel
import nebulosa.image.format.ImageData
import nebulosa.image.format.ImageData.Companion.representableRange
import nebulosa.io.SeekableSource
import okio.Buffer
import okio.Sink
Expand All @@ -16,8 +17,12 @@ internal data class SeekableSourceImageData(
override val height: Int,
override val numberOfChannels: Int,
private val bitpix: Bitpix,
private val range: ClosedFloatingPointRange<Float>,
) : ImageData {

private val rangeDelta = range.endInclusive - range.start
private val rescale = range.start != 0f || range.endInclusive != 1f

@JvmField internal val channelSizeInBytes = (numberOfPixels * bitpix.byteLength).toLong()
@JvmField internal val totalSizeInBytes = channelSizeInBytes * numberOfChannels

Expand Down Expand Up @@ -84,7 +89,9 @@ internal data class SeekableSourceImageData(
n = (size / bitpix.byteLength).toInt()

repeat(n) {
output[pos++] = buffer.readPixel(bitpix)
val pixel = buffer.readPixel(bitpix)
output[pos++] = if (rescale) pixel.representableRange(range.start, range.endInclusive, rangeDelta)
else pixel
}

remainingPixels -= n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ interface ImageData {
val blue: FloatArray

fun readChannelTo(channel: ImageChannel, output: FloatArray)

companion object {

@Suppress("NOTHING_TO_INLINE")
inline fun Float.representableRange(rangeMin: Float, rangeMax: Float, rangeDelta: Float = rangeMax - rangeMin): Float {
return if (this < rangeMin) 0f
else if (this > rangeMax) 1f
else (this - rangeMin) / rangeDelta
}
}
}

0 comments on commit 8437bec

Please sign in to comment.