-
Notifications
You must be signed in to change notification settings - Fork 18
/
pure.go
205 lines (172 loc) · 4.92 KB
/
pure.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
// +build !nopure
package main
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"os"
"github.com/anthonynsimon/bild/transform"
"github.com/bamiaux/rez"
"github.com/disintegration/gift"
"github.com/disintegration/imaging"
nfnt_resize "github.com/nfnt/resize"
"golang.org/x/image/draw"
)
func init() {
RegisterPureResizer("imaging_box", resizeImaging)
RegisterPureResizer("gift_box", resizeGift)
RegisterPureResizer("Nfnt_NearestNeighbor", resizeNfntNearestNeighbor)
RegisterPureResizer("rez_bilinear", resizeRezBilinear)
RegisterPureResizer("x_image_draw", resizeXImageDraw)
RegisterPureResizer("bild_resize", resizeBild)
}
func resizeBild(origName, newName string) (int, int64) {
origFile, _ := os.Open(origName)
origImage, _ := jpeg.Decode(origFile)
origFileStat, _ := origFile.Stat()
origFile.Close()
resized := transform.Resize(origImage, 150, 150, transform.NearestNeighbor)
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()
}
func resizeNfnt(origName, newName string, interp nfnt_resize.InterpolationFunction) (int, int64) {
origFile, _ := os.Open(origName)
origImage, _ := jpeg.Decode(origFile)
origFileStat, _ := origFile.Stat()
origFile.Close()
var resized image.Image
resized = nfnt_resize.Thumbnail(150, 150, origImage, interp)
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()
}
func resizeNfntNearestNeighbor(origName, newName string) (int, int64) {
return resizeNfnt(origName, newName, nfnt_resize.NearestNeighbor)
}
func getSize(a, b, c int) int {
d := a * b / c
return (d + 1) & -1
}
func resizeRez(origName, newName string, filter rez.Filter) (int, int64) {
origFile, _ := os.Open(origName)
origImage, _ := jpeg.Decode(origFile)
origFileStat, _ := origFile.Stat()
origFile.Close()
var resized image.Image
src, ok := origImage.(*image.YCbCr)
if !ok {
fmt.Println("input picture is not ycbcr")
return 0, origFileStat.Size()
}
p := origImage.Bounds().Size()
w, h := 150, getSize(150, p.Y, p.X)
if p.X < p.Y {
w, h = getSize(150, p.X, p.Y), 150
}
resized = image.NewYCbCr(image.Rect(0, 0, w, h), src.SubsampleRatio)
err := rez.Convert(resized, origImage, filter)
if err != nil {
fmt.Println("unable to convert picture", err)
return 0, origFileStat.Size()
}
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()
}
func resizeRezBilinear(origName, newName string) (int, int64) {
return resizeRez(origName, newName, rez.NewBilinearFilter())
}
func resizeImaging(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
origImage, _ := imaging.Open(origName)
var resized image.Image
resized = imaging.Fit(origImage, 150, 150, imaging.Box)
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()
}
func resizeGift(origName, newName string) (int, int64) {
origFile, _ := os.Open(origName)
origImage, _ := jpeg.Decode(origFile)
origFileStat, _ := origFile.Stat()
origFile.Close()
var g = gift.New()
p := origImage.Bounds().Size()
if p.X > p.Y {
g.Add(gift.Resize(150, 0, gift.BoxResampling))
} else {
g.Add(gift.Resize(0, 150, gift.BoxResampling))
}
resized := image.NewRGBA(g.Bounds(origImage.Bounds()))
g.Draw(resized, origImage)
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()
}
func resizeXImageDraw(origName, newName string) (int, int64) {
origFile, _ := os.Open(origName)
origImage, _ := jpeg.Decode(origFile)
origFileStat, _ := origFile.Stat()
origFile.Close()
p := origImage.Bounds().Size()
w, h := 150, getSize(150, p.Y, p.X)
if p.X < p.Y {
w, h = getSize(150, p.X, p.Y), 150
}
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
draw.Draw(dst, dst.Bounds(), image.White, image.ZP, draw.Src)
draw.ApproxBiLinear.Scale(dst, dst.Bounds(), origImage, origImage.Bounds(), draw.Src, nil)
b := new(bytes.Buffer)
jpeg.Encode(b, dst, 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()
}