mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-07 19:51:51 +09:00
hw impl of ipf codec (encoding not tested)
This commit is contained in:
@@ -153,8 +153,8 @@ function ycocgToRGB(co, cg, ys, as) { // ys: 4 Y-values
|
||||
// cocg = 0x7777
|
||||
// ys = 0x7777
|
||||
|
||||
co = (co - 7) / 8
|
||||
cg = (cg - 7) / 8
|
||||
co = (co - 7) / 8.0
|
||||
cg = (cg - 7) / 8.0
|
||||
|
||||
let y1 = (ys & 15) / 15.0
|
||||
let a1 = as & 15
|
||||
|
||||
162
assets/disk0/dipf.js
Normal file
162
assets/disk0/dipf.js
Normal file
@@ -0,0 +1,162 @@
|
||||
if (!exec_args[1]) {
|
||||
printerrln("Usage: decodeipf input.ipf")
|
||||
return 1
|
||||
}
|
||||
|
||||
let filename = exec_args[1]
|
||||
|
||||
const MAGIC = [0x1F, 0x54, 0x53, 0x56, 0x4D, 0x69, 0x50, 0x46]
|
||||
const port = filesystem._toPorts("A")[0]
|
||||
|
||||
|
||||
com.sendMessage(port, "DEVRST\x17")
|
||||
com.sendMessage(port, `OPENR"${filename}",1`)
|
||||
let statusCode = com.getStatusCode(port)
|
||||
|
||||
if (statusCode != 0) {
|
||||
printerrln(`No such file (${statusCode})`)
|
||||
return statusCode
|
||||
}
|
||||
|
||||
com.sendMessage(port, "READ")
|
||||
statusCode = com.getStatusCode(port)
|
||||
if (statusCode != 0) {
|
||||
printerrln("READ failed with "+statusCode)
|
||||
return statusCode
|
||||
}
|
||||
|
||||
con.clear(); con.curs_set(0)
|
||||
|
||||
let readCount = 0
|
||||
|
||||
function readBytes(length) {
|
||||
let ptr = sys.malloc(length)
|
||||
let requiredBlocks = Math.floor((readCount + length) / 4096) - Math.floor(readCount / 4096)
|
||||
|
||||
let completedReads = 0
|
||||
|
||||
//serial.println(`readBytes(${length}); readCount = ${readCount}`)
|
||||
|
||||
for (let bc = 0; bc < requiredBlocks + 1; bc++) {
|
||||
if (completedReads >= length) break
|
||||
|
||||
if (readCount % 4096 == 0) {
|
||||
//serial.println("READ from serial")
|
||||
// pull the actual message
|
||||
sys.poke(-4093 - port, 6);sys.sleep(0) // spinning is required as Graal run is desynced with the Java side
|
||||
|
||||
let blockTransferStatus = ((sys.peek(-4085 - port*2) & 255) | ((sys.peek(-4086 - port*2) & 255) << 8))
|
||||
let thisBlockLen = blockTransferStatus & 4095
|
||||
if (thisBlockLen == 0) thisBlockLen = 4096 // [1, 4096]
|
||||
let hasMore = (blockTransferStatus & 0x8000 != 0)
|
||||
|
||||
|
||||
//serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - k) & 255).toString(16).padStart(2,'0')).join()}]`)
|
||||
|
||||
let remaining = Math.min(thisBlockLen, length - completedReads)
|
||||
|
||||
//serial.println(`Pulled a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}, remaining = ${remaining}`)
|
||||
|
||||
// copy from read buffer to designated position
|
||||
sys.memcpy(-4097, ptr + completedReads, remaining)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += remaining
|
||||
completedReads += remaining
|
||||
}
|
||||
else {
|
||||
let padding = readCount % 4096
|
||||
let remaining = length - completedReads
|
||||
let thisBlockLen = Math.min(4096 - padding, length - completedReads)
|
||||
|
||||
//serial.println(`padding = ${padding}; remaining = ${remaining}`)
|
||||
|
||||
//serial.println(`block: (${thisBlockLen})[${[...Array(thisBlockLen).keys()].map(k => (sys.peek(-4097 - padding - k) & 255).toString(16).padStart(2,'0')).join()}]`)
|
||||
|
||||
//serial.println(`Reusing a block (${thisBlockLen}); readCount = ${readCount}, completedReads = ${completedReads}`)
|
||||
|
||||
// copy from read buffer to designated position
|
||||
sys.memcpy(-4097 - padding, ptr + completedReads, thisBlockLen)
|
||||
|
||||
// increment readCount properly
|
||||
readCount += thisBlockLen
|
||||
completedReads += thisBlockLen
|
||||
}
|
||||
}
|
||||
|
||||
//serial.println(`END readBytes(${length}); readCount = ${readCount}\n`)
|
||||
|
||||
return ptr
|
||||
}
|
||||
|
||||
function readInt() {
|
||||
let b = readBytes(4)
|
||||
let i = (sys.peek(b) & 255) | ((sys.peek(b+1) & 255) << 8) | ((sys.peek(b+2) & 255) << 16) | ((sys.peek(b+3) & 255) << 24)
|
||||
|
||||
//serial.println(`readInt(); bytes: ${sys.peek(b)}, ${sys.peek(b+1)}, ${sys.peek(b+2)}, ${sys.peek(b+3)} = ${i}\n`)
|
||||
|
||||
sys.free(b)
|
||||
return i
|
||||
}
|
||||
|
||||
function readShort() {
|
||||
let b = readBytes(2)
|
||||
let i = (sys.peek(b) & 255) | ((sys.peek(b+1) & 255) << 8)
|
||||
|
||||
//serial.println(`readShort(); bytes: ${sys.peek(b)}, ${sys.peek(b+1)} = ${i}\n`)
|
||||
|
||||
sys.free(b)
|
||||
return i
|
||||
}
|
||||
|
||||
function readByte() {
|
||||
let b = readBytes(1)
|
||||
let i = (sys.peek(b) & 255)
|
||||
|
||||
//serial.println(`readShort(); bytes: ${sys.peek(b)}, ${sys.peek(b+1)} = ${i}\n`)
|
||||
|
||||
sys.free(b)
|
||||
return i
|
||||
}
|
||||
|
||||
|
||||
let magic = readBytes(8)
|
||||
let magicMatching = true
|
||||
|
||||
// check if magic number matches
|
||||
MAGIC.forEach((b,i) => {
|
||||
let testb = sys.peek(magic + i) & 255 // for some reason this must be located here
|
||||
if (testb != b) {
|
||||
magicMatching = false
|
||||
}
|
||||
})
|
||||
sys.free(magic)
|
||||
if (!magicMatching) {
|
||||
println("Not an IPF file (MAGIC mismatch)")
|
||||
return 1
|
||||
}
|
||||
|
||||
let imgw = readShort()
|
||||
let imgh = readShort()
|
||||
let hasAlpha = (readShort() != 0)
|
||||
let ipfType = readByte()
|
||||
sys.free(readBytes(9)) // skip 10 bytes
|
||||
|
||||
graphics.setGraphicsMode(4)
|
||||
|
||||
let infile =
|
||||
(0 == ipfType) ? readBytes((imgw * imgh / 16) * ((hasAlpha) ? 20 : 12)) :
|
||||
(3 == ipfType) ? readBytes((imgw * imgh / 16) * ((hasAlpha) ? 24 : 16)) : null
|
||||
|
||||
if (null == infile) {
|
||||
printerrln("Unsupported IPF configuration: "+ipfType)
|
||||
sys.free(infile)
|
||||
return 1
|
||||
}
|
||||
|
||||
if (0 == ipfType)
|
||||
graphics.decodeIpf1(infile, -1048577, -1310721, imgw, imgh, hasAlpha)
|
||||
else if (3 == ipfType)
|
||||
graphics.decodeIpf2(infile, -1048577, -1310721, imgw, imgh, hasAlpha)
|
||||
|
||||
sys.free(infile)
|
||||
@@ -1,8 +1,7 @@
|
||||
package net.torvald.tsvm
|
||||
|
||||
import com.badlogic.gdx.graphics.Pixmap
|
||||
import com.badlogic.gdx.math.MathUtils.floor
|
||||
import com.badlogic.gdx.math.MathUtils.round
|
||||
import com.badlogic.gdx.math.MathUtils.*
|
||||
import net.torvald.UnsafeHelper
|
||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toUint
|
||||
import net.torvald.tsvm.peripheral.GraphicsAdapter
|
||||
@@ -269,7 +268,7 @@ class GraphicsJSR223Delegate(val vm: VM) {
|
||||
val inPixmap = Pixmap(data, 0, data.size)
|
||||
val gpu = getFirstGPU()
|
||||
|
||||
if (width <= -1.0 && height <= -1.0 && gpu != null) {
|
||||
if (width <= -1f && height <= -1f && gpu != null) {
|
||||
if (inPixmap.width > inPixmap.height) {
|
||||
val scale = inPixmap.height.toFloat() / inPixmap.width.toFloat()
|
||||
width = gpu.config.width
|
||||
@@ -322,7 +321,7 @@ class GraphicsJSR223Delegate(val vm: VM) {
|
||||
val inPixmap = Pixmap(data, 0, data.size)
|
||||
val gpu = getFirstGPU()
|
||||
|
||||
if (width <= -1.0 && height <= -1.0 && gpu != null) {
|
||||
if (width <= -1f && height <= -1f && gpu != null) {
|
||||
if (inPixmap.width > inPixmap.height) {
|
||||
val scale = inPixmap.height.toFloat() / inPixmap.width.toFloat()
|
||||
width = gpu.config.width
|
||||
@@ -539,4 +538,396 @@ class GraphicsJSR223Delegate(val vm: VM) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun chromaToFourBits(f: Float): Int {
|
||||
return (round(f * 8) + 7).coerceIn(0..15)
|
||||
}
|
||||
|
||||
fun blockEncodeToYCoCg(blockX: Int, blockY: Int, srcPtr: Int, width: Int, channels: Int, hasAlpha: Boolean, pattern: Int): List<Any> {
|
||||
val Ys = IntArray(16)
|
||||
val As = IntArray(16)
|
||||
val COs = FloatArray(16)
|
||||
val CGs = FloatArray(16)
|
||||
|
||||
for (py in 0..3) { for (px in 0..3) {
|
||||
// TODO oob-check
|
||||
val ox = blockX * 4 + px
|
||||
val oy = blockY * 4 + py
|
||||
val t = bayerKernels[pattern % bayerKernels.size][4 * (py % 4) + (px % 4)]
|
||||
val offset = channels * (oy * width + ox)
|
||||
|
||||
val r0 = vm.peek(srcPtr + offset+0L)!!.toUint() / 255f
|
||||
val g0 = vm.peek(srcPtr + offset+1L)!!.toUint() / 255f
|
||||
val b0 = vm.peek(srcPtr + offset+2L)!!.toUint() / 255f
|
||||
val a0 = if (hasAlpha) vm.peek(srcPtr + offset+3L)!! / 255f else 1f
|
||||
|
||||
val r = floor((t / 15f + r0) * 15f) / 15f
|
||||
val g = floor((t / 15f + g0) * 15f) / 15f
|
||||
val b = floor((t / 15f + b0) * 15f) / 15f
|
||||
val a = floor((t / 15f + a0) * 15f) / 15f
|
||||
|
||||
val co = r - b // [-1..1]
|
||||
val tmp = b + co / 2f
|
||||
val cg = g - tmp // [-1..1]
|
||||
val y = tmp + cg / 2f // [0..1]
|
||||
|
||||
val index = py * 4 + px
|
||||
Ys[index] = round(y * 15)
|
||||
As[index] = round(a * 15)
|
||||
COs[index] = co
|
||||
CGs[index] = cg
|
||||
}}
|
||||
|
||||
return listOf(Ys, As, COs, CGs)
|
||||
}
|
||||
fun encodeIpf1(srcPtr: Int, destPtr: Int, width: Int, height: Int, channels: Int, hasAlpha: Boolean, pattern: Int) {
|
||||
var writeCount = 0L
|
||||
|
||||
for (blockY in 0 until ceil(height / 4f)) {
|
||||
for (blockX in 0 until ceil(width / 4f)) {
|
||||
val (_1, _2, _3, _4) = blockEncodeToYCoCg(blockX, blockY, srcPtr, width, channels, hasAlpha, pattern)
|
||||
val Ys = _1 as IntArray; val As = _2 as IntArray; val COs = _3 as FloatArray; val CGs = _4 as FloatArray
|
||||
|
||||
// subsample by averaging
|
||||
val cos1 = chromaToFourBits((COs[0]+ COs[1]+ COs[4]+ COs[5]) / 4f)
|
||||
val cos2 = chromaToFourBits((COs[2]+ COs[3]+ COs[6]+ COs[7]) / 4f)
|
||||
val cos3 = chromaToFourBits((COs[8]+ COs[9]+ COs[12]+COs[13]) / 4f)
|
||||
val cos4 = chromaToFourBits((COs[10]+COs[11]+COs[14]+COs[15]) / 4f)
|
||||
|
||||
val cgs1 = chromaToFourBits((CGs[0]+ CGs[1]+ CGs[4]+ CGs[5]) / 4f)
|
||||
val cgs2 = chromaToFourBits((CGs[2]+ CGs[3]+ CGs[6]+ CGs[7]) / 4f)
|
||||
val cgs3 = chromaToFourBits((CGs[8]+ CGs[9]+ CGs[12]+CGs[13]) / 4f)
|
||||
val cgs4 = chromaToFourBits((CGs[10]+CGs[11]+CGs[14]+CGs[15]) / 4f)
|
||||
|
||||
// append encoded blocks to the file
|
||||
val outBlock = destPtr + writeCount
|
||||
|
||||
vm.poke(outBlock+ 0, ((cos2 shl 4) or cos1).toByte())
|
||||
vm.poke(outBlock+ 1, ((cos4 shl 4) or cos3).toByte())
|
||||
vm.poke(outBlock+ 2, ((cgs2 shl 4) or cgs1).toByte())
|
||||
vm.poke(outBlock+ 3, ((cgs4 shl 4) or cgs3).toByte())
|
||||
vm.poke(outBlock+ 4, ((Ys[1] shl 4) or Ys[0]).toByte())
|
||||
vm.poke(outBlock+ 5, ((Ys[5] shl 4) or Ys[4]).toByte())
|
||||
vm.poke(outBlock+ 6, ((Ys[3] shl 4) or Ys[2]).toByte())
|
||||
vm.poke(outBlock+ 7, ((Ys[7] shl 4) or Ys[6]).toByte())
|
||||
vm.poke(outBlock+ 8, ((Ys[9] shl 4) or Ys[8]).toByte())
|
||||
vm.poke(outBlock+ 9, ((Ys[13] shl 4) or Ys[12]).toByte())
|
||||
vm.poke(outBlock+10, ((Ys[11] shl 4) or Ys[10]).toByte())
|
||||
vm.poke(outBlock+11, ((Ys[15] shl 4) or Ys[14]).toByte())
|
||||
|
||||
if (hasAlpha) {
|
||||
vm.poke(outBlock+12, ((As[1] shl 4) or As[0]).toByte())
|
||||
vm.poke(outBlock+13, ((As[5] shl 4) or As[4]).toByte())
|
||||
vm.poke(outBlock+14, ((As[3] shl 4) or As[2]).toByte())
|
||||
vm.poke(outBlock+15, ((As[7] shl 4) or As[6]).toByte())
|
||||
vm.poke(outBlock+16, ((As[9] shl 4) or As[8]).toByte())
|
||||
vm.poke(outBlock+17, ((As[13] shl 4) or As[12]).toByte())
|
||||
vm.poke(outBlock+18, ((As[11] shl 4) or As[10]).toByte())
|
||||
vm.poke(outBlock+19, ((As[15] shl 4) or As[14]).toByte())
|
||||
writeCount += 8
|
||||
}
|
||||
writeCount += 12
|
||||
}}
|
||||
}
|
||||
|
||||
fun encodeIpf2(srcPtr: Int, destPtr: Int, width: Int, height: Int, channels: Int, hasAlpha: Boolean, pattern: Int) {
|
||||
var writeCount = 0L
|
||||
|
||||
for (blockY in 0 until ceil(height / 4f)) {
|
||||
for (blockX in 0 until ceil(width / 4f)) {
|
||||
val (_1, _2, _3, _4) = blockEncodeToYCoCg(blockX, blockY, srcPtr, width, channels, hasAlpha, pattern)
|
||||
val Ys = _1 as IntArray; val As = _2 as IntArray; val COs = _3 as FloatArray; val CGs = _4 as FloatArray
|
||||
|
||||
// subsample by averaging
|
||||
val cos1 = chromaToFourBits((COs[0]+COs[1]) / 2f)
|
||||
val cos2 = chromaToFourBits((COs[2]+COs[3]) / 2f)
|
||||
val cos3 = chromaToFourBits((COs[4]+COs[5]) / 2f)
|
||||
val cos4 = chromaToFourBits((COs[6]+COs[7]) / 2f)
|
||||
val cos5 = chromaToFourBits((COs[8]+COs[9]) / 2f)
|
||||
val cos6 = chromaToFourBits((COs[10]+COs[11]) / 2f)
|
||||
val cos7 = chromaToFourBits((COs[12]+COs[13]) / 2f)
|
||||
val cos8 = chromaToFourBits((COs[14]+COs[15]) / 2f)
|
||||
|
||||
val cgs1 = chromaToFourBits((CGs[0]+CGs[1]) / 2f)
|
||||
val cgs2 = chromaToFourBits((CGs[2]+CGs[3]) / 2f)
|
||||
val cgs3 = chromaToFourBits((CGs[4]+CGs[5]) / 2f)
|
||||
val cgs4 = chromaToFourBits((CGs[6]+CGs[7]) / 2f)
|
||||
val cgs5 = chromaToFourBits((CGs[8]+CGs[9]) / 2f)
|
||||
val cgs6 = chromaToFourBits((CGs[10]+CGs[11]) / 2f)
|
||||
val cgs7 = chromaToFourBits((CGs[12]+CGs[13]) / 2f)
|
||||
val cgs8 = chromaToFourBits((CGs[14]+CGs[15]) / 2f)
|
||||
|
||||
// append encoded blocks to the file
|
||||
val outBlock = destPtr + writeCount
|
||||
|
||||
vm.poke(outBlock+ 0, ((cos2 shl 4) or cos1).toByte())
|
||||
vm.poke(outBlock+ 1, ((cos4 shl 4) or cos3).toByte())
|
||||
vm.poke(outBlock+ 2, ((cos6 shl 4) or cos5).toByte())
|
||||
vm.poke(outBlock+ 3, ((cos8 shl 4) or cos7).toByte())
|
||||
vm.poke(outBlock+ 4, ((cgs2 shl 4) or cgs1).toByte())
|
||||
vm.poke(outBlock+ 5, ((cgs4 shl 4) or cgs3).toByte())
|
||||
vm.poke(outBlock+ 6, ((cgs6 shl 4) or cgs5).toByte())
|
||||
vm.poke(outBlock+ 7, ((cgs8 shl 4) or cgs7).toByte())
|
||||
vm.poke(outBlock+ 8, ((Ys[1] shl 4) or Ys[0]).toByte())
|
||||
vm.poke(outBlock+ 9, ((Ys[5] shl 4) or Ys[4]).toByte())
|
||||
vm.poke(outBlock+10, ((Ys[3] shl 4) or Ys[2]).toByte())
|
||||
vm.poke(outBlock+11, ((Ys[7] shl 4) or Ys[6]).toByte())
|
||||
vm.poke(outBlock+12, ((Ys[9] shl 4) or Ys[8]).toByte())
|
||||
vm.poke(outBlock+13, ((Ys[13] shl 4) or Ys[12]).toByte())
|
||||
vm.poke(outBlock+14, ((Ys[11] shl 4) or Ys[10]).toByte())
|
||||
vm.poke(outBlock+15, ((Ys[15] shl 4) or Ys[14]).toByte())
|
||||
|
||||
if (hasAlpha) {
|
||||
vm.poke(outBlock+16, ((As[1] shl 4) or As[0]).toByte())
|
||||
vm.poke(outBlock+17, ((As[5] shl 4) or As[4]).toByte())
|
||||
vm.poke(outBlock+18, ((As[3] shl 4) or As[2]).toByte())
|
||||
vm.poke(outBlock+19, ((As[7] shl 4) or As[6]).toByte())
|
||||
vm.poke(outBlock+20, ((As[9] shl 4) or As[8]).toByte())
|
||||
vm.poke(outBlock+21, ((As[13] shl 4) or As[12]).toByte())
|
||||
vm.poke(outBlock+22, ((As[11] shl 4) or As[10]).toByte())
|
||||
vm.poke(outBlock+23, ((As[15] shl 4) or As[14]).toByte())
|
||||
writeCount += 8
|
||||
}
|
||||
writeCount += 16
|
||||
}}
|
||||
}
|
||||
|
||||
private fun clampRGB(f: Float) = f.coerceIn(0f, 1f)
|
||||
private fun ycocgToRGB(co: Int, cg: Int, ys: Int, As: Int): Array<Int> { // ys: 4 Y-values
|
||||
// return [R1|G1, B1|A1, R2|G2, B2|A2, R3|G3, B3|A3, R4|G4, B4|A4]
|
||||
|
||||
// cocg = 0x7777
|
||||
// ys = 0x7777
|
||||
|
||||
val co = (co - 7) / 8f
|
||||
val cg = (cg - 7) / 8f
|
||||
|
||||
val y1 = (ys and 15) / 15f
|
||||
val a1 = As and 15
|
||||
var tmp = y1 - cg / 2f
|
||||
val g1 = clampRGB(cg + tmp)
|
||||
val b1 = clampRGB(tmp - co / 2f)
|
||||
val r1 = clampRGB(b1 + co)
|
||||
|
||||
val y2 = ((ys shr 4) and 15) / 15f
|
||||
val a2 = (As shr 4) and 15
|
||||
tmp = y2 - cg / 2f
|
||||
val g2 = clampRGB(cg + tmp)
|
||||
val b2 = clampRGB(tmp - co / 2f)
|
||||
val r2 = clampRGB(b2 + co)
|
||||
|
||||
val y3 = ((ys shr 8) and 15) / 15f
|
||||
val a3 = (As shr 8) and 15
|
||||
tmp = y3 - cg / 2f
|
||||
val g3 = clampRGB(cg + tmp)
|
||||
val b3 = clampRGB(tmp - co / 2f)
|
||||
val r3 = clampRGB(b3 + co)
|
||||
|
||||
val y4 = ((ys shr 12) and 15) / 15f
|
||||
val a4 = (As shr 12) and 15
|
||||
tmp = y4 - cg / 2f
|
||||
val g4 = clampRGB(cg + tmp)
|
||||
val b4 = clampRGB(tmp - co / 2f)
|
||||
val r4 = clampRGB(b4 + co)
|
||||
|
||||
return arrayOf(
|
||||
(round(r1 * 15) shl 4) or round(g1 * 15),
|
||||
(round(b1 * 15) shl 4) or a1,
|
||||
(round(r2 * 15) shl 4) or round(g2 * 15),
|
||||
(round(b2 * 15) shl 4) or a2,
|
||||
(round(r3 * 15) shl 4) or round(g3 * 15),
|
||||
(round(b3 * 15) shl 4) or a3,
|
||||
(round(r4 * 15) shl 4) or round(g4 * 15),
|
||||
(round(b4 * 15) shl 4) or a4,
|
||||
)
|
||||
}
|
||||
|
||||
private fun ycocgToRGB(co1: Int, co2: Int, cg1: Int, cg2: Int, ys: Int, As: Int): Array<Int> { // ys: 4 Y-values
|
||||
// return [R1|G1, B1|A1, R2|G2, B2|A2, R3|G3, B3|A3, R4|G4, B4|A4]
|
||||
|
||||
// cocg = 0x7777
|
||||
// ys = 0x7777
|
||||
|
||||
val co1 = (co1 - 7) / 8f
|
||||
val co2 = (co2 - 7) / 8f
|
||||
val cg1 = (cg1 - 7) / 8f
|
||||
val cg2 = (cg2 - 7) / 8f
|
||||
|
||||
val y1 = (ys and 15) / 15f
|
||||
val a1 = As and 15
|
||||
var tmp = y1 - cg1 / 2f
|
||||
val g1 = clampRGB(cg1 + tmp)
|
||||
val b1 = clampRGB(tmp - co1 / 2f)
|
||||
val r1 = clampRGB(b1 + co1)
|
||||
|
||||
val y2 = ((ys shr 4) and 15) / 15f
|
||||
val a2 = (As shr 4) and 15
|
||||
tmp = y2 - cg1 / 2f
|
||||
val g2 = clampRGB(cg1 + tmp)
|
||||
val b2 = clampRGB(tmp - co1 / 2f)
|
||||
val r2 = clampRGB(b2 + co1)
|
||||
|
||||
val y3 = ((ys shr 8) and 15) / 15f
|
||||
val a3 = (As shr 8) and 15
|
||||
tmp = y3 - cg2 / 2f
|
||||
val g3 = clampRGB(cg2 + tmp)
|
||||
val b3 = clampRGB(tmp - co2 / 2f)
|
||||
val r3 = clampRGB(b3 + co2)
|
||||
|
||||
val y4 = ((ys shr 12) and 15) / 15f
|
||||
val a4 = (As shr 12) and 15
|
||||
tmp = y4 - cg2 / 2f
|
||||
val g4 = clampRGB(cg2 + tmp)
|
||||
val b4 = clampRGB(tmp - co2 / 2f)
|
||||
val r4 = clampRGB(b4 + co2)
|
||||
|
||||
return arrayOf(
|
||||
(round(r1 * 15) shl 4) or round(g1 * 15),
|
||||
(round(b1 * 15) shl 4) or a1,
|
||||
(round(r2 * 15) shl 4) or round(g2 * 15),
|
||||
(round(b2 * 15) shl 4) or a2,
|
||||
(round(r3 * 15) shl 4) or round(g3 * 15),
|
||||
(round(b3 * 15) shl 4) or a3,
|
||||
(round(r4 * 15) shl 4) or round(g4 * 15),
|
||||
(round(b4 * 15) shl 4) or a4,
|
||||
)
|
||||
}
|
||||
|
||||
fun decodeIpf1(srcPtr: Int, destRG: Int, destBA: Int, width: Int, height: Int, hasAlpha: Boolean) {
|
||||
val sign = if (destRG >= 0) 1 else -1
|
||||
if (destRG * destBA < 0) throw IllegalArgumentException("Both destination memories must be on the same domain (both being Usermem or HWmem)")
|
||||
val sptr = srcPtr.toLong()
|
||||
val dptr1 = destRG.toLong()
|
||||
val dptr2 = destBA.toLong()
|
||||
var readCount = 0
|
||||
fun readShort() =
|
||||
vm.peek(sptr + readCount++)!!.toUint() or vm.peek(sptr + readCount++)!!.toUint().shl(8)
|
||||
|
||||
|
||||
for (blockY in 0 until ceil(height / 4f)) {
|
||||
for (blockX in 0 until ceil(width / 4f)) {
|
||||
val rg = IntArray(16) // [R1G1, R2G2, R3G3, R4G4, ...]
|
||||
val ba = IntArray(16)
|
||||
|
||||
val co = readShort()
|
||||
val cg = readShort()
|
||||
val y1 = readShort()
|
||||
val y2 = readShort()
|
||||
val y3 = readShort()
|
||||
val y4 = readShort()
|
||||
|
||||
var a1 = 65535; var a2 = 65535; var a3 = 65535; var a4 = 65535
|
||||
|
||||
if (hasAlpha) {
|
||||
a1 = readShort()
|
||||
a2 = readShort()
|
||||
a3 = readShort()
|
||||
a4 = readShort()
|
||||
}
|
||||
|
||||
var corner = ycocgToRGB(co and 15, cg and 15, y1, a1)
|
||||
rg[0] = corner[0];ba[0] = corner[1]
|
||||
rg[1] = corner[2];ba[1] = corner[3]
|
||||
rg[4] = corner[4];ba[4] = corner[5]
|
||||
rg[5] = corner[6];ba[5] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 4) and 15, (cg shr 4) and 15, y2, a2)
|
||||
rg[2] = corner[0];ba[2] = corner[1]
|
||||
rg[3] = corner[2];ba[3] = corner[3]
|
||||
rg[6] = corner[4];ba[6] = corner[5]
|
||||
rg[7] = corner[6];ba[7] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 8) and 15, (cg shr 8) and 15, y3, a3)
|
||||
rg[8] = corner[0];ba[8] = corner[1]
|
||||
rg[9] = corner[2];ba[9] = corner[3]
|
||||
rg[12] = corner[4];ba[12] = corner[5]
|
||||
rg[13] = corner[6];ba[13] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 12) and 15, (cg shr 12) and 15, y4, a4)
|
||||
rg[10] = corner[0];ba[10] = corner[1]
|
||||
rg[11] = corner[2];ba[11] = corner[3]
|
||||
rg[14] = corner[4];ba[14] = corner[5]
|
||||
rg[15] = corner[6];ba[15] = corner[7]
|
||||
|
||||
|
||||
// move decoded pixels into memory
|
||||
for (py in 0..3) { for (px in 0..3) {
|
||||
val ox = blockX * 4 + px
|
||||
val oy = blockY * 4 + py
|
||||
val offset = oy * 560 + ox
|
||||
vm.poke(dptr1 + offset*sign, rg[py * 4 + px].toByte())
|
||||
vm.poke(dptr2 + offset*sign, ba[py * 4 + px].toByte())
|
||||
}}
|
||||
}}
|
||||
}
|
||||
|
||||
fun decodeIpf2(srcPtr: Int, destRG: Int, destBA: Int, width: Int, height: Int, hasAlpha: Boolean) {
|
||||
val sign = if (destRG >= 0) 1 else -1
|
||||
if (destRG * destBA < 0) throw IllegalArgumentException("Both destination memories must be on the same domain (both being Usermem or HWmem)")
|
||||
val sptr = srcPtr.toLong()
|
||||
val dptr1 = destRG.toLong()
|
||||
val dptr2 = destBA.toLong()
|
||||
var readCount = 0
|
||||
fun readShort() =
|
||||
vm.peek(sptr + readCount++)!!.toUint() or vm.peek(sptr + readCount++)!!.toUint().shl(8)
|
||||
fun readInt() =
|
||||
vm.peek(sptr + readCount++)!!.toUint() or vm.peek(sptr + readCount++)!!.toUint().shl(8) or vm.peek(sptr + readCount++)!!.toUint().shl(16) or vm.peek(sptr + readCount++)!!.toUint().shl(24)
|
||||
|
||||
|
||||
for (blockY in 0 until ceil(height / 4f)) {
|
||||
for (blockX in 0 until ceil(width / 4f)) {
|
||||
val rg = IntArray(16) // [R1G1, R2G2, R3G3, R4G4, ...]
|
||||
val ba = IntArray(16)
|
||||
|
||||
val co = readInt()
|
||||
val cg = readInt()
|
||||
val y1 = readShort()
|
||||
val y2 = readShort()
|
||||
val y3 = readShort()
|
||||
val y4 = readShort()
|
||||
|
||||
var a1 = 65535; var a2 = 65535; var a3 = 65535; var a4 = 65535
|
||||
|
||||
if (hasAlpha) {
|
||||
a1 = readShort()
|
||||
a2 = readShort()
|
||||
a3 = readShort()
|
||||
a4 = readShort()
|
||||
}
|
||||
|
||||
var corner = ycocgToRGB(co and 15, (co shr 8) and 15, cg and 15, (cg shr 8) and 15, y1, a1)
|
||||
rg[0] = corner[0];ba[0] = corner[1]
|
||||
rg[1] = corner[2];ba[1] = corner[3]
|
||||
rg[4] = corner[4];ba[4] = corner[5]
|
||||
rg[5] = corner[6];ba[5] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 4) and 15, (co shr 12) and 15, (cg shr 4) and 15, (cg shr 12) and 15, y2, a2)
|
||||
rg[2] = corner[0];ba[2] = corner[1]
|
||||
rg[3] = corner[2];ba[3] = corner[3]
|
||||
rg[6] = corner[4];ba[6] = corner[5]
|
||||
rg[7] = corner[6];ba[7] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 16) and 15, (co shr 24) and 15, (cg shr 16) and 15, (cg shr 24) and 15, y3, a3)
|
||||
rg[8] = corner[0];ba[8] = corner[1]
|
||||
rg[9] = corner[2];ba[9] = corner[3]
|
||||
rg[12] = corner[4];ba[12] = corner[5]
|
||||
rg[13] = corner[6];ba[13] = corner[7]
|
||||
|
||||
corner = ycocgToRGB((co shr 20) and 15, (co shr 28) and 15, (cg shr 20) and 15, (cg shr 28) and 15, y4, a4)
|
||||
rg[10] = corner[0];ba[10] = corner[1]
|
||||
rg[11] = corner[2];ba[11] = corner[3]
|
||||
rg[14] = corner[4];ba[14] = corner[5]
|
||||
rg[15] = corner[6];ba[15] = corner[7]
|
||||
|
||||
|
||||
// move decoded pixels into memory
|
||||
for (py in 0..3) { for (px in 0..3) {
|
||||
val ox = blockX * 4 + px
|
||||
val oy = blockY * 4 + py
|
||||
val offset = oy * 560 + ox
|
||||
vm.poke(dptr1 + offset*sign, rg[py * 4 + px].toByte())
|
||||
vm.poke(dptr2 + offset*sign, ba[py * 4 + px].toByte())
|
||||
}}
|
||||
}}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user