forked from RobCherry/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vips.go
1631 lines (1443 loc) · 39.7 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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package govips
/*
#cgo pkg-config: vips
#include "vips.h"
*/
import "C"
import (
"errors"
"fmt"
"image"
"image/color"
"io"
"io/ioutil"
"os"
"sync"
"sync/atomic"
"unsafe"
)
// Special constants used to signify a zero value instead of the default value.
const (
INT_ZERO = -1
FLOAT_ZERO = -1.0
STRING_ZERO = "GOVIPS_STRING_ZERO"
DEFAULT_CONCURRENCY = 0
DEFAULT_CACHE_MAX = 1000
DEFAULT_CACHE_MAX_FILES = 100
DEFAULT_CACHE_MAX_MEMORY = 100 * 1024 * 1024
)
var (
ErrInitialize = errors.New("Failed to initialize libvips")
ErrConfigure = errors.New("Failed to configure libvips")
ErrLoad = errors.New("Failed to load image")
ErrSave = errors.New("Failed to save image")
ErrEmbed = errors.New("Failed to embed image")
ErrCrop = errors.New("Failed to crop image")
ErrShrink = errors.New("Failed to shrink image")
ErrReduce = errors.New("Failed to reduce image")
ErrResize = errors.New("Failed to resize image")
ErrAffine = errors.New("Failed to affine image")
ErrBlur = errors.New("Failed to blur image")
ErrSharpen = errors.New("Failed to sharpen image")
ErrFlatten = errors.New("Failed to flatten image")
ErrColourspace = errors.New("Failed to convert colourspace of image")
ErrICCTransform = errors.New("Failed to transform colourspace of image")
)
var (
VIPS_BACKGROUND_BLACK = []float64{0}
VIPS_BACKGROUND_WHITE = []float64{255}
)
type Config struct {
Concurrency int
CacheMax int
CacheMaxFiles int
CacheMaxMemory int
}
var (
initializeLock sync.Mutex
initialized uint32
)
func Initialize() error {
initializeLock.Lock()
defer initializeLock.Unlock()
if atomic.LoadUint32(&initialized) == 0 {
if err := C.vips_init(cApplicationNane); err != 0 {
C.vips_shutdown()
return ErrInitialize
}
atomic.StoreUint32(&initialized, 1)
}
return nil
}
func InitializeWithConfig(config Config) error {
err := Initialize()
if err != nil {
return err
}
return Configure(config)
}
func Configure(config Config) error {
initializeLock.Lock()
defer initializeLock.Unlock()
if atomic.LoadUint32(&initialized) == 0 {
return ErrConfigure
}
if config.Concurrency > 0 {
C.vips_concurrency_set(C.int(config.Concurrency))
} else {
C.vips_concurrency_set(C.int(DEFAULT_CONCURRENCY))
}
if config.CacheMax > 0 {
C.vips_cache_set_max(C.int(config.CacheMax))
} else {
C.vips_cache_set_max(C.int(DEFAULT_CACHE_MAX))
}
if config.CacheMaxFiles > 0 {
C.vips_cache_set_max_files(C.int(config.CacheMaxFiles))
} else {
C.vips_cache_set_max_files(C.int(DEFAULT_CACHE_MAX_FILES))
}
if config.CacheMaxMemory > 0 {
C.vips_cache_set_max_mem(C.size_t(config.CacheMaxMemory))
} else {
C.vips_cache_set_max_mem(C.size_t(DEFAULT_CACHE_MAX_MEMORY))
}
return nil
}
func Shutdown() {
initializeLock.Lock()
defer initializeLock.Unlock()
if atomic.LoadUint32(&initialized) == 1 {
C.vips_shutdown()
atomic.StoreUint32(&initialized, 0)
}
}
func ThreadShutdown() {
C.vips_thread_shutdown()
}
func ErrorBuffer() error {
C.vips_error_freeze()
defer C.vips_error_thaw()
errorBuffer := C.GoString(C.vips_error_buffer())
if len(errorBuffer) == 0 {
return nil
}
C.vips_error_clear()
return errors.New(errorBuffer)
}
// Constants
var (
cApplicationNane = C.CString("govips")
cVIPS_META_ICC_NAME = C.CString(C.VIPS_META_ICC_NAME)
)
type VipsInterpretation int
func (i VipsInterpretation) toC() C.VipsInterpretation {
return C.VipsInterpretation(i)
}
const (
VIPS_INTERPRETATION_ERROR VipsInterpretation = C.VIPS_INTERPRETATION_ERROR
VIPS_INTERPRETATION_MULTIBAND VipsInterpretation = C.VIPS_INTERPRETATION_MULTIBAND
VIPS_INTERPRETATION_B_W VipsInterpretation = C.VIPS_INTERPRETATION_B_W
VIPS_INTERPRETATION_HISTOGRAM VipsInterpretation = C.VIPS_INTERPRETATION_HISTOGRAM
VIPS_INTERPRETATION_XYZ VipsInterpretation = C.VIPS_INTERPRETATION_XYZ
VIPS_INTERPRETATION_LAB VipsInterpretation = C.VIPS_INTERPRETATION_LAB
VIPS_INTERPRETATION_CMYK VipsInterpretation = C.VIPS_INTERPRETATION_CMYK
VIPS_INTERPRETATION_LABQ VipsInterpretation = C.VIPS_INTERPRETATION_LABQ
VIPS_INTERPRETATION_RGB VipsInterpretation = C.VIPS_INTERPRETATION_RGB
VIPS_INTERPRETATION_CMC VipsInterpretation = C.VIPS_INTERPRETATION_CMC
VIPS_INTERPRETATION_LCH VipsInterpretation = C.VIPS_INTERPRETATION_LCH
VIPS_INTERPRETATION_LABS VipsInterpretation = C.VIPS_INTERPRETATION_LABS
VIPS_INTERPRETATION_sRGB VipsInterpretation = C.VIPS_INTERPRETATION_sRGB
VIPS_INTERPRETATION_YXY VipsInterpretation = C.VIPS_INTERPRETATION_YXY
VIPS_INTERPRETATION_FOURIER VipsInterpretation = C.VIPS_INTERPRETATION_FOURIER
VIPS_INTERPRETATION_RGB16 VipsInterpretation = C.VIPS_INTERPRETATION_RGB16
VIPS_INTERPRETATION_GREY16 VipsInterpretation = C.VIPS_INTERPRETATION_GREY16
VIPS_INTERPRETATION_MATRIX VipsInterpretation = C.VIPS_INTERPRETATION_MATRIX
VIPS_INTERPRETATION_scRGB VipsInterpretation = C.VIPS_INTERPRETATION_scRGB
VIPS_INTERPRETATION_HSV VipsInterpretation = C.VIPS_INTERPRETATION_HSV
VIPS_INTERPRETATION_LAST VipsInterpretation = C.VIPS_INTERPRETATION_LAST
)
type VipsAccess int
func (a VipsAccess) toC() C.VipsAccess {
switch a {
case VIPS_ACCESS_RANDOM:
return C.VIPS_ACCESS_RANDOM
case VIPS_ACCESS_SEQUENTIAL:
return C.VIPS_ACCESS_SEQUENTIAL
case VIPS_ACCESS_SEQUENTIAL_UNBUFFERED:
return C.VIPS_ACCESS_SEQUENTIAL_UNBUFFERED
case VIPS_ACCESS_LAST:
return C.VIPS_ACCESS_LAST
default:
return C.VIPS_ACCESS_RANDOM
}
}
const (
VIPS_ACCESS_RANDOM VipsAccess = iota
VIPS_ACCESS_SEQUENTIAL
VIPS_ACCESS_SEQUENTIAL_UNBUFFERED
VIPS_ACCESS_LAST
)
const (
JPEG_QUANTIZATION_TABLE_DEFAULT int = 0
JPEG_QUANTIZATION_TABLE_FLAT
JPEG_QUANTIZATION_TABLE_MSSIM
JPEG_QUANTIZATION_TABLE_IMAGEMAGICK
JPEG_QUANTIZATION_TABLE_PSNR_HVS_M
)
type PngFilter int
func (p PngFilter) toC() C.VipsForeignPngFilter {
switch p {
case VIPS_PNG_FILTER_ALL:
return C.VIPS_FOREIGN_PNG_FILTER_ALL
case VIPS_PNG_FILTER_NONE:
return C.VIPS_FOREIGN_PNG_FILTER_NONE
case VIPS_PNG_FILTER_SUB:
return C.VIPS_FOREIGN_PNG_FILTER_SUB
case VIPS_PNG_FILTER_UP:
return C.VIPS_FOREIGN_PNG_FILTER_UP
case VIPS_PNG_FILTER_AVG:
return C.VIPS_FOREIGN_PNG_FILTER_AVG
case VIPS_PNG_FILTER_PAETH:
return C.VIPS_FOREIGN_PNG_FILTER_PAETH
default:
return C.VIPS_FOREIGN_PNG_FILTER_ALL
}
}
const (
VIPS_PNG_FILTER_DEFAULT PngFilter = iota
VIPS_PNG_FILTER_NONE
VIPS_PNG_FILTER_SUB
VIPS_PNG_FILTER_UP
VIPS_PNG_FILTER_AVG
VIPS_PNG_FILTER_PAETH
VIPS_PNG_FILTER_ALL
)
type WebpPreset int
func (p WebpPreset) toC() C.VipsForeignWebpPreset {
switch p {
case VIPS_WEBP_PRESET_DEFAULT:
return C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT
case VIPS_WEBP_PRESET_PICTURE:
return C.VIPS_FOREIGN_WEBP_PRESET_PICTURE
case VIPS_WEBP_PRESET_PHOTO:
return C.VIPS_FOREIGN_WEBP_PRESET_PHOTO
case VIPS_WEBP_PRESET_DRAWING:
return C.VIPS_FOREIGN_WEBP_PRESET_DRAWING
case VIPS_WEBP_PRESET_ICON:
return C.VIPS_FOREIGN_WEBP_PRESET_ICON
case VIPS_WEBP_PRESET_TEXT:
return C.VIPS_FOREIGN_WEBP_PRESET_TEXT
default:
return C.VIPS_FOREIGN_WEBP_PRESET_LAST
}
}
const (
VIPS_WEBP_PRESET_DEFAULT WebpPreset = iota
VIPS_WEBP_PRESET_PICTURE
VIPS_WEBP_PRESET_PHOTO
VIPS_WEBP_PRESET_DRAWING
VIPS_WEBP_PRESET_ICON
VIPS_WEBP_PRESET_TEXT
VIPS_WEBP_PRESET_LAST
)
type VipsExtend int
func (e VipsExtend) toC() C.VipsExtend {
switch e {
case VIPS_EXTEND_BLACK:
return C.VIPS_EXTEND_BLACK
case VIPS_EXTEND_COPY:
return C.VIPS_EXTEND_COPY
case VIPS_EXTEND_REPEAT:
return C.VIPS_EXTEND_REPEAT
case VIPS_EXTEND_MIRROR:
return C.VIPS_EXTEND_MIRROR
case VIPS_EXTEND_WHITE:
return C.VIPS_EXTEND_WHITE
case VIPS_EXTEND_BACKGROUND:
return C.VIPS_EXTEND_BACKGROUND
default:
return C.VIPS_EXTEND_BLACK
}
}
const (
VIPS_EXTEND_BLACK VipsExtend = iota
VIPS_EXTEND_COPY
VIPS_EXTEND_REPEAT
VIPS_EXTEND_MIRROR
VIPS_EXTEND_WHITE
VIPS_EXTEND_BACKGROUND
)
type VipsKernel int
func (k VipsKernel) toC() C.VipsKernel {
switch k {
case VIPS_KERNEL_NEAREST:
return C.VIPS_KERNEL_NEAREST
case VIPS_KERNEL_LINEAR:
return C.VIPS_KERNEL_LINEAR
case VIPS_KERNEL_CUBIC:
return C.VIPS_KERNEL_CUBIC
case VIPS_KERNEL_LANCZOS2:
return C.VIPS_KERNEL_LANCZOS2
case VIPS_KERNEL_LANCZOS3:
return C.VIPS_KERNEL_LANCZOS3
case VIPS_KERNEL_LAST:
return C.VIPS_KERNEL_LAST
default:
return C.VIPS_KERNEL_LANCZOS3
}
}
const (
VIPS_KERNEL_NEAREST VipsKernel = iota
VIPS_KERNEL_LINEAR
VIPS_KERNEL_CUBIC
VIPS_KERNEL_LANCZOS2
VIPS_KERNEL_LANCZOS3
VIPS_KERNEL_LAST
)
type VipsPrecision int
func (p VipsPrecision) toC() C.VipsPrecision {
switch p {
case VIPS_PRECISION_INTEGER:
return C.VIPS_PRECISION_INTEGER
case VIPS_PRECISION_FLOAT:
return C.VIPS_PRECISION_FLOAT
case VIPS_PRECISION_APPROXIMATE:
return C.VIPS_PRECISION_APPROXIMATE
case VIPS_PRECISION_LAST:
return C.VIPS_PRECISION_LAST
default:
return C.VIPS_PRECISION_INTEGER
}
}
const (
VIPS_PRECISION_INTEGER VipsPrecision = iota
VIPS_PRECISION_FLOAT
VIPS_PRECISION_APPROXIMATE
VIPS_PRECISION_LAST
)
type VipsIntent int
func (i VipsIntent) toC() C.VipsIntent {
switch i {
case VIPS_INTENT_PERCEPTUAL:
return C.VIPS_INTENT_PERCEPTUAL
case VIPS_INTENT_RELATIVE:
return C.VIPS_INTENT_RELATIVE
case VIPS_INTENT_SATURATION:
return C.VIPS_INTENT_SATURATION
case VIPS_INTENT_ABSOLUTE:
return C.VIPS_INTENT_ABSOLUTE
case VIPS_INTENT_LAST:
return C.VIPS_INTENT_LAST
default:
return C.VIPS_INTENT_PERCEPTUAL
}
}
const (
VIPS_INTENT_PERCEPTUAL = iota
VIPS_INTENT_RELATIVE
VIPS_INTENT_SATURATION
VIPS_INTENT_ABSOLUTE
VIPS_INTENT_LAST
)
// Image
type VipsImage struct {
cVipsImage *C.struct__VipsImage
goBytes []byte
}
func (v *VipsImage) Bounds() image.Rectangle {
if v.cVipsImage == nil {
return image.ZR
}
return image.Rect(0, 0, int(C.vips_image_get_width(v.cVipsImage)), int(C.vips_image_get_height(v.cVipsImage)))
}
func (v *VipsImage) Interpretation() VipsInterpretation {
if v.cVipsImage == nil {
return VIPS_INTERPRETATION_ERROR
}
return VipsInterpretation(C.vips_image_guess_interpretation(v.cVipsImage))
}
func (v *VipsImage) Bands() int {
if v.cVipsImage == nil {
return 0
}
return int(v.cVipsImage.Bands)
}
func (v *VipsImage) HasProfile() bool {
if v.cVipsImage == nil {
return false
}
return C.vips_image_get_typeof(v.cVipsImage, cVIPS_META_ICC_NAME) != 0
}
func (v *VipsImage) RemoveProfile() {
C.vips_image_remove(v.cVipsImage, cVIPS_META_ICC_NAME)
}
func (v *VipsImage) Free() {
if v.cVipsImage != nil {
C.g_object_unref(C.gpointer(v.cVipsImage))
v.cVipsImage = nil
}
if v.goBytes != nil {
v.goBytes = nil
}
}
func newVipsImage(i *C.struct__VipsImage, b []byte) *VipsImage {
return &VipsImage{cVipsImage: i, goBytes: b}
}
// Decode
type DecodeOptions struct {
Access VipsAccess
Disc bool
}
func (o DecodeOptions) toC() cDecodeOptions {
return cDecodeOptions{
Access: o.Access.toC(),
Disc: toGBool(o.Disc),
}
}
type cDecodeOptions struct {
Access C.VipsAccess
Disc C.gboolean
}
func (c *cDecodeOptions) Free() {
}
type DecodeGifOptions struct {
DecodeOptions
Page int
}
func (o DecodeGifOptions) toC() cDecodeGifOptions {
return cDecodeGifOptions{
cDecodeOptions: o.DecodeOptions.toC(),
Page: C.gint(o.Page),
}
}
type cDecodeGifOptions struct {
cDecodeOptions
Page C.gint
}
func (c *cDecodeGifOptions) Free() {
c.cDecodeOptions.Free()
}
type DecodeJpegOptions struct {
DecodeOptions
Shrink int
Fail bool
Autorotate bool
}
func (o DecodeJpegOptions) toC() cDecodeJpegOptions {
if o.Shrink >= 8 {
o.Shrink = 8
} else if o.Shrink >= 4 {
o.Shrink = 4
} else if o.Shrink >= 2 {
o.Shrink = 2
} else {
o.Shrink = 1
}
return cDecodeJpegOptions{
cDecodeOptions: o.DecodeOptions.toC(),
Shrink: C.gint(o.Shrink),
Fail: toGBool(o.Fail),
Autorotate: toGBool(o.Autorotate),
}
}
type cDecodeJpegOptions struct {
cDecodeOptions
Shrink C.gint
Fail C.gboolean
Autorotate C.gboolean
}
func (c *cDecodeJpegOptions) Free() {
c.cDecodeOptions.Free()
}
type DecodeMagickOptions struct {
DecodeOptions
AllFrames bool
Density string
Page int
}
func (o DecodeMagickOptions) toC() cDecodeMagickOptions {
var density *C.char
if o.Density == STRING_ZERO {
density = C.CString("")
} else if o.Density != "" {
density = C.CString(o.Density)
}
return cDecodeMagickOptions{
cDecodeOptions: o.DecodeOptions.toC(),
AllFrames: toGBool(o.AllFrames),
Density: density,
Page: C.gint(o.Page),
}
}
type cDecodeMagickOptions struct {
cDecodeOptions
AllFrames C.gboolean
Density *C.char
Page C.gint
}
func (c *cDecodeMagickOptions) Free() {
c.cDecodeOptions.Free()
if c.Density != nil {
C.free(unsafe.Pointer(c.Density))
c.Density = nil
}
}
type DecodeWebpOptions struct {
DecodeOptions
Shrink int
}
func (o DecodeWebpOptions) toC() cDecodeWebpOptions {
if o.Shrink < 1 {
o.Shrink = 1
}
return cDecodeWebpOptions{
cDecodeOptions: o.DecodeOptions.toC(),
Shrink: C.gint(o.Shrink),
}
}
type cDecodeWebpOptions struct {
cDecodeOptions
Shrink C.gint
}
func (c *cDecodeWebpOptions) Free() {
c.cDecodeOptions.Free()
}
func DecodeGifReader(r io.Reader, options *DecodeGifOptions) (*VipsImage, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodeGifBytes(b, options)
}
func DecodeGifBytes(b []byte, options *DecodeGifOptions) (*VipsImage, error) {
if options == nil {
options = &DecodeGifOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var i *C.struct__VipsImage
if C.govips_gifload_buffer(unsafe.Pointer(&b[0]), C.size_t(len(b)), &i, cOptions.Page, cOptions.Access, cOptions.Disc) != 0 {
return nil, ErrLoad
}
return newVipsImage(i, b), nil
}
func DecodeJpegReader(r io.Reader, options *DecodeJpegOptions) (*VipsImage, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodeJpegBytes(b, options)
}
func DecodeJpegBytes(b []byte, options *DecodeJpegOptions) (*VipsImage, error) {
if options == nil {
options = &DecodeJpegOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var i *C.struct__VipsImage
if C.govips_jpegload_buffer(unsafe.Pointer(&b[0]), C.size_t(len(b)), &i, cOptions.Shrink, cOptions.Fail, cOptions.Autorotate, cOptions.Access, cOptions.Disc) != 0 {
return nil, ErrLoad
}
return newVipsImage(i, b), nil
}
func DecodeMagickReader(r io.Reader, options *DecodeMagickOptions) (*VipsImage, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodeMagickBytes(b, options)
}
func DecodeMagickBytes(b []byte, options *DecodeMagickOptions) (*VipsImage, error) {
if options == nil {
options = &DecodeMagickOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var i *C.struct__VipsImage
if C.govips_magickload_buffer(unsafe.Pointer(&b[0]), C.size_t(len(b)), &i, cOptions.AllFrames, cOptions.Density, cOptions.Page, cOptions.Access, cOptions.Disc) != 0 {
return nil, ErrLoad
}
return newVipsImage(i, b), nil
}
func DecodePngReader(r io.Reader, options *DecodeOptions) (*VipsImage, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodePngBytes(b, options)
}
func DecodePngBytes(b []byte, options *DecodeOptions) (*VipsImage, error) {
if options == nil {
options = &DecodeOptions{}
}
cOptions := options.toC()
var i *C.struct__VipsImage
if C.govips_pngload_buffer(unsafe.Pointer(&b[0]), C.size_t(len(b)), &i, cOptions.Access, cOptions.Disc) != 0 {
return nil, ErrLoad
}
return newVipsImage(i, b), nil
}
func DecodeWebpReader(r io.Reader, options *DecodeWebpOptions) (*VipsImage, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodeWebpBytes(b, options)
}
func DecodeWebpBytes(b []byte, options *DecodeWebpOptions) (*VipsImage, error) {
if options == nil {
options = &DecodeWebpOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var i *C.struct__VipsImage
if C.govips_webpload_buffer(unsafe.Pointer(&b[0]), C.size_t(len(b)), &i, cOptions.Shrink, cOptions.Access, cOptions.Disc) != 0 {
return nil, ErrLoad
}
return newVipsImage(i, b), nil
}
// Encode
type EncodeJpegOptions struct {
Q int
Profile string
OptimizeCoding bool
Interlace bool
Strip bool
NoSubsample bool
TrellisQuantization bool
OvershootDeringing bool
OptimizeScans bool
QuantizationTable int
}
func (o EncodeJpegOptions) toC() cEncodeJpegOptions {
if o.Q == 0 {
o.Q = 75
} else if o.Q == INT_ZERO {
o.Q = 0
}
var profile *C.char
if o.Profile == STRING_ZERO {
profile = C.CString("")
} else if o.Profile != "" {
profile = C.CString(o.Profile)
}
return cEncodeJpegOptions{
Q: C.gint(o.Q),
Profile: profile,
OptimizeCoding: toGBool(o.OptimizeCoding),
Interlace: toGBool(o.Interlace),
Strip: toGBool(o.Strip),
NoSubsample: toGBool(o.NoSubsample),
TrellisQuantization: toGBool(o.TrellisQuantization),
OvershootDeringing: toGBool(o.OvershootDeringing),
OptimizeScans: toGBool(o.OptimizeScans),
QuantizationTable: C.gint(o.QuantizationTable),
}
}
type cEncodeJpegOptions struct {
Q C.gint
Profile *C.char
OptimizeCoding C.gboolean
Interlace C.gboolean
Strip C.gboolean
NoSubsample C.gboolean
TrellisQuantization C.gboolean
OvershootDeringing C.gboolean
OptimizeScans C.gboolean
QuantizationTable C.gint
}
func (c *cEncodeJpegOptions) Free() {
if c.Profile != nil {
C.free(unsafe.Pointer(c.Profile))
c.Profile = nil
}
}
func EncodeJpegFile(i *VipsImage, file *os.File, options *EncodeJpegOptions) error {
if options == nil {
options = &EncodeJpegOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
cFileName := C.CString(file.Name())
defer C.free(unsafe.Pointer(cFileName))
if C.govips_jpegsave(i.cVipsImage, cFileName, cOptions.Q, cOptions.Profile, cOptions.OptimizeCoding, cOptions.Interlace, cOptions.Strip, cOptions.NoSubsample, cOptions.TrellisQuantization, cOptions.OvershootDeringing, cOptions.OptimizeScans, cOptions.QuantizationTable) != 0 {
return ErrSave
}
return nil
}
func EncodeJpegBytes(i *VipsImage, options *EncodeJpegOptions) ([]byte, error) {
if options == nil {
options = &EncodeJpegOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var obuf unsafe.Pointer
olen := C.size_t(0)
if C.govips_jpegsave_buffer(i.cVipsImage, &obuf, &olen, cOptions.Q, cOptions.Profile, cOptions.OptimizeCoding, cOptions.Interlace, cOptions.Strip, cOptions.NoSubsample, cOptions.TrellisQuantization, cOptions.OvershootDeringing, cOptions.OptimizeScans, cOptions.QuantizationTable) != 0 {
return nil, ErrSave
}
defer C.g_free(C.gpointer(obuf))
bytes := C.GoBytes(obuf, C.int(olen))
return bytes, nil
}
type EncodePngOptions struct {
Compression int
Interlace bool
Profile string
Filter PngFilter
}
func (o EncodePngOptions) toC() cEncodePngOptions {
if o.Compression == 0 {
o.Compression = 6
} else if o.Compression == INT_ZERO {
o.Compression = 0
}
var profile *C.char
if o.Profile == STRING_ZERO {
profile = C.CString("")
} else if o.Profile != "" {
profile = C.CString(o.Profile)
}
return cEncodePngOptions{
Compression: C.gint(o.Compression),
Interlace: toGBool(o.Interlace),
Profile: profile,
Filter: o.Filter.toC(),
}
}
type cEncodePngOptions struct {
Compression C.gint
Interlace C.gboolean
Profile *C.char
Filter C.VipsForeignPngFilter
}
func (c *cEncodePngOptions) Free() {
if c.Profile != nil {
C.free(unsafe.Pointer(c.Profile))
c.Profile = nil
}
}
func EncodePngFile(i *VipsImage, file *os.File, options *EncodePngOptions) error {
if options == nil {
options = &EncodePngOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
cFileName := C.CString(file.Name())
defer C.free(unsafe.Pointer(cFileName))
if C.govips_pngsave(i.cVipsImage, cFileName, cOptions.Compression, cOptions.Interlace, cOptions.Profile, cOptions.Filter) != 0 {
return ErrSave
}
return nil
}
func EncodePngBytes(i *VipsImage, options *EncodePngOptions) ([]byte, error) {
if options == nil {
options = &EncodePngOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var obuf unsafe.Pointer
olen := C.size_t(0)
if C.govips_pngsave_buffer(i.cVipsImage, &obuf, &olen, cOptions.Compression, cOptions.Interlace, cOptions.Profile, cOptions.Filter) != 0 {
return nil, ErrSave
}
defer C.g_free(C.gpointer(obuf))
bytes := C.GoBytes(obuf, C.int(olen))
return bytes, nil
}
type EncodeWebpOptions struct {
Q int
Lossless bool
Preset WebpPreset
SmartSubsample bool
NearLossless bool
AlphaQ int
}
func (o EncodeWebpOptions) toC() cEncodeWebpOptions {
if o.Q == 0 {
o.Q = 75
} else if o.Q == INT_ZERO {
o.Q = 0
}
if o.AlphaQ == 0 {
o.AlphaQ = 100
}
return cEncodeWebpOptions{
Q: C.gint(o.Q),
Lossless: toGBool(o.Lossless),
Preset: o.Preset.toC(),
SmartSubsample: toGBool(o.SmartSubsample),
NearLossless: toGBool(o.NearLossless),
AlphaQ: C.gint(o.AlphaQ),
}
}
type cEncodeWebpOptions struct {
Q C.gint
Lossless C.gboolean
Preset C.VipsForeignWebpPreset
SmartSubsample C.gboolean
NearLossless C.gboolean
AlphaQ C.gint
}
func (c *cEncodeWebpOptions) Free() {
}
func EncodeWebpFile(i *VipsImage, file *os.File, options *EncodeWebpOptions) error {
if options == nil {
options = &EncodeWebpOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
cFileName := C.CString(file.Name())
defer C.free(unsafe.Pointer(cFileName))
if C.govips_webpsave(i.cVipsImage, cFileName, cOptions.Q, cOptions.Lossless, cOptions.Preset, cOptions.SmartSubsample, cOptions.NearLossless, cOptions.AlphaQ) != 0 {
return ErrSave
}
return nil
}
func EncodeWebpBytes(i *VipsImage, options *EncodeWebpOptions) ([]byte, error) {
if options == nil {
options = &EncodeWebpOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var obuf unsafe.Pointer
olen := C.size_t(0)
if C.govips_webpsave_buffer(i.cVipsImage, &obuf, &olen, cOptions.Q, cOptions.Lossless, cOptions.Preset, cOptions.SmartSubsample, cOptions.NearLossless, cOptions.AlphaQ) != 0 {
return nil, ErrSave
}
defer C.g_free(C.gpointer(obuf))
bytes := C.GoBytes(obuf, C.int(olen))
return bytes, nil
}
// Operations
type EmbedOptions struct {
Extend VipsExtend
Background []float64
}
func (o EmbedOptions) toC() cEmbedOptions {
var Background *C.struct__VipsArrayDouble
if o.Background != nil && len(o.Background) > 0 {
Background = newVipsArrayDouble(o.Background)
}
return cEmbedOptions{
Extend: o.Extend.toC(),
Background: Background,
}
}
type cEmbedOptions struct {
Extend C.VipsExtend
Background *C.struct__VipsArrayDouble
}
func (c *cEmbedOptions) Free() {
if c.Background != nil {
vipsArrayDoubleUnref(c.Background)
c.Background = nil
}
}
func Embed(v *VipsImage, x, y, width, height int, options *EmbedOptions) (*VipsImage, error) {
if options == nil {
options = &EmbedOptions{}
}
cOptions := options.toC()
defer cOptions.Free()
var i *C.struct__VipsImage
if C.govips_embed(v.cVipsImage, &i, C.int(x), C.int(y), C.int(width), C.int(height), cOptions.Extend, cOptions.Background) != 0 {
return nil, ErrEmbed
}
return newVipsImage(i, v.goBytes), nil
}
func ExtractArea(v *VipsImage, left, top, width, height int) (*VipsImage, error) {
var i *C.struct__VipsImage
if C.govips_extract_area(v.cVipsImage, &i, C.int(left), C.int(top), C.int(width), C.int(height)) != 0 {
return nil, ErrCrop
}
return newVipsImage(i, v.goBytes), nil
}
func Crop(v *VipsImage, left, top, width, height int) (*VipsImage, error) {
return ExtractArea(v, left, top, width, height)
}
func Shrink(v *VipsImage, xshrink, yshrink float64) (*VipsImage, error) {
var i *C.struct__VipsImage
if C.govips_shrink(v.cVipsImage, &i, C.double(xshrink), C.double(yshrink)) != 0 {
return nil, ErrShrink
}
return newVipsImage(i, v.goBytes), nil
}
func ShrinkH(v *VipsImage, xshrink float64) (*VipsImage, error) {
var i *C.struct__VipsImage
if C.govips_shrinkh(v.cVipsImage, &i, C.double(xshrink)) != 0 {
return nil, ErrShrink
}
return newVipsImage(i, v.goBytes), nil
}
func ShrinkV(v *VipsImage, yshrink float64) (*VipsImage, error) {
var i *C.struct__VipsImage
if C.govips_shrinkv(v.cVipsImage, &i, C.double(yshrink)) != 0 {
return nil, ErrShrink
}
return newVipsImage(i, v.goBytes), nil
}
func Reduce(v *VipsImage, xshrink, yshrink float64, kernel VipsKernel) (*VipsImage, error) {
var i *C.struct__VipsImage
if C.govips_reduce(v.cVipsImage, &i, C.double(xshrink), C.double(yshrink), C.VipsKernel(kernel)) != 0 {
return nil, ErrReduce
}
return newVipsImage(i, v.goBytes), nil