Skip to content

Commit

Permalink
deinterleave functions fix
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory Pomadchin <gr.pomadchin@gmail.com>
  • Loading branch information
pomadchin committed Mar 2, 2017
1 parent 947131b commit 0d48955
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Expand Up @@ -190,7 +190,7 @@ abstract class GeoTiffMultibandTile(
val bytesPerSample = bandType.bytesPerSample

getSegments(0 until segmentCount).foreach { case (segmentIndex, geoTiffSegment) =>
val bytes = GeoTiffSegment.deinterleave(geoTiffSegment.bytes, bandCount, bytesPerSample)(bandIndex)
val bytes = GeoTiffSegment.deinterleave(geoTiffSegment.bytes, bandCount, bytesPerSample, bandIndex)
compressedBandBytes(segmentIndex) = compressor.compress(bytes, segmentIndex)
}

Expand All @@ -199,10 +199,10 @@ abstract class GeoTiffMultibandTile(
} else {
val bandSegmentCount = segmentCount / bandCount
val compressedBandBytes = Array.ofDim[Array[Byte]](bandSegmentCount)
val start = bandSegmentCount * bandIndex
val segmentOffset = bandSegmentCount * bandIndex

segmentBytes.getSegments(start until bandSegmentCount + start).foreach { case (segmentIndex, segment) =>
compressedBandBytes(segmentIndex - start) = segment.clone
segmentBytes.getSegments(segmentOffset until bandSegmentCount + segmentOffset).foreach { case (segmentIndex, segment) =>
compressedBandBytes(segmentIndex - segmentOffset) = segment.clone
}

GeoTiffTile(new ArraySegmentBytes(compressedBandBytes), decompressor, segmentLayout, compression, cellType, Some(bandType))
Expand Down
Expand Up @@ -176,9 +176,14 @@ object GeoTiffSegment {
bands
}

def deinterleave(bytes: Array[Byte], bandCount: Int, bytesPerSample: Int, index: Int): Array[Byte] =
deinterleave(bytes, bandCount, bytesPerSample, index :: Nil).head

def deinterleave(bytes: Array[Byte], bandCount: Int, bytesPerSample: Int, indices: Traversable[Int]): Array[Array[Byte]] = {
val indexToPosition = indices.toList.zipWithIndex.toMap
val actualBandCount = indices.size
val indicesList = indices.toList
val bandToIndex = indicesList.zipWithIndex.toMap
val actualBandCount = indicesList.length

val bands: Array[Array[Byte]] = new Array[Array[Byte]](actualBandCount)
val segmentSize = bytes.length / bandCount
cfor(0)(_ < actualBandCount, _ + 1) { i =>
Expand All @@ -188,9 +193,8 @@ object GeoTiffSegment {
val bb = ByteBuffer.wrap(bytes)
cfor(0)(_ < segmentSize, _ + bytesPerSample) { offset =>
cfor(0)(_ < bandCount, _ + 1) { band =>
if(indices.exists(_ == band)) {
bb.get(bands(band), offset, bytesPerSample)
}
if(indicesList.contains(band)) bb.get(bands(bandToIndex(band)), offset, bytesPerSample)
else bb.position(bb.position() + bytesPerSample)
}
}

Expand Down Expand Up @@ -236,28 +240,29 @@ object GeoTiffSegment {
}

def deinterleaveBitSegment(segment: GeoTiffSegment, cols: Int, rows: Int, bandCount: Int, index: Int): Array[Byte] =
deinterleaveBitSegment(segment, cols, rows, bandCount, index :: Nil)(0)
deinterleaveBitSegment(segment, cols, rows, bandCount, index :: Nil).head

def deinterleaveBitSegment(segment: GeoTiffSegment, cols: Int, rows: Int, bandCount: Int, indices: Traversable[Int]): Array[Array[Byte]] = {
val paddedCols = {
val bytesWidth = (cols + 7) / 8
bytesWidth * 8
}
val resultByteCount = (paddedCols / 8) * rows
val indexToPosition = indices.toList.zipWithIndex.toMap
val actualBandCount = indices.size
val indicesList = indices.toList
val bandToIndex = indicesList.zipWithIndex.toMap
val actualBandCount = indicesList.length

// packed byte arrays for each band in this segment
val bands = Array.fill[Array[Byte]](actualBandCount)(Array.ofDim[Byte](resultByteCount))

cfor(0)(_ < segment.size, _ + 1) { i =>
val bandIndex = i % bandCount
if(indices.exists(_ == bandIndex)) {
if(indicesList.contains(bandIndex)) {
val j = i / bandCount
val col = j % cols
val row = j / cols
val i2 = (row * paddedCols) + col
BitArrayTile.update(bands(indexToPosition(bandIndex)), i2, segment.getInt(i))
BitArrayTile.update(bands(bandToIndex(bandIndex)), i2, segment.getInt(i))
}
}

Expand Down

0 comments on commit 0d48955

Please sign in to comment.