imgA-gif: image/gif (read + write, animation, transparency, loops, comments) and codecs/Quantize (median cut, octree, Floyd-Steinberg); ImageMagick / Pillow fixtures
This commit is contained in:
1 parent
ba91879bc5
commit
603689bc14
8 files changed
+1549
No files matched your search
@@ -0,0 +1,133 @@
|
||||
# GIF fixtures (tools/fixtures.py): GIFs written by ImageMagick and Pillow (static, interlaced, 2..256 colours, grey,
|
||||
# transparency, animations with every disposal method, sub-rectangle frames, local colour tables, loop counts,
|
||||
# comments), each with ImageMagick's decode of every frame as the reference.
|
||||
# tests/gen/gif.luau { { name, data, width, height, loops, frames = { { w, h, x, y, delay, dispose, crc } },
|
||||
# first = crc of the first frame on a transparent canvas, comment? } }
|
||||
# tests/gen/gif_bench.luau a 512 x 384 photo-like GIF (256 colours) and a 40-frame 128 x 128 animation
|
||||
# CRCs are of rgba8 with transparent pixels (alpha 0) zeroed to 0, 0, 0, 0. delay in centiseconds.
|
||||
import os, struct, tempfile, zlib
|
||||
|
||||
PILLOW = r'''
|
||||
import io, sys, random
|
||||
from PIL import Image, ImageDraw
|
||||
out = sys.argv[1]
|
||||
rng = random.Random(4)
|
||||
def frame(w, h, seed, alpha=False):
|
||||
im = Image.new("RGBA", (w, h), (0, 0, 0, 0) if alpha else (seed * 40 % 256, 90, 200, 255))
|
||||
d = ImageDraw.Draw(im)
|
||||
for i in range(5):
|
||||
x, y = rng.randrange(w), rng.randrange(h)
|
||||
d.ellipse([x - 6, y - 6, x + 6, y + 6], fill=(rng.randrange(256), rng.randrange(256), seed * 60 % 256, 255))
|
||||
return im
|
||||
# animation with per-frame disposal, durations, loop
|
||||
fr = [frame(48, 32, i) for i in range(4)]
|
||||
fr[0].save(out + "/pil_anim.gif", save_all=True, append_images=fr[1:], duration=[50, 120, 70, 200], loop=2, disposal=[0, 1, 2, 3], optimize=False)
|
||||
# transparent animation (sub-rectangles from Pillow's optimizer)
|
||||
fr = [frame(40, 40, i, alpha=True) for i in range(5)]
|
||||
fr[0].save(out + "/pil_alpha.gif", save_all=True, append_images=fr[1:], duration=80, loop=0, disposal=2)
|
||||
# a static interlaced palette image with a comment
|
||||
im = frame(61, 37, 9).convert("P", palette=Image.Palette.ADAPTIVE, colors=32)
|
||||
im.save(out + "/pil_interlaced.gif", interlace=True, comment=b"made by tools/fixtures/gif.py")
|
||||
# 1-bit and a 3-colour image (minimum code size 2)
|
||||
Image.frombytes("L", (17, 9), bytes((x * 255 // 16) if (x + y) % 3 else 0 for y in range(9) for x in range(17))).convert("1").save(out + "/pil_1bit.gif")
|
||||
im = Image.new("P", (23, 19)); im.putpalette([255, 0, 0, 0, 255, 0, 0, 0, 255])
|
||||
im.putdata([(x * y) % 3 for y in range(19) for x in range(23)]); im.save(out + "/pil_3col.gif")
|
||||
'''
|
||||
|
||||
|
||||
def run_im(ctx, args):
|
||||
return ctx.nix("imagemagick", ["magick"] + args)
|
||||
|
||||
|
||||
def reference(ctx, path):
|
||||
info = run_im(ctx, ["identify", "-format", "%w %h %X %Y %T %D %W %H %[comment]|", path]).decode("latin-1")
|
||||
raw = run_im(ctx, [path, "rgba:-"])
|
||||
frames, pos = [], 0
|
||||
comment = None
|
||||
for line in info.split("|"):
|
||||
if not line.strip():
|
||||
continue
|
||||
parts = line.split(" ", 8)
|
||||
w, h, x, y, delay = int(parts[0]), int(parts[1]), int(parts[2]), int(parts[3]), int(parts[4])
|
||||
dispose = parts[5]
|
||||
cw, ch = int(parts[6]), int(parts[7])
|
||||
if len(parts) > 8 and parts[8]:
|
||||
comment = parts[8]
|
||||
px = bytearray(raw[pos:pos + w * h * 4]); pos += w * h * 4
|
||||
for i in range(0, len(px), 4):
|
||||
if px[i + 3] == 0:
|
||||
px[i:i + 4] = b"\0\0\0\0"
|
||||
frames.append({"w": w, "h": h, "x": x, "y": y, "delay": delay, "dispose": dispose.lower(), "px": bytes(px), "crc": zlib.crc32(bytes(px))})
|
||||
assert pos == len(raw), (path, pos, len(raw))
|
||||
# the first frame on a transparent canvas
|
||||
f = frames[0]
|
||||
canvas = bytearray(cw * ch * 4)
|
||||
for yy in range(f["h"]):
|
||||
for xx in range(f["w"]):
|
||||
X, Y = f["x"] + xx, f["y"] + yy
|
||||
if 0 <= X < cw and 0 <= Y < ch:
|
||||
p = f["px"][(yy * f["w"] + xx) * 4:(yy * f["w"] + xx) * 4 + 4]
|
||||
if p[3]:
|
||||
canvas[(Y * cw + X) * 4:(Y * cw + X) * 4 + 4] = p
|
||||
for f in frames:
|
||||
del f["px"]
|
||||
return cw, ch, frames, zlib.crc32(bytes(canvas)), comment
|
||||
|
||||
|
||||
def loops_of(data):
|
||||
i = data.find(b"NETSCAPE2.0")
|
||||
if i < 0:
|
||||
return None
|
||||
return data[i + 13] | (data[i + 14] << 8)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
work = tempfile.mkdtemp(prefix="gif-fx-")
|
||||
files = []
|
||||
|
||||
def im(name, args):
|
||||
p = os.path.join(work, name + ".gif")
|
||||
run_im(ctx, args + [p])
|
||||
files.append((name, p))
|
||||
|
||||
im("rose", ["rose:"])
|
||||
im("rose_2col", ["rose:", "-colors", "2"])
|
||||
im("rose_16col_interlaced", ["rose:", "-colors", "16", "-interlace", "GIF"])
|
||||
im("rose_dither", ["rose:", "-resize", "200%", "-dither", "FloydSteinberg", "-colors", "64"])
|
||||
im("logo", ["logo:", "-resize", "30%"])
|
||||
im("gray", ["-size", "64x40", "gradient:", "-colorspace", "gray"])
|
||||
im("transparent", ["-size", "40x30", "xc:none", "-fill", "red", "-draw", "circle 20,15 20,5"])
|
||||
for disp in ("None", "Background", "Previous"):
|
||||
im("anim_" + disp.lower(), ["-delay", "7", "-dispose", disp, "-size", "30x20", "xc:red",
|
||||
"-size", "10x8", "-page", "+5+4", "xc:blue", "-page", "+12+9", "xc:lime",
|
||||
"-page", "+0+0", "-size", "6x6", "xc:yellow", "-loop", "3"])
|
||||
# local colour tables: frames with different 256-colour palettes
|
||||
im("anim_local", ["-delay", "20", "rose:", "(", "rose:", "-negate", ")", "(", "logo:", "-resize", "70x46!", ")", "-loop", "0"])
|
||||
# optimised transparency animation
|
||||
im("anim_optimized", ["-delay", "5", "-size", "50x50", "xc:white", "-fill", "black",
|
||||
"(", "+clone", "-draw", "circle 10,10 10,5", ")", "(", "+clone", "-draw", "circle 30,30 30,24", ")",
|
||||
"(", "+clone", "-draw", "circle 40,12 40,6", ")", "-layers", "OptimizeTransparency", "-loop", "0"])
|
||||
ctx.python(["pillow"], "import sys; sys.argv = ['', %r]\n" % work + PILLOW)
|
||||
for n in ("pil_anim", "pil_alpha", "pil_interlaced", "pil_1bit", "pil_3col"):
|
||||
files.append((n, os.path.join(work, n + ".gif")))
|
||||
cases = []
|
||||
for name, p in files:
|
||||
data = open(p, "rb").read()
|
||||
cw, ch, frames, first, comment = reference(ctx, p)
|
||||
c = {"name": name, "data": data, "width": cw, "height": ch, "frames": frames, "first": first}
|
||||
lp = loops_of(data)
|
||||
if lp is not None:
|
||||
c["loops"] = lp # the raw NETSCAPE2.0 repeat value
|
||||
if comment:
|
||||
c["comment"] = comment
|
||||
cases.append(c)
|
||||
ctx.emit("gif", cases)
|
||||
|
||||
bench = os.path.join(work, "bench.gif")
|
||||
run_im(ctx, ["-size", "512x384", "-seed", "3", "plasma:fractal", "-colors", "256", bench])
|
||||
anim = os.path.join(work, "bench_anim.gif")
|
||||
args = ["-delay", "4"]
|
||||
for i in range(40):
|
||||
args += ["(", "-size", "128x128", "radial-gradient:", "-roll", "+%d+%d" % (i * 3, i * 2), ")"]
|
||||
run_im(ctx, args + ["-colors", "128", "-loop", "0", anim])
|
||||
ctx.emit("gif_bench", {"still": open(bench, "rb").read(), "anim": open(anim, "rb").read()})
|
||||
Reference in new issue
Block a user