Skip to content

Commit

Permalink
Fix PNG elevation decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
ComBatVision committed Jun 23, 2024
1 parent b5ee69c commit 2138994
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ open class ElevationDecoder: Closeable {

fun decodePng(
bytes: ByteArray, tileScale: Float = 1.0f, tileOffset: Float = 0.0f,
coverageScale: Float = 1.0f, coverageOffset: Float = 0.0f, dataNull: Short? = null
coverageScale: Float = 1.0f, coverageOffset: Float = 0.0f, dataNull: Float? = null
): Buffer {
val png = PngReaderInt(ByteArrayInputStream(bytes))
require(png.imgInfo.channels == 1 && png.imgInfo.bitDepth == 16) {
Expand All @@ -114,15 +114,15 @@ open class ElevationDecoder: Closeable {
}

// Check if scale and offset should be applied to elevation data
return if (tileOffset != 1.0f || tileScale != 0.0f || coverageScale != 1.0f || coverageOffset != 0.0f) {
return if (tileScale != 1.0f || tileOffset != 0.0f || coverageScale != 1.0f || coverageOffset != 0.0f) {
// Apply scale and offset to INT16 values (except null data value) and return them as FLOAT32
val pixels = FloatArray(png.imgInfo.cols * png.imgInfo.rows)
var row = 0
while (png.hasMoreRows()) {
png.readRowInt().scanline.forEachIndexed { i, pixel ->
val h = pixel.toShort()
pixels[row * png.imgInfo.cols + i] = if (h == dataNull) h.toFloat()
else (h * tileScale + tileOffset) * coverageScale + coverageOffset
val rawHeight = pixel.toFloat()
pixels[row * png.imgInfo.cols + i] = if (rawHeight == dataNull) Float.MAX_VALUE
else (rawHeight * tileScale + tileOffset) * coverageScale + coverageOffset
}
row++
}
Expand All @@ -133,9 +133,10 @@ open class ElevationDecoder: Closeable {
// Use INT16 value as is
val pixels = ShortArray(png.imgInfo.cols * png.imgInfo.rows)
var row = 0
val dataNullInt = dataNull?.roundToInt()
while (png.hasMoreRows()) {
png.readRowInt().scanline.forEachIndexed { i, pixel ->
pixels[row * png.imgInfo.cols + i] = pixel.toShort()
pixels[row * png.imgInfo.cols + i] = if (pixel == dataNullInt) Short.MIN_VALUE else pixel.toShort()
}
row++
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ open class GpkgElevationDataFactory(
return if (isFloat) elevationDecoder.decodeTiff(tileUserData.tileData)
else elevationDecoder.decodePng(
tileUserData.tileData, griddedTile.scale, griddedTile.offset,
griddedCoverage.scale, griddedCoverage.offset, griddedCoverage.dataNull?.roundToInt()?.toShort()
griddedCoverage.scale, griddedCoverage.offset, griddedCoverage.dataNull
)
}

Expand Down

0 comments on commit 2138994

Please sign in to comment.