9
9
import texture2ddecoder
10
10
from PIL import Image
11
11
12
- from ..enums import BuildTarget , TextureFormat
12
+ from ..enums import BuildTarget
13
+ from ..enums import TextureFormat as TF
13
14
from ..helpers import TextureSwizzler
14
15
15
16
if TYPE_CHECKING :
16
17
from ..classes import Texture2D
17
18
18
19
19
- TF = TextureFormat
20
-
21
20
TEXTURE_FORMAT_BLOCK_SIZE_TABLE : Dict [TF , Optional [Tuple [int , int ]]] = {}
22
21
for tf in TF :
23
22
if tf .name .startswith ("ASTC" ):
32
31
TEXTURE_FORMAT_BLOCK_SIZE_TABLE [tf ] = block_size
33
32
34
33
35
- def get_compressed_image_size (width : int , height : int , texture_format : TextureFormat ):
34
+ def get_compressed_image_size (width : int , height : int , texture_format : TF ):
36
35
block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE [texture_format ]
37
36
if block_size is None :
38
37
return (width , height )
@@ -88,7 +87,7 @@ def pad_image(img: Image.Image, pad_width: int, pad_height: int) -> Image.Image:
88
87
89
88
90
89
def compress_etcpak (
91
- data : bytes , width : int , height : int , target_texture_format : TextureFormat
90
+ data : bytes , width : int , height : int , target_texture_format : TF
92
91
) -> bytes :
93
92
import etcpak
94
93
@@ -115,7 +114,7 @@ def compress_etcpak(
115
114
116
115
117
116
def compress_astc (
118
- data : bytes , width : int , height : int , target_texture_format : TextureFormat
117
+ data : bytes , width : int , height : int , target_texture_format : TF
119
118
) -> bytes :
120
119
astc_image = astc_encoder .ASTCImage (
121
120
astc_encoder .ASTCType .U8 , width , height , 1 , data
@@ -139,24 +138,24 @@ def image_to_texture2d(
139
138
platform : int = 0 ,
140
139
platform_blob : Optional [bytes ] = None ,
141
140
flip : bool = True ,
142
- ) -> Tuple [bytes , TextureFormat ]:
143
- if not isinstance (target_texture_format , TextureFormat ):
144
- target_texture_format = TextureFormat (target_texture_format )
141
+ ) -> Tuple [bytes , TF ]:
142
+ if not isinstance (target_texture_format , TF ):
143
+ target_texture_format = TF (target_texture_format )
145
144
146
145
if flip :
147
146
img = img .transpose (Image .FLIP_TOP_BOTTOM )
148
147
149
148
# defaults
150
149
compress_func = None
151
- tex_format = TF .RGBA32
150
+ texture_format = TF .RGBA32
152
151
pil_mode = "RGBA"
153
152
154
153
# DXT / BC
155
154
if target_texture_format in [TF .DXT1 , TF .DXT1Crunched ]:
156
- tex_format = TF .DXT1
155
+ texture_format = TF .DXT1
157
156
compress_func = compress_etcpak
158
157
elif target_texture_format in [TF .DXT5 , TF .DXT5Crunched ]:
159
- tex_format = TF .DXT5
158
+ texture_format = TF .DXT5
160
159
compress_func = compress_etcpak
161
160
elif target_texture_format in [TF .BC4 , TF .BC5 , TF .BC7 ]:
162
161
compress_func = compress_etcpak
@@ -166,28 +165,32 @@ def image_to_texture2d(
166
165
block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE [target_texture_format ]
167
166
assert block_size is not None
168
167
if img .mode == "RGB" :
169
- tex_format = getattr (TF , f"ASTC_RGB_{ block_size [0 ]} x{ block_size [1 ]} " )
168
+ texture_format = getattr (
169
+ TF , f"ASTC_RGB_{ block_size [0 ]} x{ block_size [1 ]} "
170
+ )
170
171
else :
171
- tex_format = getattr (TF , f"ASTC_RGBA_{ block_size [0 ]} x{ block_size [1 ]} " )
172
+ texture_format = getattr (
173
+ TF , f"ASTC_RGBA_{ block_size [0 ]} x{ block_size [1 ]} "
174
+ )
172
175
else :
173
- tex_format = target_texture_format
176
+ texture_format = target_texture_format
174
177
compress_func = compress_astc
175
178
# ETC
176
179
elif target_texture_format in [TF .ETC_RGB4 , TF .ETC_RGB4Crunched , TF .ETC_RGB4_3DS ]:
177
180
if target_texture_format == TF .ETC_RGB4_3DS :
178
- tex_format = TF .ETC_RGB4_3DS
181
+ texture_format = TF .ETC_RGB4_3DS
179
182
else :
180
- tex_format = target_texture_format
183
+ texture_format = target_texture_format
181
184
compress_func = compress_etcpak
182
185
elif target_texture_format == TF .ETC2_RGB :
183
- tex_format = TF .ETC2_RGB
186
+ texture_format = TF .ETC2_RGB
184
187
compress_func = compress_etcpak
185
188
elif target_texture_format in [TF .ETC2_RGBA8 , TF .ETC2_RGBA8Crunched , TF .ETC2_RGBA1 ]:
186
- tex_format = TF .ETC2_RGBA8
189
+ texture_format = TF .ETC2_RGBA8
187
190
compress_func = compress_etcpak
188
191
# L
189
192
elif target_texture_format == TF .Alpha8 :
190
- tex_format = TF .Alpha8
193
+ texture_format = TF .Alpha8
191
194
pil_mode = "L"
192
195
# R - should probably be merged into #L, as pure R is used as Alpha
193
196
# but need test data for this first
@@ -199,7 +202,7 @@ def image_to_texture2d(
199
202
TF .EAC_R ,
200
203
TF .EAC_R_SIGNED ,
201
204
]:
202
- tex_format = TF .R8
205
+ texture_format = TF .R8
203
206
pil_mode = "R"
204
207
# RGBA
205
208
elif target_texture_format in [
@@ -211,52 +214,50 @@ def image_to_texture2d(
211
214
TF .PVRTC_RGB4 ,
212
215
TF .ATC_RGB4 ,
213
216
]:
214
- tex_format = TF .RGB24
217
+ texture_format = TF .RGB24
215
218
pil_mode = "RGB"
216
219
# everything else defaulted to RGBA
217
220
218
221
switch_swizzle = None
219
222
if platform == BuildTarget .Switch and platform_blob :
220
223
gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
221
224
222
- if tex_format == TextureFormat .RGB24 :
223
- tex_format = TextureFormat .RGBA32
225
+ if texture_format == TF .RGB24 :
226
+ texture_format = TF .RGBA32
224
227
pil_mode = "RGBA"
225
- elif tex_format == TextureFormat .BGR24 :
226
- tex_format = TextureFormat .BGRA32
228
+ elif texture_format == TF .BGR24 :
229
+ texture_format = TF .BGRA32
227
230
pil_mode = "BGRA"
228
231
229
- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (tex_format )
232
+ block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
230
233
if not block_size :
231
234
raise NotImplementedError (
232
- f"Not implemented swizzle format: { tex_format .name } "
235
+ f"Not implemented swizzle format: { texture_format .name } "
233
236
)
234
237
235
238
width , height = TextureSwizzler .get_padded_texture_size (
236
239
img .width , img .height , * block_size , gobs_per_block
237
240
)
238
241
switch_swizzle = (block_size , gobs_per_block )
239
242
else :
240
- width , height = get_compressed_image_size (img .width , img .height , tex_format )
243
+ width , height = get_compressed_image_size (img .width , img .height , texture_format )
241
244
242
245
img = pad_image (img , width , height )
243
246
if compress_func :
244
247
enc_img = compress_func (
245
- img .tobytes ("raw" , pil_mode ), img .width , img .height , tex_format
248
+ img .tobytes ("raw" , pil_mode ), img .width , img .height , texture_format
246
249
)
247
250
else :
248
251
enc_img = img .tobytes ("raw" , pil_mode )
249
252
250
253
if switch_swizzle is not None :
251
254
block_size , gobs_per_block = switch_swizzle
252
- enc_img = bytes (
253
- TextureSwizzler .swizzle (enc_img , width , height , * block_size , gobs_per_block )
254
- )
255
+ enc_img = TextureSwizzler .swizzle (enc_img , width , height , * block_size , gobs_per_block )
255
256
256
- return enc_img , tex_format
257
+ return enc_img , texture_format
257
258
258
259
259
- def assert_rgba (img : Image .Image , target_texture_format : TextureFormat ) -> Image .Image :
260
+ def assert_rgba (img : Image .Image , target_texture_format : TF ) -> Image .Image :
260
261
if img .mode == "RGB" :
261
262
img = img .convert ("RGBA" )
262
263
assert (
@@ -295,7 +296,7 @@ def parse_image_data(
295
296
image_data : bytes ,
296
297
width : int ,
297
298
height : int ,
298
- texture_format : Union [int , TextureFormat ],
299
+ texture_format : Union [int , TF ],
299
300
version : Tuple [int , int , int , int ],
300
301
platform : int ,
301
302
platform_blob : Optional [bytes ] = None ,
@@ -308,8 +309,8 @@ def parse_image_data(
308
309
if not image_data :
309
310
raise ValueError ("Texture2D has no image data" )
310
311
311
- if not isinstance (texture_format , TextureFormat ):
312
- texture_format = TextureFormat (texture_format )
312
+ if not isinstance (texture_format , TF ):
313
+ texture_format = TF (texture_format )
313
314
314
315
if platform == BuildTarget .XBOX360 and texture_format in XBOX_SWAP_FORMATS :
315
316
image_data = swap_bytes_for_xbox (image_data )
@@ -320,12 +321,12 @@ def parse_image_data(
320
321
gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
321
322
322
323
pil_mode = "RGBA"
323
- if texture_format == TextureFormat .RGB24 :
324
- texture_format = TextureFormat .RGBA32
325
- elif texture_format == TextureFormat .BGR24 :
326
- texture_format = TextureFormat .BGRA32
324
+ if texture_format == TF .RGB24 :
325
+ texture_format = TF .RGBA32
326
+ elif texture_format == TF .BGR24 :
327
+ texture_format = TF .BGRA32
327
328
pil_mode = "BGRA"
328
- elif texture_format == TextureFormat .Alpha8 :
329
+ elif texture_format == TF .Alpha8 :
329
330
texture_format = texture_format
330
331
pil_mode = "L"
331
332
@@ -355,11 +356,8 @@ def parse_image_data(
355
356
356
357
if switch_swizzle is not None :
357
358
block_size , gobs_per_block , pil_mode = switch_swizzle
358
- image_data = bytes (
359
- TextureSwizzler .deswizzle (
360
- image_data , width , height , * block_size , gobs_per_block
361
- )
362
- )
359
+ image_data = TextureSwizzler .deswizzle (image_data , width , height , * block_size , gobs_per_block )
360
+
363
361
364
362
conv_func = CONV_TABLE .get (texture_format )
365
363
if not conv_func :
@@ -553,13 +551,17 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
553
551
return Image .frombytes ("RGB" , (width , height ), rgb , "raw" , "RGB" )
554
552
555
553
556
- CONV_TABLE : Dict [TextureFormat , Callable [[bytes , int , int ], Image .Image ]] = {
554
+ CONV_TABLE : Dict [TF , Callable [[bytes , int , int ], Image .Image ]] = {
557
555
TF .Alpha8 : lambda data , w , h : pillow (data , w , h , "L" , "raw" , "L" ),
558
- TF .ARGB4444 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "RGBA;4B" , (2 , 1 , 0 , 3 )),
556
+ TF .ARGB4444 : lambda data , w , h : pillow (
557
+ data , w , h , "RGBA" , "raw" , "RGBA;4B" , (2 , 1 , 0 , 3 )
558
+ ),
559
559
TF .RGB24 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "RGB" ),
560
560
TF .RGBA32 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "RGBA" ),
561
561
TF .ARGB32 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "ARGB" ),
562
- TF .ARGBFloat : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "RGBAF" , (2 , 1 , 0 , 3 )),
562
+ TF .ARGBFloat : lambda data , w , h : pillow (
563
+ data , w , h , "RGBA" , "raw" , "RGBAF" , (2 , 1 , 0 , 3 )
564
+ ),
563
565
TF .RGB565 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "BGR;16" ),
564
566
TF .BGR24 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "BGR" ),
565
567
TF .R8 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "R" ),
@@ -568,7 +570,9 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
568
570
TF .DXT1 : lambda data , w , h : pillow (data , w , h , "RGBA" , "bcn" , 1 ),
569
571
TF .DXT3 : lambda data , w , h : pillow (data , w , h , "RGBA" , "bcn" , 2 ),
570
572
TF .DXT5 : lambda data , w , h : pillow (data , w , h , "RGBA" , "bcn" , 3 ),
571
- TF .RGBA4444 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "RGBA;4B" , (3 , 2 , 1 , 0 )),
573
+ TF .RGBA4444 : lambda data , w , h : pillow (
574
+ data , w , h , "RGBA" , "raw" , "RGBA;4B" , (3 , 2 , 1 , 0 )
575
+ ),
572
576
TF .BGRA32 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "BGRA" ),
573
577
TF .RHalf : lambda data , w , h : half (data , w , h , "R" , "raw" , "R" ),
574
578
TF .RGHalf : lambda data , w , h : rg (data , w , h , "RGB" , "raw" , "RGE" ),
0 commit comments