forked from microo8/wallgen
/
wallgen.go
158 lines (138 loc) · 3.31 KB
/
wallgen.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"github.com/golang/freetype/truetype"
"github.com/microo8/wallgen/data"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"log"
"net/http"
"os"
"sync"
)
const unsplashUrl string = "https://source.unsplash.com/random"
var (
width = flag.Int("w", 1920, "width of the image")
height = flag.Int("h", 1080, "height of the image")
output = flag.String("o", "wallpaper.png", "output file")
text = flag.String("t", "MEH", "printed text")
fontSize = flag.Int("font-size", 120, "Font size for the text")
dpi = flag.Int("dpi", 100, "DPI for the text")
)
type pixelColor struct {
r, g, b, a uint32
}
func (c pixelColor) RGBA() (uint32, uint32, uint32, uint32) {
return c.r, c.g, c.b, c.a
}
//Flip returns a copy of input that has been flipped horizontally and vertically.
func Flip(input image.Image) image.Image {
var wg sync.WaitGroup
//create new image
bounds := input.Bounds()
newImg := image.NewRGBA(bounds)
for x := 0; x < bounds.Max.X; x++ {
x := x
wg.Add(1)
go func() {
defer wg.Done()
for y := 0; y < bounds.Max.Y; y++ {
newImg.Set(bounds.Max.X-x, bounds.Max.Y-y, input.At(x, y))
}
}()
}
wg.Wait()
return newImg
}
//InvertColors returns a copy of input that has its colors inverted.
func InvertColors(input image.Image) image.Image {
var wg sync.WaitGroup
//create new image
bounds := input.Bounds()
newImg := image.NewRGBA(bounds)
var currentPixelColor color.Color
var r, g, b, a uint32
for x := 0; x < bounds.Max.X; x++ {
x := x
wg.Add(1)
go func() {
defer wg.Done()
for y := 0; y < bounds.Max.Y; y++ {
r, g, b, a = input.At(x, y).RGBA()
currentPixelColor = pixelColor{
r: 0xffff - r,
g: 0xffff - g,
b: 0xffff - b,
a: a,
}
newImg.Set(x, y, currentPixelColor)
}
}()
}
wg.Wait()
return newImg
}
func main() {
flag.Parse()
chimg := make(chan image.Image)
go func() {
resp, err := http.Get(fmt.Sprintf("%s/%dx%d", unsplashUrl, *width, *height))
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(resp.Body)
if err != nil {
log.Fatal(err)
}
chimg <- img
}()
//load font
chmask := make(chan image.Image)
go func() {
fontBytes, err := ubuntu.Asset("Ubuntu-B.ttf")
if err != nil {
log.Fatal(err)
}
f, err := truetype.Parse(fontBytes)
if err != nil {
log.Fatal(err)
}
//generate text mast
mask := image.NewRGBA(image.Rect(0, 0, *width, *height))
//draw.Draw(mask, mask.Bounds(), image.White, image.ZP, draw.Src)
d := &font.Drawer{
Dst: mask,
Src: image.White,
Face: truetype.NewFace(f, &truetype.Options{
Size: float64(*fontSize),
DPI: float64(*dpi),
Hinting: font.HintingNone,
}),
}
d.Dot = fixed.Point26_6{
X: (fixed.I(*width) - d.MeasureString(*text)) / 2,
Y: fixed.I(*height) / 2,
}
d.DrawString(*text)
chmask <- mask
}()
mask := <-chmask
img := <-chimg
finalDst := image.NewRGBA(img.Bounds())
changedDst := InvertColors(Flip(img))
//Convert dst
dstB := img.Bounds()
draw.Draw(finalDst, finalDst.Bounds(), img, dstB.Min, draw.Src)
draw.DrawMask(finalDst, finalDst.Bounds(), changedDst, image.ZP, mask, image.ZP, draw.Over)
file, err := os.Create(*output)
if err != nil {
log.Fatal(err)
}
png.Encode(file, finalDst)
}