"""Decode saved bytes downloaded from Filepetal UI; this does not perform conversion."""
from pathlib import Path
from PIL import Image, ImageChops
import json, hashlib
ROOT=Path(__file__).resolve().parent
manifest=json.loads((ROOT/'sample-manifest.json').read_text())
inputs={s['name']:s for s in manifest['samples']}
checks=[]
for path in sorted(ROOT.glob('*.jpg')):
    im=Image.open(path);im.load();assert im.format=='JPEG' and im.mode=='RGB'
    stem=path.stem.removesuffix('-white').removesuffix('-black')
    src=inputs[stem+'.png'];assert im.size==(src['width'],src['height'])
    checks.append({'name':path.name,'source':stem+'.png','bytes':path.stat().st_size,'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'width':im.width,'height':im.height,'decoded_format':im.format,'mode':im.mode,'center_rgb':list(im.getpixel((im.width//2,im.height//2)))})
assert len(checks)==8
bands={name:[list(Image.open(ROOT/name).getpixel((x,64))) for x in [32,96,160,224]] for name in ['rgba-bands-white.jpg','rgba-bands-black.jpg','palette-trns-white.jpg','palette-trns-black.jpg']}
assert bands['rgba-bands-white.jpg']==bands['palette-trns-white.jpg']
assert bands['rgba-bands-black.jpg']==bands['palette-trns-black.jpg']
assert (ROOT/'rgba-bands-white.jpg').read_bytes()==(ROOT/'palette-trns-white.jpg').read_bytes()
assert (ROOT/'rgba-bands-black.jpg').read_bytes()==(ROOT/'palette-trns-black.jpg').read_bytes()
gray=Image.open(ROOT/'gray16-adjacent.png');jpg=Image.open(ROOT/'gray16-adjacent.jpg')
precision={'input_gray_values':[gray.getpixel((64,32)),gray.getpixel((192,32))],'output_rgb':[list(jpg.getpixel((64,32))),list(jpg.getpixel((192,32)))]}
assert precision['input_gray_values']==[32768,32769]
assert precision['output_rgb'][0]==precision['output_rgb'][1]
errors=[{'name':'side-4097.png','error':'Image dimensions must be at most 4096 pixels per side and 16 million pixels total.','download_count':0},{'name':'truncated.png','error':'This image could not be opened in this browser.','download_count':0},{'name':'renamed.png','error':'This file is not a valid PNG file.','download_count':0}]
result={'date':'2026-10-06','method':'Actual UI-generated data:image/jpeg download bytes, then Pillow full decode; 8 successful runs and 3 failures','browser_scope':'Codex in-app desktop browser; engine version not recorded; no physical-phone, other-engine, ICC, HDR or metadata claim','outputs':checks,'alpha_band_center_rgb':bands,'gray16_precision_probe':precision,'failures':errors,'limits_not_tested':['20 MiB byte boundary','16 million total pixels boundary'],'encoder_quality_request':0.92}
(ROOT/'output-verification.json').write_text(json.dumps(result,indent=2)+'\n',encoding='utf-8')
print(json.dumps(result))
