Skip to content

Commit 89ce3d8

Browse files
Optimizations. Pixel buffer file
1 parent 2f6957e commit 89ce3d8

File tree

4 files changed

+94
-56
lines changed

4 files changed

+94
-56
lines changed

.idea/xcoder.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

system/lib/features/sc/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from system.bytestream import Writer
88
from system.lib.console import Console
99
from system.lib.features.files import write_sc
10-
from system.lib.images import get_pixel_size, pixel_type2str, split_image, rgba2bytes
10+
from system.lib.images import get_pixel_size, split_image, rgba2bytes
1111
from system.localization import locale
1212

1313

@@ -65,13 +65,6 @@ def compile_sc(_dir, from_memory=None, img_data=None, folder_export=None):
6565
width, height = img.size
6666
pixel_size = get_pixel_size(pixel_type)
6767

68-
img = img.convert('RGBA')
69-
x = Image.new('RGBA', img.size, (0, 0, 0, 1))
70-
x.paste(img, (0, 0), img)
71-
img = x
72-
73-
img = img.convert(pixel_type2str(pixel_type))
74-
7568
file_size = width * height * pixel_size + 5
7669

7770
logger.info(locale.about_sc % (name, picture_index, pixel_type, width, height))

system/lib/images.py

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,97 @@
88
from system.localization import locale
99

1010

11-
def join_image(img, pixels):
11+
def load_image_from_buffer(img):
1212
width, height = img.size
13-
loaded_img = img.load()
14-
pixel_index = 0
15-
chunk_size = 32
13+
img_loaded = img.load()
1614

17-
x_chunks_count = width // chunk_size
18-
y_chunks_count = height // chunk_size
19-
x_rest = width % chunk_size
20-
y_rest = height % chunk_size
15+
with open('pixel_buffer', 'rb') as pixel_buffer:
16+
channels_count = int.from_bytes(pixel_buffer.read(1), 'little')
17+
print(channels_count)
18+
19+
for y in range(height):
20+
for x in range(width):
21+
img_loaded[x, y] = tuple(pixel_buffer.read(channels_count))
22+
23+
24+
def join_image(img):
25+
with open('pixel_buffer', 'rb') as pixel_buffer:
26+
channels_count = int.from_bytes(pixel_buffer.read(1), 'little')
27+
28+
width, height = img.size
29+
loaded_img = img.load()
30+
chunk_size = 32
31+
32+
x_chunks_count = width // chunk_size
33+
y_chunks_count = height // chunk_size
34+
x_rest = width % chunk_size
35+
y_rest = height % chunk_size
36+
37+
for y_chunk in range(y_chunks_count):
38+
for x_chunk in range(x_chunks_count):
39+
for y in range(chunk_size):
40+
for x in range(chunk_size):
41+
loaded_img[x_chunk * chunk_size + x, y_chunk * chunk_size + y] = tuple(
42+
pixel_buffer.read(channels_count)
43+
)
2144

22-
for y_chunk in range(y_chunks_count):
23-
for x_chunk in range(x_chunks_count):
2445
for y in range(chunk_size):
46+
for x in range(x_rest):
47+
loaded_img[(width - x_rest) + x, y_chunk * chunk_size + y] = tuple(
48+
pixel_buffer.read(channels_count)
49+
)
50+
Console.progress_bar(locale.join_pic, y_chunk, y_chunks_count)
51+
for x_chunk in range(x_chunks_count):
52+
for y in range(y_rest):
2553
for x in range(chunk_size):
26-
loaded_img[x_chunk * chunk_size + x, y_chunk * chunk_size + y] = pixels[pixel_index]
27-
pixel_index += 1
54+
loaded_img[x_chunk * chunk_size + x, (height - y_rest) + y] = tuple(
55+
pixel_buffer.read(channels_count)
56+
)
2857

29-
for y in range(chunk_size):
30-
for x in range(x_rest):
31-
loaded_img[(width - x_rest) + x, y_chunk * chunk_size + y] = pixels[pixel_index]
32-
pixel_index += 1
33-
Console.progress_bar(locale.join_pic, y_chunk, y_chunks_count)
34-
for x_chunk in range(x_chunks_count):
3558
for y in range(y_rest):
36-
for x in range(chunk_size):
37-
loaded_img[x_chunk * chunk_size + x, (height - y_rest) + y] = pixels[pixel_index]
38-
pixel_index += 1
59+
for x in range(x_rest):
60+
loaded_img[x + (width - x_rest), y + (height - y_rest)] = tuple(pixel_buffer.read(channels_count))
3961

40-
for y in range(y_rest):
41-
for x in range(x_rest):
42-
loaded_img[x + (width - x_rest), y + (height - y_rest)] = pixels[pixel_index]
43-
pixel_index += 1
4462

63+
def split_image(img: Image):
64+
def add_pixel(pixel: tuple):
65+
loaded_image[pixel_index % width, int(pixel_index / width)] = pixel
4566

46-
def split_image(img):
47-
pixels = []
4867
width, height = img.size
49-
loaded_img = img.load()
68+
loaded_image = img.load()
69+
loaded_clone = img.copy().load()
5070
chunk_size = 32
5171

5272
x_chunks_count = width // chunk_size
5373
y_chunks_count = height // chunk_size
5474
x_rest = width % chunk_size
5575
y_rest = height % chunk_size
5676

77+
pixel_index = 0
78+
5779
for y_chunk in range(y_chunks_count):
5880
for x_chunk in range(x_chunks_count):
5981
for y in range(chunk_size):
6082
for x in range(chunk_size):
61-
pixels.append(loaded_img[x + (x_chunk * chunk_size), y + (y_chunk * chunk_size)])
83+
add_pixel(loaded_clone[x + (x_chunk * chunk_size), y + (y_chunk * chunk_size)])
84+
pixel_index += 1
6285

6386
for y in range(chunk_size):
6487
for x in range(x_rest):
65-
pixels.append(loaded_img[x + (width - x_rest), y + (y_chunk * chunk_size)])
88+
add_pixel(loaded_clone[x + (width - x_rest), y + (y_chunk * chunk_size)])
89+
pixel_index += 1
6690
Console.progress_bar(locale.split_pic, y_chunk, y_chunks_count)
6791

6892
for x_chunk in range(width // chunk_size):
6993
for y in range(y_rest):
7094
for x in range(chunk_size):
71-
pixels.append(loaded_img[x + (x_chunk * chunk_size), y + (height - y_rest)])
95+
add_pixel(loaded_clone[x + (x_chunk * chunk_size), y + (height - y_rest)])
96+
pixel_index += 1
7297

7398
for y in range(y_rest):
7499
for x in range(x_rest):
75-
pixels.append(loaded_img[x + (width - x_rest), y + (height - y_rest)])
76-
img.putdata(pixels)
100+
add_pixel(loaded_clone[x + (width - x_rest), y + (height - y_rest)])
101+
pixel_index += 1
77102

78103

79104
def get_pixel_size(_type):
@@ -89,17 +114,19 @@ def get_pixel_size(_type):
89114
def pixel_type2str(_type):
90115
if _type in range(4):
91116
return 'RGBA'
92-
if _type in (4,):
117+
elif _type == 4:
93118
return 'RGB'
94-
if _type in (6,):
119+
elif _type == 6:
95120
return 'LA'
96-
if _type in (10,):
121+
elif _type == 10:
97122
return 'L'
123+
98124
raise Exception(locale.unk_type % _type)
99125

100126

101-
def bytes2rgba(data: Reader, _type, img, pix):
127+
def bytes2rgba(data: Reader, _type, img):
102128
read_pixel = None
129+
channels_count = 4
103130
if _type in (0, 1):
104131
def read_pixel():
105132
return data.read_ubyte(), data.read_ubyte(), data.read_ubyte(), data.read_ubyte()
@@ -112,30 +139,41 @@ def read_pixel():
112139
p = data.read_uint16()
113140
return (p >> 11 & 31) << 3, (p >> 6 & 31) << 3, (p >> 1 & 31) << 3, (p & 255) << 7
114141
elif _type == 4:
142+
channels_count = 3
143+
115144
def read_pixel():
116145
p = data.read_uint16()
117146
return (p >> 11 & 31) << 3, (p >> 5 & 63) << 2, (p & 31) << 3
118147
elif _type == 6:
148+
channels_count = 2
149+
119150
def read_pixel():
120151
return (data.read_ubyte(), data.read_ubyte())[::-1]
121152
elif _type == 10:
153+
channels_count = 1
154+
122155
def read_pixel():
123156
return data.read_ubyte()
124157

125-
if read_pixel is not None:
158+
if read_pixel is None:
159+
return
160+
161+
with open('pixel_buffer', 'wb') as pixel_buffer:
162+
pixel_buffer.write(channels_count.to_bytes(1, 'little'))
163+
126164
width, height = img.size
127165
point = -1
128166
for y in range(height):
129167
for x in range(width):
130-
pix.append(read_pixel())
168+
pixel = read_pixel()
169+
for channel in pixel:
170+
pixel_buffer.write(channel.to_bytes(1, 'little'))
131171

132172
curr = Console.percent(y, height)
133173
if curr > point:
134174
Console.progress_bar(locale.crt_pic, y, height)
135175
point = curr
136-
print()
137-
138-
img.putdata(pix)
176+
print()
139177

140178

141179
def rgba2bytes(sc, img, _type):

system/lib/objects/texture.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import os
2+
13
from PIL import Image
24

3-
from system.lib.images import pixel_type2str, bytes2rgba, join_image
5+
from system.lib.images import pixel_type2str, bytes2rgba, join_image, load_image_from_buffer
46

57

68
class SWFTexture:
@@ -13,17 +15,22 @@ def __init__(self):
1315
self.image = None
1416

1517
def load(self, swf, tag: int, has_texture: bool):
16-
self.pixel_type = swf.reader.read_byte() # pixel_type
18+
self.pixel_type = swf.reader.read_byte()
1719
self.width, self.height = (swf.reader.read_uint16(), swf.reader.read_uint16())
1820

1921
if has_texture:
2022
img = Image.new(pixel_type2str(self.pixel_type), (self.width, self.height))
21-
pixels = []
2223

23-
bytes2rgba(swf.reader, self.pixel_type, img, pixels)
24+
bytes2rgba(swf.reader, self.pixel_type, img)
25+
26+
print(pixel_type2str(self.pixel_type))
2427

2528
if tag in (27, 28):
26-
join_image(img, pixels)
29+
join_image(img)
2730
print()
31+
else:
32+
load_image_from_buffer(img)
33+
34+
os.remove('pixel_buffer')
2835

2936
self.image = img

0 commit comments

Comments
 (0)