diff --git a/mspconvert.py b/mspconvert.py index 34a74a3..788ea41 100755 --- a/mspconvert.py +++ b/mspconvert.py @@ -6,15 +6,17 @@ # one at https://mozilla.org/MPL/2.0/. import sys +import re print("MS Paint file converter utility v0.1") print("Written by A.M. Rowsell, MPL Version 2.0 license\n") if len(sys.argv) < 2: - print("Please provide filename (without extension)!") - print("Example usage:\n./mspconvert.py DONUT") + print("Please provide filename!") + print("Example usage:\n./mspconvert.py DONUT.MSP") sys.exit(255) filename = sys.argv[1] +filename_ne = re.split(r"\.", filename)[0] width = 0 height = 0 # The output file follows the very simple XBM format @@ -29,19 +31,19 @@ static unsigned char {0}_bits [] = outputData = b'' try: - with open(filename + ".MSP", 'rb') as f: - versionString = f.read(4) - if versionString == b'\x4c\x69\x6e\x53': + with open(filename, 'rb') as f: + versionString = f.read(4) # check for the magic bytes + if versionString == b'\x4c\x69\x6e\x53': # this represents the string "LinS" version = 2 - elif versionString == b'\x44\x61\x6e\x4d': + elif versionString == b'\x44\x61\x6e\x4d': # this represents the string "DanM" version = 1 else: - print("The given file {0}.MSP is not a valid Microsoft Paint file!".format(filename)) - sys.exit(255) + print("The given file {0} is not a valid Microsoft Paint file!".format(filename)) + sys.exit(255) # exit with -1 if version == 2: print("Version 2 Paint file detected...") - width = int.from_bytes(f.read(2), "little") + Width = int.from_bytes(f.read(2), "little") height = int.from_bytes(f.read(2), "little") size = int((width * height) / 8) f.seek((height * 2) + 32) # seek to the start of image data @@ -52,7 +54,7 @@ try: for i in range(0,rllLen): outputData += rllValue size -= 1 - else: + else: # read the following number of bytes verbatim rllLen = int.from_bytes(byte, "little") for i in range(0,rllLen): outputData += f.read(1) @@ -61,7 +63,7 @@ try: for i in range(0, size): outputData += b'\xff' - with open(filename + "_converted.xbm", 'w') as f: + with open(filename_ne + "_converted.xbm", 'w') as f: print("Writing output file...") f.write(outputString.format(filename, width, height)) f.write(" {\n") @@ -93,17 +95,17 @@ try: q = 0 outputString += " };" - with open(filename + "_converted.xbm", 'w') as f: + with open(filename_ne + "_converted.xbm", 'w') as f: print("Writing output file...") f.write(outputString) print("Done!") sys.exit(0) except FileNotFoundError: - print("{0}.MSP does not exist! Quitting...".format(filename)) + print("{0} does not exist! Quitting...".format(filename)) sys.exit(255) except PermissionError: - print("Unable to open {0}.MSP -- insufficient permissions! Quitting...".format(filename)) + print("Unable to open {0} -- insufficient permissions! Quitting...".format(filename)) sys.exit(255) except Exception: print("Something went wrong! Quitting...")