forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
512 lines (406 loc) · 9.9 KB
/
process.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package main
/*
#cgo pkg-config: vips
#cgo LDFLAGS: -s -w
#include "vips.h"
*/
import "C"
import (
"errors"
"log"
"math"
"os"
"runtime"
"unsafe"
)
type imageType int
const (
UNKNOWN = C.UNKNOWN
JPEG = C.JPEG
PNG = C.PNG
WEBP = C.WEBP
GIF = C.GIF
)
var imageTypes = map[string]imageType{
"jpeg": JPEG,
"jpg": JPEG,
"png": PNG,
"webp": WEBP,
"gif": GIF,
}
type gravityType int
const (
CENTER gravityType = iota
NORTH
EAST
SOUTH
WEST
SMART
)
var gravityTypes = map[string]gravityType{
"ce": CENTER,
"no": NORTH,
"ea": EAST,
"so": SOUTH,
"we": WEST,
"sm": SMART,
}
type resizeType int
const (
FIT resizeType = iota
FILL
CROP
)
var resizeTypes = map[string]resizeType{
"fit": FIT,
"fill": FILL,
"crop": CROP,
}
type processingOptions struct {
Resize resizeType
Width int
Height int
Gravity gravityType
Enlarge bool
Format imageType
}
var vipsSupportSmartcrop bool
var vipsTypeSupportLoad = make(map[imageType]bool)
var vipsTypeSupportSave = make(map[imageType]bool)
func initVips() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.vips_initialize(); err != 0 {
C.vips_shutdown()
log.Fatalln("unable to start vips!")
}
C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
C.vips_cache_set_max(500)
if len(os.Getenv("IMGPROXY_VIPS_LEAK_CHECK")) > 0 {
C.vips_leak_set(C.gboolean(1))
}
if len(os.Getenv("IMGPROXY_VIPS_CACHE_TRACE")) > 0 {
C.vips_cache_set_trace(C.gboolean(1))
}
vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
if int(C.vips_type_find_load_go(C.JPEG)) != 0 {
vipsTypeSupportLoad[JPEG] = true
}
if int(C.vips_type_find_load_go(C.PNG)) != 0 {
vipsTypeSupportLoad[PNG] = true
}
if int(C.vips_type_find_load_go(C.WEBP)) != 0 {
vipsTypeSupportLoad[WEBP] = true
}
if int(C.vips_type_find_load_go(C.GIF)) != 0 {
vipsTypeSupportLoad[GIF] = true
}
if int(C.vips_type_find_save_go(C.JPEG)) != 0 {
vipsTypeSupportSave[JPEG] = true
}
if int(C.vips_type_find_save_go(C.PNG)) != 0 {
vipsTypeSupportSave[PNG] = true
}
if int(C.vips_type_find_save_go(C.WEBP)) != 0 {
vipsTypeSupportSave[WEBP] = true
}
}
func shutdownVips() {
C.vips_shutdown()
}
func randomAccessRequired(po processingOptions) int {
if po.Gravity == SMART {
return 1
}
return 0
}
func round(f float64) int {
return int(f + .5)
}
func extractMeta(img *C.VipsImage) (int, int, int, bool) {
width := int(img.Xsize)
height := int(img.Ysize)
angle := C.VIPS_ANGLE_D0
flip := false
orientation := C.vips_get_exif_orientation(img)
if orientation >= 5 && orientation <= 8 {
width, height = height, width
}
if orientation == 3 || orientation == 4 {
angle = C.VIPS_ANGLE_D180
}
if orientation == 5 || orientation == 6 {
angle = C.VIPS_ANGLE_D90
}
if orientation == 7 || orientation == 8 {
angle = C.VIPS_ANGLE_D270
}
if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
flip = true
}
return width, height, angle, flip
}
func calcScale(width, height int, po processingOptions) float64 {
if (po.Width == width && po.Height == height) || (po.Resize != FILL && po.Resize != FIT) {
return 1
}
fsw, fsh, fow, foh := float64(width), float64(height), float64(po.Width), float64(po.Height)
wr := fow / fsw
hr := foh / fsh
if po.Resize == FIT {
return math.Min(wr, hr)
}
return math.Max(wr, hr)
}
func calcShink(scale float64, imgtype imageType) int {
shrink := int(1.0 / scale)
if imgtype != JPEG {
return shrink
}
switch {
case shrink >= 16:
return 8
case shrink >= 8:
return 4
case shrink >= 4:
return 2
}
return 1
}
func calcCrop(width, height int, po processingOptions) (left, top int) {
left = (width - po.Width + 1) / 2
top = (height - po.Height + 1) / 2
if po.Gravity == NORTH {
top = 0
}
if po.Gravity == EAST {
left = width - po.Width
}
if po.Gravity == SOUTH {
top = height - po.Height
}
if po.Gravity == WEST {
left = 0
}
return
}
func processImage(data []byte, imgtype imageType, po processingOptions, t *timer) ([]byte, error) {
defer C.vips_cleanup()
defer keepAlive(data)
if po.Gravity == SMART && !vipsSupportSmartcrop {
return nil, errors.New("Smart crop is not supported by used version of libvips")
}
img, err := vipsLoadImage(data, imgtype, 1)
if err != nil {
return nil, err
}
defer C.clear_image(&img)
t.Check()
imgWidth, imgHeight, angle, flip := extractMeta(img)
// Ensure we won't crop out of bounds
if !po.Enlarge || po.Resize == CROP {
if imgWidth < po.Width {
po.Width = imgWidth
}
if imgHeight < po.Height {
po.Height = imgHeight
}
}
if po.Width != imgWidth || po.Height != imgHeight {
if po.Resize == FILL || po.Resize == FIT {
scale := calcScale(imgWidth, imgHeight, po)
// Do some shrink-on-load
if scale < 1.0 {
if imgtype == JPEG || imgtype == WEBP {
shrink := calcShink(scale, imgtype)
scale = scale * float64(shrink)
if tmp, e := vipsLoadImage(data, imgtype, shrink); e == nil {
C.swap_and_clear(&img, tmp)
} else {
return nil, e
}
}
}
premultiplied := false
var bandFormat C.VipsBandFormat
if vipsImageHasAlpha(img) {
if bandFormat, err = vipsPremultiply(&img); err != nil {
return nil, err
}
premultiplied = true
}
if err = vipsResize(&img, scale); err != nil {
return nil, err
}
if premultiplied {
if err = vipsUnpremultiply(&img, bandFormat); err != nil {
return nil, err
}
}
}
}
if err = vipsImportColourProfile(&img); err != nil {
return nil, err
}
if err = vipsFixColourspace(&img); err != nil {
return nil, err
}
t.Check()
if angle != C.VIPS_ANGLE_D0 || flip {
if err = vipsImageCopyMemory(&img); err != nil {
return nil, err
}
if angle != C.VIPS_ANGLE_D0 {
if err = vipsRotate(&img, angle); err != nil {
return nil, err
}
}
if flip {
if err = vipsFlip(&img); err != nil {
return nil, err
}
}
}
t.Check()
if (po.Width != imgWidth || po.Height != imgHeight) && (po.Resize == FILL || po.Resize == CROP) {
if po.Gravity == SMART {
if err = vipsImageCopyMemory(&img); err != nil {
return nil, err
}
if err = vipsSmartCrop(&img, po.Width, po.Height); err != nil {
return nil, err
}
} else {
left, top := calcCrop(int(img.Xsize), int(img.Ysize), po)
if err = vipsCrop(&img, left, top, po.Width, po.Height); err != nil {
return nil, err
}
}
t.Check()
}
return vipsSaveImage(img, po.Format)
}
func vipsLoadImage(data []byte, imgtype imageType, shrink int) (*C.struct__VipsImage, error) {
var img *C.struct__VipsImage
if C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), C.int(shrink), &img) != 0 {
return nil, vipsError()
}
return img, nil
}
func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error) {
var ptr unsafe.Pointer
defer C.g_free_go(&ptr)
err := C.int(0)
imgsize := C.size_t(0)
switch imgtype {
case JPEG:
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
case PNG:
err = C.vips_pngsave_go(img, &ptr, &imgsize)
case WEBP:
err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
}
if err != 0 {
return nil, vipsError()
}
return C.GoBytes(ptr, C.int(imgsize)), nil
}
func vipsImageHasAlpha(img *C.struct__VipsImage) bool {
return C.vips_image_hasalpha_go(img) > 0
}
func vipsPremultiply(img **C.struct__VipsImage) (C.VipsBandFormat, error) {
var tmp *C.struct__VipsImage
format := C.vips_band_format(*img)
if C.vips_premultiply_go(*img, &tmp) != 0 {
return 0, vipsError()
}
C.swap_and_clear(img, tmp)
return format, nil
}
func vipsUnpremultiply(img **C.struct__VipsImage, format C.VipsBandFormat) error {
var tmp *C.struct__VipsImage
if C.vips_unpremultiply_go(*img, &tmp) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
if C.vips_cast_go(*img, &tmp, format) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsResize(img **C.struct__VipsImage, scale float64) error {
var tmp *C.struct__VipsImage
if C.vips_resize_go(*img, &tmp, C.double(scale)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsRotate(img **C.struct__VipsImage, angle int) error {
var tmp *C.struct__VipsImage
if C.vips_rot_go(*img, &tmp, C.VipsAngle(angle)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsFlip(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_flip_horizontal_go(*img, &tmp) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsCrop(img **C.struct__VipsImage, left, top, width, height int) error {
var tmp *C.struct__VipsImage
if C.vips_extract_area_go(*img, &tmp, C.int(left), C.int(top), C.int(width), C.int(height)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsSmartCrop(img **C.struct__VipsImage, width, height int) error {
var tmp *C.struct__VipsImage
if C.vips_smartcrop_go(*img, &tmp, C.int(width), C.int(height)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsImportColourProfile(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_need_icc_import(*img) > 0 {
profile, err := cmykProfilePath()
if err != nil {
return err
}
if C.vips_icc_import_go(*img, &tmp, C.CString(profile)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
}
return nil
}
func vipsFixColourspace(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_image_guess_interpretation(*img) != C.VIPS_INTERPRETATION_sRGB {
if C.vips_colourspace_go(*img, &tmp, C.VIPS_INTERPRETATION_sRGB) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
}
return nil
}
func vipsImageCopyMemory(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if tmp = C.vips_image_copy_memory(*img); tmp == nil {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsError() error {
return errors.New(C.GoString(C.vips_error_buffer()))
}