-
Notifications
You must be signed in to change notification settings - Fork 18
/
fastjpeg.go
48 lines (39 loc) · 951 Bytes
/
fastjpeg.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
// +build fastjpeg all
package main
import (
"bytes"
"fmt"
"image/jpeg"
"os"
"camlistore.org/pkg/images/fastjpeg"
"camlistore.org/pkg/images/resize"
)
func init() {
if fastjpeg.Available() {
RegisterResizer("fastjpeg", resizeFastjpeg)
} else {
fmt.Println("Cannot find djpeg in PATH, will skip fastjpeg tests")
}
}
func resizeFastjpeg(origName, newName string) (int, int64) {
origFile, _ := os.Open(origName)
origFileStat, _ := origFile.Stat()
downsampled, err := fastjpeg.DecodeDownsample(origFile, 8)
origFile.Close()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
resized := resize.Resize(downsampled, downsampled.Bounds(), 150, 150)
b := new(bytes.Buffer)
jpeg.Encode(b, resized, nil)
blen := b.Len()
cacheFile, err := os.Create(newName)
defer cacheFile.Close()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
b.WriteTo(cacheFile)
return blen, origFileStat.Size()
}