|
11 | 11 | from .localization import locale |
12 | 12 | from .math.point import Point |
13 | 13 | 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 |
19 | 15 |
|
20 | 16 | CHUNK_SIZE = 32 |
21 | 17 |
|
22 | 18 |
|
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 | + ) |
30 | 34 |
|
31 | 35 |
|
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: |
33 | 39 | mode = get_format_by_pixel_type(pixel_type) |
| 40 | + bytes_per_pixel = get_byte_count_by_pixel_type(pixel_type) |
34 | 41 | image = Image.new(mode, (width, height)) |
35 | 42 |
|
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 |
38 | 46 |
|
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) |
42 | 48 |
|
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 |
46 | 52 |
|
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) |
49 | 55 |
|
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 | + ) |
55 | 65 |
|
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)) |
57 | 67 |
|
58 | | - Console.progress_bar(locale.join_pic, chunk_index, chunk_count) |
| 68 | + Console.progress_bar(locale.join_pic, chunk_index, chunk_count) |
59 | 69 |
|
60 | 70 | return image |
61 | 71 |
|
@@ -125,23 +135,6 @@ def get_format_by_pixel_type(pixel_type: int) -> str: |
125 | 135 | raise Exception(locale.unknown_pixel_type % pixel_type) |
126 | 136 |
|
127 | 137 |
|
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 | | - |
145 | 138 | def save_texture(writer: Writer, image: Image.Image, pixel_type: int) -> None: |
146 | 139 | encode_pixel = get_pixel_encode_function(pixel_type) |
147 | 140 | if encode_pixel is None: |
|
0 commit comments