/
flat-init.go
79 lines (70 loc) · 1.48 KB
/
flat-init.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
package main
import (
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"os"
"sort"
"sync"
)
func check(e error) {
if e != nil {
fmt.Println("NOPE")
panic(e)
}
}
func main() {
srcPath := os.Args[1]
dstPath := os.Args[2]
reader, err := os.Open(srcPath)
check(err)
src, srcFmt, err := image.Decode(reader)
check(err)
fmt.Println("Decoded source", srcPath, "as", srcFmt)
scale := 8
boundsMax := src.Bounds().Max
smallMax := boundsMax.Div(scale)
flat := image.NewNRGBA64(image.Rectangle{image.ZP, smallMax})
samplePix := func(x, y int) (r, g, b []int) {
var rs, gs, bs []int
for i := x - 25; i <= x+25; i += 5 {
if i < 0 || i >= boundsMax.X {
continue
}
for j := y - 25; j <= y+25; j += 5 {
if j < 0 || j >= boundsMax.Y {
continue
}
r, g, b, _ := src.At(i, j).RGBA()
rs = append(rs, int(r))
gs = append(gs, int(g))
bs = append(bs, int(b))
}
}
return rs, gs, bs
}
var wg sync.WaitGroup
for i := 0; i < smallMax.X; i++ {
for j := 0; j < smallMax.Y; j++ {
wg.Add(1)
go func(x, y int) {
defer wg.Done()
r, g, b := samplePix(x*scale+scale/2, y*scale+scale/2)
sort.Ints(r)
sort.Ints(g)
sort.Ints(b)
idx := len(r) / 8
flat.SetNRGBA64(x, y, color.NRGBA64{
uint16(r[idx]), uint16(g[idx]), uint16(b[idx]),
0xFFFF})
}(i, j)
}
}
wg.Wait()
writer, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE, 0600)
check(err)
//jpeg.Encode(writer, proxy, nil)
png.Encode(writer, flat)
}