tools: rle_encode: Add support for rendering 2-bit RLEs to C src
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
parent
06f1ed36b0
commit
67d2ed83d2
1 changed files with 40 additions and 30 deletions
|
@ -268,21 +268,52 @@ def encode_8bit(im):
|
||||||
|
|
||||||
return (im.width, im.height, bytes(rle))
|
return (im.width, im.height, bytes(rle))
|
||||||
|
|
||||||
def render_c(image, fname):
|
def render_c(image, fname, indent, depth):
|
||||||
print(f'// 1-bit RLE, generated from {fname}, {len(image[2])} bytes')
|
extra_indent = ' ' * indent
|
||||||
print(f'static const uint8_t {varname(fname)}[] = {{')
|
if len(image) == 3:
|
||||||
print(' ', end='')
|
print(f'{extra_indent}// {depth}-bit RLE, generated from {fname}, '
|
||||||
|
f'{len(image[2])} bytes')
|
||||||
|
(x, y, pixels) = image
|
||||||
|
else:
|
||||||
|
print(f'{extra_indent}// {depth}-bit RLE, generated from {fname}, '
|
||||||
|
f'{len(image)} bytes')
|
||||||
|
pixels = image
|
||||||
|
|
||||||
|
print(f'{extra_indent}static const uint8_t {varname(fname)}[] = {{')
|
||||||
|
print(f'{extra_indent} ', end='')
|
||||||
i = 0
|
i = 0
|
||||||
for rl in image[2]:
|
for rl in pixels:
|
||||||
print(f' {hex(rl)},', end='')
|
print(f' {hex(rl)},', end='')
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
if i == 12:
|
if i == 12:
|
||||||
print('\n ', end='')
|
print(f'\n{extra_indent} ', end='')
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
print('\n};')
|
print('\n};')
|
||||||
|
|
||||||
|
def render_py(image, fname, indent, depth):
|
||||||
|
extra_indent = ' ' * indent
|
||||||
|
if len(image) == 3:
|
||||||
|
print(f'{extra_indent}# {depth}-bit RLE, generated from {fname}, '
|
||||||
|
f'{len(image[2])} bytes')
|
||||||
|
(x, y, pixels) = image
|
||||||
|
print(f'{extra_indent}{varname(fname)} = (')
|
||||||
|
print(f'{extra_indent} {x}, {y},')
|
||||||
|
else:
|
||||||
|
print(f'{extra_indent}# {depth}-bit RLE, generated from {fname}, '
|
||||||
|
f'{len(image)} bytes')
|
||||||
|
pixels = image[3:]
|
||||||
|
print(f'{extra_indent}{varname(fname)} = (')
|
||||||
|
print(f'{extra_indent} {image[0:1]}')
|
||||||
|
print(f'{extra_indent} {image[1:3]}')
|
||||||
|
|
||||||
|
# Split the bytestring to ensure each line is short enough to
|
||||||
|
# be absorbed on the target if needed.
|
||||||
|
for i in range(0, len(pixels), 16):
|
||||||
|
print(f'{extra_indent} {pixels[i:i+16]}')
|
||||||
|
print(f'{extra_indent})')
|
||||||
|
|
||||||
|
|
||||||
def decode_to_ascii(image):
|
def decode_to_ascii(image):
|
||||||
(sx, sy, rle) = image
|
(sx, sy, rle) = image
|
||||||
data = bytearray(2*sx)
|
data = bytearray(2*sx)
|
||||||
|
@ -310,8 +341,6 @@ def decode_to_ascii(image):
|
||||||
# Check the image is the correct length
|
# Check the image is the correct length
|
||||||
assert(dp == 0)
|
assert(dp == 0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='RLE encoder tool.')
|
parser = argparse.ArgumentParser(description='RLE encoder tool.')
|
||||||
parser.add_argument('files', nargs='+',
|
parser.add_argument('files', nargs='+',
|
||||||
help='files to be encoded')
|
help='files to be encoded')
|
||||||
|
@ -327,7 +356,6 @@ parser.add_argument('--8bit', action='store_true', dest='eightbit',
|
||||||
help='Generate 8-bit image')
|
help='Generate 8-bit image')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
extra_indent = ' ' * args.indent
|
|
||||||
if args.eightbit:
|
if args.eightbit:
|
||||||
encoder = encode_8bit
|
encoder = encode_8bit
|
||||||
depth = 8
|
depth = 8
|
||||||
|
@ -342,27 +370,9 @@ for fname in args.files:
|
||||||
image = encoder(Image.open(fname))
|
image = encoder(Image.open(fname))
|
||||||
|
|
||||||
if args.c:
|
if args.c:
|
||||||
render_c(image, fname)
|
render_c(image, fname, args.indent, depth)
|
||||||
else:
|
else:
|
||||||
if len(image) == 3:
|
render_py(image, fname, args.indent, depth)
|
||||||
print(f'{extra_indent}# {depth}-bit RLE, generated from {fname}, '
|
|
||||||
f'{len(image[2])} bytes')
|
|
||||||
(x, y, pixels) = image
|
|
||||||
print(f'{extra_indent}{varname(fname)} = (')
|
|
||||||
print(f'{extra_indent} {x}, {y},')
|
|
||||||
else:
|
|
||||||
print(f'{extra_indent}# {depth}-bit RLE, generated from {fname}, '
|
|
||||||
f'{len(image)} bytes')
|
|
||||||
pixels = image[3:]
|
|
||||||
print(f'{extra_indent}{varname(fname)} = (')
|
|
||||||
print(f'{extra_indent} {image[0:1]}')
|
|
||||||
print(f'{extra_indent} {image[1:3]}')
|
|
||||||
|
|
||||||
# Split the bytestring to ensure each line is short enough to
|
|
||||||
# be absorbed on the target if needed.
|
|
||||||
for i in range(0, len(pixels), 16):
|
|
||||||
print(f'{extra_indent} {pixels[i:i+16]}')
|
|
||||||
print(f'{extra_indent})')
|
|
||||||
|
|
||||||
if args.ascii:
|
if args.ascii:
|
||||||
print()
|
print()
|
||||||
|
|
Loading…
Reference in a new issue