diff --git a/assets/disk0/home/jpdectest.js b/assets/disk0/home/jpdectest.js index 173a83c..d151991 100644 --- a/assets/disk0/home/jpdectest.js +++ b/assets/disk0/home/jpdectest.js @@ -14,7 +14,7 @@ dma.comToRam(0, 0, infile, fileLen) println("decoding") // decode -const [imgw, imgh, imageData] = graphics.decodeImageResample(infile, fileLen, 560, 448) +const [imgw, imgh, imageData] = graphics.decodeImageResample(infile, fileLen, 0, 448) println(`dim: ${imgw}x${imgh}`) println(`converting to displayable format...`) diff --git a/assets/disk0/test.png b/assets/disk0/test.png new file mode 100644 index 0000000..8cf97f8 Binary files /dev/null and b/assets/disk0/test.png differ diff --git a/assets/disk0/transparent.png b/assets/disk0/transparent.png new file mode 100644 index 0000000..45c1311 Binary files /dev/null and b/assets/disk0/transparent.png differ diff --git a/tsvm_core/src/net/torvald/tsvm/GraphicsJSR223Delegate.kt b/tsvm_core/src/net/torvald/tsvm/GraphicsJSR223Delegate.kt index 832db96..aa92df2 100644 --- a/tsvm_core/src/net/torvald/tsvm/GraphicsJSR223Delegate.kt +++ b/tsvm_core/src/net/torvald/tsvm/GraphicsJSR223Delegate.kt @@ -202,13 +202,34 @@ class GraphicsJSR223Delegate(val vm: VM) { } /** + * @param width0 new image width. If either width or height is zero, the resulting image will be proportionally scaled using the other value. If both are zero, original image dimension will be used. + * @param height0 new image height. If either width or height is zero, the resulting image will be proportionally scaled using the other value. If both are zero, original image dimension will be used. + * * Will always return 4-channel image data */ - fun decodeImageResample(srcFilePtr: Int, srcFileLen: Int, width: Int, height: Int): IntArray { + fun decodeImageResample(srcFilePtr: Int, srcFileLen: Int, width0: Int, height0: Int): IntArray { + var width = width0 + var height = height0 + + val data = ByteArray(srcFileLen) UnsafeHelper.memcpyRaw(null, vm.usermem.ptr + srcFilePtr, data, UnsafeHelper.getArrayOffset(data), srcFileLen.toLong()) val inPixmap = Pixmap(data, 0, data.size) + + if (width <= 0 && height <= 0) { + width = inPixmap.width + height = inPixmap.height + } + else if (width <= 0) { + val scale = inPixmap.width.toFloat() / inPixmap.height.toFloat() + width = (height * scale).roundToInt() + } + else if (height <= 0) { + val scale = inPixmap.width.toFloat() / inPixmap.height.toFloat() + height = (width * scale).roundToInt() + } + val pixmap = Pixmap(width, height, Pixmap.Format.RGBA8888) inPixmap.filter = Pixmap.Filter.BiLinear pixmap.filter = Pixmap.Filter.BiLinear