-
Notifications
You must be signed in to change notification settings - Fork 0
/
vips.go
85 lines (76 loc) · 1.92 KB
/
vips.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
package main
import (
"log"
"time"
"github.com/davidbyttow/govips/v2/vips"
)
// vipsImage is an image managed by the vips library
// Attempts to use this library indicates that there are
// concurrency issues under heavy use - when loading and processing
// multiple images in parallel, the library will sometimes fail with
// a SIGSEGV, or a thread will hang.
type vipsImage struct {
Image
img *vips.ImageRef
}
func vipsInit() {
// Make vips less noisy.
vips.LoggingSettings(nil, vips.LogLevelError)
vips.Startup(nil)
}
// NewVipsImage returns an image loaded and managed by the
// cgo bindings to libvips.
func NewVipsImage(src string) (Image, error) {
vimg, err := vips.NewImageFromFile(src)
if err != nil {
return nil, err
}
return &vipsImage{img: vimg}, nil
}
func (v *vipsImage) Width() int {
return v.img.Width()
}
func (v *vipsImage) Height() int {
return v.img.Height()
}
func (v *vipsImage) Rotate(deg RotateDegrees) error {
switch deg {
case Rotate90:
v.img.Rotate(vips.Angle90)
case Rotate180:
v.img.Rotate(vips.Angle180)
case Rotate270:
v.img.Rotate(vips.Angle270)
}
return nil
}
func (v *vipsImage) Write(destFile string, mtime time.Time, w, h, q int) {
vimg, err := v.img.Copy()
if err != nil {
log.Fatalf("%s: copy: %v", destFile, err)
}
// scale it down to fit within the width & height
xr := float64(w) / float64(vimg.Width())
yr := float64(h) / float64(vimg.Height())
if xr < 1 || yr < 1 {
// Image is larger than requested size, so scale it down
var err error
if xr < yr {
err = vimg.Resize(xr, vips.KernelAuto)
} else {
err = vimg.Resize(yr, vips.KernelAuto)
}
if err != nil {
log.Fatalf("%s: resize: %v", destFile, err)
}
}
jp := vips.NewJpegExportParams()
jp.Quality = q
b, _, err := vimg.ExportJpeg(jp)
if err != nil {
log.Fatalf("%s: export: %v", destFile, err)
}
if err := cp(b, destFile, mtime); err != nil {
log.Fatalf("%s: write: %v", destFile, err)
}
}