6
6
if TYPE_CHECKING :
7
7
from PIL ._imaging import PixelAccess # type: ignore[reportPrivateImportUsage]
8
8
9
- from .bytestream import Reader , Writer
10
- from .console import Console
11
- from .localization import locale
12
- from .math .point import Point
13
- from .matrices import Matrix2x3
14
- from .pixel_utils import get_pixel_encode_function , get_raw_mode
9
+ from xcoder .bytestream import Reader , Writer
10
+ from xcoder .console import Console
11
+ from xcoder .localization import locale
12
+ from xcoder .math .point import Point
13
+ from xcoder .matrices import Matrix2x3
14
+ from xcoder .pixel_utils import get_pixel_encode_function , get_raw_mode
15
15
16
16
CHUNK_SIZE = 32
17
17
@@ -47,11 +47,11 @@ def join_image(
47
47
raw_mode = get_raw_mode (pixel_type )
48
48
49
49
for chunk_index in range (chunk_count ):
50
- chunk_x = chunk_index % chunk_count_x
51
- chunk_y = chunk_index // chunk_count_x
50
+ chunk_x = ( chunk_index % chunk_count_x ) * CHUNK_SIZE
51
+ chunk_y = ( chunk_index // chunk_count_x ) * CHUNK_SIZE
52
52
53
- chunk_width = min (width - chunk_x * CHUNK_SIZE , CHUNK_SIZE )
54
- chunk_height = min (height - chunk_y * CHUNK_SIZE , CHUNK_SIZE )
53
+ chunk_width = min (width - chunk_x , CHUNK_SIZE )
54
+ chunk_height = min (height - chunk_y , CHUNK_SIZE )
55
55
56
56
sub_image = Image .frombuffer (
57
57
mode ,
@@ -63,7 +63,7 @@ def join_image(
63
63
1 ,
64
64
)
65
65
66
- image .paste (sub_image , (chunk_x * CHUNK_SIZE , chunk_y * CHUNK_SIZE ))
66
+ image .paste (sub_image , (chunk_x , chunk_y ))
67
67
68
68
Console .progress_bar (locale .join_pic , chunk_index , chunk_count )
69
69
@@ -76,40 +76,33 @@ def _add_pixel(
76
76
image [pixel_index % width , int (pixel_index / width )] = color
77
77
78
78
79
- def split_image (img : Image .Image ) -> None :
80
- width , height = img .size
79
+ def split_image (image : Image .Image ) -> Image . Image :
80
+ width , height = image .size
81
81
82
- loaded_image = img . load ( )
83
- if loaded_image is None :
84
- raise Exception ( "loaded_image is None" )
82
+ chunk_count_x = math . ceil ( width / CHUNK_SIZE )
83
+ chunk_count_y = math . ceil ( height / CHUNK_SIZE )
84
+ chunk_count = chunk_count_x * chunk_count_y
85
85
86
- loaded_clone = img .copy ().load ()
87
- if loaded_clone is None :
88
- raise Exception ("loaded_clone is None" )
86
+ split_image_buffers = []
89
87
90
- x_chunks_count = width // CHUNK_SIZE
91
- y_chunks_count = height // CHUNK_SIZE
88
+ for chunk_index in range (chunk_count ):
89
+ chunk_x = (chunk_index % chunk_count_x ) * CHUNK_SIZE
90
+ chunk_y = (chunk_index // chunk_count_x ) * CHUNK_SIZE
92
91
93
- pixel_index = 0
92
+ chunk_width = min (width - chunk_x , CHUNK_SIZE )
93
+ chunk_height = min (height - chunk_y , CHUNK_SIZE )
94
94
95
- for y_chunk in range (y_chunks_count + 1 ):
96
- for x_chunk in range (x_chunks_count + 1 ):
97
- for y in range (CHUNK_SIZE ):
98
- pixel_y = (y_chunk * CHUNK_SIZE ) + y
99
- if pixel_y >= height :
100
- break
95
+ chunk = image .crop (
96
+ (chunk_x , chunk_y , chunk_x + chunk_width , chunk_y + chunk_height )
97
+ )
101
98
102
- for x in range (CHUNK_SIZE ):
103
- pixel_x = (x_chunk * CHUNK_SIZE ) + x
104
- if pixel_x >= width :
105
- break
99
+ split_image_buffers .append (chunk .tobytes ("raw" ))
106
100
107
- _add_pixel (
108
- loaded_image , pixel_index , width , loaded_clone [pixel_x , pixel_y ]
109
- )
110
- pixel_index += 1
101
+ Console .progress_bar (locale .split_pic , chunk_index , chunk_count )
111
102
112
- Console .progress_bar (locale .split_pic , y_chunk , y_chunks_count + 1 )
103
+ return Image .frombuffer (
104
+ image .mode , image .size , b"" .join (split_image_buffers ), "raw"
105
+ )
113
106
114
107
115
108
def get_byte_count_by_pixel_type (pixel_type : int ) -> int :
@@ -136,19 +129,28 @@ def get_format_by_pixel_type(pixel_type: int) -> str:
136
129
137
130
138
131
def save_texture (writer : Writer , image : Image .Image , pixel_type : int ) -> None :
132
+ raw_mode = get_raw_mode (pixel_type )
139
133
encode_pixel = get_pixel_encode_function (pixel_type )
140
- if encode_pixel is None :
141
- raise Exception (locale .unknown_pixel_type % pixel_type )
142
134
143
135
width , height = image .size
144
136
145
137
pixels = image .getdata ()
146
- for y in range (height ):
147
- for x in range (width ):
148
- # noinspection PyTypeChecker
149
- writer .write (encode_pixel (pixels [y * width + x ]))
150
138
151
- Console .progress_bar (locale .writing_pic , y , height )
139
+ # Some packers for raw_encoder are absent
140
+ # https://github.com/python-pillow/Pillow/blob/58e48745cc7b6c6f7dd26a50fe68d1a82ea51562/src/encode.c#L337
141
+ # https://github.com/python-pillow/Pillow/blob/main/src/libImaging/Pack.c#L668
142
+ if raw_mode != image .mode and encode_pixel is not None :
143
+ for y in range (height ):
144
+ for x in range (width ):
145
+ # noinspection PyTypeChecker
146
+ writer .write (encode_pixel (pixels [y * width + x ]))
147
+
148
+ Console .progress_bar (locale .writing_pic , y , height )
149
+
150
+ return
151
+
152
+ writer .write (image .tobytes ("raw" , raw_mode , 0 , 1 ))
153
+ Console .progress_bar (locale .writing_pic , height - 1 , height )
152
154
153
155
154
156
def transform_image (
0 commit comments