/
thumbnail.go
238 lines (213 loc) · 5.41 KB
/
thumbnail.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
package composite
import (
"context"
"image"
"runtime"
"github.com/pkg/errors"
"github.com/oov/downscale"
"github.com/oov/psd/blend"
)
var (
errLayerNotFound = errors.New("composite: layer not found")
errNoCanvas = errors.New("composite: no canvas")
)
func trimUnusedArea(src *image.NRGBA, threshold byte) {
w, h := src.Rect.Dx(), src.Rect.Dy()
stride := src.Stride
pix := src.Pix[:h*stride]
rect := image.Rectangle{Min: image.Point{X: w, Y: h}, Max: image.Point{X: -1, Y: -1}}
for y := 0; y < h; y++ {
for l := 0; l < w; l++ {
if pix[l*4+3] > threshold {
if l < rect.Min.X {
rect.Min.X = l
}
for r := w - 1; r >= l; r-- {
if pix[r*4+3] > threshold {
if r > rect.Max.X {
rect.Max.X = r
}
break
}
}
if y < rect.Min.Y {
rect.Min.Y = y
}
rect.Max.Y = y
break
}
}
pix = pix[stride:]
}
if rect.Dx()*rect.Dy() == w*h {
return
}
// trim
d := src.Pix[:h*stride]
s := d[stride*rect.Min.Y+rect.Min.X*4:]
w, h = rect.Dx()*4, rect.Dy()
for y := 0; y < h; y++ {
copy(d, s[:w])
d = d[w:]
s = s[stride:]
}
src.Rect = rect
src.Stride = w
src.Pix = src.Pix[:rect.Dy()*w]
}
// Thumbnail creates thumbnail.
func (t *Tree) Thumbnail(seqID int, size int, tempBuffer []byte) (*image.NRGBA, error) {
ld, ok := t.layerImage[seqID]
if !ok {
return nil, errors.Wrap(errLayerNotFound, "composite: cannot create thumbnail")
}
if ld.Canvas == nil || ld.Canvas.Rect().Empty() {
return nil, errors.Wrap(errNoCanvas, "composite: cannot create thumbnail")
}
rect := ld.Canvas.Rect()
src := &image.NRGBA{
Rect: rect,
Stride: rect.Dx() * 4,
}
if src.Stride*src.Rect.Dy() > len(tempBuffer) {
src.Pix = make([]byte, src.Stride*src.Rect.Dy())
} else {
src.Pix = tempBuffer[:src.Stride*src.Rect.Dy()]
}
if err := ld.Canvas.Render(context.Background(), src); err != nil {
return nil, errors.Wrap(err, "composite: failed to create thumbnail")
}
trimUnusedArea(src, 0)
rect = src.Rect
var dest *image.NRGBA
sw, sh := rect.Dx(), rect.Dy()
if sw*sh == 0 {
return nil, errors.Wrap(errNoCanvas, "composite: failed to create thumbnail")
}
if sw > sh {
dest = image.NewNRGBA(image.Rect(0, 0, size, sh*size/sw))
} else {
dest = image.NewNRGBA(image.Rect(0, 0, sw*size/sh, size))
}
if err := downscale.NRGBAFast(context.Background(), dest, src); err != nil {
return nil, errors.Wrap(err, "composite: failed to create thumbnail")
}
return dest, nil
}
func gatherLayer(layers *[]*Layer, l *Layer) {
*layers = append(*layers, l)
for i := range l.Children {
gatherLayer(layers, &l.Children[i])
}
}
func (t *Tree) Thumbnails(ctx context.Context, size int) (map[int]*image.NRGBA, error) {
var layers []*Layer
gatherLayer(&layers, &t.Root)
nLayers := len(layers)
n := runtime.GOMAXPROCS(0)
for n > 1 && n<<1 > nLayers {
n--
}
pc := ¶llelContext{}
pc.Wg.Add(n)
step := nLayers / n
m := make(map[int]*image.NRGBA)
idx := 0
for i := 1; i < n; i++ {
go t.thumbnailsInner(pc, m, layers, size, idx, idx+step)
idx += step
}
go t.thumbnailsInner(pc, m, layers, size, idx, nLayers)
if err := pc.Wait(ctx); err != nil {
return nil, err
}
return m, nil
}
func (t *Tree) thumbnailsInner(pc *parallelContext, m map[int]*image.NRGBA, layers []*Layer, size, sIdx, eIdx int) {
defer pc.Done()
bufLen := 0
for i := sIdx; i < eIdx; i++ {
ld, ok := t.layerImage[layers[i].SeqID]
if !ok || ld.Canvas == nil {
continue
}
rect := ld.Canvas.Rect()
l := rect.Dx() * 4 * rect.Dy()
if bufLen < l {
bufLen = l
}
}
var buf []byte
if bufLen > 0 {
buf = make([]byte, bufLen)
}
for i := sIdx; i < eIdx; i++ {
t, err := t.Thumbnail(layers[i].SeqID, size, buf)
if err != nil {
continue
}
pc.M.Lock()
m[layers[i].SeqID] = t
pc.M.Unlock()
}
}
func (t *Tree) ThumbnailSheet(ctx context.Context, size int) (*image.NRGBA, map[int]image.Rectangle, error) {
m, err := t.Thumbnails(ctx, size)
if err != nil {
return nil, nil, err
}
var indices []int
for i := range m {
indices = append(indices, i)
}
textureSize := 1
nIndices := len(indices)
for {
n := (textureSize / size) * (textureSize / size)
if n >= nIndices {
break
}
textureSize += textureSize
}
if textureSize > 4096 {
return nil, nil, errors.New("composite: could not create thumbnail sheet because too many layers")
}
img := image.NewNRGBA(image.Rect(0, 0, textureSize, textureSize))
mpt := make(map[int]image.Rectangle)
n := runtime.GOMAXPROCS(0)
for n > 1 && n<<1 > nIndices {
n--
}
pc := ¶llelContext{}
pc.Wg.Add(n)
step := nIndices / n
idx := 0
for i := 1; i < n; i++ {
t.thumbnailSheetInner(pc, img, mpt, m, indices, idx, idx+step, size)
idx += step
}
t.thumbnailSheetInner(pc, img, mpt, m, indices, idx, nIndices, size)
if err := pc.Wait(ctx); err != nil {
return nil, nil, err
}
return img, mpt, nil
}
func (t *Tree) thumbnailSheetInner(pc *parallelContext, img *image.NRGBA, mpt map[int]image.Rectangle, m map[int]*image.NRGBA, indices []int, sIdx, eIdx, size int) {
defer pc.Done()
iw := img.Rect.Dx() / size
for i := sIdx; i < eIdx; i++ {
if pc.Aborted() {
return
}
idx := indices[i]
thumb := m[idx]
n := i / iw
x, y := (i-n*iw)*size, n*size
ox, oy := x+(size-thumb.Rect.Dx())/2, y+(size-thumb.Rect.Dy())/2
rect := image.Rect(x, y, x+size, y+size)
blend.Copy.Draw(img, image.Rect(ox, oy, ox+thumb.Rect.Dx(), oy+thumb.Rect.Dy()), thumb, image.Point{})
pc.M.Lock()
mpt[idx] = rect
pc.M.Unlock()
}
}