@@ -71,24 +71,24 @@ def image_to_texture2d(
7171 # ASTC
7272 elif target_texture_format .name .startswith ("ASTC" ):
7373 raw_img = img .tobytes ("raw" , "RGBA" )
74+ raw_img = astc_encoder .ASTCImage (
75+ astc_encoder .ASTCType .U8 , img .width , img .height , 1 , raw_img
76+ )
77+ if img .mode == "RGB" :
78+ tex_format = getattr (TF , f"ASTC_RGB_{ block_size [0 ]} x{ block_size [1 ]} " )
79+ else :
80+ tex_format = getattr (TF , f"ASTC_RGBA_{ block_size [0 ]} x{ block_size [1 ]} " )
81+
82+ swizzle = astc_encoder .ASTCSwizzle .from_str ("RGBA" )
7483
7584 block_size = tuple (
7685 map (int , target_texture_format .name .rsplit ("_" , 1 )[1 ].split ("x" ))
7786 )
7887 context , lock = get_astc_context (block_size )
79-
8088 with lock :
81- raw_img = astc_encoder .ASTCImage (
82- astc_encoder .ASTCType .U8 , img .width , img .height , 1 , raw_img
83- )
84- if img .mode == "RGB" :
85- tex_format = getattr (TF , f"ASTC_RGB_{ block_size [0 ]} x{ block_size [1 ]} " )
86- else :
87- tex_format = getattr (TF , f"ASTC_RGBA_{ block_size [0 ]} x{ block_size [1 ]} " )
88-
89- swizzle = astc_encoder .ASTCSwizzle .from_str ("RGBA" )
9089 enc_img = context .compress (raw_img , swizzle )
91- tex_format = target_texture_format
90+
91+ tex_format = target_texture_format
9292 # A
9393 elif target_texture_format == TF .Alpha8 :
9494 enc_img = img .tobytes ("raw" , "A" )
@@ -252,13 +252,13 @@ def atc(image_data: bytes, width: int, height: int, alpha: bool) -> Image.Image:
252252
253253
254254def astc (image_data : bytes , width : int , height : int , block_size : tuple ) -> Image .Image :
255- context , lock = get_astc_context (block_size )
255+ image = astc_encoder .ASTCImage (astc_encoder .ASTCType .U8 , width , height , 1 )
256+ texture_size = calculate_astc_compressed_size (width , height , block_size )
257+ if len (image_data ) < texture_size :
258+ raise ValueError (f"Invalid ASTC data size: { len (image_data )} < { texture_size } " )
256259
260+ context , lock = get_astc_context (block_size )
257261 with lock :
258- image = astc_encoder .ASTCImage (astc_encoder .ASTCType .U8 , width , height , 1 )
259- texture_size = calculate_astc_compressed_size (width , height , block_size )
260- if len (image_data ) < texture_size :
261- raise ValueError (f"Invalid ASTC data size: { len (image_data )} < { texture_size } " )
262262 context .decompress (
263263 image_data [:texture_size ], image , astc_encoder .ASTCSwizzle .from_str ("RGBA" )
264264 )
0 commit comments