forked from egonelbre/gophers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
458 lines (362 loc) · 10.1 KB
/
update.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"image"
"image/jpeg"
"image/png"
"golang.org/x/image/draw"
)
const (
ThumbnailSize = 128
MaxColumns = 6
InkscapePath = `c:\Program Files\Inkscape\inkscape.exe`
)
const README_HEADER = `
# Gophers....
The images and art-work in this repository are under [CC0 license](https://creativecommons.org/publicdomain/zero/1.0/).
However, if you do use something, you are encouraged to:
* tweet about the used, remixed or printed result @egonelbre
* submit new ideas via twitter @egonelbre
* request some sketch to be vectorized
Do note that, the Go gopher was designed by [Renee French](http://reneefrench.blogspot.com/).
The Gopher character design is licensed under the Creative Commons 3.0 Attributions license.
Read http://blog.golang.org/gopher for more details.
<img src=".thumb/animation/2bit-sprite/demo.gif ">
`
const VECTOR_HEADER = `
# Vector
Here are svg images that can be modified for your own needs.
`
const SKETCHES_HEADER = `
# Sketches
Here are several hand-drawn images. Let me know if you would like to
see a particular one be vectorized.
`
type ImageLink struct {
Thumb string
Actual string
Bounds image.Rectangle
}
type Collage struct {
Image *image.RGBA
X, Y int
ColumnsPerRow int
CellSize int
Name string
Output string
Folder string
Links []ImageLink
}
func NewCollage(count, columnsPerRow, cellSize int) *Collage {
if count < columnsPerRow {
columnsPerRow = count
}
rowCount := (count + columnsPerRow - 1) / columnsPerRow
bounds := image.Rect(0, 0, columnsPerRow*cellSize, rowCount*cellSize)
collage := &Collage{
Image: image.NewRGBA(bounds),
X: 0, Y: 0,
ColumnsPerRow: columnsPerRow,
CellSize: cellSize,
}
draw.Draw(collage.Image, collage.Image.Bounds(), image.White, image.ZP, draw.Src)
return collage
}
func (collage *Collage) Bounds(x, y int) image.Rectangle {
x0 := x * collage.CellSize
y0 := y * collage.CellSize
return image.Rect(x0, y0, x0+collage.CellSize, y0+collage.CellSize)
}
func (collage *Collage) Draw(path string, m image.Image) {
frame := collage.Bounds(collage.X, collage.Y)
collage.Links = append(collage.Links, ImageLink{
Actual: path,
Bounds: frame,
})
inner := FitBoundsIntoFrame(m.Bounds(), frame)
draw.CatmullRom.Scale(collage.Image, inner, m, m.Bounds(), draw.Over, nil)
collage.X++
if collage.X >= collage.ColumnsPerRow {
collage.X = 0
collage.Y++
}
}
func MakeCollage(name, folder, output string) *Collage {
log.Printf("Creating collage\n")
log.Printf("> name : %v\n", name)
log.Printf("> folder: %v\n", folder)
log.Printf("> save : %v\n", output)
files, err := ioutil.ReadDir(folder)
if err != nil {
log.Printf("> ERROR: %v\n", err)
return nil
}
if len(files) == 0 {
log.Printf("> error: no files\n")
return nil
}
sort.Sort(FileInfos(files))
collage := NewCollage(len(files), MaxColumns, ThumbnailSize)
collage.Name = name
collage.Output = output
collage.Folder = folder
for _, file := range files {
path := filepath.Join(folder, file.Name())
log.Printf("> add: %v\n", path)
m, err := LoadImage(path)
if err != nil {
log.Printf("> error: %v\n", err)
continue
}
collage.Draw(path, m)
}
if err := SaveImage(collage.Image, output); err != nil {
log.Printf("> ERROR: %v\n", err)
}
return collage
}
type Thumbs struct {
Size int
Name string
Output string
Folder string
Links []ImageLink
}
func (thumbs *Thumbs) ExportSVG(actual, out string) {
os.MkdirAll(filepath.Dir(out), 0755)
os.Remove(out)
// inkscape -h 128 -e hiking.png hiking.svg
cmd := exec.Command(InkscapePath,
"-h", strconv.Itoa(thumbs.Size),
"-e", out,
actual)
cmd.Run()
thumbs.Links = append(thumbs.Links, ImageLink{
Actual: actual,
Thumb: out,
})
}
func (thumbs *Thumbs) Downscale(actual, out string, m image.Image) image.Image {
targetSize := image.Point{0, thumbs.Size}
targetSize.X = m.Bounds().Dx() * thumbs.Size / m.Bounds().Dy()
inner := image.Rectangle{image.ZP, targetSize}
thumbs.Links = append(thumbs.Links, ImageLink{
Actual: actual,
Thumb: out,
Bounds: inner,
})
rgba := image.NewRGBA(inner)
draw.CatmullRom.Scale(rgba, rgba.Bounds(), m, m.Bounds(), draw.Over, nil)
return rgba
}
func MakeThumbs(name, folder, output string) *Thumbs {
log.Printf("Creating thumbs\n")
log.Printf("> name : %v\n", name)
log.Printf("> folder: %v\n", folder)
log.Printf("> save : %v\n", output)
files, err := ioutil.ReadDir(folder)
if err != nil {
log.Printf("> ERROR: %v\n", err)
return nil
}
if len(files) == 0 {
log.Printf("> error: no files\n")
return nil
}
sort.Sort(FileInfos(files))
thumbs := &Thumbs{}
thumbs.Size = ThumbnailSize
thumbs.Name = name
thumbs.Output = output
thumbs.Folder = folder
for _, file := range files {
path := filepath.Join(folder, file.Name())
log.Printf("> add: %v\n", path)
outpath := filepath.Join(output, file.Name())
outpath = ReplaceExt(outpath, ".png")
if filepath.Ext(path) == ".svg" {
thumbs.ExportSVG(path, outpath)
continue
}
m, err := LoadImage(path)
if err != nil {
log.Printf("> error: %v\n", err)
continue
}
out := thumbs.Downscale(path, outpath, m)
if err := SavePNG(out, outpath); err != nil {
log.Printf("> error: %v\n", err)
continue
}
}
return thumbs
}
func main() {
dirs, _ := ioutil.ReadDir("sketch")
sort.Sort(FileInfos(dirs))
sketches := []*Thumbs{}
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
thumbs := MakeThumbs(
strings.Title(dir.Name()),
filepath.Join("sketch", dir.Name()),
filepath.Join(".thumb", "sketch", dir.Name()))
if thumbs != nil {
sketches = append(sketches, thumbs)
}
}
dirs, _ = ioutil.ReadDir("vector")
sort.Sort(FileInfos(dirs))
vectors := []*Thumbs{}
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
thumbs := MakeThumbs(
strings.Title(dir.Name()),
filepath.Join("vector", dir.Name()),
filepath.Join(".thumb", "vector", dir.Name()))
if thumbs != nil {
vectors = append(vectors, thumbs)
}
}
file, err := os.Create("README.md")
if err != nil {
panic(err)
}
defer file.Close()
fmt.Fprintf(file, "%v\n", README_HEADER)
fmt.Fprintf(file, "%v\n", VECTOR_HEADER)
file.Write(CreateThumbsIndex(false, vectors))
fmt.Fprintf(file, "%v\n", SKETCHES_HEADER)
file.Write(CreateThumbsIndex(false, sketches))
}
func CreateThumbsIndex(withtitle bool, thumbsets []*Thumbs) []byte {
var buf bytes.Buffer
for _, thumbs := range thumbsets {
if withtitle {
fmt.Fprintf(&buf, "\n### [%v](%v)\n\n",
thumbs.Name,
filepath.ToSlash(thumbs.Folder))
}
for _, thumb := range thumbs.Links {
fmt.Fprintf(&buf, "[<img src=\"%v\">](%v)\n",
filepath.ToSlash(thumb.Thumb),
filepath.ToSlash(thumb.Actual))
}
}
fmt.Fprintf(&buf, "\n\n")
return buf.Bytes()
}
func CreateCollageIndex(withtitle bool, collages []*Collage) []byte {
var buf bytes.Buffer
for _, collage := range collages {
if withtitle {
fmt.Fprintf(&buf, "\n### [%v](%v)\n\n",
collage.Name,
filepath.ToSlash(collage.Folder))
}
fmt.Fprintf(&buf, "[<img src=\"%v\">](%v)\n",
filepath.ToSlash(collage.Output),
filepath.ToSlash(collage.Folder))
/*
fmt.Fprintf(&buf, "<div>\n")
fmt.Fprintf(&buf, " <img src=\"%v\" usemap=\"#%v\" />\n", , collage.Name)
fmt.Fprintf(&buf, " <map name=\"%v\">\n", collage.Name)
for _, link := range collage.Links {
r := link.Bounds
fmt.Fprintf(&buf, " <area shape=\"rect\" ")
fmt.Fprintf(&buf, "coords=\"%v,%v,%v,%v\" ", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
fmt.Fprintf(&buf, "href=\"%v\" ", filepath.ToSlash(link.Actual))
fmt.Fprintf(&buf, ">\n")
}
fmt.Fprintf(&buf, " </map>\n")
fmt.Fprintf(&buf, "</div>\n\n")
*/
}
return buf.Bytes()
}
/* geometry */
func FitBoundsIntoFrame(bounds, frame image.Rectangle) image.Rectangle {
size := bounds.Size()
frameSize := frame.Size()
targetSize := image.Point{}
aspect := float64(size.X) / float64(size.Y)
if aspect < 1.0 { // x is smaller
targetSize.X = frameSize.Y * size.X / size.Y
targetSize.Y = frameSize.Y
} else { // y is smaller
targetSize.X = frameSize.X
targetSize.Y = frameSize.X * size.Y / size.X
}
frameCenter := frame.Min.Add(frameSize.Div(2))
x0 := frameCenter.X - targetSize.X/2
x1 := frameCenter.X + targetSize.X/2
y0 := frame.Max.Y - targetSize.Y
y1 := frame.Max.Y
return image.Rectangle{
Min: image.Point{x0, y0},
Max: image.Point{x1, y1},
}
}
/* basic file utilties */
type FileInfos []os.FileInfo
func (xs FileInfos) Len() int { return len(xs) }
func (xs FileInfos) Swap(i, k int) { xs[i], xs[k] = xs[k], xs[i] }
func (xs FileInfos) Less(i, k int) bool {
return xs[i].Name() < xs[k].Name()
}
func LoadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
m, _, err := image.Decode(file)
return m, err
}
func ReplaceExt(path, ext string) string {
return path[:len(path)-len(filepath.Ext(path))] + ext
}
func SaveImage(m image.Image, path string) error {
switch filepath.Ext(path) {
case ".jpg":
return SaveJPG(m, path)
case ".png":
return SavePNG(m, path)
}
return errors.New("unknown output format")
}
func SaveJPG(m image.Image, path string) error {
os.MkdirAll(filepath.Dir(path), 0755)
path = ReplaceExt(path, ".jpg")
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return jpeg.Encode(file, m, &jpeg.Options{Quality: 90})
}
func SavePNG(m image.Image, path string) error {
os.MkdirAll(filepath.Dir(path), 0755)
path = ReplaceExt(path, ".png")
path = path[:len(path)-len(filepath.Ext(path))] + ".png"
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, m)
}