From 76889970b49aaf31930af9ded85cc6e1113a3d98 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 2 May 2022 01:57:59 +0900 Subject: [PATCH] two different ipf versions impl --- assets/disk0/decodeipf.js | 2 +- assets/disk0/decodeipf2.js | 257 +++++++++++++++++++++++++++++++++++++ assets/disk0/encodeipf.js | 4 +- assets/disk0/encodeipf2.js | 254 ++++++++++++++++++++++++++++++++++++ 4 files changed, 514 insertions(+), 3 deletions(-) create mode 100644 assets/disk0/decodeipf2.js create mode 100644 assets/disk0/encodeipf2.js diff --git a/assets/disk0/decodeipf.js b/assets/disk0/decodeipf.js index 5fe128a..64dceae 100644 --- a/assets/disk0/decodeipf.js +++ b/assets/disk0/decodeipf.js @@ -151,7 +151,7 @@ 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] // cocg = 0x7777 -// ys = 0x0000 +// ys = 0x7777 let co = ((cocg & 15) - 7) / 8 let cg = (((cocg >>> 4) & 15) - 7) / 8 diff --git a/assets/disk0/decodeipf2.js b/assets/disk0/decodeipf2.js new file mode 100644 index 0000000..453d760 --- /dev/null +++ b/assets/disk0/decodeipf2.js @@ -0,0 +1,257 @@ +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(10)) // skip 10 bytes + +// TODO: gzip + +function clampRGB(f) { + return (f > 1.0) ? 1.0 : (f < 0.0) ? 0.0 : f +} + +function ycocgToRGB(co1, co2, cg1, cg2, ys, as) { // 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 + + co1 = (co1 - 7) / 8 + co2 = (co2 - 7) / 8 + cg1 = (cg1 - 7) / 8 + cg2 = (cg2 - 7) / 8 + + let y1 = (ys & 15) / 15.0 + let a1 = as & 15 + let tmp = y1 - cg1 / 2.0 + let g1 = clampRGB(cg1 + tmp) + let b1 = clampRGB(tmp - co1 / 2.0) + let r1 = clampRGB(b1 + co1) + + let y2 = ((ys >>> 4) & 15) / 15.0 + let a2 = (as >>> 4) & 15 + tmp = y2 - cg1 / 2.0 + let g2 = clampRGB(cg1 + tmp) + let b2 = clampRGB(tmp - co1 / 2.0) + let r2 = clampRGB(b2 + co1) + + let y3 = ((ys >>> 8) & 15) / 15.0 + let a3 = (as >>> 8) & 15 + tmp = y3 - cg2 / 2.0 + let g3 = clampRGB(cg2 + tmp) + let b3 = clampRGB(tmp - co2 / 2.0) + let r3 = clampRGB(b3 + co2) + + let y4 = ((ys >>> 12) & 15) / 15.0 + let a4 = (as >>> 12) & 15 + tmp = y4 - cg2 / 2.0 + let g4 = clampRGB(cg2 + tmp) + let b4 = clampRGB(tmp - co2 / 2.0) + let r4 = clampRGB(b4 + co2) + + 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 co = readInt() + let cg = readInt() + let y1 = readShort() + let y2 = readShort() + let y3 = readShort() + 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(co & 15, (co >> 8) & 15, cg & 15, (cg >> 8) & 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 >> 4) & 15, (co >> 12) & 15, (cg >> 4) & 15, (cg >> 12) & 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 >> 16) & 15, (co >> 24) & 15, (cg >> 16) & 15, (cg >> 24) & 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 >> 20) & 15, (co >> 28) & 15, (cg >> 20) & 15, (cg >> 28) & 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 (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]) + }} +}} diff --git a/assets/disk0/encodeipf.js b/assets/disk0/encodeipf.js index 8b29ceb..f05ea1d 100644 --- a/assets/disk0/encodeipf.js +++ b/assets/disk0/encodeipf.js @@ -42,14 +42,14 @@ 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: 16 bytes) + (total: 12 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: 24 bytes) + (total: 20 bytes) Subsampling mask: diff --git a/assets/disk0/encodeipf2.js b/assets/disk0/encodeipf2.js new file mode 100644 index 0000000..4b3e46f --- /dev/null +++ b/assets/disk0/encodeipf2.js @@ -0,0 +1,254 @@ +/* +TSVM Interchangeable Picture Format + +Image is divided into 4x4 blocks and each block is serialised, then the entire file is gzipped + + +# File Structure +\x1F T S V M i P F +[HEADER] +[Blocks.gz] + +- Header + uint16 WIDTH + uint16 HEIGHT + uint16 HAS ALPHA + byte[10] RESERVED + +- *.gz + uint32 UNCOMPRESSED SIZE + * PAYLOAD + +- Blocks + 4x4 pixels are sampled, then divided into YCoCg planes. + CoCg planes are "chroma subsampled" by 4:2:0, then quantised to 4 bits (8 bits for CoCg combined) + Y plane is quantised to 4 bits + + By doing so, CoCg planes will reduce to 4 pixels + For the description of packing, pixels in Y plane will be numbered as: + 0 1 2 3 + 4 5 6 7 + 8 9 A B + C D E F + + Bits are packed like so: + + uint32 SUBSAMPLING MASK (unimplemented; dont write this) + uint8 [Co-2 | Co-1] + uint8 [Co-4 | Co-3] + uint8 [Co-6 | Co-5] + uint8 [Co-8 | Co-7] + uint8 [Cg-2 | Cg-1] + uint8 [Cg-4 | Cg-3] + uint8 [Cg-6 | Cg-5] + uint8 [Cg-8 | Cg-7] + uint16 [Y1 | Y0 | Y5 | Y4] + uint16 [Y3 | Y2 | Y7 | Y6] + uint16 [Y9 | Y8 | YD | YC] + uint16 [YB | YA | YF | YE] + (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: 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: encodeipf input.jpg output.ipf [/noalpha]") + return 1 +} + +let configUseAlpha = !(exec_args[3] != undefined && exec_args[3].toLowerCase() == "/noalpha") + +let pattern = 0 + +filesystem.open("A", exec_args[1], "R") + +let status = com.getStatusCode(0) +let infile = undefined +if (0 != status) return status + +// read file +let fileLen = filesystem.getFileLen("A") +infile = sys.malloc(fileLen) +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 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) ? 24 : 16)) + +let bayerKernels = [ + [ + 0,8,2,10, + 12,4,14,6, + 3,11,1,9, + 15,7,13,5, + ], + [ + 8,2,10,0, + 4,14,6,12, + 11,1,9,3, + 7,13,5,15, + ], + [ + 7,13,5,15, + 8,2,10,0, + 4,14,6,12, + 11,1,9,3, + ], + [ + 15,7,13,5, + 0,8,2,10, + 12,4,14,6, + 3,11,1,9, + ] +].map(it => it.map(it => (it + 0.5) / 16)) + +function chromaToFourBits(f) { + let r = Math.round(f * 8) + 7 + return (r < 0) ? 0 : (r > 15) ? 15 : r +} + +for (let blockY = 0; blockY < Math.ceil(imgh / 4.0); blockY++) { +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) + + for (let py = 0; py < 4; py++) { for (let px = 0; px < 4; px++) { + // TODO oob-check + let ox = blockX * 4 + px + let oy = blockY * 4 + py + let t = bayerKernels[pattern % bayerKernels.length][4 * (py % 4) + (px % 4)] + let offset = channels * (oy * imgw + ox) + + let r0 = sys.peek(imageData + offset) / 255.0 + let g0 = sys.peek(imageData + offset+1) / 255.0 + let b0 = sys.peek(imageData + offset+2) / 255.0 + let a0 = (hasAlpha) ? sys.peek(imageData + offset+3) / 255.0 : 1.0 + + let r = Math.floor((t / 15 + r0) * 15) / 15 + let g = Math.floor((t / 15 + g0) * 15) / 15 + let b = Math.floor((t / 15 + b0) * 15) / 15 + let a = Math.floor((t / 15 + a0) * 15) / 15 + + let co = r - b // [-1..1] + let tmp = b + co / 2.0 + let cg = g - tmp // [-1..1] + let y = tmp + cg / 2.0 // [0..1] + + let index = py * 4 + px + ys[index] = Math.round(y * 15) + as[index] = Math.round(a * 15) + cos[index] = co + cgs[index] = cg + }} + + // subsample by averaging + let cos1 = chromaToFourBits((cos[0]+cos[1]) / 2.0) + let cos2 = chromaToFourBits((cos[2]+cos[3]) / 2.0) + let cos3 = chromaToFourBits((cos[4]+cos[5]) / 2.0) + let cos4 = chromaToFourBits((cos[6]+cos[7]) / 2.0) + let cos5 = chromaToFourBits((cos[8]+cos[9]) / 2.0) + let cos6 = chromaToFourBits((cos[10]+cos[11]) / 2.0) + let cos7 = chromaToFourBits((cos[12]+cos[13]) / 2.0) + let cos8 = chromaToFourBits((cos[14]+cos[15]) / 2.0) + + let cgs1 = chromaToFourBits((cgs[0]+cgs[1]) / 2.0) + let cgs2 = chromaToFourBits((cgs[2]+cgs[3]) / 2.0) + let cgs3 = chromaToFourBits((cgs[4]+cgs[5]) / 2.0) + let cgs4 = chromaToFourBits((cgs[6]+cgs[7]) / 2.0) + let cgs5 = chromaToFourBits((cgs[8]+cgs[9]) / 2.0) + let cgs6 = chromaToFourBits((cgs[10]+cgs[11]) / 2.0) + let cgs7 = chromaToFourBits((cgs[12]+cgs[13]) / 2.0) + let cgs8 = chromaToFourBits((cgs[14]+cgs[15]) / 2.0) + + + // append encoded blocks to the file + let outBlock = writeBuf + writeCount + + sys.poke(outBlock+ 0, (cos2 << 4) | cos1) + sys.poke(outBlock+ 1, (cos4 << 4) | cos3) + sys.poke(outBlock+ 2, (cos6 << 4) | cos5) + sys.poke(outBlock+ 3, (cos8 << 4) | cos7) + sys.poke(outBlock+ 4, (cgs2 << 4) | cgs1) + sys.poke(outBlock+ 5, (cgs4 << 4) | cgs3) + sys.poke(outBlock+ 6, (cgs6 << 4) | cgs5) + sys.poke(outBlock+ 7, (cgs8 << 4) | cgs7) + sys.poke(outBlock+ 8, (ys[1] << 4) | ys[0]) + sys.poke(outBlock+ 9, (ys[5] << 4) | ys[4]) + sys.poke(outBlock+10, (ys[3] << 4) | ys[2]) + sys.poke(outBlock+11, (ys[7] << 4) | ys[6]) + sys.poke(outBlock+12, (ys[9] << 4) | ys[8]) + sys.poke(outBlock+13, (ys[13] << 4) | ys[12]) + sys.poke(outBlock+14, (ys[11] << 4) | ys[10]) + sys.poke(outBlock+15, (ys[15] << 4) | ys[14]) + + if (hasAlpha) { + sys.poke(outBlock+16, (as[1] << 4) | as[0]) + sys.poke(outBlock+17, (as[5] << 4) | as[4]) + sys.poke(outBlock+18, (as[3] << 4) | as[2]) + sys.poke(outBlock+19, (as[7] << 4) | as[6]) + sys.poke(outBlock+20, (as[9] << 4) | as[8]) + sys.poke(outBlock+21, (as[13] << 4) | as[12]) + sys.poke(outBlock+22, (as[11] << 4) | as[10]) + sys.poke(outBlock+23, (as[15] << 4) | as[14]) + writeCount += 8 + } + writeCount += 16 + +}} + +// 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 + 0x03, 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) \ No newline at end of file