351 lines
19 KiB
Python
351 lines
19 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare Bone Quest's compact masks from the retained public-domain SVG.
|
|
|
|
Optional authoring dependencies: Python 3, Pillow and rsvg-convert. Normal game
|
|
builds use the generated metadata and BONES.DAT and do not run this converter. The SVG remains
|
|
unchanged; labels/background are omitted and source bone silhouettes are reduced
|
|
into a 28-structure pixel mask. All coordinates below refer to a 436x842 render of the
|
|
source's nominal 435.687x841.89 viewBox; the SVG is rendered at 2x before reduction. No SVG is parsed on the CoCo.
|
|
"""
|
|
import argparse
|
|
import copy
|
|
import hashlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import xml.etree.ElementTree as ET
|
|
from PIL import Image, ImageChops
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
RASTER_SCALE = 2
|
|
RASTER_WIDTH, RASTER_HEIGHT = 436 * RASTER_SCALE, 842 * RASTER_SCALE
|
|
|
|
def scaled_box(box):
|
|
return tuple(value * RASTER_SCALE for value in box)
|
|
|
|
def crop(mask, box):
|
|
return mask.crop(scaled_box(box))
|
|
ORIGINAL_GROUPS = [
|
|
('CRANIUM', ['Cranium', 'g1297']),
|
|
('MANDIBLE', ['Mandible']),
|
|
('CLAVICLE', ['ClavicleLeft', 'ClavicleRight']),
|
|
('SCAPULA', ['Scapula']),
|
|
('STERNUM', ['Sternum', 'Manubrium']),
|
|
('RIBS', ['g845', 'g1609']),
|
|
('CERVICAL', ['CervicalVertebrae']),
|
|
('THORACIC', ['ThoracicVertebrae']),
|
|
('LUMBAR', ['LumbarVertebrae']),
|
|
('SACRUM', ['Sacrum']),
|
|
('COCCYX', ['Coccyx']),
|
|
('PELVIS', ['PelvicGirdle', 'g447']),
|
|
('HUMERUS', ['HumerusLeft', 'HumerusRight']),
|
|
('RADIUS', ['RadiusLeft', 'RadiusRight']),
|
|
('ULNA', ['UlnaLeft', 'UlnaRight']),
|
|
('CARPALS', ['CarpalsLeft', 'CarpalsRight']),
|
|
('METACARPALS', ['MetacarpalsLeft', 'MetacarpalsRight']),
|
|
('FINGERS', ['PhalangesLeft', 'PhalangesRight']),
|
|
('FEMUR', ['g3760', 'FemurLeft', 'path479', 'path481', 'path513', 'path499']),
|
|
('PATELLA', ['PatellaLeft', 'PatellaRight']),
|
|
('TIBIA', ['TibiaLeft', 'TibiaRight']),
|
|
('FIBULA', ['FibulaLeft', 'FibulaRight']),
|
|
('TARSALS', ['TarsalsLeft', 'TarsalsRight']),
|
|
('METATARSALS', ['MetatarsalsLeft', 'MetatarsalsRight']),
|
|
('TOES', ['PhalangesFootLeft', 'PhalangesFootRight']),
|
|
]
|
|
|
|
|
|
def selected_svg(source, elements, selected):
|
|
root = ET.Element(source.tag, {k:v for k,v in source.attrib.items()
|
|
if k in ('version','width','height','viewBox')})
|
|
root.append(copy.deepcopy(elements['defs3096']))
|
|
def prune(element, active=False):
|
|
active = active or element.get('id') in selected
|
|
result = ET.Element(element.tag, element.attrib)
|
|
for child in element:
|
|
kept = prune(child, active)
|
|
if kept is not None:
|
|
result.append(kept)
|
|
return result if active or len(result) else None
|
|
skeleton = prune(elements['layer3'])
|
|
if skeleton is None:
|
|
raise ValueError('No skeleton source groups selected')
|
|
root.append(skeleton)
|
|
return ET.tostring(root)
|
|
|
|
|
|
def c_array(name, values, kind='unsigned char', static=False):
|
|
lines = ['static ' if static else '']
|
|
prefix = ('static ' if static else '') + 'const '+kind+' '+name+'[] = {\n'
|
|
return prefix + ''.join(' '+','.join(map(str,values[n:n+24]))+',\n'
|
|
for n in range(0,len(values),24)) + '};\n\n'
|
|
|
|
|
|
|
|
STRUCTURES = ['SKULL','CERVICAL','MANUBRIUM','STERNUM','XIPHOID','LUMBAR',
|
|
'ILIUM','SACRUM','COCCYX','PUBIS','FEMUR','PATELLA','TARSALS',
|
|
'METATARSALS','TOES','ORBIT','NASAL','CLAVICLE','SCAPULA','RIBS',
|
|
'HUMERUS','ULNA','RADIUS','CARPALS','METACARPALS','FINGERS','FIBULA','TIBIA']
|
|
PAINT_ORDER = [28,12,13,14,26,27,10,6,9,18,8,7,5,19,1,0,3,4,2,17,20,21,22,23,24,25,11,15,16]
|
|
DETAIL_VIEW = [1,1,5,5,5,4,4,4,4,4,0,0,3,3,3,1,1,5,5,5,0,0,0,2,2,2,0,0]
|
|
VIEWS = [('overview',(59,20,405,822),159,184),('head',(163,17,243,154),114,98),
|
|
('hand',(313,397,410,500),185,98),('foot',(199,747,275,825),191,98),
|
|
('spine',(126,274,279,423),201,98),('chest',(107,132,300,308),215,98)]
|
|
|
|
|
|
def packed_runs(rows, width):
|
|
"""Block: initial y,x, then skip,length pairs; 255 escapes a16-bit skip."""
|
|
if not rows:
|
|
return []
|
|
result = [rows[0][0],rows[0][1]]
|
|
end = rows[0][0]*width+rows[0][1]
|
|
for y,x,length in rows:
|
|
skip=y*width+x-end
|
|
assert skip>=0 and 0<length<=255
|
|
if skip>=255:
|
|
result.extend((255,skip>>8,skip&255))
|
|
else:
|
|
result.append(skip)
|
|
result.append(length)
|
|
end=y*width+x+length
|
|
return result
|
|
|
|
|
|
def row_runs(pixels,width,height,value):
|
|
rows=[]
|
|
for y in range(height):
|
|
x=0
|
|
while x<width:
|
|
if pixels[y*width+x]!=value:
|
|
x+=1
|
|
continue
|
|
start=x
|
|
while x<width and pixels[y*width+x]==value:
|
|
x+=1
|
|
rows.append((y,start,x-start))
|
|
return rows
|
|
|
|
|
|
def decode(data,offsets,width,height):
|
|
pixels=bytearray(width*height)
|
|
for structure in range(30):
|
|
p,end=offsets[structure],offsets[structure+1]
|
|
if p==end:
|
|
continue
|
|
y,x=data[p:p+2];p+=2
|
|
while p<end:
|
|
skip=data[p];p+=1
|
|
if skip==255:
|
|
skip=data[p]*256+data[p+1];p+=2
|
|
x+=skip
|
|
while x>=width:
|
|
x-=width;y+=1
|
|
length=data[p];p+=1
|
|
assert 0<=y<height and 0<=x<x+length<=width
|
|
for xx in range(x,x+length):
|
|
assert pixels[y*width+xx]==0
|
|
pixels[y*width+xx]=structure+1
|
|
x+=length
|
|
assert p==end
|
|
return pixels
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--rsvg-convert',default=shutil.which('rsvg-convert'))
|
|
args=parser.parse_args()
|
|
if not args.rsvg_convert:
|
|
parser.error('Optional asset authoring requires rsvg-convert; ordinary builds use generated C.')
|
|
source_path=ROOT/'assets/skeleton-source.svg'
|
|
source=ET.parse(source_path).getroot()
|
|
elements={e.get('id'):e for e in source.iter() if e.get('id')}
|
|
# Relax the artist's widely splayed anatomical left thumb (screen right).
|
|
# Rotate the complete three-bone chain together about its wrist attachment;
|
|
# the original SVG and all relative bone lengths/joints remain unchanged.
|
|
thumb_ids = ('g1977', 'g1951', 'g1963')
|
|
for ident in thumb_ids:
|
|
element = elements[ident]
|
|
assert not element.get('transform'), 'Review the source thumb transform'
|
|
element.set('transform', 'rotate(30 367 416)')
|
|
def render(ids):
|
|
if any(i not in elements for i in ids):
|
|
raise ValueError('Missing source SVG group: '+str(ids))
|
|
result=subprocess.run([args.rsvg_convert,'-w',str(RASTER_WIDTH),'-h',str(RASTER_HEIGHT)],
|
|
input=selected_svg(source,elements,set(ids)),capture_output=True,check=True)
|
|
return Image.open(io.BytesIO(result.stdout)).convert('RGBA')
|
|
def alpha(ids):return render(ids).getchannel('A')
|
|
def clipped(mask,box):
|
|
result=Image.new('L',mask.size);result.paste(crop(mask,box),scaled_box(box)[:2]);return result
|
|
original=[alpha(ids) for code,ids in ORIGINAL_GROUPS]
|
|
sternum=alpha(['Sternum']);pelvis=alpha(['PelvicGirdle']);skull=alpha(['Skull'])
|
|
# The artist's explicit eye-recess paths provide both selectable orbits.
|
|
for ident in ('path1577','path1579'):
|
|
e=elements[ident];e.set('style',e.get('style','')+';opacity:1')
|
|
orbit=alpha(['path1577','path1579'])
|
|
# Select the visible nasal recess from the source's dark facial region.
|
|
skull_rgb=render(['Skull']);nasal=Image.new('L',(RASTER_WIDTH,RASTER_HEIGHT))
|
|
for y in range(75*RASTER_SCALE,103*RASTER_SCALE):
|
|
for x in range(195*RASTER_SCALE,213*RASTER_SCALE):
|
|
red,green,blue,a=skull_rgb.getpixel((x,y))
|
|
if a>128 and (299*red+587*green+114*blue)//1000<140:
|
|
nasal.putpixel((x,y),255)
|
|
cartilage=[]
|
|
for group in ('g845','g1609'):
|
|
for e in elements[group].iter():
|
|
if 'fill:#e1e2e3' in e.get('style',''):
|
|
cartilage.append(e.get('id'))
|
|
cartilage_mask=alpha(cartilage)
|
|
# Preserve the rib faces rather than merging the original overlapping
|
|
# shaded polygons into a solid chest plate. The original source luminance
|
|
# separates light faces from darker boundaries and cast shadows.
|
|
rib_rgb=render(['g845','g1609']);ribs=Image.new('L',(RASTER_WIDTH,RASTER_HEIGHT))
|
|
raw=rib_rgb.tobytes()
|
|
for pos in range(RASTER_WIDTH*RASTER_HEIGHT):
|
|
red,green,blue,a=raw[pos*4:pos*4+4]
|
|
if a>=128 and max(red,green,blue)-min(red,green,blue)>30 and (299*red+587*green+114*blue)//1000>=211:
|
|
ribs.putpixel((pos%RASTER_WIDTH,pos//RASTER_WIDTH),255)
|
|
masks=[skull,original[6],alpha(['Manubrium']),clipped(sternum,(0,0,436,228)),
|
|
clipped(sternum,(0,228,436,842)),original[8],clipped(pelvis,(0,0,436,371)),
|
|
original[9],original[10],alpha(['g447']),original[18],original[19],
|
|
original[22],original[23],original[24],orbit,nasal,original[2],original[3],
|
|
ribs,original[12],original[14],original[13],original[15],original[16],
|
|
original[17],original[21],original[20]]
|
|
context=ImageChops.lighter(original[7],clipped(pelvis,(0,371,436,842)))
|
|
masks.append(ImageChops.lighter(context,cartilage_mask))
|
|
# Retain the artist's darker bone boundaries and joint lines. A separate
|
|
# black ink owner keeps these visible when the selected bone changes color.
|
|
source_rgb=render(['layer3'])
|
|
source_ink=Image.new('L',source_rgb.size)
|
|
ink_bytes=bytearray(RASTER_WIDTH*RASTER_HEIGHT)
|
|
source_bytes=source_rgb.tobytes()
|
|
for pos in range(RASTER_WIDTH*RASTER_HEIGHT):
|
|
red,green,blue,a=source_bytes[pos*4:pos*4+4]
|
|
if a>=128 and (299*red+587*green+114*blue)//1000<180:
|
|
ink_bytes[pos]=255
|
|
source_ink.frombytes(bytes(ink_bytes))
|
|
manifest=dict(schemaVersion=3,encoding='delta-runs-v1',source=dict(file='assets/skeleton-source.svg',
|
|
sha256=hashlib.sha256(source_path.read_bytes()).hexdigest(),author='Mariana Ruiz Villarreal (LadyofHats)',
|
|
license='Public domain dedication',page='https://commons.wikimedia.org/wiki/File:Human_skeleton_front_en.svg'),
|
|
coordinateRender=[RASTER_WIDTH,RASTER_HEIGHT],sourceCoordinates=[436,842],pixelAspect=[1,2],topics=[dict(id=i,code=name) for i,name in enumerate(STRUCTURES)],
|
|
contextIndex=28,inkIndex=29,drawCount=30,
|
|
derivedRegions=dict(leftThumbPose=dict(elements=list(thumb_ids),rotationDegrees=30,pivot=[367,416],purpose='Relax the anatomical left thumb on screen right without changing bone lengths or joints'),sternum='Native Sternum path split atsource y228: body above, xiphoid below',
|
|
ilium='Native PelvicGirdle upperwings clipped atsource y371; remaining lowerhip isneutralcontext',
|
|
pubis='Nativeg447 pairedpubicarch',orbit='Nativepath1577/path1579 eye-recess shapes withopacityflattened',
|
|
nasal='Source facialregion x195..212 y75..102 withluminance<140',
|
|
ribs='Native g845/g1609 bright bone-face pixels, excluding gray cartilage; source rib matte occludes rear scapula',
|
|
internalInk='Native full-skeleton luminance below180/255, box resampled coverage at least70/255; cavity cores retain their own selectable masks'),
|
|
originalSourceGroups={name:ids for name,ids in ORIGINAL_GROUPS},views=[])
|
|
code=['/* Generated source-derived Bone Quest structure masks. */\n#include "map_data.h"\n\n']
|
|
generated=ROOT/'assets/generated';generated.mkdir(parents=True,exist_ok=True)
|
|
sizes=0;view_data=[];overview=None;asset=bytearray();data_offsets=[]
|
|
for view_index,(name,box,width,height) in enumerate(VIEWS):
|
|
pixels=bytearray(width*height)
|
|
for topic in PAINT_ORDER:
|
|
if topic==19:
|
|
# Rear scapula and unlabelled spine must not fill the rib gaps.
|
|
matte=crop(original[5],box).resize((width,height),Image.Resampling.LANCZOS)
|
|
for p,value in enumerate(matte.tobytes()):
|
|
if value>=64 and pixels[p] in (19,29):pixels[p]=0
|
|
cart=crop(cartilage_mask,box).resize((width,height),Image.Resampling.LANCZOS)
|
|
for p,value in enumerate(cart.tobytes()):
|
|
if value>=64:pixels[p]=29
|
|
mode=Image.Resampling.LANCZOS
|
|
layer=crop(masks[topic],box).resize((width,height),mode)
|
|
threshold=128 if topic in (0,6,10,18,19) else 64
|
|
for p,value in enumerate(layer.tobytes()):
|
|
if value>=threshold:pixels[p]=topic+1
|
|
# Store internal separators. The black canvas supplies exterior contours,
|
|
# avoiding duplicate edge data and an extra runtime outline pass.
|
|
before=pixels[:]
|
|
for y in range(height):
|
|
for x in range(width):
|
|
p=y*width+x
|
|
adjacent=[before[n] for n in (p-1,p+1,p-width,p+width)
|
|
if 0<=n<len(before) and (n//width==y or n%width==x)]
|
|
if before[p]==29 and any(0<v<29 for v in adjacent):pixels[p]=30
|
|
elif before[p]==1 and any(v in (16,17) for v in adjacent):pixels[p]=30
|
|
elif before[p]==4 and any(v in (3,5) for v in adjacent):pixels[p]=30
|
|
ink_layer=crop(source_ink,box).resize((width,height),Image.Resampling.BOX)
|
|
ink_values=ink_layer.tobytes()
|
|
for p,value in enumerate(ink_values):
|
|
if value>=70 and pixels[p] and pixels[p] not in (16,17):pixels[p]=30
|
|
# Keep at least one visible fill pixel for every represented topic; this
|
|
# protects the smallest overview structures from stroke quantization.
|
|
for owner in range(1,29):
|
|
if owner in before and owner not in pixels:
|
|
candidates=[p for p,v in enumerate(before) if v==owner]
|
|
p=min(candidates,key=lambda p:ink_values[p])
|
|
pixels[p]=owner
|
|
counts=[pixels.count(i+1) for i in range(30)]
|
|
if view_index==0:
|
|
overview=pixels[:]
|
|
if not all(counts[:28]):raise ValueError('Astructuredisappearedfromoverview:'+str(counts))
|
|
data=[];offsets=[0]
|
|
for i in range(30):data.extend(packed_runs(row_runs(pixels,width,height,i+1),width));offsets.append(len(data))
|
|
assert decode(data,offsets,width,height)==pixels
|
|
stem='bone' if view_index==0 else name
|
|
data_offsets.append(len(asset));asset.extend(data)
|
|
code.append(c_array(stem+'_offsets',offsets,'unsigned',static=True))
|
|
sizes+=len(offsets)*2;view_data.append((stem,width,height))
|
|
manifest['views'].append(dict(id=view_index,name=name,sourceBounds=list(box),width=width,height=height,
|
|
fileOffset=data_offsets[-1],spanBytes=len(data),offsetBytes=len(offsets)*2,pixels={STRUCTURES[i] if i<28 else 'CONTEXT' if i==28 else 'INK':counts[i] for i in range(30)}))
|
|
# The black canvas supplies the exterior outlines; ink is internal.
|
|
display=pixels
|
|
sample=next((i for i,view in enumerate(DETAIL_VIEW) if view==view_index),0)
|
|
colors=[(0,0,0)]+[(255,255,0) if i==sample else(255,255,255) for i in range(28)]+[(255,255,255),(0,0,0)]
|
|
image=Image.new('RGB',(width,height));image.putdata([colors[value] for value in display]);image.save(generated/(name+'-preview.png'))
|
|
image.resize((width*2,height*4),Image.Resampling.NEAREST).save(generated/(name+'-display-preview.png'))
|
|
palette=[(0,0,85)]+[((i*73+80)%220+35,(i*41)%220+35,(i*109)%220+35) for i in range(29)]+[(0,0,0)]
|
|
image.putdata([palette[value] for value in pixels]);image.save(generated/(name+'-regions.png'))
|
|
boxes=[]
|
|
for topic in range(1,29):
|
|
points=[(p%VIEWS[0][2],p//VIEWS[0][2]) for p,value in enumerate(overview) if value==topic]
|
|
xmin=min(x for x,y in points);ymin=min(y for x,y in points)
|
|
boxes.extend((xmin,ymin,max(x for x,y in points)-xmin+1,max(y for x,y in points)-ymin+1))
|
|
code.append('const unsigned char bone_boxes[28][4] = {'+','.join('{'+','.join(map(str,boxes[i:i+4]))+'}' for i in range(0,112,4))+'};\n')
|
|
code.append(c_array('bone_detail_view',DETAIL_VIEW));code.append(c_array('bone_view_widths',[v[1] for v in view_data]));code.append(c_array('bone_view_heights',[v[2] for v in view_data]))
|
|
code.append(c_array('bone_view_data_offsets',data_offsets,'unsigned'))
|
|
code.append('const unsigned * const bone_view_offsets[BONE_VIEW_COUNT] = {'+','.join(v[0]+'_offsets' for v in view_data)+'};\n')
|
|
sizes+=len(boxes)+len(DETAIL_VIEW)+len(view_data)*6+4
|
|
raw_size=len(asset)
|
|
asset.extend(bytes((-len(asset))%256))
|
|
if len(asset)>32768:raise ValueError('Banked geometry exceeds32768-byte budget:'+str(len(asset)))
|
|
checksum=sum(asset)&65535
|
|
code.append('const unsigned bone_asset_size = '+str(len(asset))+';\n')
|
|
code.append('const unsigned bone_asset_checksum = '+str(checksum)+';\n')
|
|
(generated/'BONES.DAT').write_bytes(asset)
|
|
manifest['asset']=dict(file='assets/generated/BONES.DAT',spanBytes=raw_size,bytes=len(asset),sha256=hashlib.sha256(asset).hexdigest(),checksum=checksum,padding='Zero padded to256 bytes')
|
|
(ROOT/'src/map_data.c').write_text(''.join(code))
|
|
(ROOT/'map_data.h').write_text("""#ifndef BONE_MAP_DATA_H
|
|
#define BONE_MAP_DATA_H
|
|
#define BONE_MAP_WIDTH 159
|
|
#define BONE_MAP_HEIGHT 184
|
|
#define BONE_VIEW_COUNT 6
|
|
#define BONE_CONTEXT_INDEX 28
|
|
#define BONE_INK_INDEX 29
|
|
#define BONE_DRAW_COUNT 30
|
|
/* Each nonempty owner block starts y,x; then (skip,length) pairs. Skip255
|
|
escapes 16-bit big-endian skip. Gaps are row-major from the previous run end.
|
|
Offsets index bytes within each view,31 entries perview.
|
|
View bytes live in external BONES.DAT, starting at bone_view_data_offsets.
|
|
Native 640x225 pixels have1:2 width:height aspect; source masks are fitted accordingly. Context is neutral; ink is black.
|
|
The black canvas supplies exterior contours; stored ink preserves internal lines.
|
|
Boxes: xmin,ymin,width,height in overview pixels. View0=overview,1=head,
|
|
2=hand,3=foot,4=pelvis/lowerback,5=chest. Selector 0 uses an overview crop. */
|
|
extern const unsigned char bone_boxes[28][4];
|
|
extern const unsigned char bone_detail_view[28];
|
|
extern const unsigned char bone_view_widths[6],bone_view_heights[6];
|
|
extern const unsigned bone_view_data_offsets[BONE_VIEW_COUNT];
|
|
extern const unsigned bone_asset_size, bone_asset_checksum;
|
|
extern const unsigned * const bone_view_offsets[BONE_VIEW_COUNT];
|
|
#endif
|
|
""")
|
|
manifest['geometryROMBytes']=sizes;manifest['detailSelector']=DETAIL_VIEW
|
|
manifest['overviewBoxes']={STRUCTURES[i]:boxes[i*4:i*4+4] for i in range(28)}
|
|
(ROOT/'data/geometry-manifest.json').write_text(json.dumps(manifest,indent=2)+'\n')
|
|
print('PASS28 structures; exact disjoint delta decoding. Metadata ROM bytes:',sizes,'Banked asset bytes:',len(asset),'Unpadded:',raw_size,'Checksum:',checksum)
|
|
|
|
|
|
if __name__=='__main__':main()
|