Skip to content

Commit

Permalink
version 2.1, bugfix rle-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp committed Apr 6, 2022
1 parent 04adf7c commit 34817d0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
4 changes: 4 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
* Store data as inline-code instead of config-block
* Encode data as raw-bytes, rle-compressed or binary-file instead of config-block
* System to support decoders and viewers for multiple scripting languages

2022-03-31 Decca (Philipp)
* Version 2.1
* Fixed some bugs regarding the rle-encoding
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Windows:
Quickstart
==========

**TicMcTile** is straightforward to use, and its really easy to convert an image to TIC-80.
**TicMcTile** is straightforward to use and its really easy to convert an image to TIC-80.
Just run `ticmctile` using the provided example images:

$ ticmctile.py singlepage-16colors.png
Expand Down Expand Up @@ -152,14 +152,14 @@ Commandline options
-v, --version show version info
-h, --help show this help
The optional arguments are only needed if the default setting doesnt meet the
required needs. A specific name for the outputfile (-o/--output) can be set.
The optional arguments are only needed if the default setting does not meet the
required needs. A specific name for the output file (-o / --output) can be set.
The output can be in different scripting languages (-l / --language). Lua is
default, but the following languages are also supported: JavaScript, Squirrel,
Fennel, Wren and Moonscript. Dont expect too much, is just different formatting.
The data can be saved as sprites (-s / --sprites) instead of tiles.
Tiles/sprites can start on a different page (-p / --page) instead of 0.
Mode to encode (-m / --mode) the tiles/sprites as part of the code as raw, rle,
Mode (-m / --mode) to encode the tiles/sprites as part of the code as raw, rle,
as a binary-file (binary) or as part of the config, which is the default.
In the PRO version of TIC-80 there are up to 8 memory banks (-b / --bank)
to store the tiles/sprites, instead of only one. The colors of the image can
Expand Down
68 changes: 38 additions & 30 deletions ticmctile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# Hint: python -m pip install pillow (install PIL on Windows)
#
# last updated by Decca / RiFT on 04.11.2021 19:30
# last updated by Decca / RiFT on 31.03.2022 01:00
#

# import modules
Expand Down Expand Up @@ -35,14 +35,14 @@ def error(self, message):
" -v, --version show version info\n"
" -h, --help show this help\n"
"\n"
"The optional arguments are only needed if the default setting doesnt meet the\n"
"required needs. A specific name for the outputfile can be set (-o/--output).\n"
"The optional arguments are only needed if the default setting does not meet the\n"
"required needs. A specific name for the output file can be set (-o / --output).\n"
"The output can be in different scripting languages (-l / --language). Lua is\n"
"default, but the following languages are also supported: JavaScript, Squirrel,\n"
"Fennel, Wren and Moonscript. Dont expect too much, is just different formatting.\n"
"The data can be saved as sprites instead of tiles (-s / --sprites).\n"
"Tiles/sprites can start on a different page (-p / --page) instead of 0.\n"
"Mode to encode (-m / --mode) the tiles/sprites as part of the code as raw, rle,\n"
"Mode (-m / --mode) to encode the tiles/sprites as part of the code as raw, rle,\n"
"as a binary-file (binary) or as part of the config, which is the default.\n"
"In the PRO version of TIC-80 there are up to 8 memory banks (-b / --bank)\n"
"to store the tiles/sprites, instead of only one. The colors of the image can\n"
Expand Down Expand Up @@ -118,7 +118,7 @@ def error(self, message):
help='keep original image colors')
parser.add_argument('-v', '--version',
action='version',
version='%(prog)s 2.0')
version='%(prog)s 2.1')
args = parser.parse_args()


Expand Down Expand Up @@ -357,6 +357,34 @@ def check_file(fileName):
return


# encode data with Run-Length Encoding (only append number if value repeats more than twice)
def encode_rle(data):
enc = ""
prev = ""
count = 1
for symbol in data:
value = chr(int(symbol, 16)+65)
if value != prev:
if prev:
enc = enc + prev
if count == 2:
enc = enc + prev
elif count > 2:
enc = enc + str(count-1)
count = 1
prev = value
else:
count = count + 1
enc = enc + prev
if count == 2:
enc = enc + prev
elif count > 2:
enc = enc + str(count-1)
if not enc[-1].isdigit():
enc = enc + "0"
return enc


# write data as BINARY output file
def write_binary():
outputCode = ""
Expand Down Expand Up @@ -439,32 +467,12 @@ def write_rle(): # noqa C901
hexWidth = '%0*x' % (2, rowValues//8) # Width aka Values per row
outputCode = outputCode + str(hexWidth)
# serialize tile values
dat = ""
rawData = ""
for tile in tiles:
dat = dat + tiles[tile] # write content
# rle encode (only append number if value repeats more than twice)
enc = ""
prev = ""
count = 1
for symbol in dat:
value = chr(int(symbol, 16)+65)
if value != prev:
if prev:
enc = enc + prev
if count == 2:
enc = enc + prev
if count > 2:
enc = enc + str(count-1)
count = 1
prev = value
else:
count = count + 1
enc = enc + prev
if count == 2:
enc = enc + prev
elif count > 2:
enc = enc + str(count-1)
outputCode = outputCode + enc + '"\n'
rawData = rawData + tiles[tile] # join contents
# encode serialized values
encData = encode_rle(rawData)
outputCode = outputCode + encData + '"\n'
try:
with open(outputFile, 'w') as file:
file.write(outputCmnt + "title: " + str(imageFile) + "\n") # write header
Expand Down

0 comments on commit 34817d0

Please sign in to comment.