Выполнение lab4 на 4
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Deterministic streaming PNG generation; no third-party Python packages."""
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import random
|
||||
import struct
|
||||
import zlib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DIRECTORY = ROOT / 'data/upload-fixtures'
|
||||
|
||||
|
||||
def chunk(file, kind, data):
|
||||
file.write(struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data)))
|
||||
|
||||
|
||||
def png(name, width, height, bit_depth, color_type, noisy=False):
|
||||
path = DIRECTORY / (name + '.png')
|
||||
channels = {2: 3, 6: 4}[color_type]
|
||||
row_bytes = width * channels * (bit_depth // 8)
|
||||
compressor = zlib.compressobj(level=1)
|
||||
random_source = random.Random(20261106)
|
||||
pattern = b'\x12\x34\x56\x78\x9a\xbc\xde\xf0' * 8192
|
||||
with path.open('wb') as file:
|
||||
file.write(b'\x89PNG\r\n\x1a\n')
|
||||
chunk(file, b'IHDR', struct.pack('>IIBBBBB', width, height, bit_depth, color_type, 0, 0, 0))
|
||||
for _ in range(height):
|
||||
compressed = compressor.compress(b'\0')
|
||||
if compressed:
|
||||
chunk(file, b'IDAT', compressed)
|
||||
remaining = row_bytes
|
||||
while remaining:
|
||||
size = min(remaining, 65536)
|
||||
block = random_source.randbytes(size) if noisy else pattern[:size]
|
||||
compressed = compressor.compress(block)
|
||||
if compressed:
|
||||
chunk(file, b'IDAT', compressed)
|
||||
remaining -= size
|
||||
chunk(file, b'IDAT', compressor.flush())
|
||||
chunk(file, b'IEND', b'')
|
||||
return {'name': name, 'path': str(path), 'content_type': 'image/png', 'width': width,
|
||||
'height': height, 'bit_depth': bit_depth, 'color_type': color_type,
|
||||
'size_bytes': path.stat().st_size, 'sha256': hashlib.sha256(path.read_bytes()).hexdigest()}
|
||||
|
||||
|
||||
def generate():
|
||||
DIRECTORY.mkdir(parents=True, exist_ok=True)
|
||||
fixtures = [png('rgb8_1mp', 1024, 768, 8, 2, noisy=True),
|
||||
png('rgba16_40mp', 8000, 5000, 16, 6),
|
||||
png('rgba16_wide_40mp', 40000000, 1, 16, 6)]
|
||||
jpeg = DIRECTORY / 'synthetic.jpg'
|
||||
if jpeg.exists():
|
||||
fixtures.append({'name': 'jpeg_1mp', 'path': str(jpeg), 'content_type': 'image/jpeg',
|
||||
'width': 1024, 'height': 768, 'size_bytes': jpeg.stat().st_size,
|
||||
'sha256': hashlib.sha256(jpeg.read_bytes()).hexdigest()})
|
||||
return fixtures
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import json
|
||||
print(json.dumps(generate(), indent=2))
|
||||
Reference in New Issue
Block a user