1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
// 25 june 2016
package main
import (
"fmt"
"os"
"image"
"image/draw"
_ "image/png"
)
type img struct {
filename string
data []byte
width int
height int
stride int
}
func main() {
if len(os.Args[1:]) == 0 {
panic("no files specified")
}
images := make([]*img, 0, len(os.Args[1:]))
for _, fn := range os.Args[1:] {
f, err := os.Open(fn)
if err != nil {
panic(err)
}
ii, _, err := image.Decode(f)
if err != nil {
panic(err)
}
f.Close()
i := image.NewRGBA(ii.Bounds())
draw.Draw(i, i.Rect, ii, ii.Bounds().Min, draw.Src)
im := &img{
filename: fn,
data: i.Pix,
width: i.Rect.Dx(),
height: i.Rect.Dy(),
stride: i.Stride,
}
images = append(images, im)
}
fmt.Println("// auto-generated by images/gen.go")
fmt.Println("#include \"test.h\"")
fmt.Println()
for i, im := range images {
fmt.Printf("static const uint32_t dat%d[] = {", i)
for j := 0; j < len(im.data); j += 4 {
if (j % (16 * 4)) == 0 {
fmt.Printf("\n\t")
} else {
fmt.Printf(" ")
}
d := uint32(im.data[j + 0]) << 16
d |= uint32(im.data[j + 1]) << 8
d |= uint32(im.data[j + 2])
d |= uint32(im.data[j + 3]) << 24
fmt.Printf("0x%08X,", d)
}
fmt.Println("\n};")
fmt.Println()
}
fmt.Println("static const struct {")
fmt.Println(" const char *name;")
fmt.Println(" void *data;")
fmt.Println(" int width;")
fmt.Println(" int height;")
fmt.Println(" int stride;")
fmt.Println("} files[] = {")
for i, im := range images {
fmt.Printf(" { %q, dat%d, %d, %d, %d },\n",
im.filename, i, im.width, im.height, im.stride)
}
fmt.Println("};")
fmt.Println()
fmt.Println("void appendImageNamed(uiImage *img, const char *name)")
fmt.Println("{")
fmt.Println(" int i;")
fmt.Println("")
fmt.Println(" i = 0;")
fmt.Println(" for (;;) {")
fmt.Println(" if (strcmp(name, files[i].name) == 0) {")
fmt.Println(" uiImageAppend(img, files[i].data, files[i].width, files[i].height, files[i].stride);")
fmt.Println(" return;")
fmt.Println(" }")
fmt.Println(" i++;")
fmt.Println(" }")
fmt.Println("}")
fmt.Println()
}
|