mirror of
https://github.com/curioustorvald/tsvm.git
synced 2026-03-13 22:46:06 +09:00
ipf codec wip
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user