code wiki / (root) / nx_toner_meter.nx

nx_toner_meter.nx source

↩ module page · 33 lines · 1819 B

1// nx_toner_meter.nx -- Calculates toner consumption based on exact pixel coverage of a page bitmap, returning coverage in PPM and standardized page units. 2const K_MAGIC_1000000: i64 = 1000000 3const K_MAGIC_50000: i64 = 50000 4// nx_toner_meter.nx -- REAL toner measurement that ignores the printer's lying gauge. Toner consumed by a 5// page is proportional to its INK COVERAGE (fraction of full-black). Because our sovereign stack owns the 6// page bitmap, we measure EXACT coverage per page -- more accurate than the printer (which counts pages and 7// assumes a flat 5%) and works for ANY cartridge (genuine or aftermarket) because it's physics, not a chip. 8// 9// Coverage is returned in PPM (parts per million of full-black): 0 = blank page, 1,000,000 = solid black, 10// 50,000 = the ISO/IEC 19752 "standard page" (5% coverage). For sgray_8: 0x00=black(full toner), 11// 0xFF=white(none); darkness = 255 - pixel. 12// 13// NEVER-BRICK (#26): pure integer arithmetic, no syscalls. Sovereign (no imports). license_tier: ORIGINAL 14// genealogy_id: project-printer-management-ipp-sclass-2026-06-20 15 16// coverage of an 8-bit grayscale bitmap (npix pixels, 1 byte each) in PPM of full-black. 17func nx_toner_coverage_ppm(bm: *u8, npix: i64) -> i64 { 18 if npix <= 0 { return 0 } 19 var sum: i64 = 0 20 var i: i64 = 0 21 while i < npix { 22 sum = sum + (255 - (bm[i] as i64)) 23 i = i + 1 24 } 25 // fraction = sum / (255*npix); in PPM = sum*1_000_000/(255*npix). (no overflow: < 2.2e15 for a letter page) 26 return (sum * K_MAGIC_1000000) / (255 * npix) 27} 28 29// the same coverage expressed in "standard ISO pages" x1000 (milli-std-pages), for human display: 30// 50,000 ppm = 1.000 std page = 1000 milli-std-pages. 31func nx_toner_std_pages_milli(coverage_ppm: i64) -> i64 { 32 return coverage_ppm * 1000 / K_MAGIC_50000 33}