forked from davidbyttow/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
533 lines (453 loc) · 14.4 KB
/
bridge.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package vips
// #cgo pkg-config: vips
// #include "bridge.h"
import "C"
import (
"fmt"
"log"
"math"
"runtime"
dbg "runtime/debug"
"unsafe"
)
const (
defaultQuality = 90
defaultCompression = 6
maxScaleFactor = 10
)
type vipsLabelOptions struct {
Text *C.char
Font *C.char
Width C.int
Height C.int
OffsetX C.int
OffsetY C.int
Alignment C.VipsAlign
DPI C.int
Margin C.int
Opacity C.float
Color [3]C.double
}
type vipsLoadOptions struct {
cOpts C.ImageLoadOptions
}
var stringBuffer4096 = fixedString(4096)
func vipsOperationNew(name string) *C.VipsOperation {
cName := C.CString(name)
defer freeCString(cName)
return C.vips_operation_new(cName)
}
func vipsCall(name string, options []*Option) error {
operation := vipsOperationNew(name)
return vipsCallOperation(operation, options)
}
func vipsCallOperation(operation *C.VipsOperation, options []*Option) error {
// Set the inputs
for _, option := range options {
if option.Output() {
continue
}
defer option.Close()
cName := C.CString(option.Name)
defer freeCString(cName)
C.gobject_set_property(
(*C.VipsObject)(unsafe.Pointer(operation)), cName, option.GValue())
}
if ret := C.vips_cache_operation_buildp(&operation); ret != 0 {
return handleVipsError()
}
defer C.g_object_unref(C.gpointer(unsafe.Pointer(operation)))
// Write back the outputs
for _, option := range options {
if !option.Output() {
continue
}
defer option.Close()
cName := C.CString(option.Name)
defer freeCString(cName)
C.g_object_get_property(
(*C.GObject)(unsafe.Pointer(operation)), (*C.gchar)(cName), option.GValue())
}
return nil
}
func vipsHasProfile(input *C.VipsImage) bool {
return int(C.has_profile_embed(input)) > 0
}
func vipsPrepareForExport(input *C.VipsImage, params *ExportParams) (*C.VipsImage, error) {
if params.StripProfile && vipsHasProfile(input) {
C.remove_icc_profile(input)
}
if params.Quality == 0 {
params.Quality = defaultQuality
}
if params.Compression == 0 {
params.Compression = defaultCompression
}
// Use a default interpretation and cast it to C type
if params.Interpretation == 0 {
params.Interpretation = Interpretation(input.Type)
}
interpretation := C.VipsInterpretation(params.Interpretation)
// Apply the proper colour space
if int(C.is_colorspace_supported(input)) == 1 && interpretation != input.Type {
var out *C.VipsImage
err := C.to_colorspace(input, &out, interpretation)
if int(err) != 0 {
return nil, handleVipsError()
}
input = out
}
return input, nil
}
func vipsLoadFromBuffer(buf []byte, opts ...LoadOption) (*C.VipsImage, ImageType, error) {
// Reference buf here so it's not garbage collected during image initialization.
defer runtime.KeepAlive(buf)
var image *C.VipsImage
imageType := vipsDetermineImageType(buf)
if imageType == ImageTypeUnknown {
if len(buf) > 2 {
log.Printf("Failed to understand image format size=%d %x %x %x", len(buf), buf[0], buf[1], buf[2])
} else {
log.Printf("Failed to understand image format size=%d", len(buf))
}
return nil, ImageTypeUnknown, ErrUnsupportedImageFormat
}
len := C.size_t(len(buf))
imageBuf := unsafe.Pointer(&buf[0])
var loadOpts vipsLoadOptions
for _, opt := range opts {
opt(&loadOpts)
}
err := C.init_image(imageBuf, len, C.int(imageType), &loadOpts.cOpts, &image)
if err != 0 {
return nil, ImageTypeUnknown, handleVipsError()
}
return image, imageType, nil
}
func vipsCopyImage(input *C.VipsImage) (*C.VipsImage, error) {
var output *C.VipsImage
cErr := C.copy_image(input, &output)
if int(cErr) != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsLoadFromMemory(buf []byte, width int, height int, bands int) (*C.VipsImage, error) {
// Reference buf here so it's not garbage collected during image initialization.
defer runtime.KeepAlive(buf)
len := C.size_t(len(buf))
imageBuf := unsafe.Pointer(&buf[0])
image := C.image_new_from_memory_copy(imageBuf, len, C.int(width), C.int(height), C.int(bands), C.VIPS_FORMAT_UCHAR)
if image == nil {
return nil, handleVipsError()
}
return image, nil
}
func vipsExportBuffer(image *C.VipsImage, params *ExportParams) ([]byte, ImageType, error) {
tmpImage, err := vipsPrepareForExport(image, params)
if err != nil {
return nil, ImageTypeUnknown, err
}
// If these are equal, then we don't want to deref the original image as
// the original will be returned if the target colorspace is not supported
if tmpImage != image {
defer C.g_object_unref(C.gpointer(tmpImage))
}
cLen := C.size_t(0)
var cErr C.int
interlaced := C.int(boolToInt(params.Interlaced))
quality := C.int(params.Quality)
lossless := C.int(boolToInt(params.Lossless))
stripMetadata := C.int(boolToInt(params.StripMetadata))
format := params.Format
if format != ImageTypeUnknown && !IsTypeSupported(format) {
return nil, ImageTypeUnknown, fmt.Errorf("cannot save to %#v", ImageTypes[format])
}
if params.BackgroundColor != nil {
tmpImage, err = vipsFlattenBackground(tmpImage, *params.BackgroundColor)
if err != nil {
return nil, ImageTypeUnknown, err
}
}
var ptr unsafe.Pointer
switch format {
case ImageTypeWEBP:
incOpCounter("save_webp_buffer")
cErr = C.save_webp_buffer(tmpImage, &ptr, &cLen, stripMetadata, quality, lossless)
case ImageTypePNG:
incOpCounter("save_png_buffer")
cErr = C.save_png_buffer(tmpImage, &ptr, &cLen, stripMetadata, C.int(params.Compression), quality, interlaced)
case ImageTypeTIFF:
incOpCounter("save_tiff_buffer")
cErr = C.save_tiff_buffer(tmpImage, &ptr, &cLen)
default:
incOpCounter("save_jpeg_buffer")
format = ImageTypeJPEG
cErr = C.save_jpeg_buffer(tmpImage, &ptr, &cLen, stripMetadata, quality, interlaced)
}
if int(cErr) != 0 {
return nil, ImageTypeUnknown, handleVipsError()
}
buf := C.GoBytes(ptr, C.int(cLen))
C.g_free(C.gpointer(ptr))
return buf, format, nil
}
func isTypeSupported(imageType ImageType) bool {
return supportedImageTypes[imageType]
}
func isColorspaceIsSupportedBuffer(buf []byte) (bool, error) {
image, _, err := vipsLoadFromBuffer(buf)
if err != nil {
return false, err
}
defer C.g_object_unref(C.gpointer(image))
return int(C.is_colorspace_supported(image)) == 1, nil
}
func isColorspaceIsSupported(image *C.VipsImage) bool {
return int(C.is_colorspace_supported(image)) == 1
}
func vipsDetermineImageType(buf []byte) ImageType {
if len(buf) < 12 {
return ImageTypeUnknown
}
if buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF {
return ImageTypeJPEG
}
if IsTypeSupported(ImageTypeGIF) && buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46 {
return ImageTypeGIF
}
if buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 {
return ImageTypePNG
}
if IsTypeSupported(ImageTypeTIFF) &&
((buf[0] == 0x49 && buf[1] == 0x49 && buf[2] == 0x2A && buf[3] == 0x0) ||
(buf[0] == 0x4D && buf[1] == 0x4D && buf[2] == 0x0 && buf[3] == 0x2A)) {
return ImageTypeTIFF
}
if IsTypeSupported(ImageTypePDF) && buf[0] == 0x25 && buf[1] == 0x50 && buf[2] == 0x44 && buf[3] == 0x46 {
return ImageTypePDF
}
if IsTypeSupported(ImageTypeWEBP) && buf[8] == 0x57 && buf[9] == 0x45 && buf[10] == 0x42 && buf[11] == 0x50 {
return ImageTypeWEBP
}
if IsTypeSupported(ImageTypeSVG) && buf[0] == 0x3c && buf[1] == 0x3f && buf[2] == 0x78 && buf[3] == 0x6d {
return ImageTypeSVG
}
// ftyp types
if IsTypeSupported(ImageTypeHEIF) && buf[4] == 0x66 && buf[5] == 0x74 && buf[6] == 0x79 && buf[7] == 0x70 {
ftyp := string(buf[8:12])
if ftyp == "heic" || ftyp == "heif" || ftyp == "hevc" || ftyp == "hevx" || ftyp == "heim" || ftyp == "heis" || ftyp == "hevm" || ftyp == "hevs" || ftyp == "mif1" {
return ImageTypeHEIF
}
}
return ImageTypeUnknown
}
func vipsFlattenBackground(input *C.VipsImage, color Color) (*C.VipsImage, error) {
incOpCounter("flatten")
var output *C.VipsImage
bg := [3]C.double{
C.double(color.R),
C.double(color.G),
C.double(color.B),
}
if int(C.has_alpha_channel(input)) > 0 {
err := C.flatten_image_background(input, &output, bg[0], bg[1], bg[2])
if int(err) != 0 {
return nil, handleVipsError()
}
C.g_object_unref(C.gpointer(input))
input = output
}
return input, nil
}
func vipsResize(input *C.VipsImage, scale, vscale float64, kernel Kernel) (*C.VipsImage, error) {
incOpCounter("resize")
var output *C.VipsImage
// Let's not be insane
scale = math.Min(scale, maxScaleFactor)
vscale = math.Min(vscale, maxScaleFactor)
defer C.g_object_unref(C.gpointer(input))
if err := C.resize_image(input, &output, C.double(scale), C.double(vscale), C.int(kernel)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsRotate(input *C.VipsImage, angle Angle) (*C.VipsImage, error) {
incOpCounter("rot")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.rot_image(input, &output, C.VipsAngle(angle)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsAutoRotateRemoveAngle(input *C.VipsImage) {
incOpCounter("autorot_remove")
C.autorot_remove_angle(input)
}
func vipsAutoRotate(input *C.VipsImage) (*C.VipsImage, error) {
incOpCounter("autorot")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.autorot_image(input, &output); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsComposite(inputs []*C.VipsImage, mode BlendMode) (*C.VipsImage, error) {
incOpCounter("composite")
var output *C.VipsImage
if err := C.composite(&inputs[0], &output, C.int(len(inputs)), C.int(mode)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsHasAlpha(image *C.VipsImage) bool {
return int(C.has_alpha_channel(image)) > 0
}
func vipsAdd(left *C.VipsImage, right *C.VipsImage) (*C.VipsImage, error) {
incOpCounter("add")
defer C.g_object_unref(C.gpointer(left))
defer C.g_object_unref(C.gpointer(right))
var output *C.VipsImage
if err := C.add(left, right, &output); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsMultiply(left *C.VipsImage, right *C.VipsImage) (*C.VipsImage, error) {
incOpCounter("multiply")
defer C.g_object_unref(C.gpointer(left))
defer C.g_object_unref(C.gpointer(right))
var output *C.VipsImage
if err := C.multiply(left, right, &output); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsExtractBand(image *C.VipsImage, band, num int) (*C.VipsImage, error) {
incOpCounter("extract")
defer C.g_object_unref(C.gpointer(image))
var output *C.VipsImage
if err := C.extract_band(image, &output, C.int(band), C.int(num)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsLinear1(image *C.VipsImage, a, b float64) (*C.VipsImage, error) {
incOpCounter("linear1")
defer C.g_object_unref(C.gpointer(image))
var output *C.VipsImage
if err := C.linear1(image, &output, C.double(a), C.double(b)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsExtractArea(input *C.VipsImage, left, top, width, height int) (*C.VipsImage, error) {
incOpCounter("extract")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.extract_image_area(input, &output, C.int(left), C.int(top), C.int(width), C.int(height)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsEmbed(input *C.VipsImage, left, top, width, height int, extend Extend) (*C.VipsImage, error) {
incOpCounter("embed")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.embed_image(input, &output, C.int(left), C.int(top), C.int(width), C.int(height), C.int(extend), 0, 0, 0); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsFlip(input *C.VipsImage, dir Direction) (*C.VipsImage, error) {
incOpCounter("flip")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.flip_image(input, &output, C.int(dir)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsInvert(input *C.VipsImage) (*C.VipsImage, error) {
incOpCounter("invert")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.invert_image(input, &output); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsGaussianBlur(input *C.VipsImage, sigma float64) (*C.VipsImage, error) {
incOpCounter("gaussblur")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.gaussian_blur(input, &output, C.double(sigma)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsJoin(input1 *C.VipsImage, input2 *C.VipsImage, dir Direction) (*C.VipsImage, error) {
incOpCounter("join")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input1))
defer C.g_object_unref(C.gpointer(input2))
if err := C.join(input1, input2, &output, C.int(dir)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsArrayJoin(inputs []*C.VipsImage, across int) (*C.VipsImage, error) {
incOpCounter("arrayjoin")
var output *C.VipsImage
if err := C.arrayjoin(&inputs[0], &output, C.int(len(inputs)), C.int(across)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsZoom(input *C.VipsImage, xFactor, yFactor int) (*C.VipsImage, error) {
incOpCounter("zoom")
var output *C.VipsImage
defer C.g_object_unref(C.gpointer(input))
if err := C.zoom_image(input, &output, C.int(xFactor), C.int(yFactor)); err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func vipsLabel(input *C.VipsImage, lp LabelParams) (*C.VipsImage, error) {
incOpCounter("label")
var output *C.VipsImage
text := C.CString(lp.Text)
font := C.CString(lp.Font)
color := [3]C.double{C.double(lp.Color.R), C.double(lp.Color.G), C.double(lp.Color.B)}
w := lp.Width.GetRounded(int(input.Xsize))
h := lp.Height.GetRounded(int(input.Ysize))
offsetX := lp.OffsetX.GetRounded(int(input.Xsize))
offsetY := lp.OffsetY.GetRounded(int(input.Ysize))
opts := vipsLabelOptions{
Text: text,
Font: font,
Width: C.int(w),
Height: C.int(h),
OffsetX: C.int(offsetX),
OffsetY: C.int(offsetY),
Alignment: C.VipsAlign(lp.Alignment),
Opacity: C.float(lp.Opacity),
Color: color,
}
defer C.free(unsafe.Pointer(text))
defer C.free(unsafe.Pointer(font))
err := C.label(input, &output, (*C.LabelOptions)(unsafe.Pointer(&opts)))
if err != 0 {
return nil, handleVipsError()
}
return output, nil
}
func handleVipsError() error {
defer C.vips_thread_shutdown()
defer C.vips_error_clear()
s := C.GoString(C.vips_error_buffer())
stack := string(dbg.Stack())
return fmt.Errorf("%s\nStack:\n%s", s, stack)
}