proportional scale of the image on image resample

This commit is contained in:
minjaesong
2022-04-07 10:36:48 +09:00
parent ba97e2982c
commit 77b95a82f7
4 changed files with 23 additions and 2 deletions

View File

@@ -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...`)

BIN
assets/disk0/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -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