25 lines
999 B
Python
25 lines
999 B
Python
"""Small deterministic PNG helpers; generation happens outside measurement."""
|
|
import random
|
|
import struct
|
|
import zlib
|
|
|
|
|
|
def chunk(name, data):
|
|
return struct.pack('!I', len(data)) + name + data + struct.pack('!I', zlib.crc32(name + data) & 0xffffffff)
|
|
|
|
|
|
def png(width, height, noise=False, rgb=True):
|
|
channels = 3 if rgb else 1
|
|
rand = random.Random(2301)
|
|
encoded = zlib.compressobj(1)
|
|
parts = []
|
|
for _ in range(height):
|
|
line = bytes(rand.getrandbits(8) for _ in range(width * channels)) if noise else b'\x7f' * (width * channels)
|
|
parts.append(encoded.compress(b'\0' + line))
|
|
parts.append(encoded.flush())
|
|
return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', width, height, 8, 2 if rgb else 0, 0, 0, 0)) + chunk(b'IDAT', b''.join(parts)) + chunk(b'IEND', b'')
|
|
|
|
|
|
def bomb():
|
|
return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', 10000, 4001, 8, 2, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(b'x')) + chunk(b'IEND', b'')
|