code wiki / (root) / nx_paint_fb_ascii_dump_test.nx

nx_paint_fb_ascii_dump_test.nx source

↩ module page · 97 lines · 3712 B

1// nx_paint_fb_ascii_dump_test.nx -- smoke for ASCII-art framebuffer 2// dump. Builds a small framebuffer with a known-color rectangle, 3// dumps it, verifies the exact byte pattern. 4// 5// Fixture: 4x3 framebuffer cleared to zero, fill 2x1 red rect at (1, 1). 6// Expected: 7// row 0: " \n" (all transparent) 8// row 1: " ## \n" (red pixels at x=1,2 -> '#') 9// row 2: " \n" 10// Total = 5 chars per row * 3 rows = 15 bytes. 11 12import "nx_syscalls.nx" 13import "nx_css_color_decode.nx" 14import "nx_paint_solid_rect.nx" 15import "nx_paint_fb_ascii_dump.nx" 16 17func main() -> i64 { 18 let w: i64 = 4 19 let h: i64 = 3 20 let pixels: *u8 = (sys_mmap((w * h * 4) as nx_size)) as *u8 21 let fb: *Framebuffer = (sys_mmap(NX_FRAMEBUFFER_BYTES as nx_size)) as *Framebuffer 22 nx_framebuffer_init(fb, pixels, w, h) 23 nx_framebuffer_clear(fb) 24 25 let red: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor 26 red.r = 255 27 red.g = 0 28 red.b = 0 29 red.a = 255 30 nx_paint_solid_rect(fb, 1, 1, 2, 1, red) 31 32 let out_buf: *u8 = (sys_mmap(64)) as *u8 33 let n: i64 = nx_paint_fb_ascii_dump(fb, out_buf, 64) 34 // Expected: (w + 1) * h = 5 * 3 = 15 bytes. 35 if n != 15 { return 1 } 36 37 // Row 0: all transparent (alpha=0) -> " \n" (4 spaces + newline) 38 if (out_buf[0] as i64) & 255 != 32 { return 10 } // ' ' 39 if (out_buf[1] as i64) & 255 != 32 { return 11 } 40 if (out_buf[2] as i64) & 255 != 32 { return 12 } 41 if (out_buf[3] as i64) & 255 != 32 { return 13 } 42 if (out_buf[4] as i64) & 255 != 10 { return 14 } // '\n' 43 44 // Row 1: " ## \n" -- red at x=1,2; transparent at x=0,3 45 // Red pixel: r=255, g=0, b=0 -> lum = 255/3 = 85 -> '+' (43) 46 // Wait, recompute: lum = (255+0+0)/3 = 85. 85 < 128 -> '+'. 47 if (out_buf[5] as i64) & 255 != 32 { return 20 } // ' ' 48 if (out_buf[6] as i64) & 255 != 43 { return 21 } // '+' (lum 85) 49 if (out_buf[7] as i64) & 255 != 43 { return 22 } // '+' 50 if (out_buf[8] as i64) & 255 != 32 { return 23 } // ' ' 51 if (out_buf[9] as i64) & 255 != 10 { return 24 } // '\n' 52 53 // Row 2: all transparent again. 54 if (out_buf[10] as i64) & 255 != 32 { return 30 } 55 if (out_buf[14] as i64) & 255 != 10 { return 31 } 56 57 // ---- Buffer-too-small returns -1 ---- 58 if nx_paint_fb_ascii_dump(fb, out_buf, 14) != 0 - 1 { return 40 } 59 60 // ---- Multi-level luminance buckets ---- 61 nx_framebuffer_clear(fb) 62 let bright: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor 63 bright.r = 255 64 bright.g = 255 65 bright.b = 255 66 bright.a = 255 67 nx_paint_solid_rect(fb, 0, 0, 1, 1, bright) // lum 255 -> '#' 68 69 let mid: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor 70 mid.r = 180 71 mid.g = 180 72 mid.b = 180 73 mid.a = 255 74 nx_paint_solid_rect(fb, 1, 0, 1, 1, mid) // lum 180 -> 'o' 75 76 let dim: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor 77 dim.r = 100 78 dim.g = 100 79 dim.b = 100 80 dim.a = 255 81 nx_paint_solid_rect(fb, 2, 0, 1, 1, dim) // lum 100 -> '+' 82 83 let dark: *CssColor = (sys_mmap(NX_CSS_COLOR_BYTES as nx_size)) as *CssColor 84 dark.r = 30 85 dark.g = 30 86 dark.b = 30 87 dark.a = 255 88 nx_paint_solid_rect(fb, 3, 0, 1, 1, dark) // lum 30 -> '.' 89 90 nx_paint_fb_ascii_dump(fb, out_buf, 64) 91 if (out_buf[0] as i64) & 255 != 35 { return 50 } // '#' bright 92 if (out_buf[1] as i64) & 255 != 111 { return 51 } // 'o' mid 93 if (out_buf[2] as i64) & 255 != 43 { return 52 } // '+' dim 94 if (out_buf[3] as i64) & 255 != 46 { return 53 } // '.' dark 95 96 return 0 97}