/
foo.go
133 lines (116 loc) · 2.86 KB
/
foo.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"image"
"image/png"
_ "image/jpeg"
_ "image/gif"
"image/color"
"os"
"bufio"
"math/rand"
"time"
)
type CypherImage interface {
Set(int, int, color.Color)
}
type CypherPixel []bool
func BlackPair() (CypherPixel, CypherPixel) {
aPixel := NewCypherPixel()
bPixel := make(CypherPixel, 4)
for i := range aPixel {
bPixel[i] = !aPixel[i]
}
return aPixel, bPixel
}
func WhitePair() (CypherPixel, CypherPixel) {
aPixel := NewCypherPixel()
bPixel := make(CypherPixel, 4)
copy(bPixel, aPixel)
return aPixel, bPixel
}
func NewCypherPixel() (CypherPixel) {
px := make(CypherPixel, 4)
perm := rand.Perm(4)
for i := range perm {
px[i] = perm[i] % 2 == 0
}
return px
}
func main() {
rand.Seed( time.Now().UTC().UnixNano())
var templatePath string
if len(os.Args) == 1 {
templatePath = "tux.png"
} else {
templatePath = os.Args[1]
}
template, err := readTemplate(templatePath)
if err != nil { panic(err) }
templateBounds := template.Bounds()
sourceHeight := templateBounds.Max.Y
sourceWidth := templateBounds.Max.X
cypherHeight := sourceHeight * 2
cypherWidth := sourceWidth * 2
image1 := image.NewRGBA(image.Rect(0, 0, cypherWidth, cypherHeight))
image2 := image.NewRGBA(image.Rect(0, 0, cypherWidth, cypherHeight))
for x := 0; x <= sourceWidth; x++ {
for y := 0; y <= sourceHeight; y++ {
setPixel(x, y, template.At(x, y), image1, image2)
}
}
writeCypers(image1, image2)
}
func setPixel(x int, y int, col color.Color, cyperImage1 CypherImage, cyperImage2 CypherImage) {
var px1, px2 CypherPixel
grayValue := color.GrayModel.Convert(col).(color.Gray).Y
if grayValue < 127 {
px1, px2 = BlackPair()
} else {
px1, px2 = WhitePair()
}
i := 0
for cX := 2*x; cX <= 2*x+1; cX++ {
for cY := 2*y; cY <= 2*y+1; cY++ {
color1 := colorForPixel(px1[i])
color2 := colorForPixel(px2[i])
cyperImage1.Set(cX, cY, color1)
cyperImage2.Set(cX, cY, color2)
i++
}
}
}
func colorForPixel(isBlack bool) (color.RGBA) {
col := color.RGBA{}
if isBlack { col.A = 255 }
return col
}
func writeCypers(image1 image.Image, image2 image.Image) {
writeCyper(image1, "foo1.png")
writeCyper(image2, "foo2.png")
}
func writeCyper(image image.Image, filename string) {
file, err := os.Create("out/" + filename)
if err != nil { panic(err) }
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
writer := bufio.NewWriter(file)
png.Encode(writer, image)
if err = writer.Flush(); err != nil { panic(err) }
}
func readTemplate(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
reader := bufio.NewReader(file)
img, _, err := image.Decode(reader)
return img, err
}