code wiki / _hdl_build / _imgscramble.nx
_imgscramble.nx source
↩ module page · 89 lines · 3576 B
1// _imgscramble.nx -- RED-TEAM INSTRUMENT for the image graders (operator 2026-08-04: "is this eval
2// doing the work or is it junk"): shuffle an image's PxP patches with a seeded deterministic permutation.
3// Scrambling DESTROYS all aesthetic/structural content (the face, the figure, the composition) while
4// approximately preserving LOCAL statistics -- so any judge that scores an image ~equal to its scramble
5// is measuring texture, not beauty, and cannot rank a styled character above a blob. license_tier: ORIGINAL
6// _imgscramble <in.png> <out.png> <patch_px>
7import "nx_syscalls.nx"
8import "nx_png_decoder.nx"
9import "nx_png.nx"
10const SCR_MAGIC_65536: i64 = 65536
11const SCR_LCG_A: i64 = 1103515245
12const SCR_LCG_C: i64 = 12345
13const SCR_LCG_M: i64 = 2147483647
14const SCR_SEED: i64 = 80404
15
16func sc_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func sc_n(v: i64) -> i64 {
18 let b: *u8 = sys_mmap(32)
19 var x: i64 = v; var i: i64 = 31
20 if x == 0 { b[i]=48 as u8; i=i-1 }
21 while x > 0 { b[i]=(48+x%10) as u8; x=x/10; i=i-1 }
22 sys_write(1,(b as i64+i+1) as *u8,31-i)
23 return 0
24}
25func sc_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8) { let c: i64 = s[i] as i64; if c>=48 { if c<=57 { v=v*10+(c-48) } } i=i+1 } return v }
26
27func main(argc: i64, argv: *i64) -> i64 {
28 let inp: *u8 = argv[1] as *u8
29 let outp: *u8 = argv[2] as *u8
30 let P: i64 = sc_atoi(argv[3] as *u8)
31 if P < 2 { sc_w("bad patch size\n" as *u8); return 2 }
32 let lenp: *i64 = sys_mmap(16) as *i64
33 let raw: *u8 = sys_read_file(inp, lenp)
34 if (raw as i64) == 0 { sc_w("read fail\n" as *u8); return 2 }
35 let res: *NxPngResult = nx_png_decode(raw, lenp[0])
36 if (res as i64) == 0 { sc_w("decode null\n" as *u8); return 3 }
37 if res.error_code != 0 { sc_w("decode err " as *u8); sc_n(res.error_code); sc_w("\n" as *u8); return 3 }
38 let hdr: *NxPngHeader = res.header
39 let w: i64 = hdr.width
40 let h: i64 = hdr.height
41 let ch: i64 = res.n_channels
42 let px: *u8 = res.pixels
43 let fb: *i64 = sys_mmap(w*h*8) as *i64
44 var q: i64 = 0
45 while q < w*h {
46 var pr: i64 = 0
47 var pg: i64 = 0
48 var pb: i64 = 0
49 if ch >= 3 { pr = px[q*ch] as i64; pg = px[q*ch+1] as i64; pb = px[q*ch+2] as i64 }
50 if ch <= 2 { pr = px[q*ch] as i64; pg = pr; pb = pr }
51 fb[q] = pr + pg*256 + pb*SCR_MAGIC_65536
52 q = q + 1
53 }
54 let nx: i64 = w/P
55 let ny: i64 = h/P
56 let n: i64 = nx*ny
57 let perm: *i64 = sys_mmap(n*8) as *i64
58 var i: i64 = 0
59 while i < n { perm[i] = i; i = i + 1 }
60 var s: i64 = SCR_SEED
61 i = n - 1
62 while i > 0 {
63 s = (s*SCR_LCG_A + SCR_LCG_C) & SCR_LCG_M
64 let j: i64 = s % (i+1)
65 let t: i64 = perm[i]; perm[i] = perm[j]; perm[j] = t
66 i = i - 1
67 }
68 let ofb: *i64 = sys_mmap(w*h*8) as *i64
69 q = 0
70 while q < w*h { ofb[q] = fb[q]; q = q + 1 } // margins ride through unscrambled
71 i = 0
72 while i < n {
73 let src: i64 = perm[i]
74 let dx: i64 = (i%nx)*P
75 let dy: i64 = (i/nx)*P
76 let sx: i64 = (src%nx)*P
77 let sy: i64 = (src/nx)*P
78 var yy: i64 = 0
79 while yy < P {
80 var xx: i64 = 0
81 while xx < P { ofb[(dy+yy)*w + dx+xx] = fb[(sy+yy)*w + sx+xx]; xx = xx + 1 }
82 yy = yy + 1
83 }
84 i = i + 1
85 }
86 write_png(ofb, w, h, outp)
87 sc_w("SCRAMBLED " as *u8); sc_n(n); sc_w(" patches of " as *u8); sc_n(P); sc_w("px -> " as *u8); sc_w(outp); sc_w("\n" as *u8)
88 return 0
89}