INNER CODE UNIT · Python
generate_mip_chain
untoldengine/UntoldEngine · scripts/texbake.py:239
def generate_mip_chain(img: "Image.Image") -> list["Image.Image"]:
"""
Return a list of PIL Images from mip 0 (full-res) down to mip N (1×1 or smallest).
Downsampling uses LANCZOS in the encoded gamma space, which is the standard
approach. For sRGB textures this introduces a small linearisation error at mip
boundaries; acceptable for production game content.
"""
# Ensure RGBA so the ASTC encoder always receives a 4-channel input.
img = img.convert("RGBA")
base_w, base_h = img.size
levels = mip_count_for(base_w, base_h)
mips: list[Image.Image] = [img]
for level in range(1, levels):
w = max(1, base_w >> level)
h = max(1, base_h >> level)
mips.append(mips[0].resize((w, h), Image.LANCZOS))
return mips