File size: 1,999 Bytes
f918b86
 
 
 
 
757768f
f918b86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# SPDX-License-Identifier: Apache-2.0
"""Build the Ink Bench reference layers (data/bench/) from the page's own data.

Each winding gets <id>_ink.png (published ink preview) and <id>_ct.png (X-ray layer
cropped from the atlas), resampled to the same grid, long side LONG px. Windings with our
ink_9um run also get <id>_gpu.png on that grid. Lossless 8-bit gray PNG, so the browser
and tools/inkbench.py read bit-identical pixels.
"""
import hashlib, json, os
from PIL import Image
LONG = 640
root = os.path.join(os.path.dirname(__file__), '..')
d = json.load(open(os.path.join(root, 'data/scroll.json')))
gpu = json.load(open(os.path.join(root, 'data/gpu.json')))['windings']
ct = Image.open(os.path.join(root, 'data/atlas_ct.jpg')).convert('L')
out = {'schema': 'unroll-bench-refs/1', 'scroll': d['scroll'], 'long': LONG, 'segments': {}}
def save(im, name):
    p = os.path.join(root, 'data/bench', name); im.save(p, optimize=True)
    return {'file': 'data/bench/' + name, 'sha256': hashlib.sha256(open(p, 'rb').read()).hexdigest()}
for w in d['windings']:
    hi = Image.open(os.path.join(root, 'data', w['hi'])).convert('L')
    s = LONG / max(hi.size); W, H = round(hi.size[0] * s), round(hi.size[1] * s)
    e = {'width': W, 'height': H, 'widthMM': w['widthMM'], 'heightMM': w['heightMM'],
         'pxPerMM': round(W / w['widthMM'], 4), 'layers': {}}
    e['layers']['ink'] = save(hi.resize((W, H), Image.LANCZOS), f"{w['id']}_ink.png")
    u0, v0, u1, v1 = w['atlas']
    c = ct.crop((round(u0 * 4096), round(v0 * 4096), round(u1 * 4096), round(v1 * 4096)))
    e['layers']['ct'] = save(c.resize((W, H), Image.LANCZOS), f"{w['id']}_ct.png")
    if w['id'] in gpu:
        g = Image.open(os.path.join(root, 'data', gpu[w['id']]['hi'])).convert('L')
        e['layers']['gpu'] = save(g.resize((W, H), Image.LANCZOS), f"{w['id']}_gpu.png")
    out['segments'][w['id']] = e
json.dump(out, open(os.path.join(root, 'data/bench/index.json'), 'w'), indent=1)
print(len(out['segments']), 'segments')