@@ -71,24 +71,24 @@ def image_to_texture2d(
71
71
# ASTC
72
72
elif target_texture_format .name .startswith ("ASTC" ):
73
73
raw_img = img .tobytes ("raw" , "RGBA" )
74
-
74
+ raw_img = astc_encoder .ASTCImage (
75
+ astc_encoder .ASTCType .U8 , img .width , img .height , 1 , raw_img
76
+ )
75
77
block_size = tuple (
76
78
map (int , target_texture_format .name .rsplit ("_" , 1 )[1 ].split ("x" ))
77
79
)
78
- context , lock = get_astc_context (block_size )
80
+ if img .mode == "RGB" :
81
+ tex_format = getattr (TF , f"ASTC_RGB_{ block_size [0 ]} x{ block_size [1 ]} " )
82
+ else :
83
+ tex_format = getattr (TF , f"ASTC_RGBA_{ block_size [0 ]} x{ block_size [1 ]} " )
84
+
85
+ swizzle = astc_encoder .ASTCSwizzle .from_str ("RGBA" )
79
86
87
+ context , lock = get_astc_context (block_size )
80
88
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" )
90
89
enc_img = context .compress (raw_img , swizzle )
91
- tex_format = target_texture_format
90
+
91
+ tex_format = target_texture_format
92
92
# A
93
93
elif target_texture_format == TF .Alpha8 :
94
94
enc_img = img .tobytes ("raw" , "A" )
@@ -252,13 +252,13 @@ def atc(image_data: bytes, width: int, height: int, alpha: bool) -> Image.Image:
252
252
253
253
254
254
def 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 } " )
256
259
260
+ context , lock = get_astc_context (block_size )
257
261
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 } " )
262
262
context .decompress (
263
263
image_data [:texture_size ], image , astc_encoder .ASTCSwizzle .from_str ("RGBA" )
264
264
)
0 commit comments