convert_color: save mip-mapped image for mip levels
This commit is contained in:
parent
12e35fd2a0
commit
99856aa1b9
@ -84,32 +84,33 @@ def pack_indices(f, indices):
|
||||
f.write(struct.pack("<B", value))
|
||||
|
||||
def mip_levels(n):
|
||||
while True:
|
||||
n = n >> 1
|
||||
while n > 0:
|
||||
yield n
|
||||
if n == 1:
|
||||
break
|
||||
n = n >> 1
|
||||
|
||||
def resize(im, l):
|
||||
new = im.resize(
|
||||
size=(l, l),
|
||||
resample=Image.Resampling.LANCZOS,
|
||||
)
|
||||
new.save(f"{l:03}.png")
|
||||
return new
|
||||
|
||||
def generate_mips(im):
|
||||
w, h = im.size
|
||||
assert w == h and npot(w) == w, (w, h)
|
||||
assert w >= 8, (w, h)
|
||||
|
||||
images = [im] + [
|
||||
im.resize(
|
||||
size=(l, l),
|
||||
resample=Image.Resampling.LANCZOS,
|
||||
)
|
||||
for l in mip_levels(w)
|
||||
]
|
||||
images = [im] + [resize(im, l) for l in mip_levels(w >> 1)]
|
||||
|
||||
return list(reversed(images))
|
||||
|
||||
def write_mip(f, mip: Image, is_twiddled: bool, convert):
|
||||
width, height = mip.size
|
||||
print("mip", width, "offset", f"{f.tell():06x}")
|
||||
pixels = list(im.convert("RGBA").getdata())
|
||||
#print("mip", width, "offset", f"{f.tell():06x}")
|
||||
pixels = list(mip.convert("RGBA").getdata())
|
||||
colors = list(convert_colors(convert, pixels))
|
||||
assert len(colors) == width * height, len(colors)
|
||||
if is_twiddled:
|
||||
new_colors = [0] * width * height
|
||||
max_twiddle_ix = twiddle_texture(new_colors, colors, width, height)
|
||||
@ -121,7 +122,7 @@ def write_mips(f, im: Image, is_twiddled: bool, convert):
|
||||
for mip in generate_mips(im):
|
||||
write_mip(f, mip, is_twiddled, convert)
|
||||
|
||||
if __name__ == "__main__":
|
||||
def main():
|
||||
in_file = sys.argv[1]
|
||||
format = sys.argv[2]
|
||||
assert sys.argv[3] in {"twiddled", "non_twiddled"}
|
||||
@ -145,6 +146,7 @@ if __name__ == "__main__":
|
||||
else:
|
||||
write_mip(f, im, is_twiddled, convert)
|
||||
else:
|
||||
assert False
|
||||
pixels = list(im.convert("P").getdata())
|
||||
palette = list(im.palette.colors)
|
||||
indices = list(convert_indices(palette, pixels))
|
||||
@ -160,3 +162,6 @@ if __name__ == "__main__":
|
||||
|
||||
with open(out_file, 'wb') as f:
|
||||
pack_indices(f, indices)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user