1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2025-01-07 15:31:49 +00:00

Better RGB -> DHGR match

This commit is contained in:
David Schmenk 2024-11-18 16:23:15 -08:00
parent 514da5b967
commit 32dc3095aa

View File

@ -10,24 +10,26 @@ const RED_MSK = $7C00
const BLU = 0 const BLU = 0
const GRN = 1 const GRN = 1
const RED = 2 const RED = 2
const cscale = 2 byte[12] ntscCycle
byte[12] ntscPrev byte[] ntscChroma
// B G R
byte[] ntscChrom // Imperical 0-255 B G R
byte[] = 48, 0, 0 //byte[] = 127, 51, 64 // BLUE
byte[] = 8, 48, 0 //byte[] = 64, 108, 0 // GREEN
byte[] = 0, 16, 16 //byte[] = 1, 77, 64 // BROWN
byte[] = 8, 0, 48 //byte[] = 64, 20, 128 // RED
//byte[] ntscChrom // Imperical 0-63 B G R
//byte[] = 32, 8, 8 byte[] = 32, 14, 16 // BLUE
//byte[] = 16, 32, 8 byte[] = 16, 28, 0 // GREEN
//byte[] = 0, 16, 16 byte[] = 0, 18, 16 // BROWN
//byte[] = 16, 8, 32 byte[] = 16, 4, 32 // RED
//byte[] ntscChrom // Ideal/simplified 0-63 B G R
//byte[] = 127/cscale, 51/cscale, 64/cscale //byte[] = 32, 16, 16 // BLUE
//byte[] = 64/cscale, 108/cscale, 0/cscale //byte[] = 16, 32, 0 // GREEN
//byte[] = 1/cscale, 77/cscale, 64/cscale //byte[] = 0, 16, 16 // BROWN
//byte[] = 64/cscale, 20/cscale, 128/cscale //byte[] = 16, 0, 32 // RED
var er, eg, eb // Running error
def rgb24to16(r, g, b) def rgb24to16(r, g, b)
return (b>>2)|((g&$F8)<<2)|((r&$F8)<<7) return (b>>2)|((g&$F8)<<2)|((r&$F8)<<7)
@ -37,50 +39,67 @@ def abs(n)
return n < 0 ?? -n :: n return n < 0 ?? -n :: n
end end
def max(a, b)
return a < b ?? b :: a
end
def rgbpix(r, g, b, x, y)#0 def rgbpix(r, g, b, x, y)#0
var pr, pg, pb var cr, cg, cb, cx
var dr, dg, db var dr, dg, db
var zdist, pdist var zdist, cdist
byte i byte i
//r = r - er/4
//g = g - eg/4
//b = b - eb/4
//puts("Match: "); puti(x & 3); putln //puts("Match: "); puti(x & 3); putln
//puti(r); putc(',');puti(g); putc(',');puti(b); putln //puti(r); putc(',');puti(g); putc(',');puti(b); putln
//puts("Prev:\n") //puts("Cycle:\n")
pr = 0 cr = 0
pg = 0 cg = 0
pb = 0 cb = 0
for i = x + 1 to x + 3 // Sum current chroma cycle
//puti(ntscPrev[(i&3)*3+RED]); putc(',') for cx = x - 1 downto x - 3
//puti(ntscPrev[(i&3)*3+GRN]); putc(',') i = (cx & 3) * 3
//puti(ntscPrev[(i&3)*3+BLU]); putln //puti(ntscCycle[i+RED]; putc(',')
pr = pr + ntscPrev[(i&3)*3+RED] //puti(ntscCycle[i+GRN]; putc(',')
pg = pg + ntscPrev[(i&3)*3+GRN] //puti(ntscCycle[i+BLU]; putln
pb = pb + ntscPrev[(i&3)*3+BLU] cr = cr + ntscCycle[i+RED]
cg = cg + ntscCycle[i+GRN]
cb = cb + ntscCycle[i+BLU]
next next
//puts("-------------\n") //puts("-------------\n")
//puti(pr); putc(',');puti(pg); putc(',');puti(pb); putln //puti(cr); putc(',');puti(cg); putc(',');puti(cb); putln
dr = r //abs(r - pr) // Subtract off chroma cycle from RGB pixel
dg = g //abs(g - pg) er = r - cr
db = b //abs(b - pb) eg = g - cg
zdist = dr*dr + dg*dg + db*db eb = b - cb
i = (x&3)*3 zdist = er*er + eg*eg + eb*eb // Distance to zero (black)
dr = dr - ntscChrom[i+RED] i = (x & 3) * 3
dg = dg - ntscChrom[i+GRN] dr = er - ntscChroma[i+RED]
db = db - ntscChrom[i+BLU] dg = eg - ntscChroma[i+GRN]
db = eb - ntscChroma[i+BLU]
cdist = dr*dr + dg*dg + db*db // Distance to chroma color
//puts("-------------\n") //puts("-------------\n")
//puti(ntscChrom[i+RED]); putc(',');puti(ntscChrom[i+GRN]); putc(',');puti(ntscChrom[i+BLU]); putln //puti(ntscChroma[i+RED]); putc(',')
pdist = dr*dr + dg*dg + db*db //puti(ntscChroma[i+GRN]); putc(',')
//puts("Dist to Zero :"); puti(zdist); putln //puti(ntscChroma[i+BLU]); putln
//puts("Dist to Chroma:"); puti(pdist); putln //puts("Dist to Zero (Black):"); puti(zdist); putln
if pdist < zdist //puts("Dist to Chroma Color:"); puti(cdist); putln
ntscPrev[i+RED] = ntscChrom[i+RED] if cdist < zdist
ntscPrev[i+GRN] = ntscChrom[i+GRN] // RGB closer to Chroma Color
ntscPrev[i+BLU] = ntscChrom[i+BLU] er = dr
eg = dg
eb = db
ntscCycle[i+RED] = ntscChroma[i+RED]
ntscCycle[i+GRN] = ntscChroma[i+GRN]
ntscCycle[i+BLU] = ntscChroma[i+BLU]
dhgrSet(x, y) dhgrSet(x, y)
else else
ntscPrev[i+RED] = 0 // RGB closer to Black
ntscPrev[i+GRN] = 0 ntscCycle[i+RED] = 0
ntscPrev[i+BLU] = 0 ntscCycle[i+GRN] = 0
ntscCycle[i+BLU] = 0
fin fin
//getc //getc
end end
@ -88,32 +107,32 @@ end
def rgbTest#0 def rgbTest#0
var i var i
for i = 0 to 64 for i = 0 to 63
rgbpix(i, 0, 0, i, 0) rgbpix(i, 0, 0, i, 0)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(0, i, 0, i, 2) rgbpix(0, i, 0, i, 2)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(0, 0, i, i, 4) rgbpix(0, 0, i, i, 4)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(i, i, i, i, 6) rgbpix(i, i, i, i, 6)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(i, i>>2, i>>2, i, 8) rgbpix(i, i>>1, i>>1, i, 8)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(i>>2, i, i>>2, i, 10) rgbpix(i>>1, i, i>>1, i, 10)
next next
memset(@ntscPrev, 0, 12) memset(@ntscCycle, 0, 12)
for i = 0 to 64 for i = 0 to 63
rgbpix(i>>2, i>>3, i, i, 12) rgbpix(i>>1, i>>1, i, i, 12)
next next
end end