-
Notifications
You must be signed in to change notification settings - Fork 4
/
scaling.go
358 lines (307 loc) · 8.63 KB
/
scaling.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"crypto/sha256"
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"github.com/bamiaux/rez"
"github.com/bmatcuk/doublestar"
"gopkg.in/yaml.v2"
)
type foundImage struct {
Path string
Hash []byte
}
type builtImageFile struct {
FileName string
Width int
Height int
}
type builtImage struct {
OriginalName string
Width int
Height int
Files []builtImageFile
}
func readManifest(manifestPath string) (manifest map[string]builtImage, err error) {
if manifestPath == "" {
return // use empty manifest
}
if _, statError := os.Stat(manifestPath); os.IsNotExist(statError) {
log.Print("Can't find manifest, starting with an empty one")
return // no manifest, starting with an empty one
}
bytes, err := readFileBytes(manifestPath)
if err != nil {
return
}
err = yaml.Unmarshal(bytes, &manifest)
return
}
func saveManifest(manifestPath string, manifest map[string]builtImage) (err error) {
if manifestPath == "" {
return // don't persist manifest
}
out, err := yaml.Marshal(manifest)
if err != nil {
return
}
df, err := os.Create(manifestPath)
if err != nil {
return err
}
defer df.Close()
_, err = df.Write(out)
return
}
func hashFile(path string) (hash []byte, err error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return
}
defer f.Close()
h := sha256.New()
if _, err = io.Copy(h, f); err != nil {
return
}
hash = h.Sum(nil)
return
}
func isBlacklisted(conf *config, filePath string) bool {
if conf.Blacklist == nil {
return false
}
relPath, err := filepath.Rel(conf.InputFolderPath(), filePath)
if err != nil {
log.Fatalf("%v", err)
}
for _, blackPath := range conf.Blacklist {
matched, err := path.Match(blackPath, relPath)
if err != nil {
log.Fatalf("Invalid blacklist pattern '%s': %v", blackPath, err)
}
if matched {
return true
}
}
return false
}
func discoverImages(conf *config, inFolder string) (results []foundImage, err error) {
matches, err := doublestar.Glob(path.Join(inFolder, "**/*.{png,jpg}"))
if err != nil {
return
}
for _, path := range matches {
if isBlacklisted(conf, path) {
continue
}
var hash []byte
hash, err = hashFile(path)
if err != nil {
return
}
results = append(results, foundImage{path, hash})
}
return
}
func getSlug(imagePath string, hash []byte) string {
_, baseName := path.Split(imagePath)
extension := path.Ext(baseName)
fileName := baseName[0 : len(baseName)-len(extension)]
hashFragment := hash[0:4] // 2900 images of same name gives 0.1% chance of collision
return fmt.Sprintf("%s-%x", fileName, hashFragment)
}
func downscaleImage(width int, height int, inputImage image.Image) (downscaled image.Image, err error) {
// allocate correct buffer type
r := image.Rect(0, 0, width, height)
switch t := inputImage.(type) {
case *image.YCbCr:
downscaled = image.NewYCbCr(r, t.SubsampleRatio)
case *image.RGBA:
downscaled = image.NewRGBA(r)
case *image.NRGBA:
downscaled = image.NewNRGBA(r)
case *image.Gray:
downscaled = image.NewGray(r)
default:
err = fmt.Errorf("Unsupported image colour format %T.", inputImage)
}
err = rez.Convert(downscaled, inputImage, rez.NewLanczosFilter(3))
return
}
func saveImage(conf *config, outPath string, extension string, img image.Image) (err error) {
log.Printf("Saving %s with ext %s", outPath, extension)
// encode the output
df, err := os.Create(outPath)
if err != nil {
return err
}
defer df.Close()
switch extension {
case ".png":
err = png.Encode(df, img)
case ".jpg":
options := jpeg.Options{Quality: conf.JpgQuality}
err = jpeg.Encode(df, img, &options)
default:
err = fmt.Errorf("Unrecognized extension %s", extension)
}
return
}
func buildImage(conf *config, imagePath string, slug string, newImages *[]string) (built builtImage, err error) {
log.Printf("Building image %s from %s", slug, imagePath)
extension := path.Ext(imagePath)
// load image
f, err := os.OpenFile(imagePath, os.O_RDONLY, 0)
if err != nil {
f.Close()
return
}
var inputImage image.Image
switch extension {
case ".png":
inputImage, err = png.Decode(f)
case ".jpg":
inputImage, err = jpeg.Decode(f)
default:
err = fmt.Errorf("Unrecognized extension %s", extension)
return
}
f.Close()
if err != nil {
return
}
built.Width = inputImage.Bounds().Dx()
built.Height = inputImage.Bounds().Dy()
// copy-paste original file
imageFolder := conf.ImageFolderPath()
originalName := fmt.Sprintf("%s-original%s", slug, extension)
built.OriginalName = originalName
originalPath := path.Join(imageFolder, originalName)
if _, err = os.Stat(originalPath); os.IsNotExist(err) {
*newImages = append(*newImages, originalPath)
err = copyFile(imagePath, originalPath)
if err != nil {
return
}
} else {
log.Printf("Original already copied, skipping: %s", originalPath)
}
builtOriginal := builtImageFile{FileName: originalName, Width: built.Width, Height: built.Height}
built.Files = append(built.Files, builtOriginal)
// resize to relevant sizes
for _, w := range conf.Widths {
if w >= built.Width {
continue // we never want to upscale
}
downscaleRatio := float64(w) / float64(built.Width)
destHeight := int(float64(built.Height) * downscaleRatio)
if downscaleRatio > conf.ScaleThreshold {
continue // too small of a change in size to be worth it
}
if downscaleRatio > conf.JpgScaleThreshold && extension == ".jpg" {
// re-encoding JPEG at a slightly smaller size either:
// - loses quality if we don't encode the output at 100
// - increases size if we encode the output at 100
continue
}
log.Printf("Downscaling %s from %v to (%d,%d)", slug, inputImage.Bounds(), w, destHeight)
outName := fmt.Sprintf("%s-%dpx%s", slug, w, extension)
outPath := path.Join(imageFolder, outName)
builtScaled := builtImageFile{FileName: outName, Width: w, Height: destHeight}
built.Files = append(built.Files, builtScaled)
if _, err := os.Stat(outPath); !os.IsNotExist(err) {
log.Printf("Image already exists, skipping: %s", outPath)
continue // already exists
}
// do the scaling
var downscaledImage image.Image
downscaledImage, err = downscaleImage(w, destHeight, inputImage)
if err != nil {
return
}
*newImages = append(*newImages, outPath)
err = saveImage(conf, outPath, extension, downscaledImage)
if err != nil {
return
}
}
// the files list should always be sorted by decreasing width
sort.Slice(built.Files, func(i, j int) bool {
return built.Files[i].Width > built.Files[j].Width
})
return
}
func optimizeImages(conf *config, newImages []string) (err error) {
if conf.OptimCommand == nil || len(newImages) == 0 {
return
}
args := append(conf.OptimCommand, newImages...)
log.Printf("Optimizing with %v", args)
cmd := exec.Command(args[0], args[1:len(args)]...)
err = cmd.Run()
return
}
// sometimes optimizing images leads to ones with larger dimensions actually
// having smaller file size, if we notice this, we want to cut the inefficient
// files out of our manifest.
func cullImages(conf *config, built builtImage) builtImage {
if !conf.doCulling {
return built
}
newFiles := []builtImageFile{}
var bestSize int64 = 1000000000 // arbitrary large number, 1GB
imageFolder := conf.ImageFolderPath()
for _, builtFile := range built.Files {
info, err := os.Stat(path.Join(imageFolder, builtFile.FileName))
if err != nil {
log.Printf("Couldn't stat %s, removing from manifest", builtFile.FileName)
continue // file doesn't exist, shouldn't be in manifest
}
if info.Size() < bestSize {
bestSize = info.Size()
newFiles = append(newFiles, builtFile)
} else {
log.Printf("Culling %s, it is %d bytes when a larger image was only %d bytes",
builtFile.FileName, info.Size(), bestSize)
}
}
built.Files = newFiles
return built
}
func buildNewManifest(conf *config, foundImages []foundImage, oldManifest map[string]builtImage) (newManifest map[string]builtImage, pathToSlug map[string]string, err error) {
newManifest = map[string]builtImage{}
pathToSlug = map[string]string{}
newImages := []string{}
inputPath := path.Join(conf.basePath, conf.InputFolder)
for _, img := range foundImages {
slug := getSlug(img.Path, img.Hash)
if built, ok := oldManifest[slug]; ok {
newManifest[slug] = cullImages(conf, built)
} else {
var built builtImage
built, err = buildImage(conf, img.Path, slug, &newImages)
if err != nil {
return
}
newManifest[slug] = built
}
var relPath string
relPath, err = filepath.Rel(inputPath, img.Path)
if err != nil {
return
}
pathToSlug[relPath] = slug
}
log.Printf("New images: %v", newImages)
err = optimizeImages(conf, newImages)
return
}