forked from cshum/imagor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
60 lines (53 loc) · 1.36 KB
/
source.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
package vips
// #include "source.h"
import "C"
import (
"fmt"
"github.com/xudaolong/imagor/vips/pointer"
"io"
"sync"
"unsafe"
)
// Source contains a libvips VipsSourceCustom and manages its lifecycle.
type Source struct {
reader io.ReadCloser
seeker io.Seeker
src *C.VipsSourceCustom
ptr unsafe.Pointer
lock sync.Mutex
}
// NewSource creates Source from reader
func NewSource(reader io.ReadCloser) *Source {
startupIfNeeded()
s := &Source{reader: reader}
seeker, ok := reader.(io.ReadSeeker)
if ok {
s.seeker = seeker
s.ptr = pointer.Save(s)
s.src = C.create_go_custom_source_with_seek(s.ptr)
} else {
s.ptr = pointer.Save(s)
s.src = C.create_go_custom_source(s.ptr)
}
return s
}
// Close source
func (s *Source) Close() {
s.lock.Lock()
if s.ptr != nil {
C.clear_source(&s.src)
pointer.Unref(s.ptr)
s.ptr = nil
_ = s.reader.Close()
log("vips", LogLevelDebug, fmt.Sprintf("closing source %p", s))
}
s.lock.Unlock()
}
// LoadImage creates a new Image from source
func (s *Source) LoadImage(params *ImportParams) (*Image, error) {
return LoadImageFromSource(s, params)
}
// LoadThumbnail creates a new Image with thumbnail crop and resize from source
func (s *Source) LoadThumbnail(width, height int, crop Interesting, size Size, params *ImportParams) (*Image, error) {
return LoadThumbnailFromSource(s, width, height, crop, size, params)
}