-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
194 lines (161 loc) · 5.36 KB
/
processor.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
package resigif
import (
"context"
"image"
"image/color"
"image/gif"
"math"
"runtime"
"golang.org/x/image/draw"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)
type processor struct {
sem *semaphore.Weighted
aspectRatio aspectRatioOption
resizeFunc ImageResizeFunc
parallelLimit int
}
func newProcessor(opts ...Option) *processor {
proc := &processor{
sem: nil,
aspectRatio: Maintain,
resizeFunc: FromDrawScaler(draw.CatmullRom),
parallelLimit: runtime.NumCPU(),
}
for _, opt := range opts {
opt(proc)
}
proc.sem = semaphore.NewWeighted(int64(proc.parallelLimit))
return proc
}
func (p *processor) resize(ctx context.Context, src *gif.GIF, width, height int) (*gif.GIF, error) {
srcWidth, srcHeight := src.Config.Width, src.Config.Height
// Calculate resize ratio & update width and height
widthRatio, heightRatio := p.calculateRatio(srcWidth, srcHeight, &width, &height)
var (
// Canvas to pile up frames
// When resizing a frame with transparent pixels, the edge pixels may be
// mixed with the transparent color and produce black jagged noise
// To avoid the noise, pile up frames on this canvas before resizing
tempCanvas = image.NewNRGBA(image.Rect(0, 0, srcWidth, srcHeight))
// Destination GIF image
dst = &gif.GIF{
Image: make([]*image.Paletted, len(src.Image)),
Delay: src.Delay,
LoopCount: src.LoopCount,
Disposal: src.Disposal,
Config: image.Config{
ColorModel: src.Config.ColorModel,
Width: width,
Height: height,
},
BackgroundIndex: src.BackgroundIndex,
}
)
var eg *errgroup.Group
eg, ctx = errgroup.WithContext(ctx)
for i, srcFrame := range src.Image {
// Check if the context is done
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// Frame size and position
// This may be different from the size of the GIF image
srcBounds := srcFrame.Bounds()
// Calculate resized frame size and position
destBounds := image.Rect(
int(math.Round(float64(srcBounds.Min.X)*widthRatio)),
int(math.Round(float64(srcBounds.Min.Y)*heightRatio)),
int(math.Round(float64(srcBounds.Max.X)*widthRatio)),
int(math.Round(float64(srcBounds.Max.Y)*heightRatio)),
)
// If the disposal method is "none", pile up the frame to tempCanvas before passing it to a goroutine
if src.Disposal[i] == gif.DisposalNone {
draw.Draw(tempCanvas, srcBounds, srcFrame, srcBounds.Min, draw.Over)
eg.Go(
p.resizeFrame(ctx, i, deepCopyImage(tempCanvas), destBounds, srcFrame.Palette, dst),
)
continue
}
// Pile up the frame to tempCanvas and resize it in a goroutine
eg.Go(
p.pileAndResizeFrame(ctx, i, srcFrame, deepCopyImage(tempCanvas), destBounds, srcFrame.Palette, dst),
)
// If the disposal method is "background", fill tempCanvas with the background color
if src.Disposal[i] == gif.DisposalBackground {
// If the transparent color is in the frame palette, use it as the background color
r, g, b, a := srcFrame.Palette[srcFrame.Palette.Index(color.Transparent)].RGBA()
if r == 0 && g == 0 && b == 0 && a == 0 {
draw.Draw(tempCanvas, srcBounds, image.Transparent, image.Point{X: 0, Y: 0}, draw.Src)
} else if globalColorTable, ok := src.Config.ColorModel.(color.Palette); ok && int(src.BackgroundIndex) < len(globalColorTable) {
// Workaround to the GIF images with empty global color table
bgColorUniform := image.NewUniform(globalColorTable[src.BackgroundIndex])
draw.Draw(tempCanvas, srcBounds, bgColorUniform, image.Point{X: 0, Y: 0}, draw.Src)
}
}
}
if err := eg.Wait(); err != nil {
return nil, err
}
return dst, nil
}
func (p *processor) calculateRatio(srcWidth, srcHeight int, width, height *int) (float64, float64) {
// Calculate resize ratio
floatSrcWidth, floatSrcHeight, floatWidth, floatHeight := float64(srcWidth), float64(srcHeight), float64(*width), float64(*height)
widthRatio := floatWidth / floatSrcWidth
heightRatio := floatHeight / floatSrcHeight
// If aspect ratio should be maintained, use the smaller ratio
// Update width and height accordingly
if p.aspectRatio == Maintain {
if widthRatio < heightRatio {
heightRatio = widthRatio
*height = int(math.Round(floatSrcHeight * heightRatio))
} else {
widthRatio = heightRatio
*width = int(math.Round(floatSrcWidth * widthRatio))
}
}
return widthRatio, heightRatio
}
func (p *processor) pileAndResizeFrame(
ctx context.Context,
index int,
frame *image.Paletted,
tempCanvas *image.NRGBA,
destBounds image.Rectangle,
srcPalette color.Palette,
dst *gif.GIF,
) func() error {
return func() error {
draw.Draw(tempCanvas, frame.Bounds(), frame, frame.Bounds().Min, draw.Over)
return p.resizeFrame(ctx, index, tempCanvas, destBounds, srcPalette, dst)()
}
}
func (p *processor) resizeFrame(
ctx context.Context,
index int,
frame *image.NRGBA,
destBounds image.Rectangle,
srcPalette color.Palette,
dst *gif.GIF,
) func() error {
return func() error {
// Acquire semaphore
if err := p.sem.Acquire(ctx, 1); err != nil {
return err
}
defer p.sem.Release(1)
// Resize image
fittedImage, err := p.resizeFunc(frame, dst.Config.Width, dst.Config.Height)
if err != nil {
return err
}
// Crop image to fit destBounds and put it into dst
dst.Image[index] = image.NewPaletted(destBounds, srcPalette)
draw.Draw(dst.Image[index], destBounds, fittedImage, destBounds.Min, draw.Src)
return nil
}
}