11"""Rasterize the test fixture's real ImGui mesh/atlas for visual inspection."""
22import argparse
33import json
4+ import sys
45from pathlib import Path
56
67import numpy as np
1011def render (path , output_directory = None , image_format = 'png' ):
1112 mesh = json .loads (path .read_text (encoding = 'utf-8' ))
1213 width , height = int (mesh ['width' ]), int (mesh ['height' ])
13- atlas = np .frombuffer (path .with_suffix ('.rgba' ).read_bytes (), dtype = np .uint8 )
14+ atlas_path = capture_atlas (path , mesh )
15+ atlas = np .frombuffer (atlas_path .read_bytes (), dtype = np .uint8 )
1416 atlas = atlas .reshape (mesh ['atlas_height' ], mesh ['atlas_width' ], 4 ).astype (np .float32 ) / 255
1517 image = np .zeros ((height , width , 3 ), np .float32 )
1618 image [:] = (0.055 , 0.06 , 0.07 )
@@ -53,6 +55,19 @@ def render(path, output_directory=None, image_format='png'):
5355 print (output )
5456
5557
58+ def capture_atlas (path , mesh ):
59+ # Legacy captures use a same-stem atlas; new runs share only within this directory.
60+ name = mesh .get ('atlas_file' )
61+ if name is None :
62+ return path .with_suffix ('.rgba' )
63+ if not isinstance (name , str ) or not name or '/' in name or '\\ ' in name or ':' in name or Path (name ).name != name :
64+ raise ValueError ('Capture atlas must be a local filename' )
65+ atlas = path .parent / name
66+ if atlas .suffix != '.rgba' or atlas .is_symlink () or atlas .resolve ().parent != path .parent .resolve ():
67+ raise ValueError ('Capture atlas escapes capture directory' )
68+ return atlas
69+
70+
5671if __name__ == '__main__' :
5772 parser = argparse .ArgumentParser (
5873 description = __doc__ ,
@@ -67,14 +82,26 @@ def render(path, output_directory=None, image_format='png'):
6782 args = parser .parse_args ()
6883 if args .documentation and (not args .output_directory or args .format != 'webp' ):
6984 parser .error ('--documentation requires --output-directory and --format webp' )
70- captures = [path for path in sorted (args .directory .glob (args .pattern ))
71- if path .with_suffix ('.rgba' ).exists ()]
85+ captures = []
86+ skipped = 0
87+ for path in sorted (args .directory .glob (args .pattern )):
88+ try :
89+ mesh = json .loads (path .read_text (encoding = 'utf-8' ))
90+ if 'lists' in mesh and capture_atlas (path , mesh ).exists ():
91+ captures .append (path )
92+ except (OSError , ValueError , TypeError ) as error :
93+ print (f'Skipping { path .name } : { error } ' , file = sys .stderr )
94+ skipped += 1
7295 if not captures :
7396 parser .error ('No matching mesh/atlas pairs found' )
7497 if args .output_directory :
7598 args .output_directory .mkdir (parents = True , exist_ok = True )
7699 for path in captures :
77- render (path , args .output_directory , args .format )
100+ try :
101+ render (path , args .output_directory , args .format )
102+ except (OSError , ValueError , KeyError , TypeError ) as error :
103+ print (f'Skipping { path .name } : { error } ' , file = sys .stderr )
104+ skipped += 1
78105 if args .documentation :
79106 # Crops select complete control groups from the production menu, with
80107 # synthetic activity summaries excluded. No game image is synthesized.
@@ -106,3 +133,5 @@ def render(path, output_directory=None, image_format='png'):
106133 with Image .open (args .output_directory / (stem + '.webp' )) as original :
107134 sheet .paste (original .crop ((0 , 174 , 650 , 742 )), (x , y + 42 ))
108135 sheet .save (args .output_directory / ('themes' + suffix + '.webp' ), lossless = True , method = 6 )
136+ if skipped :
137+ sys .exit (1 )
0 commit comments