"""mkimggolden.py -- goldens for tests/mm_test.cpp from Pillow alone (no numpy). Writes tests/imaging_golden.h: the FNV-1a of Image.resize(LANCZOS) over an RGBA picture at sizes that shrink, grow, keep one axis, keep both, and end at one pixel, and of the picture pasted on white with its alpha as the mask (core/mm/imaging.h promises both bit for bit). The picture is mm_test's splitmix hash of (frame, y, x, channel), so a stride or an axis swapped anywhere shows. Also writes the decoder fixtures tests/data/media/quad.{png,bmp,jpg,webp} and quad-alpha.png: 33 x 17 pixels, red top left, green top right, blue bottom left, white bottom right (the split is at x = 16 and y = 8), which a flipped row order, a mirrored row or a wrong stride cannot reproduce. BMP stores rows bottom up. quad-alpha.png carries the same colours with alpha rising across columns as round(255 * x / 32). Run from anywhere: python3 tests/mkimggolden.py """ import os import PIL from PIL import Image HERE = os.path.dirname(os.path.abspath(__file__)) M = (1 << 64) - 1 def pix(f, y, x, c): z = (f * 0x9E3779B97F4A7C15 + y * 0xBF58476D1CE4E5B9 + x * 0x94D049BB133111EB + c * 0x2545F4914F6CDD1D + 12345) & M z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & M z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & M return (z ^ (z >> 31)) & 255 def fnv(data): h = 0xcbf29ce484222325 for b in data: h = ((h ^ b) * 0x100000001b3) & M return h SRC_W, SRC_H = 37, 23 TARGETS = [(16, 9), (80, 50), (37, 11), (12, 23), (37, 23), (1, 1), (1, 23), (37, 1), (100, 7), (36, 22)] QUAD_W, QUAD_H = 33, 17 QUAD = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 255)] def quad_colour(y, x): return QUAD[(0 if y < 8 else 2) + (0 if x < 16 else 1)] def main(): src = Image.frombytes("RGBA", (SRC_W, SRC_H), bytes( pix(0, y, x, c) for y in range(SRC_H) for x in range(SRC_W) for c in range(4))) lines = ["/* imaging_golden.h -- GENERATED by mkimggolden.py from Pillow %s; do not edit. */" % PIL.__version__, "#pragma once", "#include ", "", "static const int32_t kGoldLanczosSrcW = %d, kGoldLanczosSrcH = %d;" % (SRC_W, SRC_H), "struct GoldLanczos { int32_t w, h; uint64_t fnv; };", "static const GoldLanczos kGoldLanczos[] = {"] for w, h in TARGETS: out = src.resize((w, h), Image.Resampling.LANCZOS) lines.append(" { %d, %d, 0x%016xull }," % (w, h, fnv(out.tobytes()))) lines.append("};") white = Image.new("RGB", src.size, (255, 255, 255)) white.paste(src, (0, 0), src.getchannel("A")) lines.append("static const uint64_t kGoldCompositeFnv = 0x%016xull;" % fnv(white.tobytes())) with open(os.path.join(HERE, "imaging_golden.h"), "w") as f: f.write("\n".join(lines) + "\n") media = os.path.join(HERE, "data", "media") q = Image.new("RGB", (QUAD_W, QUAD_H)) q.putdata([quad_colour(y, x) for y in range(QUAD_H) for x in range(QUAD_W)]) q.save(os.path.join(media, "quad.png")) q.save(os.path.join(media, "quad.bmp")) q.save(os.path.join(media, "quad.jpg"), quality=95, subsampling=0) q.save(os.path.join(media, "quad.webp"), lossless=True) qa = Image.new("RGBA", (QUAD_W, QUAD_H)) qa.putdata([quad_colour(y, x) + (round(255 * x / (QUAD_W - 1)),) for y in range(QUAD_H) for x in range(QUAD_W)]) qa.save(os.path.join(media, "quad-alpha.png")) if __name__ == "__main__": main()