[FL-2052] New build system based on scons (#1269)
This commit is contained in:
@@ -15,8 +15,13 @@ class Image:
|
||||
self.data = data
|
||||
|
||||
def write(self, filename):
|
||||
file = open(filename, "wb")
|
||||
file.write(self.data)
|
||||
with open(filename, "wb") as file:
|
||||
file.write(self.data)
|
||||
|
||||
def data_as_carray(self):
|
||||
return (
|
||||
"{" + "".join("0x{:02x},".format(img_byte) for img_byte in self.data) + "}"
|
||||
)
|
||||
|
||||
|
||||
def is_file_an_icon(filename):
|
||||
@@ -24,8 +29,62 @@ def is_file_an_icon(filename):
|
||||
return extension in ICONS_SUPPORTED_FORMATS
|
||||
|
||||
|
||||
class ImageTools:
|
||||
__pil_unavailable = False
|
||||
__hs2_unavailable = False
|
||||
|
||||
@staticmethod
|
||||
def is_processing_slow():
|
||||
try:
|
||||
from PIL import Image, ImageOps
|
||||
import heatshrink2
|
||||
|
||||
return False
|
||||
except ImportError as e:
|
||||
return True
|
||||
|
||||
def __init__(self):
|
||||
self.logger = logging.getLogger()
|
||||
|
||||
def png2xbm(self, file):
|
||||
if self.__pil_unavailable:
|
||||
return subprocess.check_output(["convert", file, "xbm:-"])
|
||||
|
||||
try:
|
||||
from PIL import Image, ImageOps
|
||||
except ImportError as e:
|
||||
self.__pil_unavailable = True
|
||||
self.logger.info("pillow module is missing, using convert cli util")
|
||||
return self.png2xbm(file)
|
||||
|
||||
with Image.open(file) as im:
|
||||
with io.BytesIO() as output:
|
||||
bw = im.convert("1")
|
||||
bw = ImageOps.invert(bw)
|
||||
bw.save(output, format="XBM")
|
||||
return output.getvalue()
|
||||
|
||||
def xbm2hs(self, data):
|
||||
if self.__hs2_unavailable:
|
||||
return subprocess.check_output(
|
||||
["heatshrink", "-e", "-w8", "-l4"], input=data
|
||||
)
|
||||
|
||||
try:
|
||||
import heatshrink2
|
||||
except ImportError as e:
|
||||
self.__hs2_unavailable = True
|
||||
self.logger.info("heatshrink2 module is missing, using heatshrink cli util")
|
||||
return self.xbm2hs(data)
|
||||
|
||||
return heatshrink2.compress(data, window_sz2=8, lookahead_sz2=4)
|
||||
|
||||
|
||||
__tools = ImageTools()
|
||||
|
||||
|
||||
def file2image(file):
|
||||
output = subprocess.check_output(["convert", file, "xbm:-"])
|
||||
output = __tools.png2xbm(file)
|
||||
assert output
|
||||
|
||||
# Extract data from text
|
||||
@@ -38,9 +97,7 @@ def file2image(file):
|
||||
data_bin = bytearray.fromhex(data_str)
|
||||
|
||||
# Encode icon data with LZSS
|
||||
data_encoded_str = subprocess.check_output(
|
||||
["heatshrink", "-e", "-w8", "-l4"], input=data_bin
|
||||
)
|
||||
data_encoded_str = __tools.xbm2hs(data_bin)
|
||||
|
||||
assert data_encoded_str
|
||||
|
||||
|
Reference in New Issue
Block a user