imgA-ops: ops/ImageOps (convert between every pixel format incl. yuv420p, crop, resize nearest/bilinear/Lanczos-3 as Pillow, flip, rotate90, rotate bilinear+expand as Pillow, blit/composite with Pillow's alpha_composite, premultiply, get/setPixel, fill, coalesce/fromFrames/frameCount/durations) with Pillow fixtures, tests on both runtimes, bench block
This commit is contained in:
1 parent
2eee0bc1a6
commit
e2b338db7e
5 files changed
+1624
No files matched your search
@@ -56,3 +56,32 @@ do
|
||||
Png.encode(img)
|
||||
end)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ ops/ImageOps
|
||||
do
|
||||
local Ops = require("@formats/ops/ImageOps")
|
||||
local img = assert(Png.decode(Bytes.reader(buffer.fromstring(PB[1].data))))
|
||||
local n = img.width * img.height * 4
|
||||
bench("imageops resize 1/2 bilinear", 2, n, function()
|
||||
Ops.resize(img, img.width // 2, img.height // 2, "bilinear")
|
||||
end)
|
||||
bench("imageops resize 1/2 lanczos", 2, n, function()
|
||||
Ops.resize(img, img.width // 2, img.height // 2, "lanczos")
|
||||
end)
|
||||
bench("imageops resize x1.5 bilinear", 1, n, function()
|
||||
Ops.resize(img, img.width * 3 // 2, img.height * 3 // 2, "bilinear")
|
||||
end)
|
||||
bench("imageops rotate 30 (expand)", 1, n, function()
|
||||
Ops.rotate(img, 30)
|
||||
end)
|
||||
bench("imageops rotate90", 3, n, function()
|
||||
Ops.rotate90(img, 1)
|
||||
end)
|
||||
bench("imageops convert rgba8 -> gray8", 3, n, function()
|
||||
Ops.convert(img, "gray8")
|
||||
end)
|
||||
local other = assert(Ops.flip(img, "h"))
|
||||
bench("imageops composite over", 3, n, function()
|
||||
Ops.composite(img, other)
|
||||
end)
|
||||
end
|
||||
@@ -0,0 +1,65 @@
|
||||
# ImageOps fixtures (tools/fixtures.py): Pillow references for ops/ImageOps.
|
||||
# tests/gen/imageops.luau { rgb = { w, h, px }, rgba = {...}, rgba2 = {...}, gray = L bytes of rgb,
|
||||
# resize = { { filter, mode, w, h, px } }, rotate = { { angle, expand, w, h, px } },
|
||||
# composite = bytes }
|
||||
# rgb / rgba: smooth gradients + noise + shapes (Pillow drawing), deterministic.
|
||||
import struct
|
||||
|
||||
CODE = r'''
|
||||
import random, struct, sys
|
||||
from PIL import Image, ImageDraw
|
||||
rng = random.Random(11)
|
||||
def src(w, h, mode):
|
||||
im = Image.new("RGBA", (w, h))
|
||||
px = im.load()
|
||||
for y in range(h):
|
||||
for x in range(w):
|
||||
px[x, y] = ((x * 255) // (w - 1), (y * 255) // (h - 1), (x * y) % 256, 255 if mode == "RGB" else (x * 7 + y * 3) % 256)
|
||||
d = ImageDraw.Draw(im)
|
||||
for _ in range(5):
|
||||
x0, y0 = rng.randrange(w), rng.randrange(h)
|
||||
d.ellipse([x0, y0, x0 + rng.randrange(4, 20), y0 + rng.randrange(4, 20)], fill=(rng.randrange(256), rng.randrange(256), rng.randrange(256), 255 if mode == "RGB" else rng.randrange(256)))
|
||||
return im.convert(mode)
|
||||
out = []
|
||||
def emit(tag, *parts):
|
||||
data = b"".join(parts)
|
||||
out.append(struct.pack("<16sI", tag.encode().ljust(16), len(data)) + data)
|
||||
rgb = src(61, 47, "RGB"); rgba = src(61, 47, "RGBA"); rgba2 = src(61, 47, "RGBA")
|
||||
hdr = lambda im: struct.pack("<II", *im.size)
|
||||
emit("rgb", hdr(rgb), rgb.tobytes()); emit("rgba", hdr(rgba), rgba.tobytes()); emit("rgba2", hdr(rgba2), rgba2.tobytes())
|
||||
emit("gray", rgb.convert("L").tobytes())
|
||||
emit("graya", rgba.convert("L").tobytes())
|
||||
F = {"nearest": Image.NEAREST, "bilinear": Image.BILINEAR, "lanczos": Image.LANCZOS}
|
||||
for fname, f in F.items():
|
||||
for (w, h) in ((30, 20), (100, 80), (61, 20), (13, 47), (7, 3), (200, 150)):
|
||||
for mode in ("RGB", "L"):
|
||||
r = rgb.convert(mode).resize((w, h), f)
|
||||
emit("resize", struct.pack("<16s4sII", fname.encode().ljust(16), mode.encode().ljust(4), w, h), r.tobytes())
|
||||
for angle, expand in ((30, True), (-45, True), (123.4, True), (17, False), (200, False)):
|
||||
r = rgba.rotate(angle, Image.BILINEAR, expand=expand)
|
||||
emit("rotate", struct.pack("<dBII", angle, 1 if expand else 0, *r.size), r.tobytes())
|
||||
emit("composite", Image.alpha_composite(rgba, rgba2).tobytes())
|
||||
sys.stdout.buffer.write(b"".join(out))
|
||||
'''
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
raw = ctx.python(["pillow"], CODE)
|
||||
p = 0
|
||||
fx = {"resize": [], "rotate": []}
|
||||
while p < len(raw):
|
||||
tag, n = struct.unpack("<16sI", raw[p:p + 20]); p += 20
|
||||
tag = tag.decode().strip()
|
||||
data = raw[p:p + n]; p += n
|
||||
if tag in ("rgb", "rgba", "rgba2"):
|
||||
w, h = struct.unpack("<II", data[:8])
|
||||
fx[tag] = {"w": w, "h": h, "px": data[8:]}
|
||||
elif tag in ("gray", "graya", "composite"):
|
||||
fx[tag] = data
|
||||
elif tag == "resize":
|
||||
f, mode, w, h = struct.unpack("<16s4sII", data[:28])
|
||||
fx["resize"].append({"filter": f.decode().strip(), "mode": mode.decode().strip(), "w": w, "h": h, "px": data[28:]})
|
||||
elif tag == "rotate":
|
||||
a, e, w, h = struct.unpack("<dBII", data[:17])
|
||||
fx["rotate"].append({"angle": a, "expand": bool(e), "w": w, "h": h, "px": data[17:]})
|
||||
ctx.emit("imageops", fx)
|
||||
Reference in new issue
Block a user