Files
MatchMaster_level/scripts/calibrate-level-number.mjs
T

13 lines
1.5 KiB
JavaScript

// Explicit glyph calibration, never called by the application; no board answers.
import{readFileSync as read,writeFileSync as write}from'node:fs';import{gunzipSync}from'node:zlib';
import{readNumber}from'../colorBlock-level-editor/src/local-vision.js';
const root=new URL('../colorBlock-level-editor/',import.meta.url),templates=[],provenance=[];
for(const [source,label]of [['level17/shot0','17'],['shot0','18']]){
const base='test/fixtures/local-vision/'+source,frame={...JSON.parse(read(new URL(base+'.json',root))),data:new Uint8ClampedArray(gunzipSync(read(new URL(base+'.rgba.gz',root))))};
const result=readNumber(frame,{x:0,y:0,w:frame.width*.32,h:frame.height*.30},{bright:true,minHeight:frame.width*.045,maxHeight:frame.width*.13});
if(result.digits.length!==label.length)throw Error('Unexpected glyph geometry');
result.digits.forEach((d,i)=>{const b=d.box,rows=[];for(let y=0;y<32;y++){let row='';for(let x=0;x<24;x++){const px=b.x+Math.floor((x+.5)*b.w/24),py=b.y+Math.floor((y+.5)*b.h/32),p=(py*frame.width+px)*4;row+=(frame.data[p]>215&&frame.data[p+1]>215&&frame.data[p+2]>210)?'1':'0';}rows.push(row);}templates.push({digit:label[i],family:'level',aspect:b.w/b.h,rows});provenance.push({source,digit:label[i],box:b});});
}
write(new URL('src/vision/level-digit-templates.js',root),'// Manually labeled single glyphs from Level HUD; no full number/level answers.\nexport const LEVEL_DIGIT_TEMPLATES='+JSON.stringify(templates)+';\n');
write(new URL('test/fixtures/local-vision/level-glyph-calibration.json',root),JSON.stringify(provenance,null,2)+'\n');