rle16_compress_cpp_image_data.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python3
  2. #
  3. # Utility to compress Marlin RGB565 TFT data to RLE16 format.
  4. # Reads the existing Marlin RGB565 cpp file and generates a new file with the additional RLE16 data.
  5. #
  6. # Usage: rle16_compress_cpp_image_data.py INPUT_FILE.cpp OUTPUT_FILE.cpp
  7. #
  8. import sys, struct, re
  9. def addCompressedData(input_file, output_file):
  10. ofile = open(output_file, 'wt')
  11. c_data_section = False
  12. c_skip_data = False
  13. c_footer = False
  14. raw_data = []
  15. rle_value = []
  16. rle_count = []
  17. arrname = ''
  18. line = input_file.readline()
  19. while line:
  20. if not c_footer:
  21. if not c_skip_data: ofile.write(line)
  22. if "};" in line:
  23. c_skip_data = False
  24. c_data_section = False
  25. c_footer = True
  26. if c_data_section:
  27. cleaned = re.sub(r"\s|,|\n", "", line)
  28. as_list = cleaned.split("0x")
  29. as_list.pop(0)
  30. raw_data += [int(x, 16) for x in as_list]
  31. if "const uint" in line:
  32. # e.g.: const uint16_t marlin_logo_480x320x16[153600] = {
  33. if "_rle16" in line:
  34. c_skip_data = True
  35. else:
  36. c_data_section = True
  37. arrname = line.split('[')[0].split(' ')[-1]
  38. print("Found data array", arrname)
  39. line = input_file.readline()
  40. input_file.close()
  41. #
  42. # RLE16 (run length 16) encoding
  43. # Convert data from from raw RGB565 to a simple run-length-encoded format for each word of data.
  44. # - Each sequence begins with a count byte N.
  45. # - If the high bit is set in N the run contains N & 0x7F + 1 unique words.
  46. # - Otherwise it repeats the following word N + 1 times.
  47. # - Each RGB565 word is stored in MSB / LSB order.
  48. #
  49. def rle_encode(data):
  50. warn = "This may take a while" if len(data) > 300000 else ""
  51. print("Compressing image data...", warn)
  52. rledata = []
  53. distinct = []
  54. i = 0
  55. while i < len(data):
  56. v = data[i]
  57. i += 1
  58. rsize = 1
  59. for j in range(i, len(data)):
  60. if v != data[j]: break
  61. i += 1
  62. rsize += 1
  63. if rsize >= 128: break
  64. # If the run is one, add to the distinct values
  65. if rsize == 1: distinct.append(v)
  66. # If distinct length >= 127, or the repeat run is 2 or more,
  67. # store the distinct run.
  68. nr = len(distinct)
  69. if nr and (nr >= 128 or rsize > 1 or i >= len(data)):
  70. rledata += [(nr - 1) | 0x80] + distinct
  71. distinct = []
  72. # If the repeat run is 2 or more, store the repeat run.
  73. if rsize > 1: rledata += [rsize - 1, v]
  74. return rledata
  75. def append_byte(data, byte, cols=240):
  76. if data == '': data = ' '
  77. data += ('0x{0:02X}, '.format(byte)) # 6 characters
  78. if len(data) % (cols * 6 + 2) == 0: data = data.rstrip() + "\n "
  79. return data
  80. def rle_emit(ofile, arrname, rledata, rawsize):
  81. col = 0
  82. i = 0
  83. outstr = ''
  84. size = 0
  85. while i < len(rledata):
  86. rval = rledata[i]
  87. i += 1
  88. if rval & 0x80:
  89. count = (rval & 0x7F) + 1
  90. outstr = append_byte(outstr, rval)
  91. size += 1
  92. for j in range(count):
  93. outstr = append_byte(outstr, rledata[i + j] >> 8)
  94. outstr = append_byte(outstr, rledata[i + j] & 0xFF)
  95. size += 2
  96. i += count
  97. else:
  98. outstr = append_byte(outstr, rval)
  99. outstr = append_byte(outstr, rledata[i] >> 8)
  100. outstr = append_byte(outstr, rledata[i] & 0xFF)
  101. i += 1
  102. size += 3
  103. outstr = outstr.rstrip()[:-1]
  104. ofile.write("\n// Saves %i bytes\nconst uint8_t %s_rle16[%d] = {\n%s\n};\n" % (rawsize - size, arrname, size, outstr))
  105. (w, h, d) = arrname.split("_")[-1].split('x')
  106. ofile.write("\nconst tImage MarlinLogo{0}x{1}x16 = MARLIN_LOGO_CHOSEN({0}, {1});\n".format(w, h))
  107. ofile.write("\n#endif // HAS_GRAPHICAL_TFT && SHOW_BOOTSCREEN\n".format(w, h))
  108. # Encode the data, write it out, close the file
  109. rledata = rle_encode(raw_data)
  110. rle_emit(ofile, arrname, rledata, len(raw_data) * 2)
  111. ofile.close()
  112. if len(sys.argv) <= 2:
  113. print("Utility to compress Marlin RGB565 TFT data to RLE16 format.")
  114. print("Reads a Marlin RGB565 cpp file and generates a new file with the additional RLE16 data.")
  115. print("Usage: rle16_compress_cpp_image_data.py INPUT_FILE.cpp OUTPUT_FILE.cpp")
  116. exit(1)
  117. output_cpp = sys.argv[2]
  118. inname = sys.argv[1].replace('//', '/')
  119. input_cpp = open(inname)
  120. print("Processing", inname, "...")
  121. addCompressedData(input_cpp, output_cpp)