Skip to content

Commit e8a7353

Browse files
authored
Merge pull request #157 from mathoudebine/feature/turing-theme-extractor
Add turing-theme-extractor program to extract resources from Turing Windows app themes (.data files)
2 parents 46f2a13 + 7cf3745 commit e8a7353

8 files changed

+78
-0
lines changed
354 KB
Loading
123 KB
Loading
201 KB
Loading
164 KB
Loading
350 KB
Loading
235 KB
Loading
146 KB
Loading

tools/turing-theme-extractor.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# turing-smart-screen-python - a Python system monitor and library for 3.5" USB-C displays like Turing Smart Screen or XuanFang
2+
# https://github.com/mathoudebine/turing-smart-screen-python/
3+
4+
# Copyright (C) 2021-2023 Matthieu Houdebine (mathoudebine)
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
# turing-theme-extractor.py: Extract resources from a Turing Smart Screen theme (.data files) made for Windows app
20+
# This program will search and extract PNGs from the theme data and extract theme in the current directory
21+
# The PNG can then be re-used to create a theme for System Monitor python program (see Wiki for theme creation)
22+
import mmap
23+
import os
24+
import sys
25+
26+
PNG_SIGNATURE = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
27+
PNG_IEND = b'\x49\x45\x4E\x44\xAE\x42\x60\x82'
28+
29+
MIN_PYTHON = (3, 7)
30+
if sys.version_info < MIN_PYTHON:
31+
print("[ERROR] Python %s.%s or later is required." % MIN_PYTHON)
32+
try:
33+
sys.exit(0)
34+
except:
35+
os._exit(0)
36+
37+
if len(sys.argv) != 2:
38+
print("Usage :")
39+
print(" turing-theme-extractor.py path/to/theme-file.data")
40+
print("Examples : ")
41+
print(" turing-theme-extractor.py \"Dragon Ball.data\"")
42+
print(" turing-theme-extractor.py \"Pikachu theme.data\"")
43+
print(" turing-theme-extractor.py NZXT_BLUR.data")
44+
try:
45+
sys.exit(0)
46+
except:
47+
os._exit(0)
48+
49+
found_png = 0
50+
51+
with open(sys.argv[1], "r+b") as theme_file:
52+
mm = mmap.mmap(theme_file.fileno(), 0)
53+
54+
# Find PNG signature in binary data
55+
start_pos=0
56+
header_found = mm.find(PNG_SIGNATURE, 0)
57+
58+
while header_found != -1:
59+
print("\nFound PNG header at 0x%06x" % header_found)
60+
61+
# Find PNG IEND chunk (= end of file)
62+
iend_found = mm.find(PNG_IEND, header_found)
63+
print("Found PNG end-of-file at 0x%06x" % iend_found)
64+
65+
# Extract PNG data to a file
66+
theme_file.seek(header_found)
67+
png_file = open('theme_res_' + str(header_found) + '.png', 'wb')
68+
png_file.write(theme_file.read(iend_found - header_found + len(PNG_IEND)))
69+
png_file.close()
70+
71+
print("PNG extracted to theme_res_%s.png" % str(header_found))
72+
found_png = found_png + 1
73+
74+
# Find next PNG signature (if any)
75+
header_found = mm.find(PNG_SIGNATURE, iend_found)
76+
77+
print("\n%d PNG files extracted from theme to current directory" % found_png)
78+

0 commit comments

Comments
 (0)