-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.go
105 lines (98 loc) · 1.84 KB
/
pi.go
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
99
100
101
102
103
104
105
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/png"
"log"
"os"
)
const (
volumes = 100
count = 10000 // images per volume
size = 16
zoom = 8
)
var bits = map[byte][4]byte{
'0': {0, 0, 0, 0},
'1': {0, 0, 0, 1},
'2': {0, 0, 1, 0},
'3': {0, 0, 1, 1},
'4': {0, 1, 0, 0},
'5': {0, 1, 0, 1},
'6': {0, 1, 1, 0},
'7': {0, 1, 1, 1},
'8': {1, 0, 0, 0},
'9': {1, 0, 0, 1},
'a': {1, 0, 1, 0},
'b': {1, 0, 1, 1},
'c': {1, 1, 0, 0},
'd': {1, 1, 0, 1},
'e': {1, 1, 1, 0},
'f': {1, 1, 1, 1},
}
var paletteBW = color.Palette{
color.RGBA{0x00, 0x00, 0x00, 255},
color.RGBA{0xff, 0xff, 0xff, 255},
}
func buildVolume(in *bufio.Reader, vol int) {
id := 0
img := image.NewPaletted(image.Rect(0, 0, size*zoom, size*zoom), paletteBW)
for n := 0; n < count; n++ {
x := 0
y := 0
for {
digit, err := in.ReadByte()
if err != nil {
log.Fatal(err)
}
if digit == '.' {
continue
}
for _, color := range bits[digit] {
for px := 0; px < zoom; px++ {
for py := 0; py < zoom; py++ {
img.SetColorIndex(x+px, y+py, color)
}
}
x += zoom
}
if x >= size*zoom {
x = 0
y += zoom
if y >= size*zoom {
break
}
}
}
fileName := fmt.Sprintf("images/pi-volume-%02d/pi-%02d-%04d.png", vol, vol, id)
fmt.Println(fileName)
f, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
if err := png.Encode(f, img); err != nil {
f.Close()
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
id++
}
}
func main() {
fin, err := os.OpenFile("pi/pi_hex_1b.txt", os.O_RDONLY, 0)
if err != nil {
log.Fatal(err)
}
in := bufio.NewReader(fin)
for vol := 0; vol < volumes; vol++ {
volDir := fmt.Sprintf("images/pi-volume-%02d", vol)
if err := os.MkdirAll(volDir, 0755); err != nil {
log.Fatal(err)
}
buildVolume(in, vol)
}
}