99import texture2ddecoder
1010from PIL import Image
1111
12- from ..enums import BuildTarget , TextureFormat
12+ from ..enums import BuildTarget
13+ from ..enums import TextureFormat as TF
1314from ..helpers import TextureSwizzler
1415
1516if TYPE_CHECKING :
1617 from ..classes import Texture2D
1718
1819
19- TF = TextureFormat
20-
2120TEXTURE_FORMAT_BLOCK_SIZE_TABLE : Dict [TF , Optional [Tuple [int , int ]]] = {}
2221for tf in TF :
2322 if tf .name .startswith ("ASTC" ):
3231 TEXTURE_FORMAT_BLOCK_SIZE_TABLE [tf ] = block_size
3332
3433
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 ):
3635 block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE [texture_format ]
3736 if block_size is None :
3837 return (width , height )
@@ -88,7 +87,7 @@ def pad_image(img: Image.Image, pad_width: int, pad_height: int) -> Image.Image:
8887
8988
9089def compress_etcpak (
91- data : bytes , width : int , height : int , target_texture_format : TextureFormat
90+ data : bytes , width : int , height : int , target_texture_format : TF
9291) -> bytes :
9392 import etcpak
9493
@@ -115,7 +114,7 @@ def compress_etcpak(
115114
116115
117116def compress_astc (
118- data : bytes , width : int , height : int , target_texture_format : TextureFormat
117+ data : bytes , width : int , height : int , target_texture_format : TF
119118) -> bytes :
120119 astc_image = astc_encoder .ASTCImage (
121120 astc_encoder .ASTCType .U8 , width , height , 1 , data
@@ -139,24 +138,24 @@ def image_to_texture2d(
139138 platform : int = 0 ,
140139 platform_blob : Optional [bytes ] = None ,
141140 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 )
145144
146145 if flip :
147146 img = img .transpose (Image .FLIP_TOP_BOTTOM )
148147
149148 # defaults
150149 compress_func = None
151- tex_format = TF .RGBA32
150+ texture_format = TF .RGBA32
152151 pil_mode = "RGBA"
153152
154153 # DXT / BC
155154 if target_texture_format in [TF .DXT1 , TF .DXT1Crunched ]:
156- tex_format = TF .DXT1
155+ texture_format = TF .DXT1
157156 compress_func = compress_etcpak
158157 elif target_texture_format in [TF .DXT5 , TF .DXT5Crunched ]:
159- tex_format = TF .DXT5
158+ texture_format = TF .DXT5
160159 compress_func = compress_etcpak
161160 elif target_texture_format in [TF .BC4 , TF .BC5 , TF .BC7 ]:
162161 compress_func = compress_etcpak
@@ -166,28 +165,32 @@ def image_to_texture2d(
166165 block_size = TEXTURE_FORMAT_BLOCK_SIZE_TABLE [target_texture_format ]
167166 assert block_size is not None
168167 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+ )
170171 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+ )
172175 else :
173- tex_format = target_texture_format
176+ texture_format = target_texture_format
174177 compress_func = compress_astc
175178 # ETC
176179 elif target_texture_format in [TF .ETC_RGB4 , TF .ETC_RGB4Crunched , TF .ETC_RGB4_3DS ]:
177180 if target_texture_format == TF .ETC_RGB4_3DS :
178- tex_format = TF .ETC_RGB4_3DS
181+ texture_format = TF .ETC_RGB4_3DS
179182 else :
180- tex_format = target_texture_format
183+ texture_format = target_texture_format
181184 compress_func = compress_etcpak
182185 elif target_texture_format == TF .ETC2_RGB :
183- tex_format = TF .ETC2_RGB
186+ texture_format = TF .ETC2_RGB
184187 compress_func = compress_etcpak
185188 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
187190 compress_func = compress_etcpak
188191 # L
189192 elif target_texture_format == TF .Alpha8 :
190- tex_format = TF .Alpha8
193+ texture_format = TF .Alpha8
191194 pil_mode = "L"
192195 # R - should probably be merged into #L, as pure R is used as Alpha
193196 # but need test data for this first
@@ -199,7 +202,7 @@ def image_to_texture2d(
199202 TF .EAC_R ,
200203 TF .EAC_R_SIGNED ,
201204 ]:
202- tex_format = TF .R8
205+ texture_format = TF .R8
203206 pil_mode = "R"
204207 # RGBA
205208 elif target_texture_format in [
@@ -211,52 +214,50 @@ def image_to_texture2d(
211214 TF .PVRTC_RGB4 ,
212215 TF .ATC_RGB4 ,
213216 ]:
214- tex_format = TF .RGB24
217+ texture_format = TF .RGB24
215218 pil_mode = "RGB"
216219 # everything else defaulted to RGBA
217220
218221 switch_swizzle = None
219222 if platform == BuildTarget .Switch and platform_blob :
220223 gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
221224
222- if tex_format == TextureFormat .RGB24 :
223- tex_format = TextureFormat .RGBA32
225+ if texture_format == TF .RGB24 :
226+ texture_format = TF .RGBA32
224227 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
227230 pil_mode = "BGRA"
228231
229- block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (tex_format )
232+ block_size = TextureSwizzler .TEXTUREFORMAT_BLOCK_SIZE_MAP .get (texture_format )
230233 if not block_size :
231234 raise NotImplementedError (
232- f"Not implemented swizzle format: { tex_format .name } "
235+ f"Not implemented swizzle format: { texture_format .name } "
233236 )
234237
235238 width , height = TextureSwizzler .get_padded_texture_size (
236239 img .width , img .height , * block_size , gobs_per_block
237240 )
238241 switch_swizzle = (block_size , gobs_per_block )
239242 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 )
241244
242245 img = pad_image (img , width , height )
243246 if compress_func :
244247 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
246249 )
247250 else :
248251 enc_img = img .tobytes ("raw" , pil_mode )
249252
250253 if switch_swizzle is not None :
251254 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 )
255256
256- return enc_img , tex_format
257+ return enc_img , texture_format
257258
258259
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 :
260261 if img .mode == "RGB" :
261262 img = img .convert ("RGBA" )
262263 assert (
@@ -295,7 +296,7 @@ def parse_image_data(
295296 image_data : bytes ,
296297 width : int ,
297298 height : int ,
298- texture_format : Union [int , TextureFormat ],
299+ texture_format : Union [int , TF ],
299300 version : Tuple [int , int , int , int ],
300301 platform : int ,
301302 platform_blob : Optional [bytes ] = None ,
@@ -308,8 +309,8 @@ def parse_image_data(
308309 if not image_data :
309310 raise ValueError ("Texture2D has no image data" )
310311
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 )
313314
314315 if platform == BuildTarget .XBOX360 and texture_format in XBOX_SWAP_FORMATS :
315316 image_data = swap_bytes_for_xbox (image_data )
@@ -320,12 +321,12 @@ def parse_image_data(
320321 gobs_per_block = TextureSwizzler .get_switch_gobs_per_block (platform_blob )
321322
322323 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
327328 pil_mode = "BGRA"
328- elif texture_format == TextureFormat .Alpha8 :
329+ elif texture_format == TF .Alpha8 :
329330 texture_format = texture_format
330331 pil_mode = "L"
331332
@@ -355,11 +356,8 @@ def parse_image_data(
355356
356357 if switch_swizzle is not None :
357358 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+
363361
364362 conv_func = CONV_TABLE .get (texture_format )
365363 if not conv_func :
@@ -553,13 +551,17 @@ def rgb9e5float(image_data: bytes, width: int, height: int) -> Image.Image:
553551 return Image .frombytes ("RGB" , (width , height ), rgb , "raw" , "RGB" )
554552
555553
556- CONV_TABLE : Dict [TextureFormat , Callable [[bytes , int , int ], Image .Image ]] = {
554+ CONV_TABLE : Dict [TF , Callable [[bytes , int , int ], Image .Image ]] = {
557555 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+ ),
559559 TF .RGB24 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "RGB" ),
560560 TF .RGBA32 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "RGBA" ),
561561 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+ ),
563565 TF .RGB565 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "BGR;16" ),
564566 TF .BGR24 : lambda data , w , h : pillow (data , w , h , "RGB" , "raw" , "BGR" ),
565567 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:
568570 TF .DXT1 : lambda data , w , h : pillow (data , w , h , "RGBA" , "bcn" , 1 ),
569571 TF .DXT3 : lambda data , w , h : pillow (data , w , h , "RGBA" , "bcn" , 2 ),
570572 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+ ),
572576 TF .BGRA32 : lambda data , w , h : pillow (data , w , h , "RGBA" , "raw" , "BGRA" ),
573577 TF .RHalf : lambda data , w , h : half (data , w , h , "R" , "raw" , "R" ),
574578 TF .RGHalf : lambda data , w , h : rg (data , w , h , "RGB" , "raw" , "RGE" ),
0 commit comments