-
Notifications
You must be signed in to change notification settings - Fork 0
/
giffy.go
183 lines (163 loc) · 4.85 KB
/
giffy.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
package carpe
// Thanks to https://github.com/nf/giffy
// Heavily modified 2018 by Henry Strickland (github.com/strickyak)
// http://www.apache.org/licenses/LICENSE-2.0
/*
Copyright 2013 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"image"
"image/color"
"image/color/palette"
"image/draw"
"image/gif"
"log"
"math"
"os"
"path/filepath"
"runtime/debug"
"time"
_ "image/gif"
_ "image/jpeg"
"image/png"
)
type ImageConverter func(img image.Image, filename string) image.Image
func sqrt64(a int64) int64 {
return int64(math.Sqrt(float64(a)))
}
const NS = 5 // How many to stack
func BuildAnimatedGif(filenames []string, delay time.Duration, converter ImageConverter, outgif string, outmean string) (failures int) {
n := (len(filenames) + NS - 1) / NS
var stacks [][]int64 = make([][]int64, 200) // hack 200
var stacknames []string = make([]string, 200) // hack 200
var ms []*image.Paletted
var sums []int64
var r image.Rectangle
var xlen, ylen int
for i, name := range filenames {
log.Printf("Reading %v [%d/%d]\n", name, i+1, len(filenames))
m, err := readImage(name)
if err != nil {
log.Printf("error reading image: %q: %v", name, err)
// Remove it, so it won't bother us and mess up the digest again.
log.Printf("Removing bad image: %q", name)
os.Remove(name)
failures++
continue
}
if converter != nil {
m = converter(m, name)
}
r = m.Bounds()
pm := image.NewPaletted(r, palette.Plan9)
draw.FloydSteinberg.Draw(pm, r, m, image.ZP)
ms = append(ms, pm)
xlen = r.Max.X - r.Min.X
ylen = r.Max.Y - r.Min.Y
if outmean != "" {
if sums == nil {
sums = make([]int64, xlen*ylen*3)
}
j := i / NS
if stacks[j] == nil {
stacks[j] = make([]int64, xlen*ylen*3)
stacknames[j]= filepath.Join(filepath.Dir(name), "stack." + filepath.Base(name) + ".png")
}
for x := 0; x < xlen; x++ {
for y := 0; y < ylen; y++ {
cr, cg, cb, _ := pm.At(x+r.Min.X, y+r.Min.Y).RGBA()
sums[x*ylen*3+y*3+0] += int64(cr) * int64(cr)
sums[x*ylen*3+y*3+1] += int64(cg) * int64(cg)
sums[x*ylen*3+y*3+2] += int64(cb) * int64(cb)
stacks[j][x*ylen*3+y*3+0] += int64(cr) * int64(cr)
stacks[j][x*ylen*3+y*3+1] += int64(cg) * int64(cg)
stacks[j][x*ylen*3+y*3+2] += int64(cb) * int64(cb)
}
}
}
}
if len(ms) == 0 {
return
}
if outmean != "" {
{
// Write the mean.
pm := image.NewPaletted(r, palette.Plan9)
for x := 0; x < xlen; x++ {
for y := 0; y < ylen; y++ {
cr := sqrt64(sums[x*ylen*3+y*3+0] / int64(len(ms)))
cg := sqrt64(sums[x*ylen*3+y*3+1] / int64(len(ms)))
cb := sqrt64(sums[x*ylen*3+y*3+2] / int64(len(ms)))
pm.Set(x+r.Min.X, y+r.Min.Y, color.NRGBA64{uint16(cr), uint16(cg), uint16(cb), 0xFFFF})
}
}
log.Println("Creating MEAN:", outmean)
os.MkdirAll(filepath.Dir(outmean), 0755)
fd, _ := os.Create(outmean)
png.Encode(fd, pm)
fd.Close()
}
// Write the stacks.
for j := 0; j < n; j++ {
if stacks[j] == nil {
continue
}
pm := image.NewPaletted(r, palette.Plan9)
for x := 0; x < xlen; x++ {
for y := 0; y < ylen; y++ {
cr := sqrt64(stacks[j][x*ylen*3+y*3+0] / int64(NS))
cg := sqrt64(stacks[j][x*ylen*3+y*3+1] / int64(NS))
cb := sqrt64(stacks[j][x*ylen*3+y*3+2] / int64(NS))
pm.Set(x+r.Min.X, y+r.Min.Y, color.NRGBA64{uint16(cr), uint16(cg), uint16(cb), 0xFFFF})
}
}
log.Println("Creating STACK:", stacknames[j])
os.MkdirAll(filepath.Dir(outmean), 0755)
fd, _ := os.Create(stacknames[j])
png.Encode(fd, pm)
fd.Close()
}
}
ds := make([]int, len(ms))
for i := range ds {
ds[i] = int(100 * delay.Seconds()) // Hundredths of a second.
}
log.Println("Generating", outgif)
os.MkdirAll(filepath.Dir(outgif), 0755)
fd, err := os.Create(outgif)
if err != nil {
debug.PrintStack()
log.Panicf("error creating %v: %v", outgif, err)
}
defer fd.Close()
err = gif.EncodeAll(fd, &gif.GIF{Image: ms, Delay: ds, LoopCount: -1})
if err != nil {
debug.PrintStack()
log.Panicf("error writing %v: %v", outgif, err)
}
err = fd.Close()
if err != nil {
debug.PrintStack()
log.Panicf("error closing %v: %v", outgif, err)
}
log.Printf("Valid inputs: %d; Error inputs: %d; output %q", len(ms), failures, outgif)
return
}
func readImage(name string) (image.Image, error) {
fd, err := os.Open(name)
if err != nil {
return nil, err
}
defer fd.Close()
m, _, err := image.Decode(fd)
return m, err
}