nx_likeness_lib.nx
buildroot/runtime/nx_likeness_lib.nx
about
nx_likeness_lib.nx -- THE IMAGE-LEVEL LIKENESS REFEREE (aesthetictwin AT45, 2026-09-18). The board had no ruler that reads a
RENDER against the PHOTOGRAPHS of the person it claims to be, so a render the operator called an ugly ventriloquist dummy was
published beside its reference with every gate green. This lib is that ruler, and every layer of the feedback loop above it
(fit, morph, texture, hair) is judged by it. It COMPOSES the estate's incumbents and re-implements none of them:
nx_photomark_lib decodes a JPEG or PNG through one door (pm_decode_file), places the four canthi and the five mouth
landmarks (pm_landmarks_cascade, pm_landmarks), measures the roll-corrected canthal tilts, and parses
the OUTSIDE oracle rows (pm_field, pm_span_int) the intake lane already banks per reference still
nx_visdesc the grayscale edge-orientation layout descriptor (80 integers) and its L1 distance -- the STRUCTURE axis
nx_cosine_similarity the signed Q10 cosine over the same two vectors -- structure read a second way
nx_skinsample_lib the skin chroma rule and the linear-light mean converted to CIE L*a*b* -- the TONE axis
THE SHAPE OF THE VERDICT. Two images are ALIGNED into one canvas by the similarity that maps each face's two eye centres onto
the canvas's two eye centres (the same normalisation on both sides, so a roll or a scale in either still is undone before
anything is compared), then SEVEN AXES are measured between the aligned pair and each is published on its own:
geom_nme_permil mean landmark distance in the canvas, permil of the canvas eye span (the four canthi, plus the five mouth
landmarks when both images carry a mouth); needs both images landmarked by a passing eye pair
tilt_delta_deg10 the larger of the two roll-corrected canthal-tilt differences; needs both tilts measured
struct_l1 nx_visdesc_l1 between the two aligned crops' descriptors
struct_cos_dist NX_COSINE_Q minus the Q10 cosine of the same descriptors (0 = parallel)
tone_dl_e3, tone_da_e3, tone_db_e3 the absolute L*, a*, b* differences (x1000) of the skin-masked linear mean of each crop;
need skin pixels in both crops
THE SAME-PERSON BAND is not a number anyone typed: `lk_band` measures every axis on EVERY PAIR of reference stills of the one
person and the band per axis is the WORST (largest) distance any same-person pair reached. A subject passes an axis iff its
distance to its NEAREST reference is within that band; the verdict is the count of axes inside, outside and unmeasured, and
INSIDE only when no measured axis is outside (the MIN-headline doctrine: no axis may be compensated by another, so there is no
weighted sum and no coefficient). The band prints the pair that set it and how many pairs derived it.
EVERY NUMBER ANNOUNCES ITS RESOURCE. The canvas side is TWICE THE SMALLEST REFERENCE EYE SPAN, floored to the descriptor's grid
(no reference is ever upsampled; a subject smaller than that is announced as upsampled); the eye line sits at 340 permil of the
canvas height because on the anchor oracle row (ri_diora_baird_14: brow top 127.8 px, eye line 197.3 px, eye span 103.4 px) the
brow top sits 680 permil of the eye span above the eye line, so with a canvas two spans wide the brow top lands on the canvas's
top edge and the bottom edge falls 1.32 spans below the eye line -- past the stomion (1.15 spans on the same row) and above the
chin (1.65). The smallest canvas is VD_GRID cells of three pixels, the least a Sobel neighbourhood needs.
ALIGNMENT SOURCES ARE NAMED, NEVER SILENT. A still with an oracle row (knowledge/refintake/<slug>.oracle, matched by basename)
aligns on the oracle's canthi at the oracle's tenth-pixel precision; a subject without one runs the photo ruler's cascade route,
then its skin route, and when the skin route REFUSES its eye pair but still holds candidate canthi, those candidates align the
crop while the geometry and tilt axes read UNMEASURED (the pair was refused, so its landmarks are not evidence). MEASURED the day
this was written: on the rejected render diora_baird_close.png the cascade's face box was the nose tip and no eye pair followed,
and the route chooser never reaches the skin route on that refusal -- a subject the ruler cannot align reads UNMEASURED, which is
neither INSIDE nor OUTSIDE, so the ladder exists to turn "could not look" into a named, degraded look wherever one is possible.
Sides follow the image: R is the image-left eye (the subject's right), as the photo ruler and the oracle rows agree.
LIB, no main. license_tier: ORIGINAL No hw writes (Rule 26).
dependencies 4 imports · 2 importers
imports: nx_syscalls.nxnx_photomark_lib.nxnx_visdesc.nxnx_cosine_similarity.nx
imported by: nx_likeness.nxnx_likeness_gate.nx
structs
| none |
consts
| 46 | const LK_I64: i64 = 8 |
| 47 | const LK_BPP: i64 = 3 |
| 48 | const LK_BYTE: i64 = 255 |
| 49 | const LK_DECI: i64 = 10 // landmarks are carried in tenths of a pixel: the oracle's own unit |
| 50 | const LK_DECI_HALF: i64 = 5 |
| 51 | const LK_PERMIL: i64 = 1000 |
| 52 | const LK_Q8: i64 = 256 // bilinear weights in 1/256 of a pixel |
| 53 | const LK_Q8_HALF: i64 = 128 |
| 54 | const LK_NL: i64 = 10 |
| 55 | const LK_CR: i64 = 13 |
| 56 | const LK_SPACE: i64 = 32 |
| 57 | const LK_TAB: i64 = 9 |
| 58 | const LK_HASH: i64 = 35 |
| 59 | const LK_AT: i64 = 64 |
| 60 | const LK_SLASH: i64 = 47 |
| 61 | const LK_PNG_SIG0: i64 = 137 |
| 62 | const LK_JPEG_SIG0: i64 = 255 |
| 63 | const LK_DIRECTIVE_ORACLE: *u8 = "@oracle " |
| 65 | const LK_EYE_Y_PERMIL: i64 = 340 |
| 66 | const LK_EYE_Y_BASIS_BROW_PERMIL: i64 = 680 |
| 67 | const LK_EYE_Y_BASIS: *u8 = "ri_diora_baird_14" |
| 68 | const LK_CANVAS_SPANS: i64 = 2 |
| 69 | const LK_CANVAS_QUARTER: i64 = 4 // the eye centres sit a quarter of the width in from each side: two spans wide |
| 70 | const LK_SOBEL_CELL: i64 = 3 // the least pixels a descriptor cell needs to hold one interior Sobel neighbourhood |
| 72 | const LK_OF_TILT_R: i64 = 21 |
| 73 | const LK_OF_TILT_L: i64 = 22 |
| 75 | const LK_AL_NONE: i64 = 0 |
| 76 | const LK_AL_ORACLE: i64 = 1 |
| 77 | const LK_AL_CASCADE: i64 = 2 |
| 78 | const LK_AL_SKIN: i64 = 3 |
| 79 | const LK_AL_CANDIDATES: i64 = 4 |
| 81 | const LK_OK: i64 = 0 |
| 82 | const LK_E_READ: i64 = 0 - 1 |
| 83 | const LK_E_DECODE: i64 = 0 - 2 |
| 84 | const LK_E_UNALIGNED: i64 = 0 - 3 |
| 86 | const LK_P_RLAT: i64 = 0 |
| 87 | const LK_P_RMED: i64 = 1 |
| 88 | const LK_P_LMED: i64 = 2 |
| 89 | const LK_P_LLAT: i64 = 3 |
| 90 | const LK_P_LS: i64 = 4 |
| 91 | const LK_P_STO: i64 = 5 |
| 92 | const LK_P_LI: i64 = 6 |
| 93 | const LK_P_CHR: i64 = 7 |
| 94 | const LK_P_CHL: i64 = 8 |
| 95 | const LK_P_CANTHI: i64 = 4 |
| 96 | const LK_P_N: i64 = 9 |
| 98 | const LK_I_RGB: i64 = 0 |
| 99 | const LK_I_W: i64 = 1 |
| 100 | const LK_I_H: i64 = 2 |
| 101 | const LK_I_CODEC: i64 = 3 |
| 102 | const LK_I_ALIGN: i64 = 4 |
| 103 | const LK_I_LM: i64 = 5 // LK_P_N points, x then y, deci-pixels: slots 5..22 |
| 104 | const LK_I_MOUTH: i64 = 23 |
| 105 | const LK_I_TILT_R: i64 = 24 |
| 106 | const LK_I_TILT_L: i64 = 25 |
| 107 | const LK_I_TILT_OK: i64 = 26 |
| 108 | const LK_I_ERX: i64 = 27 // the eye centres (midpoint of each eye's canthi), deci-pixels |
| 109 | const LK_I_ERY: i64 = 28 |
| 110 | const LK_I_ELX: i64 = 29 |
| 111 | const LK_I_ELY: i64 = 30 |
| 112 | const LK_I_IOD: i64 = 31 // the eye-centre span, deci-pixels |
| 113 | const LK_I_EYES: i64 = 32 // 1 when the canthi are landmarks (a passing eye pair or an oracle row) |
| 114 | const LK_I_STATUS: i64 = 33 |
| 115 | const LK_I_ROUTE: i64 = 34 |
| 116 | const LK_I_PAIRFAIL: i64 = 35 |
| 117 | const LK_I_UPSAMPLED: i64 = 36 |
| 118 | const LK_I_N: i64 = 37 |
| 120 | const LK_F_CROP: i64 = 0 |
| 121 | const LK_F_GRAY: i64 = 1 |
| 122 | const LK_F_SKIN_N: i64 = 2 |
| 123 | const LK_F_L: i64 = 3 |
| 124 | const LK_F_A: i64 = 4 |
| 125 | const LK_F_B: i64 = 5 |
| 126 | const LK_F_GRID: i64 = 6 // cells per side of the shading and texture grids |
| 127 | const LK_F_SHADE: i64 = 7 // pointer: grid*grid cell-mean luminances |
| 128 | const LK_F_TEX: i64 = 8 // pointer: grid*grid cell gradient energies, normalised to sum LK_PERMIL |
| 129 | const LK_F_TEXE: i64 = 9 // gradient energy per interior pixel (|gx| + |gy|, Sobel) |
| 130 | const LK_F_VD: i64 = 10 // nx_visdesc_dim() slots follow |
| 132 | const LK_AX_GEOM: i64 = 0 |
| 133 | const LK_AX_TILT: i64 = 1 |
| 134 | const LK_AX_SL1: i64 = 2 |
| 135 | const LK_AX_SCOS: i64 = 3 |
| 136 | const LK_AX_TL: i64 = 4 |
| 137 | const LK_AX_TA: i64 = 5 |
| 138 | const LK_AX_TB: i64 = 6 |
| 139 | const LK_AX_SHADE: i64 = 7 // NX_COSINE_Q minus the cosine of the two CENTRED shading grids (Pearson r as a Q10 distance) |
| 140 | const LK_AX_TEX: i64 = 8 // L1 between the two normalised texture-energy layouts (0..2 * LK_PERMIL) |
| 141 | const LK_AX_TEXE: i64 = 9 // absolute difference of gradient energy per pixel |
| 142 | const LK_AX_N: i64 = 10 |
| 143 | const LK_AXR_MEAS: i64 = 10 // LK_AX_N flags: 1 measured, 0 unmeasured (slots 10..19) |
| 144 | const LK_AXR_GEOM_PTS: i64 = 20 |
| 145 | const LK_AXR_COS: i64 = 21 |
| 146 | const LK_AXR_N: i64 = 22 |
| 148 | const LK_B_MAX: i64 = 0 // LK_AX_N slots each |
| 149 | const LK_B_MEAN: i64 = 10 |
| 150 | const LK_B_PI: i64 = 20 |
| 151 | const LK_B_PJ: i64 = 30 |
| 152 | const LK_B_PAIRS: i64 = 40 |
| 153 | const LK_B_TOTAL: i64 = 50 |
| 154 | const LK_B_N: i64 = 51 |
| 156 | const LK_S_DIST: i64 = 0 // LK_AX_N slots: the nearest reference's distance per axis |
| 157 | const LK_S_NEAR: i64 = 10 // LK_AX_N slots: which reference |
| 158 | const LK_S_STATE: i64 = 20 // LK_AX_N slots: LK_ST_* |
| 159 | const LK_S_INSIDE: i64 = 30 |
| 160 | const LK_S_OUTSIDE: i64 = 31 |
| 161 | const LK_S_UNMEASURED: i64 = 32 |
| 162 | const LK_S_VERDICT: i64 = 33 |
| 163 | const LK_S_N: i64 = 34 |
| 164 | const LK_ST_UNMEASURED: i64 = 0 |
| 165 | const LK_ST_INSIDE: i64 = 1 |
| 166 | const LK_ST_OUTSIDE: i64 = 2 |
| 168 | const LK_V_INSIDE: i64 = 0 |
| 169 | const LK_V_OUTSIDE: i64 = 1 |
| 170 | const LK_V_UNMEASURED: i64 = 5 |
| 171 | const LK_V_NOBAND: i64 = 6 |
| 173 | const LK_C_FACE: i64 = 0 |
| 174 | const LK_C_EYE: i64 = 1 |
| 175 | const LK_C_OBUF: i64 = 2 |
| 176 | const LK_C_OLEN: i64 = 3 |
| 177 | const LK_C_OPATH: i64 = 4 |
| 178 | const LK_C_FACE_RC: i64 = 5 |
| 179 | const LK_C_EYE_RC: i64 = 6 |
| 180 | const LK_C_N: i64 = 7 |
| 982 | const LK_R_IMGS: i64 = 0 |
| 983 | const LK_R_FEATS: i64 = 1 |
| 984 | const LK_R_PATHS: i64 = 2 |
| 985 | const LK_R_N: i64 = 3 |
| 986 | const LK_R_LISTED: i64 = 4 |
| 987 | const LK_R_UNREADABLE: i64 = 5 |
| 988 | const LK_R_UNDECODABLE: i64 = 6 |
| 989 | const LK_R_UNALIGNED: i64 = 7 |
| 990 | const LK_R_CANVAS: i64 = 8 |
| 991 | const LK_R_MIN_IOD: i64 = 9 |
| 992 | const LK_R_MIN_PATH: i64 = 10 |
| 993 | const LK_R_BAND: i64 = 11 |
| 994 | const LK_R_PAIRS: i64 = 12 |
| 995 | const LK_R_ORACLE_BYTES: i64 = 13 |
| 996 | const LK_R_GRID: i64 = 14 |
| 997 | const LK_R_GEOM_BAND: i64 = 15 |
| 998 | const LK_R_GEOM_PAIRS: i64 = 16 |
| 999 | const LK_R_SLOTS: i64 = 17 |
functions
| 182 | func lk_ruler_version() -> i64 { return 1 } called by 1: lk_print_canvas |
| 183 | func lkh_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } |
| 184 | func lkh_max(a: i64, b: i64) -> i64 { if a > b { return a } return b } called by 1: lk_axes |
| 185 | func lkh_min(a: i64, b: i64) -> i64 { if a < b { return a } return b } |
| 187 | func lkh_div_round(num: i64, den: i64) -> i64 |
| 192 | func lkh_div_floor(num: i64, den: i64) -> i64 called by 1: lk_sample |
| 197 | func lkh_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } |
| 198 | func lkh_streq(a: *u8, b: *u8) -> i64 |
| 205 | func lkh_basename(p: *u8) -> *u8 |
| 212 | func lkh_span_copy(buf: *u8, s: i64, e: i64) -> *u8 |
| 220 | func lk_align_name(k: i64) -> *u8 called by 1: lk_print_img |
| 227 | func lk_axis_name(k: i64) -> *u8 |
| 240 | func lk_state_name(k: i64) -> *u8 called by 1: lk_print_score |
| 245 | func lk_verdict_name(k: i64) -> *u8 called by 1: lk_print_verdict |
| 253 | func lk_ctx_new() -> *i64 |
| 263 | func lk_ctx_cascades(ctx: *i64) -> i64 |
| 269 | func lk_ctx_oracle(ctx: *i64, path: *u8) -> i64 |
| 281 | func lk_img_new() -> *i64 |
| 287 | func lk_img_set_rgb(img: *i64, rgb: *u8, w: i64, h: i64) -> i64 |
| 293 | func lk_pt_x(img: *i64, p: i64) -> i64 { return img[LK_I_LM + 2 * p] } |
| 294 | func lk_pt_y(img: *i64, p: i64) -> i64 { return img[LK_I_LM + 2 * p + 1] } |
| 295 | func lk_pt_set(img: *i64, p: i64, x: i64, y: i64) -> i64 { img[LK_I_LM + 2 * p] = x; img[LK_I_LM + 2 * p + 1] = y; return 0 } |
| 297 | func lk_img_finish(img: *i64) -> i64 |
| 311 | func lk_field_xy10(buf: *u8, ls: i64, le: i64, idx: i64, out2: *i64) -> i64 |
| 325 | func lkh_line_end(buf: *u8, ls: i64, n: i64) -> i64 |
| 334 | func lk_oracle_row(ctx: *i64, base: *u8, out2: *i64) -> i64 |
| 357 | func lk_landmarks_from_oracle(buf: *u8, ls: i64, le: i64, img: *i64) -> i64 |
| 384 | func lk_landmarks_from_ruler(res: *i64, img: *i64, align: i64) -> i64 |
| 411 | func lk_align_ruler(ctx: *i64, img: *i64) -> i64 |
| 442 | func lk_load(ctx: *i64, path: *u8, img: *i64) -> i64 |
| 463 | func lk_map(aRx: i64, aRy: i64, aLx: i64, aLy: i64, bRx: i64, bRy: i64, bLx: i64, bLy: i64, px: i64, py: i64, out2: *i64) -> i64 |
| 480 | func lk_sample(rgb: *u8, w: i64, h: i64, sx: i64, sy: i64, out3: *i64) -> i64 |
| 518 | func lk_resample(img: *i64, dRx: i64, dRy: i64, dLx: i64, dLy: i64, ow: i64, oh: i64) -> *u8 |
| 539 | func lk_img_map_landmarks(src: *i64, dst: *i64, dRx: i64, dRy: i64, dLx: i64, dLy: i64) -> i64 |
| 559 | func lk_canvas(min_iod_deci: i64) -> i64 |
| 566 | func lk_canvas_eye_rx(c: i64) -> i64 { return (c / LK_CANVAS_QUARTER) * LK_DECI } |
| 567 | func lk_canvas_eye_lx(c: i64) -> i64 { return (c - c / LK_CANVAS_QUARTER) * LK_DECI } |
| 568 | func lk_canvas_eye_y(c: i64) -> i64 { return (c * LK_EYE_Y_PERMIL / LK_PERMIL) * LK_DECI } |
| 569 | func lk_canvas_span(c: i64) -> i64 { return lk_canvas_eye_lx(c) - lk_canvas_eye_rx(c) } called by 5: mainlk_grid_from_bandlk_featureslk_geom_pairlk_print_canvas calls 2: lk_canvas_eye_lxlk_canvas_eye_rx |
| 571 | func lk_crop(img: *i64, c: i64) -> *u8 |
| 574 | func lk_feat_new() -> *i64 |
| 581 | func lk_feat_vd(feat: *i64) -> *i64 { return (feat as i64 + LK_F_VD * LK_I64) as *i64 } |
| 585 | func lk_grid_from_band(c: i64, geom_band_permil: i64) -> i64 |
| 593 | func lk_features(img: *i64, c: i64, grid: i64, feat: *i64) -> i64 called by 3: mainlk_refs_loadlk_score_subject calls 13: lk_cropsys_mmappm_lumanx_visdesc_extractlk_feat_vdvd_wrap_gray+7 |
| 665 | func lk_centred_cos(a: *i64, b: *i64, n: i64) -> i64 |
| 680 | func lk_geom_pair(imgA: *i64, imgB: *i64, c: i64, out2: *i64) -> i64 called by 2: lk_band_geometrylk_axes calls 10: lk_canvas_eye_rxlk_canvas_eye_lxlk_canvas_eye_ysys_mmaplk_maplk_pt_x+4 |
| 707 | func lk_band_geometry(imgs: *i64, n: i64, c: i64, pairs_out: *i64) -> i64 |
| 728 | func lk_axes(imgA: *i64, featA: *i64, imgB: *i64, featB: *i64, c: i64, ax: *i64) -> i64 called by 3: mainlk_bandlk_score_img calls 9: sys_mmaplk_geom_pairlkh_maxlkh_abslk_feat_vdnx_visdesc_l1+3 |
| 781 | func lk_band(imgs: *i64, feats: *i64, n: i64, c: i64, band: *i64) -> i64 |
| 815 | func lk_score_img(sub: *i64, subf: *i64, imgs: *i64, feats: *i64, n: i64, c: i64, band: *i64, sc: *i64) -> i64 |
| 858 | func lk_list_read(ctx: *i64, path: *u8, out_n: *i64) -> *i64 called by 1: lk_refs_load calls 6: sys_mmapsys_read_filelkh_span_copylkh_slenlkh_line_endlk_ctx_oracle |
| 918 | func lk_print_img(label: *u8, path: *u8, img: *i64) -> i64 |
| 929 | func lk_print_feat(path: *u8, feat: *i64) -> i64 |
| 936 | func lk_print_canvas(c: i64, min_iod: i64, min_path: *u8, nrefs: i64) -> i64 called by 2: mainlk_refs_load calls 7: pm_putspm_kvlk_ruler_versionlk_canvas_spanlk_canvas_eye_rxlk_canvas_eye_lx+1 |
| 946 | func lk_print_band(band: *i64, paths: *i64) -> i64 |
| 960 | func lk_print_score(path: *u8, sc: *i64, band: *i64, paths: *i64) -> i64 |
| 973 | func lk_print_verdict(path: *u8, sc: *i64) -> i64 |
| 1002 | func lk_refs_load(ctx: *i64, list: *u8, set: *i64) -> i64 called by 3: mainmainlk_score calls 17: sys_mmaplk_list_readpm_putspm_kvlk_ctx_cascadeslk_img_new+11 |
| 1079 | func lk_score_subject(set: *i64, path: *u8, sub: *i64, sc: *i64) -> i64 |
| 1090 | func lk_score(ctx: *i64, subject: *u8, list: *u8, sc: *i64) -> i64 |