| 1 | """Writes imagefile's fixtures (IMG5): each format, carrying canaries in |
| 2 | every kind of metadata the walkers must drop. Run from anywhere: |
| 3 | |
| 4 | python3 -I internal/mapa/imagefile/testdata/make.py |
| 5 | |
| 6 | It needs Pillow with WebP. The outputs are committed; this script is |
| 7 | their provenance. Every canary starts CANARY- so a test can find any. |
| 8 | """ |
| 9 | import io |
| 10 | import os |
| 11 | import struct |
| 12 | import sys |
| 13 | import zlib |
| 14 | |
| 15 | from PIL import Image, ImageCms, PngImagePlugin |
| 16 | |
| 17 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 18 | ICC = ImageCms.ImageCmsProfile(ImageCms.createProfile("sRGB")).tobytes() |
| 19 | XMP = (b'<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF><rdf:Description ' |
| 20 | b'author="CANARY-XMP-AUTHOR" GCamera:MotionPhoto="1"/></rdf:RDF>' |
| 21 | b'</x:xmpmeta>') |
| 22 | |
| 23 | |
| 24 | def picture(mode="RGB", size=(64, 48)): |
| 25 | """A picture with structure, so a lost segment shows in pixels.""" |
| 26 | image = Image.new(mode, size) |
| 27 | pixels = image.load() |
| 28 | for x in range(size[0]): |
| 29 | for y in range(size[1]): |
| 30 | value = ((x * 7) % 256, (y * 11) % 256, ((x + y) * 5) % 256) |
| 31 | if mode == "RGBA": |
| 32 | value += ((x * y) % 256,) |
| 33 | if mode == "CMYK": |
| 34 | value += ((x * 3) % 256,) |
| 35 | pixels[x, y] = value |
| 36 | return image |
| 37 | |
| 38 | |
| 39 | def exif(orientation=6): |
| 40 | data = Image.Exif() |
| 41 | data[0x0112] = orientation |
| 42 | data[0x010F] = "CANARY-EXIF-MAKE" |
| 43 | data[0x013B] = "CANARY-EXIF-ARTIST" |
| 44 | data[0x8825] = {1: "N", 2: (53.0, 20.0, 0.0), 3: "W", |
| 45 | 4: (6.0, 15.0, 0.0), 0x1B: "CANARY-GPS"} |
| 46 | return data.tobytes() |
| 47 | |
| 48 | |
| 49 | def save(name, data): |
| 50 | with open(os.path.join(HERE, name), "wb") as out: |
| 51 | out.write(data) |
| 52 | |
| 53 | |
| 54 | def encoded(image, fmt, **options): |
| 55 | buffer = io.BytesIO() |
| 56 | image.save(buffer, fmt, **options) |
| 57 | return buffer.getvalue() |
| 58 | |
| 59 | |
| 60 | def png_chunk(kind, data): |
| 61 | body = kind + data |
| 62 | return struct.pack(">I", len(data)) + body + struct.pack( |
| 63 | ">I", zlib.crc32(body) & 0xFFFFFFFF) |
| 64 | |
| 65 | |
| 66 | def png_fixtures(): |
| 67 | info = PngImagePlugin.PngInfo() |
| 68 | info.add_text("Comment", "CANARY-PNG-TEXT") |
| 69 | info.add_text("Author", "CANARY-PNG-ZTXT", zip=True) |
| 70 | info.add_itxt("XML:com.adobe.xmp", XMP.decode()) |
| 71 | raw = encoded(picture("RGBA"), "PNG", pnginfo=info, icc_profile=ICC, |
| 72 | exif=exif(1)) |
| 73 | iend = raw.rindex(b"IEND") - 4 |
| 74 | extra = png_chunk(b"tIME", b"\x07\xea\x0b\x11\x0c\x00\x00") + \ |
| 75 | png_chunk(b"prVt", b"CANARY-PNG-PRIVATE") |
| 76 | save("meta.png", raw[:iend] + extra + raw[iend:] + b"CANARY-PNG-TRAILER") |
| 77 | frames = [picture("RGBA"), picture("RGBA").rotate(90)] |
| 78 | save("anim.png", encoded(frames[0], "PNG", save_all=True, |
| 79 | append_images=frames[1:], pnginfo=info)) |
| 80 | |
| 81 | |
| 82 | def jpeg_segment(marker, payload): |
| 83 | return b"\xff" + bytes([marker]) + struct.pack(">H", len(payload) + 2) \ |
| 84 | + payload |
| 85 | |
| 86 | |
| 87 | def after_soi(raw, *segments): |
| 88 | return raw[:2] + b"".join(segments) + raw[2:] |
| 89 | |
| 90 | |
| 91 | def jpeg_fixtures(): |
| 92 | meta = dict(exif=exif(6), icc_profile=ICC, comment=b"CANARY-JPEG-COM") |
| 93 | xmp = jpeg_segment(0xE1, b"http://ns.adobe.com/xap/1.0/\x00" + XMP) |
| 94 | base = encoded(picture(), "JPEG", quality=90, **meta) |
| 95 | save("baseline.jpg", after_soi(base, xmp) + b"CANARY-JPEG-TRAILER") |
| 96 | save("progressive.jpg", encoded(picture(), "JPEG", progressive=True, |
| 97 | **meta)) |
| 98 | save("restart.jpg", encoded(picture(), "JPEG", restart_marker_blocks=2, |
| 99 | **meta)) |
| 100 | save("cmyk.jpg", encoded(picture("CMYK"), "JPEG", **meta)) |
| 101 | # A JFIF thumbnail of 3x1 pixels whose bytes are the canary, and a |
| 102 | # JFXX extension segment. |
| 103 | thumb = jpeg_segment(0xE0, b"JFIF\x00\x01\x02\x00\x00\x01\x00\x01" |
| 104 | b"\x03\x01CANARY-TH") |
| 105 | jfxx = jpeg_segment(0xE0, b"JFXX\x00\x13CANARY-JFXX") |
| 106 | plain = encoded(picture(), "JPEG", quality=90) |
| 107 | assert plain[2:4] == b"\xff\xe0" |
| 108 | app0_end = 4 + struct.unpack(">H", plain[4:6])[0] |
| 109 | save("thumbnail.jpg", plain[:2] + thumb + jfxx + plain[app0_end:]) |
| 110 | # A Motion Photo: an MPF APP2 naming a second picture, and a video |
| 111 | # after EOI. |
| 112 | mpf = jpeg_segment(0xE2, b"MPF\x00CANARY-MPF") |
| 113 | save("motion.jpg", after_soi(base, xmp, mpf) + plain + |
| 114 | b"\x00\x00\x00\x18ftypmp42CANARY-VIDEO") |
| 115 | |
| 116 | |
| 117 | def webp_fixtures(): |
| 118 | meta = dict(exif=exif(1), xmp=XMP, icc_profile=ICC) |
| 119 | save("lossy.webp", encoded(picture("RGB"), "WEBP", quality=80, **meta)) |
| 120 | save("lossless.webp", encoded(picture("RGBA"), "WEBP", lossless=True, |
| 121 | **meta)) |
| 122 | frames = [picture("RGBA"), picture("RGBA").rotate(90)] |
| 123 | save("anim.webp", encoded(frames[0], "WEBP", save_all=True, |
| 124 | append_images=frames[1:], loop=0, **meta)) |
| 125 | |
| 126 | |
| 127 | def gif_fixtures(): |
| 128 | frames = [picture().convert("P"), picture().rotate(90).convert("P")] |
| 129 | raw = encoded(frames[0], "GIF", save_all=True, append_images=frames[1:], |
| 130 | loop=0, comment=b"CANARY-GIF-COMMENT") |
| 131 | xmp = b"\x21\xff\x0bXMP DataXMP" + bytes([len(b"CANARY-GIF-XMP")]) + \ |
| 132 | b"CANARY-GIF-XMP\x00" |
| 133 | text = b"\x21\x01\x0c" + b"\x00" * 12 + bytes([16]) + \ |
| 134 | b"CANARY-GIF-TEXT!" + b"\x00" |
| 135 | trailer = raw.rindex(b"\x3b") |
| 136 | save("anim.gif", raw[:trailer] + xmp + text + raw[trailer:] + |
| 137 | b"CANARY-GIF-TRAILER") |
| 138 | |
| 139 | |
| 140 | if __name__ == "__main__": |
| 141 | png_fixtures() |
| 142 | jpeg_fixtures() |
| 143 | webp_fixtures() |
| 144 | gif_fixtures() |
| 145 | sys.exit(0) |
| 146 | |