#!/usr/bin/env python3 """Generate the image-library host test corpus (development only). The generated images and expected hashes are committed; `make test-images` does not need PIL. Run this script after changing the decoder or corpus. """ import gzip import os import struct import zlib from PIL import Image, ImageDraw, ImageOps ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OUT = os.path.join(ROOT, "testdata", "images") def fnv(im, alpha=False): h = 2166136261 for r, g, b, a in im.convert("RGBA").getdata(): # RGB under a fully transparent pixel is not meaningful. if not alpha or a != 0: px = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) h = ((h ^ (px & 255)) * 16777619) & 0xffffffff h = ((h ^ ((px >> 8) & 255)) * 16777619) & 0xffffffff if alpha: h = ((h ^ a) * 16777619) & 0xffffffff return h def main(): os.makedirs(os.path.join(OUT, "ref"), exist_ok=True) w, h = 97, 61 rgb = Image.new("RGB", (w, h)) p = rgb.load() for y in range(h): for x in range(w): p[x, y] = ((x * 3 + y * 7) % 256, (x * 5) % 256, (y * 11) % 256) rgba = rgb.convert("RGBA") q = rgba.load() for y in range(h): for x in range(w): q[x, y] = (q[x, y][0], q[x, y][1], q[x, y][2], (x * 9) % 256) gray = rgb.convert("L") bw = rgb.convert("1") pal = Image.new("P", (w, h)) pal.putpalette([i % 256 for i in range(768)]) pp = pal.load() for y in range(h): for x in range(w): pp[x, y] = (x + y) % 16 pal16 = pal.copy() pal_inter = pal.copy() rgb_inter = rgb.copy() rgba_inter = rgba.copy() gray_inter = gray.copy() files = {} def save(im, name, alpha=False, **kw): im.save(os.path.join(OUT, name), **kw) files[name] = (im, alpha) save(rgb, "rgb.png") save(rgba, "rgba.png", True) save(gray, "gray.png") save(bw, "bw.png") save(pal, "pal.png", True, transparency=5) files["pal.png"] = (Image.open(os.path.join(OUT, "pal.png")).convert("RGBA"), True) save(pal16, "pal4.png") save(rgb_inter, "rgb_inter.png", interlace=1) save(rgba_inter, "rgba_inter.png", True, interlace=1) save(gray_inter, "gray_inter.png", interlace=1) # 16-bit grayscale: values are multiples of 257, so high byte is the # 8-bit gradient. Expected hash comes from the 8-bit image. g16 = Image.new("I;16", (w, h)) g8 = Image.new("L", (w, h)) gg = g16.load() g8p = g8.load() for y in range(h): for x in range(w): v = (x * 3 + y * 7) % 256 gg[x, y] = v * 257 g8p[x, y] = v g16.save(os.path.join(OUT, "gray16.png")) files["gray16.png"] = (g8, False) save(rgb, "rgb.bmp") if os.path.exists(os.path.join(OUT, "rle8.bmp")): files["rle8.bmp"] = (Image.open(os.path.join(OUT, "rle8.bmp")).convert("RGB"), False) # Top-down BMP: reverse the stored rows and make the DIB height negative. src = open(os.path.join(OUT, "rgb.bmp"), "rb").read() off = struct.unpack_from("