ipf codec wip

This commit is contained in:
minjaesong
2022-05-01 15:44:53 +09:00
parent 687cc181e6
commit 37d6f76a5f
2 changed files with 332 additions and 31 deletions

249
assets/disk0/decodeipf.js Normal file
View File

@@ -0,0 +1,249 @@
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)
sys.free(readBytes(12)) // skip 10 bytes
// TODO: gzip
function ycocgToRGB(cocg, ys, as) { // ys: 4 Y-values
// return [R1|G1, B1|A1, R2|G2, B2|A2, R3|G3, B3|A3, R4|G4, B4|A4]
let co = 0.0//((cocg & 15) - 7.5) / 7.5
let cg = 0.0//((cocg >> 4) & 15 - 7.5) / 7.5
let y1 = (ys & 15) / 15.0
let a1 = as & 15
let tmp = y1 - cg / 2
let g1 = cg + tmp
let b1 = tmp - co / 2
let r1 = b1 + co
let y2 = ((ys >> 4) & 15) / 15.0
let a2 = (as >> 4) & 15
tmp = y2 - cg / 2
let g2 = cg + tmp
let b2 = tmp - co / 2
let r2 = b2 + co
let y3 = ((ys >> 8) & 15) / 15.0
let a3 = (as >> 8) & 15
tmp = y3 - cg / 2
let g3 = cg + tmp
let b3 = tmp - co / 2
let r3 = b3 + co
let y4 = ((ys >> 12) & 15) / 15.0
let a4 = (as >> 12) & 15
tmp = y4 - cg / 2
let g4 = cg + tmp
let b4 = tmp - co / 2
let r4 = b4 + co
return [
(Math.round(r1 * 15) << 4) | Math.round(g1 * 15),
(Math.round(b1 * 15) << 4) | a1,
(Math.round(r2 * 15) << 4) | Math.round(g2 * 15),
(Math.round(b2 * 15) << 4) | a2,
(Math.round(r3 * 15) << 4) | Math.round(g3 * 15),
(Math.round(b3 * 15) << 4) | a3,
(Math.round(r4 * 15) << 4) | Math.round(g4 * 15),
(Math.round(b4 * 15) << 4) | a4,
]
}
graphics.setGraphicsMode(4)
for (let blockY = 0; blockY < Math.ceil(imgh / 4.0); blockY++) {
for (let blockX = 0; blockX < Math.ceil(imgw / 4.0); blockX++) {
let rg = new Uint8Array(16) // [R1G1, R2G2, R3G3, R4G4, ...]
let ba = new Uint8Array(16)
let cocg1 = readByte()
let y1 = readShort()
let cocg2 = readByte()
let y2 = readShort()
let cocg3 = readByte()
let y3 = readShort()
let cocg4 = readByte()
let y4 = readShort()
let a1 = 65535; let a2 = 65535; let a3 = 65535; let a4 = 65535
if (hasAlpha) {
a1 = readShort()
a2 = readShort()
a3 = readShort()
a4 = readShort()
}
let corner = ycocgToRGB(cocg1, 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(cocg2, 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(cocg3, 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(cocg4, 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 (let py = 0; py < 4; py++) { for (let px = 0; px < 4; px++) {
let ox = blockX * 4 + px
let oy = blockY * 4 + py
let offset = oy * 560 + ox
sys.poke(-1048577 - offset, rg[py * 4 + px])
sys.poke(-1310721 - offset, ba[py * 4 + px])
}}
}}

View File

@@ -33,6 +33,7 @@ Image is divided into 4x4 blocks and each block is serialised, then the entire f
Bits are packed like so:
uint32 SUBSAMPLING MASK (unimplemented; dont write this)
uint8 [Cg-Top Left | Co-Top Left]
uint16 [Y1 | Y0 | Y5 | Y4]
uint8 [Cg-Top Right | Co-Top Right]
@@ -41,21 +42,38 @@ Image is divided into 4x4 blocks and each block is serialised, then the entire f
uint16 [Y9 | Y8 | YD | YC]
uint8 [Cg-Bottom Right | Co-Bottom Right]
uint16 [YB | YA | YF | YE]
(total: 96 bytes)
(total: 16 bytes)
If has alpha, append following bytes for alpha values
uint16 [a1 | a0 | a5 | a4]
uint16 [a3 | a2 | a7 | a6]
uint16 [a9 | a8 | aD | aC]
uint16 [aB | aA | aF | aE]
(total: 160 bytes)
(total: 24 bytes)
Subsampling mask:
Least significant byte for top-left, most significant for bottom-right
For example, this default pattern
00 00 01 01
00 00 01 01
10 10 11 11
10 10 11 11
turns into:
01010000 -> 0x30
01010000 -> 0x30
11111010 -> 0xFA
11111010 -> 0xFA
which packs into: [ 30 | 30 | FA | FA ] (because little endian)
*/
if (!exec_args[2]) {
printerrln("Usage: entsvmipf input.jpg output.ipf")
printerrln("Usage: encodeipf input.jpg output.ipf")
return 1
}
@@ -74,23 +92,42 @@ dma.comToRam(0, 0, infile, fileLen)
// decode
const [imgw, imgh, imageData, channels] = graphics.decodeImage(infile, fileLen) // stored as [R | G | B | (A)]
sys.free(infile)
let hasAlpha = (4 == channels) && configUseAlpha
let blockCount = Math.ceil(imgh / 4.0) * Math.ceil(imgw / 4.0)
let serialisedBlocks = sys.malloc((hasAlpha) ? blockCount * 20 : blockCount * 12)
let blocksWriteCount = 0
let outBlock = sys.malloc(64)
let blockSize = Math.ceil(imgh / 4.0) * Math.ceil(imgw / 4.0)
let blockWidth = Math.ceil(imgw / 4.0)
println(`Dim: ${imgw}x${imgh}, channels: ${channels}, Has alpha: ${hasAlpha}`)
// TODO write output to dedicated ptr and gzip it
let writeCount = 0
let writeBuf = sys.malloc(blockSize * ((hasAlpha) ? 20 : 12))
function chromaToFourBits(f) {
return Math.round((f * 7.5) + 7.5)
}
for (let blockY = 0; blockY < Math.ceil(imgh / 4.0); blockY++) {
for (let blockX = 0; blockX < Math.ceil(imgw / 4.0); blockx++) {
let pixelWordOffset = channels * (blockY * 4) * imgw + (blockX * 4)
let ys = Uint8Array(16)
let as = Uint8Array(16)
let cos = Float32Array(16)
let cgs = Float32Array(16)
for (let blockX = 0; blockX < Math.ceil(imgw / 4.0); blockX++) {
// println(`Encoding block ${1 + blockY * blockWidth + blockX}/${blockSize}`) // print statement is making things slower...
let ys = new Uint8Array(16)
let as = new Uint8Array(16)
let cos = new Float32Array(16)
let cgs = new Float32Array(16)
// TODO 4x4 bayer dither
for (let py = 0; py < 4; py++) { for (let px = 0; px < 4; px++) {
let offset = imageData + pixelWordOffset + 4 * (px + py * imgw)
let r = sys.peek(offset) / 255.0
let g = sys.peek(offset+1) / 255.0
let b = sys.peek(offset+2) / 255.0
let a = (hasAlpha) ? sys.peek(offset+3) / 255.0 : 1.0
// TODO oob-check
let ox = blockX * 4 + px
let oy = blockY * 4 + py
let offset = channels * (oy * imgw + ox)
let r = sys.peek(imageData + offset) / 255.0
let g = sys.peek(imageData + offset+1) / 255.0
let b = sys.peek(imageData + offset+2) / 255.0
let a = (hasAlpha) ? sys.peek(imageData + offset+3) / 255.0 : 1.0
let co = r - b // [-1..1]
let tmp = b + co / 2.0
@@ -105,17 +142,17 @@ for (let blockX = 0; blockX < Math.ceil(imgw / 4.0); blockx++) {
}}
// subsample by averaging
cos1 = Math.round((((cos[0]+cos[1]+cos[4]+cos[5]) / 4.0) + 1) * 15)
cos2 = Math.round((((cos[2]+cos[3]+cos[6]+cos[7]) / 4.0) + 1) * 15)
cos3 = Math.round((((cos[8]+cos[9]+cos[12]+cos[13]) / 4.0) + 1) * 15)
cos4 = Math.round((((cos[10]+cos[11]+cos[14]+cos[15]) / 4.0) + 1) * 15)
cgs1 = Math.round((((cgs[0]+cgs[1]+cgs[4]+cgs[5]) / 4.0) + 1) * 15)
cgs2 = Math.round((((cgs[2]+cgs[3]+cgs[6]+cgs[7]) / 4.0) + 1) * 15)
cgs3 = Math.round((((cgs[8]+cgs[9]+cgs[12]+cgs[13]) / 4.0) + 1) * 15)
cgs4 = Math.round((((cgs[10]+cgs[11]+cgs[14]+cgs[15]) / 4.0) + 1) * 15)
let cos1 = chromaToFourBits((cos[0]+cos[1]+cos[4]+cos[5]) / 8.0)
let cos2 = chromaToFourBits((cos[2]+cos[3]+cos[6]+cos[7]) / 8.0)
let cos3 = chromaToFourBits((cos[8]+cos[9]+cos[12]+cos[13]) / 8.0)
let cos4 = chromaToFourBits((cos[10]+cos[11]+cos[14]+cos[15]) / 8.0)
let cgs1 = chromaToFourBits((cgs[0]+cgs[1]+cgs[4]+cgs[5]) / 8.0)
let cgs2 = chromaToFourBits((cgs[2]+cgs[3]+cgs[6]+cgs[7]) / 8.0)
let cgs3 = chromaToFourBits((cgs[8]+cgs[9]+cgs[12]+cgs[13]) / 8.0)
let cgs4 = chromaToFourBits((cgs[10]+cgs[11]+cgs[14]+cgs[15]) / 8.0)
// append encoded blocks
let outBlock = serialisedBlocks + blocksWriteCount
// append encoded blocks to the file
let outBlock = writeBuf + writeCount
sys.poke(outBlock+ 0, (cgs1 << 4) | cos1)
sys.poke(outBlock+ 1, (ys[1] << 4) | ys[0])
@@ -139,11 +176,26 @@ for (let blockX = 0; blockX < Math.ceil(imgw / 4.0); blockx++) {
sys.poke(outBlock+17, (as[13] << 4) | as[12])
sys.poke(outBlock+18, (as[11] << 4) | as[10])
sys.poke(outBlock+19, (as[15] << 4) | as[14])
blocksWriteCount += 8
writeCount += 8
}
blocksWriteCount += 12
writeCount += 12
}}
// TODO open outfile, write header, write serialisedBlocks.gz
// write header to the output file
let headerBytes = [
0x1F, 0x54, 0x53, 0x56, 0x4D, 0x69, 0x50, 0x46, // magic
imgw & 255, (imgw >> 8) & 255, // width
imgh & 255, (imgh >> 8) & 255, // height
((hasAlpha) ? 1 : 0), 0x00, // has alpha
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // reserved
]
filesystem.open("A", exec_args[2], "W")
filesystem.writeBytes("A", headerBytes)
filesystem.open("A", exec_args[2], "A")
dma.ramToCom(writeBuf, 0, writeCount)
sys.free(outBlock)
sys.free(imageData)
sys.free(writeBuf)