tools: rle_encode: Experimental color encoder
The output from this is a bit too bulky so the code isn't hooked up to anything at the moment.
This commit is contained in:
parent
fbc5ee87d2
commit
28640d431e
1 changed files with 57 additions and 10 deletions
|
@ -9,27 +9,74 @@ def encode(im):
|
||||||
|
|
||||||
rle = []
|
rle = []
|
||||||
rl = 0
|
rl = 0
|
||||||
px = 0
|
px = pixels[0, 0]
|
||||||
|
|
||||||
|
def encode_pixel(px, rl):
|
||||||
|
while rl > 255:
|
||||||
|
rle.append(255)
|
||||||
|
rle.append(0)
|
||||||
|
rl -= 255
|
||||||
|
rle.append(rl)
|
||||||
|
|
||||||
for y in range(im.height):
|
for y in range(im.height):
|
||||||
for x in range(im.width):
|
for x in range(im.width):
|
||||||
newpx = pixels[x, y]
|
newpx = pixels[x, y]
|
||||||
if newpx == px:
|
if newpx == px:
|
||||||
if rl < 255:
|
|
||||||
rl += 1
|
rl += 1
|
||||||
else:
|
assert(rl < (1 << 21))
|
||||||
# Handle overflow
|
|
||||||
rle.append(255)
|
|
||||||
rle.append(0)
|
|
||||||
rl = 1
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Code the previous run
|
||||||
|
encode_pixel(px, rl)
|
||||||
|
|
||||||
# Start a new run
|
# Start a new run
|
||||||
rle.append(rl)
|
|
||||||
rl = 1
|
rl = 1
|
||||||
px = newpx
|
px = newpx
|
||||||
|
|
||||||
# Handle the final run
|
# Handle the final run
|
||||||
rle.append(rl)
|
encode_pixel(px, rl)
|
||||||
|
|
||||||
|
return (im.width, im.height, bytes(rle))
|
||||||
|
|
||||||
|
def encode_8bit(im):
|
||||||
|
pixels = im.load()
|
||||||
|
|
||||||
|
rle = []
|
||||||
|
rl = 0
|
||||||
|
px = pixels[0, 0]
|
||||||
|
|
||||||
|
def encode_pixel(px, rl):
|
||||||
|
print(rl)
|
||||||
|
px = (px[0] & 0xe0) | ((px[1] & 0xe0) >> 3) | ((px[2] & 0xc0) >> 6)
|
||||||
|
|
||||||
|
rle.append(px)
|
||||||
|
if rl > 0:
|
||||||
|
rle.append(px)
|
||||||
|
rl -= 2
|
||||||
|
if rl > (1 << 14):
|
||||||
|
rle.append(0x80 | ((rl >> 14) & 0x7f))
|
||||||
|
if rl > (1 << 7):
|
||||||
|
rle.append(0x80 | ((rl >> 7) & 0x7f))
|
||||||
|
if rl >= 0:
|
||||||
|
rle.append( rl & 0x7f )
|
||||||
|
|
||||||
|
for y in range(im.height):
|
||||||
|
for x in range(im.width):
|
||||||
|
newpx = pixels[x, y]
|
||||||
|
if newpx == px:
|
||||||
|
rl += 1
|
||||||
|
assert(rl < (1 << 21))
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Code the previous run
|
||||||
|
encode_pixel(px, rl)
|
||||||
|
|
||||||
|
# Start a new run
|
||||||
|
rl = 1
|
||||||
|
px = newpx
|
||||||
|
|
||||||
|
# Handle the final run
|
||||||
|
encode_pixel(px, rl)
|
||||||
|
|
||||||
return (im.width, im.height, bytes(rle))
|
return (im.width, im.height, bytes(rle))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue