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

New improved Buffer implementation and remove TypedArrays #1881

Merged
merged 8 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions korge-core/src/commonMain/kotlin/korlibs/ffi/FFILib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ fun Buffer.setFFIPointer(offset: Int, value: FFIPointer?) {
}

fun Buffer.getUnalignedFFIPointer(offset: Int): FFIPointer? {
return CreateFFIPointer(if (FFI_POINTER_SIZE == 8) getUnalignedInt64(offset) else getUnalignedInt32(offset).toLong())
return CreateFFIPointer(if (FFI_POINTER_SIZE == 8) getS64(offset) else getS32(offset).toLong())
}
fun Buffer.setUnalignedFFIPointer(offset: Int, value: FFIPointer?) {
if (FFI_POINTER_SIZE == 8) setUnalignedInt64(offset, value.address) else setUnalignedInt32(offset, value.address.toInt())
if (FFI_POINTER_SIZE == 8) set64(offset, value.address) else set32(offset, value.address.toInt())
}

expect fun FFILibSym(lib: FFILib): FFILibSym
Expand Down
4 changes: 2 additions & 2 deletions korge-core/src/commonMain/kotlin/korlibs/image/format/ASE.kt
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ object ASE : ImageFormatWithContainer("ase") {
val compressedData = cs.readAvailable()
.uncompress(ZLib, tilesWidth * tilesHeight * bytesPerTile)
val data = when (bitsPerTile) {
32 -> compressedData.getS32LEArray(0, tilesWidth * tilesHeight)
32 -> compressedData.getS32ArrayLE(0, tilesWidth * tilesHeight)
else -> TODO("Only supported 32-bits per tile")
}
AseTilemapCell(
Expand Down Expand Up @@ -580,7 +580,7 @@ object ASE : ImageFormatWithContainer("ase") {
val compressedData =
cs.readBytesExact(compressedDataLength) // (Tile Width) x (Tile Height x Number of Tiles)
val data = compressedData.uncompress(ZLib)
val ints = data.getS32LEArray(0, tileWidth * tileHeight * ntiles)
val ints = data.getS32ArrayLE(0, tileWidth * tileHeight * ntiles)
val bitmap =
Bitmap32(tileWidth, tileHeight * ntiles, RgbaArray(ints)).also {
if (props.premultipliedSure) it.premultiplyInplaceIfRequired()
Expand Down
4 changes: 2 additions & 2 deletions korge-core/src/commonMain/kotlin/korlibs/image/format/EXIF.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ object EXIF {
}
val data: ByteArray = s.readBytesExact(dataFormat.indexBytes(nComponent))

fun readUShort(index: Int): Int = data.getU16(index * 2, little = endian.isLittle)
fun readInt(index: Int): Int = data.getS32(index * 4, little = endian.isLittle)
fun readUShort(index: Int): Int = data.getU16(index * 2, littleEndian = endian.isLittle)
fun readInt(index: Int): Int = data.getS32(index * 4, littleEndian = endian.isLittle)

if (debug) {
if (dataFormat == DataFormat.STRING) {
Expand Down
4 changes: 2 additions & 2 deletions korge-core/src/commonMain/kotlin/korlibs/image/format/WEBP.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ private class WebpWASM(bytes: ByteArray) : korlibs.wasm.WASMLib(bytes) {
val infoPtr = get_info(dataPtr, bytes.size)
val buffer = Buffer(readBytes(infoPtr, 12))
freeBytes(dataPtr)
val success = buffer.getUnalignedInt32(0)
return if (success != 0) SizeInt(buffer.getUnalignedInt32(4), buffer.getUnalignedInt32(8)) else null
val success = buffer.getS32(0)
return if (success != 0) SizeInt(buffer.getS32(4), buffer.getS32(8)) else null
}

fun decodeWebpBytes(bytes: ByteArray): Bitmap32? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object FFIGdiNativeImageFormatProvider : BaseNativeImageFormatProvider() {
//val bitmapData = IntArray(32)
val bmpData = Buffer(4 * 32)
val res2 = Gdiplus.GdipBitmapLockBits(pimage[0], rect, ImageLockModeRead, if (premultiplied) Format32bppPArgb else Format32bppArgb, bmpData)
val strideS = bmpData.getUnalignedInt32(8)
val strideS = bmpData.getS32(8)
val stride = strideS.absoluteValue
val ptr = bmpData.getUnalignedFFIPointer(16)

Expand Down
24 changes: 12 additions & 12 deletions korge-core/src/commonMain/kotlin/korlibs/io/stream/AsyncStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -597,32 +597,32 @@ suspend fun AsyncInputStream.skip(count: Int) {
}

suspend fun AsyncInputStream.readUByteArray(count: Int): UByteArray = readBytesExact(count).asUByteArray()
suspend fun AsyncInputStream.readShortArrayLE(count: Int): ShortArray = readBytesExact(count * 2).getS16LEArray(
suspend fun AsyncInputStream.readShortArrayLE(count: Int): ShortArray = readBytesExact(count * 2).getS16ArrayLE(
0,
count
)
suspend fun AsyncInputStream.readShortArrayBE(count: Int): ShortArray = readBytesExact(count * 2).getS16BEArray(
suspend fun AsyncInputStream.readShortArrayBE(count: Int): ShortArray = readBytesExact(count * 2).getS16ArrayBE(
0,
count
)
suspend fun AsyncInputStream.readCharArrayLE(count: Int): CharArray = readBytesExact(count * 2).getU16LEArray(0, count)
suspend fun AsyncInputStream.readCharArrayBE(count: Int): CharArray = readBytesExact(count * 2).getU16BEArray(0, count)
suspend fun AsyncInputStream.readIntArrayLE(count: Int): IntArray = readBytesExact(count * 4).getS32LEArray(0, count)
suspend fun AsyncInputStream.readIntArrayBE(count: Int): IntArray = readBytesExact(count * 4).getS32BEArray(0, count)
suspend fun AsyncInputStream.readLongArrayLE(count: Int): LongArray = readBytesExact(count * 8).getS64LEArray(0, count)
suspend fun AsyncInputStream.readLongArrayBE(count: Int): LongArray = readBytesExact(count * 8).getS64LEArray(0, count)
suspend fun AsyncInputStream.readFloatArrayLE(count: Int): FloatArray = readBytesExact(count * 4).getF32LEArray(
suspend fun AsyncInputStream.readCharArrayLE(count: Int): CharArray = readBytesExact(count * 2).getU16ArrayLE(0, count)
suspend fun AsyncInputStream.readCharArrayBE(count: Int): CharArray = readBytesExact(count * 2).getU16ArrayBE(0, count)
suspend fun AsyncInputStream.readIntArrayLE(count: Int): IntArray = readBytesExact(count * 4).getS32ArrayLE(0, count)
suspend fun AsyncInputStream.readIntArrayBE(count: Int): IntArray = readBytesExact(count * 4).getS32ArrayBE(0, count)
suspend fun AsyncInputStream.readLongArrayLE(count: Int): LongArray = readBytesExact(count * 8).getS64ArrayLE(0, count)
suspend fun AsyncInputStream.readLongArrayBE(count: Int): LongArray = readBytesExact(count * 8).getS64ArrayLE(0, count)
suspend fun AsyncInputStream.readFloatArrayLE(count: Int): FloatArray = readBytesExact(count * 4).getF32ArrayLE(
0,
count
)
suspend fun AsyncInputStream.readFloatArrayBE(count: Int): FloatArray = readBytesExact(count * 4).getF32BEArray(
suspend fun AsyncInputStream.readFloatArrayBE(count: Int): FloatArray = readBytesExact(count * 4).getF32ArrayBE(
0,
count
)
suspend fun AsyncInputStream.readDoubleArrayLE(count: Int): DoubleArray =
readBytesExact(count * 8).getF64LEArray(0, count)
readBytesExact(count * 8).getF64ArrayLE(0, count)
suspend fun AsyncInputStream.readDoubleArrayBE(count: Int): DoubleArray =
readBytesExact(count * 8).getF64BEArray(0, count)
readBytesExact(count * 8).getF64ArrayBE(0, count)

suspend fun AsyncInputStream.readShortArray(count: Int, endian: Endian): ShortArray = if (endian.isLittle) readShortArrayLE(count) else readShortArrayBE(count)
suspend fun AsyncInputStream.readCharArray(count: Int, endian: Endian): CharArray = if (endian.isLittle) readCharArrayLE(count) else readCharArrayBE(count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ class FastByteArrayInputStream(val ba: ByteArray, offset: Int = 0, val start: In
fun readBytes(count: Int) = ba.getS8Array(offset(count), count)

// Arrays
fun readShortArrayLE(count: Int): ShortArray = ba.getS16LEArray(offset(count * 2), count)
fun readShortArrayBE(count: Int): ShortArray = ba.getS16BEArray(offset(count * 2), count)
fun readShortArrayLE(count: Int): ShortArray = ba.getS16ArrayLE(offset(count * 2), count)
fun readShortArrayBE(count: Int): ShortArray = ba.getS16ArrayBE(offset(count * 2), count)

fun readCharArrayLE(count: Int): CharArray = ba.getU16LEArray(offset(count * 2), count)
fun readCharArrayBE(count: Int): CharArray = ba.getU16BEArray(offset(count * 2), count)
fun readCharArrayLE(count: Int): CharArray = ba.getU16ArrayLE(offset(count * 2), count)
fun readCharArrayBE(count: Int): CharArray = ba.getU16ArrayBE(offset(count * 2), count)

fun readIntArrayLE(count: Int): IntArray = ba.getS32LEArray(offset(count * 4), count)
fun readIntArrayBE(count: Int): IntArray = ba.getS32BEArray(offset(count * 4), count)
fun readIntArrayLE(count: Int): IntArray = ba.getS32ArrayLE(offset(count * 4), count)
fun readIntArrayBE(count: Int): IntArray = ba.getS32ArrayBE(offset(count * 4), count)

fun readLongArrayLE(count: Int): LongArray = ba.getS64LEArray(offset(count * 8), count)
fun readLongArrayBE(count: Int): LongArray = ba.getS64BEArray(offset(count * 8), count)
fun readLongArrayLE(count: Int): LongArray = ba.getS64ArrayLE(offset(count * 8), count)
fun readLongArrayBE(count: Int): LongArray = ba.getS64ArrayBE(offset(count * 8), count)

fun readFloatArrayLE(count: Int): FloatArray = ba.getF32LEArray(offset(count * 4), count)
fun readFloatArrayBE(count: Int): FloatArray = ba.getF32BEArray(offset(count * 4), count)
fun readFloatArrayLE(count: Int): FloatArray = ba.getF32ArrayLE(offset(count * 4), count)
fun readFloatArrayBE(count: Int): FloatArray = ba.getF32ArrayBE(offset(count * 4), count)

fun readDoubleArrayLE(count: Int): DoubleArray = ba.getF64LEArray(offset(count * 8), count)
fun readDoubleArrayBE(count: Int): DoubleArray = ba.getF64BEArray(offset(count * 8), count)
fun readDoubleArrayLE(count: Int): DoubleArray = ba.getF64ArrayLE(offset(count * 8), count)
fun readDoubleArrayBE(count: Int): DoubleArray = ba.getF64ArrayBE(offset(count * 8), count)

// Variable Length
fun readU_VL(): Int {
Expand Down
24 changes: 12 additions & 12 deletions korge-core/src/commonMain/kotlin/korlibs/io/stream/SyncStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -510,23 +510,23 @@ fun SyncStream.readAll(): ByteArray = readBytes(available.toInt())

fun SyncInputStream.readUByteArray(count: Int): UByteArray = readBytesExact(count).asUByteArray()

fun SyncInputStream.readShortArrayLE(count: Int): ShortArray = readBytesExact(count * 2).getS16LEArray(0, count)
fun SyncInputStream.readShortArrayBE(count: Int): ShortArray = readBytesExact(count * 2).getS16BEArray(0, count)
fun SyncInputStream.readShortArrayLE(count: Int): ShortArray = readBytesExact(count * 2).getS16ArrayLE(0, count)
fun SyncInputStream.readShortArrayBE(count: Int): ShortArray = readBytesExact(count * 2).getS16ArrayBE(0, count)

fun SyncInputStream.readCharArrayLE(count: Int): CharArray = readBytesExact(count * 2).getU16LEArray(0, count)
fun SyncInputStream.readCharArrayBE(count: Int): CharArray = readBytesExact(count * 2).getU16BEArray(0, count)
fun SyncInputStream.readCharArrayLE(count: Int): CharArray = readBytesExact(count * 2).getU16ArrayLE(0, count)
fun SyncInputStream.readCharArrayBE(count: Int): CharArray = readBytesExact(count * 2).getU16ArrayBE(0, count)

fun SyncInputStream.readIntArrayLE(count: Int): IntArray = readBytesExact(count * 4).getS32LEArray(0, count)
fun SyncInputStream.readIntArrayBE(count: Int): IntArray = readBytesExact(count * 4).getS32BEArray(0, count)
fun SyncInputStream.readIntArrayLE(count: Int): IntArray = readBytesExact(count * 4).getS32ArrayLE(0, count)
fun SyncInputStream.readIntArrayBE(count: Int): IntArray = readBytesExact(count * 4).getS32ArrayBE(0, count)

fun SyncInputStream.readLongArrayLE(count: Int): LongArray = readBytesExact(count * 8).getS64LEArray(0, count)
fun SyncInputStream.readLongArrayBE(count: Int): LongArray = readBytesExact(count * 8).getS64BEArray(0, count)
fun SyncInputStream.readLongArrayLE(count: Int): LongArray = readBytesExact(count * 8).getS64ArrayLE(0, count)
fun SyncInputStream.readLongArrayBE(count: Int): LongArray = readBytesExact(count * 8).getS64ArrayBE(0, count)

fun SyncInputStream.readFloatArrayLE(count: Int): FloatArray = readBytesExact(count * 4).getF32LEArray(0, count)
fun SyncInputStream.readFloatArrayBE(count: Int): FloatArray = readBytesExact(count * 4).getF32BEArray(0, count)
fun SyncInputStream.readFloatArrayLE(count: Int): FloatArray = readBytesExact(count * 4).getF32ArrayLE(0, count)
fun SyncInputStream.readFloatArrayBE(count: Int): FloatArray = readBytesExact(count * 4).getF32ArrayBE(0, count)

fun SyncInputStream.readDoubleArrayLE(count: Int): DoubleArray = readBytesExact(count * 8).getF64LEArray(0, count)
fun SyncInputStream.readDoubleArrayBE(count: Int): DoubleArray = readBytesExact(count * 8).getF64BEArray(0, count)
fun SyncInputStream.readDoubleArrayLE(count: Int): DoubleArray = readBytesExact(count * 8).getF64ArrayLE(0, count)
fun SyncInputStream.readDoubleArrayBE(count: Int): DoubleArray = readBytesExact(count * 8).getF64ArrayBE(0, count)

fun SyncOutputStream.write8(v: Int): Unit = write(v)

Expand Down
Loading