code wiki / (root) / nx_galx_thumb.nx

nx_galx_thumb.nx source

↩ module page · 212 lines · 10794 B

1// nx_galx_thumb.nx -- SOVEREIGN gallery THUMBNAIL generator. Composes the existing sovereign codecs 2// (nx_png_decoder = RFC2083 decode, nx_png = RGB8 encode) with an integer box-average downscale -- NO 3// ffmpeg / PIL / libpng. Produces a small thumbnail PNG (max dimension TH_MAX px) so the gallery grid 4// loads ~KB thumbs instead of ~MB full-res images. Output is CID-addressed under knowledge/store/galx-thumb/ 5// so the daemon serves it at GET /thumb/<CID>. 6// 7// MEMORY DISCIPLINE: nx_png_decode mmaps a 64MiB IDAT buffer per call and the toolchain has no sys_munmap, 8// so a single process MUST NOT decode many images. The batch mode therefore FORKS one child per image -- 9// the kernel reclaims every mmap on child exit, bounding RSS regardless of corpus size (same sovereign 10// pattern as nx_galx_fill / nx_torrent_get). 11// 12// modes: 13// nx_galx_thumb one <src.png> <out.png> -- thumbnail a single image (the gate path) 14// nx_galx_thumb batch <start> <count> -- walk knowledge/status/galx_cid_paths.tsv [start,start+count), 15// thumbnail each to galx-thumb/<CID>.png, idempotent (skip if present) 16// license_tier: ORIGINAL 17import "nx_syscalls.nx" 18import "nx_png_decoder.nx" 19import "nx_png.nx" 20import "nx_img_bytes_to_rgb.nx" // R3: THE single gate-proven unified decoder (JPEG/GIF/WebP/BMP/TIFF/TGA/PCX/ICO/PNM + PNG all colour types incl palette + 16-bit) 21const TH_MAGIC_65536: i64 = 65536 22const TH_MAGIC_1000000000: i64 = 1000000000 23const TH_MAGIC_1024: i64 = 1024 24 25const TH_MAX: i64 = 256 // max thumbnail dimension in px (data-driven cap, rule 11) 26const TH_DIR: *u8 = "knowledge/store/galx-thumb" as *u8 27 28func th_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 29func th_n(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 30func th_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ if s[i]>=(48 as u8){ if s[i]<=(57 as u8){ v=v*10+((s[i]-(48 as u8)) as i64) } } i=i+1 } return v } 31func th_cat(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[off+i]=s[i];i=i+1} return off+i } 32// mkdir via x86_64 mkdirat=258 directly (the portable path used across the gallery organs). 33func th_mkdir(path: *u8) -> i64 { return __syscall(258, 0-100, path as i64, 0x1ed, 0, 0, 0) } 34 35// R3 (th_decode_any): the thumbnail DECODER CHOKEPOINT. Composes nx_img_bytes_to_rgb -- the estate's single 36// gate-proven unified decoder (2026-08-05): JPEG baseline+progressive, GIF87a/89a, WebP-lossless, BMP, TIFF, 37// TGA, PCX, ICO/CUR, PNM, and PNG all colour types incl palette + sub-byte depths + Adam7 + 16-bit high byte. 38// Returns interleaved 8-bit RGB (w*h*3), sets wh[0]=w wh[1]=h, and returns 0 on an unsupported or corrupt 39// input -- NEVER a wrong image. This is a strict superset of the old PNG-only path, so palette and 16-bit PNG 40// that flooded galx_pipeline.log with decode errors now thumbnail, and JPEG/GIF/WebP thumbnail for the first 41// time -- one decode ruler, no duplicate. 42func th_decode_any(raw: *u8, rawlen: i64, out_wh: *i64) -> *u8 { 43 return nx_img_bytes_to_rgb(raw, rawlen, out_wh) 44} 45 46// Thumbnail ONE image: read -> decode -> box-average downscale to <=TH_MAX -> write RGB8 PNG. 47// Returns 0 on success, negative on any failure (caller treats negatives as a skip, never a crash). 48func th_one(src: *u8, out: *u8) -> i64 { 49 let szp: *i64 = sys_mmap(16) as *i64 50 let buf: *u8 = sys_read_file(src, szp) 51 let n: i64 = szp[0] 52 if (buf as i64) == 0 { return 0 - 1 } 53 if n < 8 { return 0 - 2 } 54 let whp: *i64 = sys_mmap(16) as *i64 55 let px: *u8 = th_decode_any(buf, n, whp) // sniff + dispatch: JPEG/GIF/WebP/PNG(all)/BMP/TIFF/TGA/PCX/ICO/PNM 56 if (px as i64) == 0 { return 0 - 3 } // unsupported format or corrupt input -- never a wrong image 57 let w: i64 = whp[0] 58 let h: i64 = whp[1] 59 if w <= 0 { return 0 - 4 } 60 if h <= 0 { return 0 - 4 } 61 let nch: i64 = 3 // nx_img_bytes_to_rgb always returns interleaved 8-bit RGB 62 let bpp: i64 = 3 63 let bps: i64 = 1 64 let stride: i64 = w * bpp 65 // target dims: preserve aspect, longest side = TH_MAX; never upscale a small source. 66 var dw: i64 = TH_MAX 67 var dh: i64 = TH_MAX 68 if w >= h { dw = TH_MAX; dh = h * TH_MAX / w } else { dh = TH_MAX; dw = w * TH_MAX / h } 69 if dw < 1 { dw = 1 } 70 if dh < 1 { dh = 1 } 71 if w <= TH_MAX { if h <= TH_MAX { dw = w; dh = h } } 72 let fb: *i64 = sys_mmap(8 * dw * dh) as *i64 73 var dy: i64 = 0 74 while dy < dh { 75 let sy0: i64 = dy * h / dh 76 var sy1: i64 = (dy + 1) * h / dh 77 if sy1 <= sy0 { sy1 = sy0 + 1 } 78 if sy1 > h { sy1 = h } 79 var dx: i64 = 0 80 while dx < dw { 81 let sx0: i64 = dx * w / dw 82 var sx1: i64 = (dx + 1) * w / dw 83 if sx1 <= sx0 { sx1 = sx0 + 1 } 84 if sx1 > w { sx1 = w } 85 var sr: i64 = 0 86 var sg: i64 = 0 87 var sb: i64 = 0 88 var cnt: i64 = 0 89 var yy: i64 = sy0 90 while yy < sy1 { 91 var xx: i64 = sx0 92 while xx < sx1 { 93 let off: i64 = yy * stride + xx * bpp 94 let rr: i64 = px[off] as i64 95 var gg: i64 = rr 96 var bb: i64 = rr 97 if nch >= 3 { 98 gg = px[off + bps] as i64 99 bb = px[off + 2 * bps] as i64 100 } 101 sr = sr + rr 102 sg = sg + gg 103 sb = sb + bb 104 cnt = cnt + 1 105 xx = xx + 1 106 } 107 yy = yy + 1 108 } 109 if cnt < 1 { cnt = 1 } 110 let cr: i64 = sr / cnt 111 let cg: i64 = sg / cnt 112 let cb: i64 = sb / cnt 113 fb[dy * dw + dx] = cr + cg * 256 + cb * TH_MAGIC_65536 114 dx = dx + 1 115 } 116 dy = dy + 1 117 } 118 return write_png(fb, dw, dh, out) 119} 120 121// does a file already exist (and is openable)? -> 1 yes, 0 no. 122func th_exists(path: *u8) -> i64 { 123 let fd: i64 = sys_openat_rd(path) 124 if fd < 0 { return 0 } 125 sys_close(fd) 126 return 1 127} 128 129func main(argc: i64, argv: *i64) -> i64 { 130 if argc < 2 { 131 th_p("usage: nx_galx_thumb one <src> <out> | batch <start> <count>\n" as *u8) 132 sys_exit(2); return 2 133 } 134 let mode: *u8 = argv[1] as *u8 135 th_mkdir(TH_DIR) 136 137 // ---- single-image mode (gate path) ---- 138 if mode[0] == (111 as u8) { // 'o' 139 if argc < 4 { th_p("usage: nx_galx_thumb one <src> <out>\n" as *u8); sys_exit(2); return 2 } 140 let rc: i64 = th_one(argv[2] as *u8, argv[3] as *u8) 141 if rc == 0 { th_p("THUMB ok -> " as *u8); th_p(argv[3] as *u8); th_p("\n" as *u8); sys_exit(0); return 0 } 142 th_p("THUMB FAIL rc=" as *u8); th_n(rc); th_p("\n" as *u8); sys_exit(1); return 1 143 } 144 145 // ---- batch mode: fork one child per image so each decode's mmaps are reclaimed on exit ---- 146 if mode[0] == (98 as u8) { // 'b' 147 var start: i64 = 0 148 var count: i64 = TH_MAGIC_1000000000 149 if argc >= 4 { start = th_atoi(argv[2] as *u8); count = th_atoi(argv[3] as *u8) } 150 let endl: i64 = start + count 151 let szp: *i64 = sys_mmap(16) as *i64 152 let lst: *u8 = sys_read_file("knowledge/status/galx_cid_paths.tsv" as *u8, szp) 153 let lsz: i64 = szp[0] 154 if (lst as i64) == 0 { th_p("THUMB-BATCH FAIL: no sidecar\n" as *u8); sys_exit(1); return 1 } 155 let st: *i64 = sys_mmap(16) as *i64 156 let cid: *u8 = sys_mmap(96) 157 let pathbuf: *u8 = sys_mmap(TH_MAGIC_1024) 158 let outbuf: *u8 = sys_mmap(512) 159 var ls: i64 = 0 160 var i: i64 = 0 161 var lineno: i64 = 0 162 var done: i64 = 0 163 var skip: i64 = 0 164 var fail: i64 = 0 165 while i <= lsz { 166 var isnl: i64 = 0 167 if i == lsz { isnl = 1 } else { if lst[i] == (10 as u8) { isnl = 1 } } 168 if isnl == 1 { 169 let llen: i64 = i - ls 170 if llen > 70 { // need at least 69-char CID + tab + 1-char path 171 if lineno >= start { if lineno < endl { 172 // CID = first 69 bytes; column tab is at ls+69; path = ls+70 .. i 173 var c: i64 = 0 174 while c < 69 { cid[c] = lst[ls + c]; c = c + 1 } cid[69] = 0 as u8 175 if lst[ls + 69] == (9 as u8) { 176 var po: i64 = 0 177 var p: i64 = ls + 70 178 while p < i { pathbuf[po] = lst[p]; po = po + 1; p = p + 1 } pathbuf[po] = 0 as u8 179 var oo: i64 = th_cat(outbuf, 0, TH_DIR) 180 oo = th_cat(outbuf, oo, "/" as *u8) 181 oo = th_cat(outbuf, oo, cid) 182 oo = th_cat(outbuf, oo, ".png" as *u8) 183 outbuf[oo] = 0 as u8 184 if th_exists(outbuf) == 1 { skip = skip + 1 } else { 185 if th_exists(pathbuf) == 0 { skip = skip + 1 } else { // source absent (e.g. NAS unmounted) -> skip, don't fork 186 let pid: i64 = sys_fork() 187 if pid == 0 { 188 let rc: i64 = th_one(pathbuf, outbuf) 189 if rc == 0 { sys_exit(0) } 190 sys_exit(1) 191 } 192 sys_wait4(pid, st, 0) 193 let ec: i64 = (st[0] >> 8) & 0xff 194 if ec == 0 { done = done + 1 } else { fail = fail + 1 } 195 if ((done + fail) % 200) == 0 { th_p(" thumbs done=" as *u8); th_n(done); th_p(" fail=" as *u8); th_n(fail); th_p(" skip=" as *u8); th_n(skip); th_p("\n" as *u8) } 196 } 197 } 198 } 199 }} 200 lineno = lineno + 1 201 } 202 ls = i + 1 203 } 204 i = i + 1 205 } 206 th_p("THUMB-BATCH done=" as *u8); th_n(done); th_p(" fail=" as *u8); th_n(fail); th_p(" skip=" as *u8); th_n(skip); th_p(" (lines " as *u8); th_n(start); th_p(".." as *u8); th_n(endl); th_p(")\n" as *u8) 207 sys_exit(0); return 0 208 } 209 210 th_p("unknown mode\n" as *u8) 211 sys_exit(2); return 2 212}