-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
119 lines (98 loc) · 2.43 KB
/
main.go
File metadata and controls
119 lines (98 loc) · 2.43 KB
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
package main
import (
"fmt"
"github.com/nfnt/resize"
image2 "image"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"math"
"net/http"
"sync"
)
var error []byte
var cacheLock sync.Mutex
var cache map[string][]byte = make(map[string][]byte)
func splitColor(color uint32) (uint32, uint32, uint32) {
return color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF
}
func colorDelta(c uint32, d uint32) int64 {
r, g, b := splitColor(c)
tR, tG, tB := splitColor(d)
factorR := int64(int(tR) - int(r))
factorG := int64(int(tG) - int(g))
factorB := int64(int(tB) - int(b))
delta := (factorR * factorR) + (factorG * factorG) + (factorB * factorB)
return delta
}
var palette []uint32
var lumaCache map[uint32]uint64 = make(map[uint32]uint64)
var paletteSet map[uint32]bool
func CalculateColorLuma(col uint32) uint64 {
rr, gg, bb := splitColor(col)
r, g, b := float64(rr), float64(gg), float64(bb)
r = math.Pow(r, 2)
g = math.Pow(g, 2)
b = math.Pow(b, 2)
r = .299 * r
g = .587 * g
b = .114 * b
importance := math.Sqrt(r + g + b)
importance = importance * 1000
return uint64(importance) + 1000
}
var FIF_altMode = true
func a(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
return
}
a := req.URL.Query()
if _, ok := a["frog"]; ok == true {
cacheLock.Lock()
if entry, ok := cache[a["frog"][0]]; ok == true {
cacheLock.Unlock()
fmt.Println("Served from cache")
w.Write(entry)
return
} else {
cacheLock.Unlock()
resp, err := http.Get("https://i.redd.it/" + a["frog"][0])
if err != nil {
w.Write(error)
return
}
defer resp.Body.Close()
image, _, err := image2.Decode(resp.Body)
if err != nil {
w.Write(error)
return
}
newImage := resize.Thumbnail(320, 200, image, resize.Lanczos3)
www := newImage.Bounds().Size()
z := encodeFif((www.X / 2) * 2, (www.Y / 4) * 4, newImage, false)
cacheLock.Lock()
cache[a["frog"][0]] = z
cacheLock.Unlock()
if len(cache) > 100 {
cache = make(map[string][]byte)
}
fmt.Println("Encoded fif!")
w.Write(z)
return
}
w.Write(error)
} else {
w.Write(error)
}
}
func main() {
fmt.Println("Generating OC palette...")
palette, _ = generatePalette()
fmt.Println("Calculating color importances...")
for i := 0; i < len(palette); i++ {
lumaCache[palette[i]] = CalculateColorLuma(palette[i])
}
http.HandleFunc("/getfif", a)
error, _ = ioutil.ReadFile("error.fif")
http.ListenAndServe(":8090", nil)
}