20
20
CHUNK_SIZE = 32
21
21
22
22
23
- def load_image_from_buffer (img : Image .Image ) -> None :
24
- width , height = img .size
25
- loaded_image = img .load ()
26
- if loaded_image is None :
27
- raise Exception ("loaded_image is None" )
28
-
23
+ def load_image_from_buffer (pixel_type : int , width : int , height : int ) -> Image .Image :
29
24
with open ("pixel_buffer" , "rb" ) as pixel_buffer :
30
- channel_count = int .from_bytes (pixel_buffer .read (1 ), "little" )
25
+ pixel_buffer .read (1 )
26
+
27
+ return Image .frombytes (
28
+ get_format_by_pixel_type (pixel_type ), (width , height ), pixel_buffer .read ()
29
+ )
31
30
32
- for y in range (height ):
33
- for x in range (width ):
34
- loaded_image [x , y ] = tuple (pixel_buffer .read (channel_count ))
35
31
32
+ def join_image (pixel_type : int , width : int , height : int ) -> Image .Image :
33
+ mode = get_format_by_pixel_type (pixel_type )
34
+ image = Image .new (mode , (width , height ))
36
35
37
- def join_image (img : Image .Image ) -> None :
38
36
with open ("pixel_buffer" , "rb" ) as pixel_buffer :
39
37
channel_count = int .from_bytes (pixel_buffer .read (1 ), "little" )
40
38
41
- width , height = img .size
42
- loaded_image = img .load ()
43
- if loaded_image is None :
44
- raise Exception ("loaded_image is None" )
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
45
42
46
- x_chunks_count = width // CHUNK_SIZE
47
- y_chunks_count = height // CHUNK_SIZE
43
+ for chunk_index in range (chunk_count ):
44
+ chunk_x = chunk_index % chunk_count_x
45
+ chunk_y = chunk_index // chunk_count_x
48
46
49
- for y_chunk in range (y_chunks_count + 1 ):
50
- for x_chunk in range (x_chunks_count + 1 ):
51
- for y in range (CHUNK_SIZE ):
52
- pixel_y = y_chunk * CHUNK_SIZE + y
53
- if pixel_y >= height :
54
- break
47
+ chunk_width = min (width - chunk_x * CHUNK_SIZE , CHUNK_SIZE )
48
+ chunk_height = min (height - chunk_y * CHUNK_SIZE , CHUNK_SIZE )
49
+
50
+ sub_image = Image .frombuffer (
51
+ mode ,
52
+ (chunk_width , chunk_height ),
53
+ pixel_buffer .read (channel_count * chunk_width * chunk_height ),
54
+ "raw" ,
55
+ )
55
56
56
- for x in range (CHUNK_SIZE ):
57
- pixel_x = x_chunk * CHUNK_SIZE + x
58
- if pixel_x >= width :
59
- break
57
+ image .paste (sub_image , (chunk_x * CHUNK_SIZE , chunk_y * CHUNK_SIZE ))
60
58
61
- loaded_image [pixel_x , pixel_y ] = tuple (
62
- pixel_buffer .read (channel_count )
63
- )
59
+ Console .progress_bar (locale .join_pic , chunk_index , chunk_count )
64
60
65
- Console . progress_bar ( locale . join_pic , y_chunk , y_chunks_count + 1 )
61
+ return image
66
62
67
63
68
64
def _add_pixel (
@@ -130,7 +126,7 @@ def get_format_by_pixel_type(pixel_type: int) -> str:
130
126
raise Exception (locale .unknown_pixel_type % pixel_type )
131
127
132
128
133
- def load_texture (reader : Reader , pixel_type : int , img : Image . Image ) -> None :
129
+ def load_texture (reader : Reader , pixel_type : int , width : int , height : int ) -> None :
134
130
channel_count = get_channel_count_by_pixel_type (pixel_type )
135
131
read_pixel = get_read_function (pixel_type )
136
132
if read_pixel is None :
@@ -139,18 +135,12 @@ def load_texture(reader: Reader, pixel_type: int, img: Image.Image) -> None:
139
135
with open ("pixel_buffer" , "wb" ) as pixel_buffer :
140
136
pixel_buffer .write (channel_count .to_bytes (1 , "little" ))
141
137
142
- width , height = img .size
143
- point = - 1
144
138
for y in range (height ):
145
139
for x in range (width ):
146
140
pixel = read_pixel (reader )
147
- for channel in pixel :
148
- pixel_buffer .write (channel .to_bytes (1 , "little" ))
141
+ pixel_buffer .write (bytearray (pixel ))
149
142
150
- curr = Console .percent (y , height )
151
- if curr > point :
152
- Console .progress_bar (locale .crt_pic , y , height )
153
- point = curr
143
+ Console .progress_bar (locale .crt_pic , y , height )
154
144
155
145
156
146
def save_texture (writer : Writer , image : Image .Image , pixel_type : int ) -> None :
@@ -161,16 +151,12 @@ def save_texture(writer: Writer, image: Image.Image, pixel_type: int) -> None:
161
151
width , height = image .size
162
152
163
153
pixels = image .getdata ()
164
- point = - 1
165
154
for y in range (height ):
166
155
for x in range (width ):
167
156
# noinspection PyTypeChecker
168
157
writer .write (encode_pixel (pixels [y * width + x ]))
169
158
170
- curr = Console .percent (y , height )
171
- if curr > point :
172
- Console .progress_bar (locale .writing_pic , y , height )
173
- point = curr
159
+ Console .progress_bar (locale .writing_pic , y , height )
174
160
175
161
176
162
def transform_image (
0 commit comments