#!/usr/bin/env python3
"""Build the HD sprite pack: upscale captured 1x bitmaps x4 with realesrgan.

Usage: python3 -I tools/hd/upscale.py <capture dir> <out dir> [--model M] [--limit N]
"""
import argparse, json, multiprocessing, os, queue, shutil, subprocess, sys, threading, time
from PIL import Image

# Real-ESRGAN ncnn Vulkan (github.com/xinntao/Real-ESRGAN releases), unpacked in <repo>/.alis-data/tools/realesrgan/
BIN = os.environ.get('HD_REALESRGAN', os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../../.alis-data/tools/realesrgan/realesrgan-ncnn-vulkan'))
import tempfile
TMP = os.path.join(tempfile.gettempdir(), 'alis-hd-%d' % os.getpid())
BATCH = 40
S = 4


def dilate(img):
    """Fill transparent pixels with the nearest opaque colour (iterative)."""
    w, h = img.size
    px = img.load()
    col = {}
    todo = set()
    for y in range(h):
        for x in range(w):
            p = px[x, y]
            if p[3] > 0:
                col[(x, y)] = p[:3]
            else:
                todo.add((x, y))
    if not col:
        return Image.new('RGB', (w, h), (0, 0, 0))
    while todo:
        new = {}
        for (x, y) in todo:
            for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)):
                c = col.get((x + dx, y + dy))
                if c:
                    new[(x, y)] = c
                    break
        if not new:
            break
        col.update(new)
        todo -= set(new)
    out = Image.new('RGB', (w, h), (0, 0, 0))
    op = out.load()
    for k, v in col.items():
        op[k] = v
    return out


def finish(rgb, orig, dest):
    alpha = orig.getchannel('A').resize((orig.width * S, orig.height * S), Image.NEAREST)
    rgb = rgb.convert('RGB')
    if rgb.size != alpha.size:
        rgb = rgb.resize(alpha.size, Image.LANCZOS)
    rgb.putalpha(alpha)
    rgb.save(dest + '.tmp', 'PNG', optimize=True)
    os.replace(dest + '.tmp', dest)


def prep(args):
    """Pool job: dilate one capture into the batch input folder."""
    cap, k, tin = args
    im = Image.open(os.path.join(cap, k + '.png')).convert('RGBA')
    if im.width < 4 or im.height < 4:
        return k
    dilate(im).save(os.path.join(tin, k + '.png'))
    return None


def post(args):
    """Pool job: alpha x4 nearest + save the final RGBA."""
    cap, out, k, tout = args
    im = Image.open(os.path.join(cap, k + '.png')).convert('RGBA')
    if im.width < 4 or im.height < 4:
        rgb = dilate(im).resize((im.width * S, im.height * S), Image.NEAREST)
    else:
        rgb = Image.open(os.path.join(tout, k + '.png'))
        rgb.load()
    finish(rgb, im, os.path.join(out, k + '.png'))
    return k


# worker specs: (gpu id, threads "load:proc:save"); two on the GPU, CPU ones on llvmpipe (-g 1) or -g -1
WORKERS = os.environ.get('HD_WORKERS', '0:2:4:2,0:2:4:2').split(',')
# only the real GPU's Vulkan driver: ncnn also starts llvmpipe (Vulkan on the CPU), ~27 s per process for nothing
if os.path.exists('/usr/share/vulkan/icd.d/radeon_icd.json'):
    os.environ.setdefault('VK_ICD_FILENAMES', '/usr/share/vulkan/icd.d/radeon_icd.json')


def run_worker(wid, spec, q, a, pool, results, lock):
    gpu, jobs = spec.split(':', 1)
    mdir = os.path.join(os.path.dirname(BIN), 'models')
    while True:
        try:
            n, chunk = q.get_nowait()
        except queue.Empty:
            return
        base = os.path.join(TMP, f'w{wid}')
        tin, tout = os.path.join(base, 'in'), os.path.join(base, 'out')
        for d in (tin, tout):
            shutil.rmtree(d, ignore_errors=True)
            os.makedirs(d)
        small = [r for r in pool.map(prep, [(a.cap, k, tin) for k in chunk]) if r]
        if len(small) < len(chunk):
            r = subprocess.run([BIN, '-i', tin, '-o', tout, '-n', a.model, '-m', mdir, '-s', str(S),
                                '-g', gpu, '-j', jobs, '-f', 'png'], capture_output=True, text=True)
            if r.returncode != 0:
                print('upscaler failed:', r.stderr[-300:], file=sys.stderr)
                continue
        done = pool.map(post, [(a.cap, a.out, k, tout) for k in chunk])
        with lock:
            results[wid] = results.get(wid, 0) + len(done)
            print(f'w{wid} (g{gpu}) +{len(done)}  total {sum(results.values())}', flush=True)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('cap')
    ap.add_argument('out')
    ap.add_argument('--model', default='realesrgan-x4plus-anime')
    ap.add_argument('--limit', type=int, default=0)
    ap.add_argument('--batch', type=int, default=0)  # 0 = split evenly: each process has a big fixed start cost
    a = ap.parse_args()
    t0 = time.time()
    os.makedirs(a.out, exist_ok=True)
    keys = sorted(f[:-4] for f in os.listdir(a.cap) if f.endswith('.png')
                  and os.path.exists(os.path.join(a.cap, f[:-4] + '.json')))
    todo = []
    for k in keys:
        dst = os.path.join(a.out, k + '.png')
        if os.path.exists(dst) and os.path.getmtime(dst) >= os.path.getmtime(os.path.join(a.cap, k + '.png')):
            continue
        todo.append(k)
    if a.limit:
        todo = todo[:a.limit]
    print(f'{len(keys)} captures, {len(todo)} to process, workers {WORKERS}')
    q = queue.Queue()
    bs = a.batch or max(1, -(-len(todo) // len(WORKERS)))
    for i in range(0, len(todo), bs):
        q.put((i, todo[i:i + bs]))
    results, lock = {}, threading.Lock()
    with multiprocessing.Pool(16) as pool:
        ts = [threading.Thread(target=run_worker, args=(i, w, q, a, pool, results, lock)) for i, w in enumerate(WORKERS)]
        for t in ts:
            t.start()
        for t in ts:
            t.join()
    sprites = {}
    for f in sorted(os.listdir(a.out)):
        if f.endswith('.png'):
            k = f[:-4]
            jp = os.path.join(a.cap, k + '.json')
            if os.path.exists(jp):
                m = json.load(open(jp))
                sprites[k] = {'w': m['w'], 'h': m['h'], 'pal': m.get('pal', {})}
    with open(os.path.join(a.out, 'sprites.json'), 'w') as fh:
        json.dump(sprites, fh, separators=(',', ':'))
    mb = sum(os.path.getsize(os.path.join(a.out, f)) for f in os.listdir(a.out)) / 1e6
    print(f'done: {len(sprites)} sprites, {mb:.1f} MB, {time.time() - t0:.1f}s, per worker {results}')


if __name__ == '__main__':
    main()
