gen_img_tests.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, ImageOps
  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. ic = rgba.resize((32, 32))
  103. ic.save(os.path.join(OUT, "icon.ico"), sizes=[(32, 32)])
  104. files["icon.ico"] = (Image.open(os.path.join(OUT, "icon.ico")).convert("RGBA"), True)
  105. save(rgb, "rgb.ppm")
  106. save(gray, "gray.pgm")
  107. save(bw, "bw.pbm")
  108. with open(os.path.join(OUT, "ascii.ppm"), "w") as f:
  109. f.write("P3\n%d %d\n255\n" % (w, h))
  110. for y in range(h):
  111. for x in range(w):
  112. f.write("%d %d %d " % p[x, y])
  113. files["ascii.ppm"] = (rgb, False)
  114. with open(os.path.join(OUT, "rgba.pam"), "wb") as f:
  115. f.write(("P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\n"
  116. "TUPLTYPE RGB_ALPHA\nENDHDR\n" % (w, h)).encode())
  117. f.write(rgba.tobytes())
  118. files["rgba.pam"] = (rgba, True)
  119. gif_src = rgb.convert("P", palette=Image.ADAPTIVE, colors=256)
  120. save(gif_src, "plain.gif")
  121. save(gif_src, "inter.gif", interlace=True)
  122. gif_src.save(os.path.join(OUT, "trans.gif"), transparency=5)
  123. files["trans.gif"] = (Image.open(os.path.join(OUT, "trans.gif")).convert("RGBA"), True)
  124. # Our decoder shows the first frame; PIL's first frame matches this source.
  125. frames = [pal.copy()]
  126. p2 = pal.copy()
  127. for y in range(h):
  128. for x in range(w):
  129. p2.putpixel((x, y), (x + y + 3) % 16)
  130. frames.append(p2)
  131. pal.save(os.path.join(OUT, "anim.gif"), save_all=True,
  132. append_images=frames[1:], duration=100, loop=0, transparency=5)
  133. files["anim.gif"] = (Image.open(os.path.join(OUT, "anim.gif")).convert("RGBA"), True)
  134. jpegs = []
  135. for name, kw in [("j444.jpg", dict(quality=90, subsampling=0)),
  136. ("j422.jpg", dict(quality=90, subsampling=1)),
  137. ("j420.jpg", dict(quality=90, subsampling=2)),
  138. ("jgray.jpg", None)]:
  139. src = gray if kw is None else rgb
  140. src.save(os.path.join(OUT, name), **(kw or {"quality": 90}))
  141. ref = src.convert("RGB")
  142. ref.save(os.path.join(OUT, "ref", name + ".ppm"))
  143. jpegs.append(name)
  144. rgb.save(os.path.join(OUT, "jrst.jpg"), quality=90, restart_marker_blocks=2)
  145. rgb.save(os.path.join(OUT, "ref", "jrst.jpg.ppm"))
  146. jpegs.append("jrst.jpg")
  147. for name, src, kw in [
  148. ("prog.jpg", rgb, dict(subsampling=2)),
  149. ("prog444.jpg", rgb, dict(subsampling=0)),
  150. ("prog422.jpg", rgb, dict(subsampling=1)),
  151. ("proggray.jpg", gray, {}),
  152. ("progrst.jpg", rgb, dict(restart_marker_blocks=2))]:
  153. src.save(os.path.join(OUT, name), quality=90, progressive=True, **kw)
  154. Image.open(os.path.join(OUT, name)).convert("RGB").save(
  155. os.path.join(OUT, "ref", name + ".ppm"))
  156. jpegs.append(name)
  157. # EXIF orientation: the reference is PIL's exif_transpose of the same
  158. # JPEG, i.e. the upright image the viewer should show.
  159. for o in range(2, 9):
  160. ex = Image.Exif()
  161. ex[0x0112] = o
  162. name = "exif%d.jpg" % o
  163. rgb.save(os.path.join(OUT, name), quality=90, exif=ex)
  164. ImageOps.exif_transpose(Image.open(os.path.join(OUT, name))).convert("RGB").save(
  165. os.path.join(OUT, "ref", name + ".ppm"))
  166. jpegs.append(name)
  167. rgb.convert("CMYK").save(os.path.join(OUT, "cmyk.jpg"), quality=90)
  168. # inflate vectors
  169. raw = (b"the quick brown fox jumps over the lazy dog\n" * 500) + bytes(range(256)) * 80
  170. open(os.path.join(OUT, "inflate.raw"), "wb").write(raw)
  171. open(os.path.join(OUT, "inflate.z"), "wb").write(zlib.compress(raw, 9))
  172. open(os.path.join(OUT, "inflate.gz"), "wb").write(gzip.compress(raw, 9))
  173. exp = []
  174. for name, (im, alpha) in files.items():
  175. exp.append((name, im.width, im.height, fnv(im, alpha), 1 if alpha else 0))
  176. with open(os.path.join(OUT, "expected.h"), "w") as f:
  177. f.write("/* Generated by tools/gen_img_tests.py. */\n")
  178. f.write("static const struct img_exp {\n"
  179. "\tconst char *path;\n"
  180. "\tunsigned int w, h;\n"
  181. "\tunsigned int hash;\n"
  182. "\tint alpha;\n"
  183. "} img_exp[] = {\n")
  184. for name, ww, hh, hhsh, aa in sorted(exp):
  185. f.write("\t{ \"%s\", %d, %d, 0x%08xu, %d },\n" % (name, ww, hh, hhsh, aa))
  186. f.write("};\n")
  187. f.write("#define IMG_EXP_COUNT (sizeof(img_exp) / sizeof(img_exp[0]))\n")
  188. f.write("static const char *const img_jpeg[] = {\n")
  189. for name in jpegs:
  190. f.write("\t\"%s\",\n" % name)
  191. f.write("};\n")
  192. f.write("#define IMG_JPEG_COUNT (sizeof(img_jpeg) / sizeof(img_jpeg[0]))\n")
  193. if __name__ == "__main__":
  194. main()