Skip to content

Commit d6bc436

Browse files
refactor(texture): texture loading is now blazingly fast
1 parent 5052e07 commit d6bc436

File tree

15 files changed

+96
-140
lines changed

15 files changed

+96
-140
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.0.1
3+
rev: v5.0.0
44
hooks:
55
- id: end-of-file-fixer
66
- repo: https://github.com/PyCQA/isort
7-
rev: 5.12.0
7+
rev: 6.0.1
88
hooks:
99
- id: isort
1010
args: ['--profile','black']
11-
- repo: https://github.com/psf/black
12-
rev: 22.12.0
13-
hooks:
14-
- id: black
15-
language_version: python3.11
1611
- repo: https://github.com/charliermarsh/ruff-pre-commit
17-
rev: 'v0.0.261'
12+
rev: v0.11.6
1813
hooks:
14+
# Run the linter.
1915
- id: ruff
20-
args: [--fix, --exit-non-zero-on-fix]
16+
args: [ --fix ]
17+
# Run the formatter.
18+
- id: ruff-format

xcoder/features/place_sprites.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def place_sprites(
7979
bbox = int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)
8080

8181
region_image = Image.open(
82-
f'{folder}{"/overwrite" if overwrite else ""}/{filename}'
82+
f"{folder}{'/overwrite' if overwrite else ''}/{filename}"
8383
).convert("RGBA")
8484

8585
sheets[region_info.texture_id].paste(

xcoder/features/sc/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ def compile_sc(
4242
file_size = width * height * pixel_size + 5
4343

4444
logger.info(
45-
locale.about_sc % (file_info.name, picture_index, pixel_type, width, height)
45+
locale.about_sc.format(
46+
filename=file_info.name,
47+
index=picture_index,
48+
pixel_type=pixel_type,
49+
width=width,
50+
height=height,
51+
)
4652
)
4753

4854
sc.write(struct.pack("<BIBHH", file_type, file_size, pixel_type, width, height))

xcoder/features/update/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_run_output(command: str):
2727

2828
def get_pip_info(outdated: bool = False) -> list:
2929
output = get_run_output(
30-
f'pip --disable-pip-version-check list {"-o" if outdated else ""}'
30+
f"pip --disable-pip-version-check list {'-o' if outdated else ''}"
3131
)
3232
output = output.splitlines()
3333
output = output[2:]

xcoder/images.py

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,61 @@
1111
from .localization import locale
1212
from .math.point import Point
1313
from .matrices import Matrix2x3
14-
from .pixel_utils import (
15-
get_channel_count_by_pixel_type,
16-
get_pixel_encode_function,
17-
get_read_function,
18-
)
14+
from .pixel_utils import get_pixel_encode_function, get_raw_mode
1915

2016
CHUNK_SIZE = 32
2117

2218

23-
def load_image_from_buffer(pixel_type: int, width: int, height: int) -> Image.Image:
24-
with open("pixel_buffer", "rb") as pixel_buffer:
25-
pixel_buffer.read(1)
26-
27-
return Image.frombuffer(
28-
get_format_by_pixel_type(pixel_type), (width, height), pixel_buffer.read()
29-
)
19+
def load_image_from_buffer(
20+
pixel_type: int, width: int, height: int, pixel_buffer: Reader
21+
) -> Image.Image:
22+
raw_mode = get_raw_mode(pixel_type)
23+
bytes_per_pixel = get_byte_count_by_pixel_type(pixel_type)
24+
25+
return Image.frombuffer(
26+
get_format_by_pixel_type(pixel_type),
27+
(width, height),
28+
pixel_buffer.read(width * height * bytes_per_pixel),
29+
"raw",
30+
raw_mode,
31+
0,
32+
1,
33+
)
3034

3135

32-
def join_image(pixel_type: int, width: int, height: int) -> Image.Image:
36+
def join_image(
37+
pixel_type: int, width: int, height: int, pixel_buffer: Reader
38+
) -> Image.Image:
3339
mode = get_format_by_pixel_type(pixel_type)
40+
bytes_per_pixel = get_byte_count_by_pixel_type(pixel_type)
3441
image = Image.new(mode, (width, height))
3542

36-
with open("pixel_buffer", "rb") as pixel_buffer:
37-
channel_count = int.from_bytes(pixel_buffer.read(1), "little")
43+
chunk_count_x = math.ceil(width / CHUNK_SIZE)
44+
chunk_count_y = math.ceil(height / CHUNK_SIZE)
45+
chunk_count = chunk_count_x * chunk_count_y
3846

39-
chunk_count_x = math.ceil(width / CHUNK_SIZE)
40-
chunk_count_y = math.ceil(height / CHUNK_SIZE)
41-
chunk_count = chunk_count_x * chunk_count_y
47+
raw_mode = get_raw_mode(pixel_type)
4248

43-
for chunk_index in range(chunk_count):
44-
chunk_x = chunk_index % chunk_count_x
45-
chunk_y = chunk_index // chunk_count_x
49+
for chunk_index in range(chunk_count):
50+
chunk_x = chunk_index % chunk_count_x
51+
chunk_y = chunk_index // chunk_count_x
4652

47-
chunk_width = min(width - chunk_x * CHUNK_SIZE, CHUNK_SIZE)
48-
chunk_height = min(height - chunk_y * CHUNK_SIZE, CHUNK_SIZE)
53+
chunk_width = min(width - chunk_x * CHUNK_SIZE, CHUNK_SIZE)
54+
chunk_height = min(height - chunk_y * CHUNK_SIZE, CHUNK_SIZE)
4955

50-
sub_image = Image.frombuffer(
51-
mode,
52-
(chunk_width, chunk_height),
53-
pixel_buffer.read(channel_count * chunk_width * chunk_height),
54-
)
56+
sub_image = Image.frombuffer(
57+
mode,
58+
(chunk_width, chunk_height),
59+
pixel_buffer.read(bytes_per_pixel * chunk_width * chunk_height),
60+
"raw",
61+
raw_mode,
62+
0,
63+
1,
64+
)
5565

56-
image.paste(sub_image, (chunk_x * CHUNK_SIZE, chunk_y * CHUNK_SIZE))
66+
image.paste(sub_image, (chunk_x * CHUNK_SIZE, chunk_y * CHUNK_SIZE))
5767

58-
Console.progress_bar(locale.join_pic, chunk_index, chunk_count)
68+
Console.progress_bar(locale.join_pic, chunk_index, chunk_count)
5969

6070
return image
6171

@@ -125,23 +135,6 @@ def get_format_by_pixel_type(pixel_type: int) -> str:
125135
raise Exception(locale.unknown_pixel_type % pixel_type)
126136

127137

128-
def load_texture(reader: Reader, pixel_type: int, width: int, height: int) -> None:
129-
channel_count = get_channel_count_by_pixel_type(pixel_type)
130-
read_pixel = get_read_function(pixel_type)
131-
if read_pixel is None:
132-
raise Exception(locale.unknown_pixel_type % pixel_type)
133-
134-
with open("pixel_buffer", "wb") as pixel_buffer:
135-
pixel_buffer.write(channel_count.to_bytes(1, "little"))
136-
137-
for y in range(height):
138-
pixel_buffer.write(
139-
b"".join([bytearray(read_pixel(reader)) for _ in range(width)])
140-
)
141-
142-
Console.progress_bar(locale.crt_pic, y, height)
143-
144-
145138
def save_texture(writer: Writer, image: Image.Image, pixel_type: int) -> None:
146139
encode_pixel = get_pixel_encode_function(pixel_type)
147140
if encode_pixel is None:

xcoder/languages/en-EU.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
"not_latest": "Not the latest version installed",
5656
"collecting_inf": "File: %s. Collecting information...",
57-
"about_sc": "About texture. Filename: %s (%d), Pixel type: %d, Size: %sx%s",
57+
"about_sc": "About texture. Filename: {filename} ({index}), Pixel type: {pixel_type}, Size: {width}x{height}",
5858
"decompression_error": "Error while decompressing! Trying to decode as is...",
5959
"skip_not_installed": "%s isn't installed! Reinitialize",
6060
"detected_comp": "%s compression detected",

xcoder/languages/ua-UA.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
"not_latest": "Встановлена не остання версія",
5656
"collecting_inf": "Файл: %s. Збираємо інформацію...",
57-
"about_sc": "Про текстуру. Назва файлу: %s (%d), Тип пікселів: %d, Величина: %sx%s",
57+
"about_sc": "Про текстуру. Назва файлу: {filename} ({index}), Тип пікселів: {pixel_type}, Величина: {width}x{height}",
5858
"decompression_error": "Помилка при розпакуванні! Намагаємся розпакувати як є...",
5959
"skip_not_installed": "%s не встановлений, повторіть налаштування",
6060
"detected_comp": "Виявлено компресію: %s",

xcoder/languages/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
"not_latest": "安装的不是最新版本",
5656
"collecting_inf": "文件:%s。正在收集信息...",
57-
"about_sc": "关于纹理。文件名:%s (%d),像素类型:%d,尺寸:%sx%s",
57+
"about_sc": "关于纹理。文件名:{filename} ({index}),像素类型:{pixel_type},尺寸:{width}x{height}",
5858
"decompression_error": "解压时出错!尝试直接解码...",
5959
"skip_not_installed": "%s 未安装!请重新初始化",
6060
"detected_comp": "检测到 %s 压缩",

xcoder/main_menu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def check_auto_update():
2323

2424
def check_files_updated():
2525
if config.has_update:
26-
logger.opt(colors=True).info(f'<green>{locale.update_done % ""}</green>')
26+
logger.opt(colors=True).info(f"<green>{locale.update_done % ''}</green>")
2727
if Console.question(locale.done_qu):
2828
latest_tag = get_tags(config.repo_owner, config.repo_name)[0]
2929
latest_tag_name = latest_tag["name"][1:]

xcoder/objects/plain_object.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99

1010
class PlainObject(ABC):
1111
@abstractmethod
12-
def load(self, swf: SupercellSWF, tag: int):
13-
...
12+
def load(self, swf: SupercellSWF, tag: int): ...

0 commit comments

Comments
 (0)