ws-webp: raw ALPH fixtures with every filter (horizontal / vertical / gradient), Config type fix

This commit is contained in:
4DBug committed 2026-10-31 09:09:35 -05:00
1 parent d19c949e95
commit 5832e65eef
2 files changed
+47 -1

No files matched your search

+46
View File
@@ -113,6 +113,50 @@ def riff(chunks):
return b"RIFF" + struct.pack("<I", len(body)) + body
def chunks_of(data):
out, p = [], 12
while p + 8 <= len(data):
fourcc, n = data[p:p + 4], struct.unpack_from("<I", data, p + 4)[0]
out.append((fourcc, data[p + 8:p + 8 + n]))
p += 8 + n + n % 2
return out
def filter_alpha(a, w, h, f):
out = bytearray(w * h)
for y in range(h):
for x in range(w):
i = y * w + x
if f == 0:
pred = 0
elif y == 0:
pred = a[i - 1] if x else 0
elif x == 0:
pred = a[i - w]
elif f == 1:
pred = a[i - 1]
elif f == 2:
pred = a[i - w]
else:
pred = max(0, min(255, a[i - 1] + a[i - w] - a[i - w - 1]))
out[i] = (a[i] - pred) & 255
return bytes(out)
def craft_alpha(tmp, base_alpha):
name, w, h, px = base_alpha
a = bytes(px[i] for i in range(3, len(px), 4))
src = dict(chunks_of(open(os.path.join(tmp, "rose_a_afnone.webp"), "rb").read()))
out = []
for f in range(4):
alph = bytes([f << 2]) + filter_alpha(a, w, h, f)
data = riff([(b"VP8X", src[b"VP8X"]), (b"ALPH", alph), (b"VP8 ", src[b"VP8 "])])
fname = "alph_raw_f%d.webp" % f
open(os.path.join(tmp, fname), "wb").write(data)
out.append((fname, {"lossy": True}))
return out
def generate(ctx):
tmp = tempfile.mkdtemp(prefix="webp_fx_")
try:
@@ -182,6 +226,8 @@ def _generate(ctx, tmp):
name = "%s_vpx%d.webp" % (n, k)
open(os.path.join(tmp, name), "wb").write(riff([(b"VP8 ", frame)]))
files.append((name, {"lossy": True}))
# raw ALPH chunks with each of the four filters, written here (rose_a's alpha over its lossy VP8 data)
files += craft_alpha(tmp, base_alpha=[s for s in sources if s[0] == "rose_a"][0])
# references
lines = []
for name, info in files: