-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaic.go
207 lines (171 loc) · 4.73 KB
/
mosaic.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package mosaic
import (
"context"
"image"
"math/rand"
"os"
"sync"
"github.com/disintegration/imaging"
"golang.org/x/image/draw"
)
// Config contains options for mosaic generation.
type Config struct {
Workers int
Blend bool
Scale float64
TileWidth int
TileHeight int
// ResizeTiles indicates whether the tile images should be resized before
// determining their primary color.
ResizeTiles bool
IndexThreshold float64
}
// Generator generates a mosaic image from the source image and the tile images.
type Generator struct {
config Config
index *Index
StatusHandler func(total int64, progress <-chan GeneratorStatus)
}
// NewGenerator creates a new Generator.
func NewGenerator(config Config, index *Index) *Generator {
return &Generator{
config: config,
index: index,
}
}
// GeneratorStatus contains the status of the mosaic generation.
type GeneratorStatus struct {
Bounds image.Rectangle
TileNumber int
Path string
Err error
}
// Generate generates a mosaic image from the source image and the tile images.
func (g *Generator) Generate(ctx context.Context, src image.Image) image.Image {
if g.config.Scale != 0 {
src = imaging.Resize(src, int(float64(src.Bounds().Dx())*g.config.Scale), 0, imaging.Lanczos)
}
output := image.NewRGBA(src.Bounds())
if g.config.Blend {
draw.Copy(output, src.Bounds().Min, src, src.Bounds(), draw.Src, nil)
}
tiles := g.tileize(ctx, src)
statusChans := make([]<-chan GeneratorStatus, g.config.Workers)
for i := 0; i < g.config.Workers; i++ {
statusChans[i] = g.matchAndSwapTiles(output, tiles)
}
g.wait(src.Bounds(), statusChans)
return output
}
func (*Generator) defaultStatusHandler(total int64, ch <-chan GeneratorStatus) {
for range ch {
// do nothing
}
}
// tileize divides the source image into tiles.
func (g *Generator) tileize(ctx context.Context, src image.Image) <-chan image.Image {
ch := make(chan image.Image)
go func() {
defer close(ch)
bounds := src.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y += g.config.TileHeight {
for x := bounds.Min.X; x < bounds.Max.X; x += g.config.TileWidth {
r := image.Rect(x, y, x+g.config.TileWidth, y+g.config.TileHeight)
if r.Max.X > bounds.Max.X {
r.Max.X = bounds.Max.X
}
if r.Max.Y > bounds.Max.Y {
r.Max.Y = bounds.Max.Y
}
select {
case ch <- subImage(src, r):
case <-ctx.Done():
return
}
}
}
}()
return ch
}
func subImage(src image.Image, rect image.Rectangle) image.Image {
si, ok := src.(subImager)
if !ok {
panic("unsupported image type")
}
return si.SubImage(rect)
}
type subImager interface {
SubImage(r image.Rectangle) image.Image
}
func pickOne(s []string) string {
return s[rand.Intn(len(s))]
}
func (g *Generator) matchAndSwapTiles(output draw.Image, tiles <-chan image.Image) <-chan GeneratorStatus {
out := make(chan GeneratorStatus)
go func() {
defer close(out)
for tile := range tiles {
status := tileStats(output.Bounds(), tile.Bounds())
c, err := primaryColor(tile, 0.01)
if err != nil {
status.Err = err
out <- status
continue
}
_, paths := g.index.FindNearest(c)
path := pickOne(paths)
status.Path = path
replacement, err := loadImage(path)
if err != nil {
// This shouldn't happen, because we already loaded all the images.
status.Err = err
out <- status
continue
}
replacement = imaging.Fill(replacement, g.config.TileWidth, g.config.TileHeight, imaging.Center, imaging.Lanczos)
if g.config.Blend {
draw.Draw(output, tile.Bounds(), replacement, image.Point{}, draw.Over)
} else {
draw.Copy(output, tile.Bounds().Min, replacement, replacement.Bounds(), draw.Src, nil)
}
out <- status
}
}()
return out
}
func tileStats(imageBounds, tileBounds image.Rectangle) GeneratorStatus {
row := tileBounds.Min.X / tileBounds.Dx()
col := tileBounds.Min.Y / tileBounds.Dy()
return GeneratorStatus{
Bounds: tileBounds,
TileNumber: 1 + row + col*(imageBounds.Dx()/tileBounds.Dx()),
}
}
func (g *Generator) wait(bounds image.Rectangle, statusChans []<-chan GeneratorStatus) {
statusCh := make(chan GeneratorStatus, g.config.Workers*2)
statusHandler := g.defaultStatusHandler
if g.StatusHandler != nil {
statusHandler = g.StatusHandler
}
totalTiles := int64((bounds.Dx() / g.config.TileWidth) * (bounds.Dy() / g.config.TileHeight))
go statusHandler(totalTiles, statusCh)
var wg sync.WaitGroup
wg.Add(len(statusChans))
for _, ch := range statusChans {
go func(ch <-chan GeneratorStatus) {
for s := range ch {
statusCh <- s
}
wg.Done()
}(ch)
}
wg.Wait()
}
func loadImage(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return imaging.Decode(f)
}