gen_img_tests.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python3
  2. """Generate the image-library host test corpus (development only).
  3. The generated images and expected hashes are committed; `make test-images`
  4. does not need PIL. Run this script after changing the decoder or corpus.
  5. """
  6. import gzip
  7. import os
  8. import struct
  9. import zlib
  10. from PIL import Image, ImageDraw
  11. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  12. OUT = os.path.join(ROOT, "testdata", "images")
  13. def fnv(im, alpha=False):
  14. h = 2166136261
  15. for r, g, b, a in im.convert("RGBA").getdata():
  16. # RGB under a fully transparent pixel is not meaningful.
  17. if not alpha or a != 0:
  18. px = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
  19. h = ((h ^ (px & 255)) * 16777619) & 0xffffffff
  20. h = ((h ^ ((px >> 8) & 255)) * 16777619) & 0xffffffff
  21. if alpha:
  22. h = ((h ^ a) * 16777619) & 0xffffffff
  23. return h
  24. def main():
  25. os.makedirs(os.path.join(OUT, "ref"), exist_ok=True)
  26. w, h = 97, 61
  27. rgb = Image.new("RGB", (w, h))
  28. p = rgb.load()
  29. for y in range(h):
  30. for x in range(w):
  31. p[x, y] = ((x * 3 + y * 7) % 256, (x * 5) % 256, (y * 11) % 256)
  32. rgba = rgb.convert("RGBA")
  33. q = rgba.load()
  34. for y in range(h):
  35. for x in range(w):
  36. q[x, y] = (q[x, y][0], q[x, y][1], q[x, y][2], (x * 9) % 256)
  37. gray = rgb.convert("L")
  38. bw = rgb.convert("1")
  39. pal = Image.new("P", (w, h))
  40. pal.putpalette([i % 256 for i in range(768)])
  41. pp = pal.load()
  42. for y in range(h):
  43. for x in range(w):
  44. pp[x, y] = (x + y) % 16
  45. pal16 = pal.copy()
  46. pal_inter = pal.copy()
  47. rgb_inter = rgb.copy()
  48. rgba_inter = rgba.copy()
  49. gray_inter = gray.copy()
  50. files = {}
  51. def save(im, name, alpha=False, **kw):
  52. im.save(os.path.join(OUT, name), **kw)
  53. files[name] = (im, alpha)
  54. save(rgb, "rgb.png")
  55. save(rgba, "rgba.png", True)
  56. save(gray, "gray.png")
  57. save(bw, "bw.png")
  58. save(pal, "pal.png", True, transparency=5)
  59. files["pal.png"] = (Image.open(os.path.join(OUT, "pal.png")).convert("RGBA"), True)
  60. save(pal16, "pal4.png")
  61. save(rgb_inter, "rgb_inter.png", interlace=1)
  62. save(rgba_inter, "rgba_inter.png", True, interlace=1)
  63. save(gray_inter, "gray_inter.png", interlace=1)
  64. # 16-bit grayscale: values are multiples of 257, so high byte is the
  65. # 8-bit gradient. Expected hash comes from the 8-bit image.
  66. g16 = Image.new("I;16", (w, h))
  67. g8 = Image.new("L", (w, h))
  68. gg = g16.load()
  69. g8p = g8.load()
  70. for y in range(h):
  71. for x in range(w):
  72. v = (x * 3 + y * 7) % 256
  73. gg[x, y] = v * 257
  74. g8p[x, y] = v
  75. g16.save(os.path.join(OUT, "gray16.png"))
  76. files["gray16.png"] = (g8, False)
  77. save(rgb, "rgb.bmp")
  78. if os.path.exists(os.path.join(OUT, "rle8.bmp")):
  79. files["rle8.bmp"] = (Image.open(os.path.join(OUT, "rle8.bmp")).convert("RGB"), False)
  80. # Top-down BMP: reverse the stored rows and make the DIB height negative.
  81. src = open(os.path.join(OUT, "rgb.bmp"), "rb").read()
  82. off = struct.unpack_from("<I", src, 10)[0]
  83. bpp = struct.unpack_from("<H", src, 28)[0]
  84. hh = struct.unpack_from("<i", src, 22)[0]
  85. stride = ((w * bpp + 31) // 32) * 4
  86. out = bytearray(src[:off])
  87. for yy in range(hh - 1, -1, -1):
  88. out += src[off + yy * stride:off + (yy + 1) * stride]
  89. struct.pack_into("<i", out, 22, -hh)
  90. open(os.path.join(OUT, "topdown.bmp"), "wb").write(out)
  91. files["topdown.bmp"] = (rgb, False)
  92. save(bw, "bw.bmp")
  93. save(rgb.convert("P", palette=Image.ADAPTIVE, colors=256), "pal.bmp")
  94. rgb32 = rgba.copy()
  95. rgb32.save(os.path.join(OUT, "rgba.bmp"))
  96. files["rgba.bmp"] = (rgb32, False) # BI_RGB alpha is ignored
  97. save(rgb, "rgb.tga")
  98. rgb.save(os.path.join(OUT, "rle.tga"), compression="tga_rle")
  99. files["rle.tga"] = (rgb, False)
  100. rgba.save(os.path.join(OUT, "rgba.tga"))
  101. files["rgba.tga"] = (rgba, True)
  102. save(rgb, "rgb.ppm")
  103. save(gray, "gray.pgm")
  104. save(bw, "bw.pbm")
  105. with open(os.path.join(OUT, "ascii.ppm"), "w") as f:
  106. f.write("P3\n%d %d\n255\n" % (w, h))
  107. for y in range(h):
  108. for x in range(w):
  109. f.write("%d %d %d " % p[x, y])
  110. files["ascii.ppm"] = (rgb, False)
  111. gif_src = rgb.convert("P", palette=Image.ADAPTIVE, colors=256)
  112. save(gif_src, "plain.gif")
  113. save(gif_src, "inter.gif", interlace=True)
  114. gif_src.save(os.path.join(OUT, "trans.gif"), transparency=5)
  115. files["trans.gif"] = (Image.open(os.path.join(OUT, "trans.gif")).convert("RGBA"), True)
  116. # Our decoder shows the first frame; PIL's first frame matches this source.
  117. frames = [pal.copy()]
  118. p2 = pal.copy()
  119. for y in range(h):
  120. for x in range(w):
  121. p2.putpixel((x, y), (x + y + 3) % 16)
  122. frames.append(p2)
  123. pal.save(os.path.join(OUT, "anim.gif"), save_all=True,
  124. append_images=frames[1:], duration=100, loop=0, transparency=5)
  125. files["anim.gif"] = (Image.open(os.path.join(OUT, "anim.gif")).convert("RGBA"), True)
  126. jpegs = []
  127. for name, kw in [("j444.jpg", dict(quality=90, subsampling=0)),
  128. ("j422.jpg", dict(quality=90, subsampling=1)),
  129. ("j420.jpg", dict(quality=90, subsampling=2)),
  130. ("jgray.jpg", None)]:
  131. src = gray if kw is None else rgb
  132. src.save(os.path.join(OUT, name), **(kw or {"quality": 90}))
  133. ref = src.convert("RGB")
  134. ref.save(os.path.join(OUT, "ref", name + ".ppm"))
  135. jpegs.append(name)
  136. rgb.save(os.path.join(OUT, "jrst.jpg"), quality=90, restart_marker_blocks=2)
  137. rgb.save(os.path.join(OUT, "ref", "jrst.jpg.ppm"))
  138. jpegs.append("jrst.jpg")
  139. rgb.save(os.path.join(OUT, "prog.jpg"), quality=90, progressive=True)
  140. # inflate vectors
  141. raw = (b"the quick brown fox jumps over the lazy dog\n" * 500) + bytes(range(256)) * 80
  142. open(os.path.join(OUT, "inflate.raw"), "wb").write(raw)
  143. open(os.path.join(OUT, "inflate.z"), "wb").write(zlib.compress(raw, 9))
  144. open(os.path.join(OUT, "inflate.gz"), "wb").write(gzip.compress(raw, 9))
  145. exp = []
  146. for name, (im, alpha) in files.items():
  147. exp.append((name, im.width, im.height, fnv(im, alpha), 1 if alpha else 0))
  148. with open(os.path.join(OUT, "expected.h"), "w") as f:
  149. f.write("/* Generated by tools/gen_img_tests.py. */\n")
  150. f.write("static const struct img_exp {\n"
  151. "\tconst char *path;\n"
  152. "\tunsigned int w, h;\n"
  153. "\tunsigned int hash;\n"
  154. "\tint alpha;\n"
  155. "} img_exp[] = {\n")
  156. for name, ww, hh, hhsh, aa in sorted(exp):
  157. f.write("\t{ \"%s\", %d, %d, 0x%08xu, %d },\n" % (name, ww, hh, hhsh, aa))
  158. f.write("};\n")
  159. f.write("#define IMG_EXP_COUNT (sizeof(img_exp) / sizeof(img_exp[0]))\n")
  160. f.write("static const char *const img_jpeg[] = {\n")
  161. for name in jpegs:
  162. f.write("\t\"%s\",\n" % name)
  163. f.write("};\n")
  164. f.write("#define IMG_JPEG_COUNT (sizeof(img_jpeg) / sizeof(img_jpeg[0]))\n")
  165. if __name__ == "__main__":
  166. main()