v / vlib / stbi / stbi_test.v
114 lines · 92 sloc · 4.09 KB · 77e978cae58fd2c701bd0cd73168dc28baa2a21f
Raw
1import os
2import stbi
3
4const vroot = @VEXEROOT
5const tfolder = os.join_path(os.vtmp_dir(), 'stbi')
6const logo_path = os.join_path(vroot, 'examples/assets/logo.png')
7const background_path = os.join_path(vroot, 'examples/flappylearning/assets/img/background.png')
8
9fn testsuite_begin() {
10 os.mkdir_all(tfolder) or {}
11}
12
13fn testsuite_end() {
14 os.rmdir_all(tfolder) or {}
15}
16
17fn test_stbi_read_write() {
18 println('Source path: ${logo_path}')
19 d_s := stbi.load(logo_path) or { panic(err) }
20 println('Image source data:\n ${d_s}')
21
22 out_path := os.join_path(tfolder, 'test.png')
23 println('Out path: ${out_path}')
24 stbi.stbi_write_png(out_path, d_s.width, d_s.height, 4, d_s.data, d_s.width * 4) or {
25 panic(err)
26 }
27
28 d_d := stbi.load(out_path) or { panic(err) }
29 println('Image dest data:\n ${d_d}')
30
31 assert d_s.width == d_d.width
32 assert d_s.height == d_d.height
33 assert d_s.nr_channels == d_d.nr_channels
34 assert d_s.original_nr_channels == 4
35
36 mut v_s := unsafe { &u32(d_s.data) }
37 mut v_d := unsafe { &u32(d_d.data) }
38 mut delta := i64(0)
39 for index in 0 .. (d_d.width * d_d.width) {
40 unsafe {
41 delta += v_s[index] - v_d[index]
42 }
43 }
44 assert 0 == delta
45 os.rm(out_path) or {}
46}
47
48fn test_stbi_resize() {
49 println('Source path: ${logo_path}')
50 d_s := stbi.load(logo_path) or { panic(err) }
51 println('Image source data:\n ${d_s}')
52
53 new_width, new_height := 100, 100
54
55 d_r := stbi.resize_uint8(d_s, new_width, new_height) or { panic(err) }
56 assert d_r.original_nr_channels == 4
57 println('Resized Image source data:\n ${d_s}')
58
59 out_path := os.join_path(tfolder, 'test.png')
60 println('Out path: ${out_path}')
61 stbi.stbi_write_png(out_path, d_r.width, d_r.height, 4, d_r.data, d_r.width * 4) or {
62 panic(err)
63 }
64
65 d_d := stbi.load(out_path) or { panic(err) }
66 println('Image dest data:\n ${d_d}')
67
68 assert d_d.width == new_width
69 assert d_d.height == new_height
70 assert d_d.nr_channels == d_r.nr_channels
71 assert d_d.original_nr_channels == 4
72 os.rm(out_path) or {}
73}
74
75fn test_load_image_with_channels_different_than_4() {
76 img := stbi.load(background_path)!
77 assert img.nr_channels == 4, 'by default, stbi.load should convert images to 4 channels'
78 assert img.original_nr_channels == 3, 'the default, should not affect the img.original_nr_channels field; it should be based solely on the original image data on disk'
79
80 img_resized := stbi.resize_uint8(img, 128, 128)!
81 assert img_resized.nr_channels == 4
82 assert img_resized.original_nr_channels == 3
83
84 img3 := stbi.load(background_path, desired_channels: 0)!
85 assert img3.nr_channels == 3, 'stbi.load, with desired_channels: 0, should return an image, without any conversion. the nr_channels should be determined by the image data'
86 assert img.original_nr_channels == 3, 'the default, should not affect the img.original_nr_channels field; it should be based solely on the original image data on disk'
87
88 img3_resized := stbi.resize_uint8(img3, 128, 128)!
89 assert img3_resized.nr_channels == 3
90 assert img3_resized.original_nr_channels == 3
91}
92
93fn test_load_image_from_memory_with_channels_different_than_4() {
94 background_bytes := os.read_bytes(background_path)!
95
96 img := stbi.load_from_memory(background_bytes.data, background_bytes.len)!
97 assert img.nr_channels == 4, 'by default, stbi.load_from_memory should convert images to 4 channels'
98 assert img.original_nr_channels == 3, 'the default should preserve the original channel count as metadata'
99
100 img3 := stbi.load_from_memory(background_bytes.data, background_bytes.len, desired_channels: 0)!
101 assert img3.nr_channels == 3, 'load_from_memory should preserve the source channel count when desired_channels is 0'
102 assert img3.original_nr_channels == 3
103}
104
105fn test_load_from_file_matches_memory_decode() {
106 background_bytes := os.read_bytes(background_path)!
107 img_from_file := stbi.load(background_path)!
108 img_from_memory := stbi.load_from_memory(background_bytes.data, background_bytes.len)!
109 assert img_from_file.width == img_from_memory.width
110 assert img_from_file.height == img_from_memory.height
111 assert img_from_file.nr_channels == img_from_memory.nr_channels
112 assert img_from_file.original_nr_channels == img_from_memory.original_nr_channels
113 assert img_from_file.ext == 'png'
114}
115