#!/usr/bin/env python3 """Validate all poses and a visible projectile against executed ROM descriptors.""" import json from PIL import Image,ImageChops from extract_graphics import ROOT,decode from extract_start_room import RGB from nes_object_graphics import P,ENTRY,STOP,descriptor,octorok OUT=ROOT/'extracted/overworld/north/validation/graphics_fix' def native_image(info,canonical=False): patterns=bytearray(4096);patterns[:1792]=P[32895:34687];patterns[1792:4096]=P[19892:22196];patterns[2272:4096]=P[53595:55419] rgb=dict(RGB);rgb.update({2:(0,0,188),34:(104,136,252),41:(56,184,0),12:(0,64,88),28:(0,136,136),44:(88,248,216)}) image=Image.new('RGBA',(16,16));offset=4 if info['sides']==1 else 0 for side in range(info['sides']): attr=info['attributes'][side];pal=0 if canonical else attr&3 row=P[103187+pal*4:103191+pal*4];tile=info['tiles'][side] column=Image.new('RGBA',(8,16));column.putdata([(0,0,0,0) if v==0 else (*rgb[row[v]],255) for n in (tile,tile+1) for v in decode(patterns[n*16:(n+1)*16])]) if attr&128:column=column.transpose(Image.Transpose.FLIP_TOP_BOTTOM) if attr&64:column=column.transpose(Image.Transpose.FLIP_LEFT_RIGHT) image.paste(column,(offset+side*info['x_separation'],0)) return image def difference(a,b): assert a.size==b.size return sum(v!=(0,0,0,0) for v in ImageChops.difference(a,b).getdata()) def main(): OUT.mkdir(exist_ok=True,parents=True);checks=[];sheet=Image.new('RGBA',(256,176),(18,22,28,255)) expected=Image.open(ROOT/'extracted/hud/runtime_initial.png').convert('RGBA') expected.paste(Image.open(ROOT/'extracted/overworld/north/room.png').convert('RGBA'),(112,15)) # Fixed regressions from the ROM descriptors, not a copied animation-heap offset. native_tiles=[[180,176,176],[184,178,178]] for phase in range(2): for col,direction in enumerate(('left','up','down','right')): info=octorok(direction,phase) assert info['animation_index']==8 off={'left':0,'up':1,'down':2,'right':0}[direction] assert info['tiles'][0]==native_tiles[phase][off]+(2 if direction=='right' else 0) assert bool(info['attributes'][0]&128)==(direction=='up') assert info['attributes'][0]&3==2 path=ROOT/f'assets/characters/octorok/octorok_{direction}_{phase}.png' actual=Image.open(path).convert('RGBA');diff=difference(actual,native_image(info,True));assert diff==0,(path,diff) im=native_image(info);expected.alpha_composite(im,(112+40+col*48,15+97+phase*48));sheet.alpha_composite(im.resize((48,48),Image.Resampling.NEAREST),(col*64,phase*64)) checks.append({'direction':direction,'phase':phase,'descriptor':info,'different_pixels':diff}) rock=descriptor(0x53);assert rock['animation_index']==84 and rock['tiles'][0]==158 and rock['sides']==1 diff=difference(Image.open(ROOT/'assets/characters/octorok/rock.png').convert('RGBA'),native_image(rock,True));assert diff==0 expected.alpha_composite(native_image(rock),(224,200));sheet.alpha_composite(native_image(rock).resize((48,48),Image.Resampling.NEAREST),(104,128));sheet.save(OUT/'preview.png') actual=Image.open(OUT/'studio_fixture.png').convert('RGBA');runtime_diff=difference(actual,expected);assert runtime_diff==0,runtime_diff report={'descriptor_entry_cpu':hex(ENTRY),'stop_before_sprite_emission_cpu':hex(STOP),'method':'Bounded original 6502 descriptor execution, including INY, native per-frame attributes, horizontal/vertical flips and half-width handling. All eight poses and an actually visible rock are rendered in the Studio fixture.','poses':checks,'rock':rock,'asset_comparisons':9,'runtime_different_pixels':runtime_diff} (OUT/'results.json').write_text(json.dumps(report,indent=2)+'\n');print('9 native asset comparisons and full Studio fixture: 0 differing pixels') if __name__=='__main__':main()