-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.go
220 lines (176 loc) · 5.09 KB
/
library.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
package library
import (
"encoding/gob"
"errors"
"github.com/klippa-app/go-libheif/library/responses"
"image"
"io"
"log"
"os"
"os/exec"
"time"
"github.com/klippa-app/go-libheif/library/requests"
"github.com/klippa-app/go-libheif/library/shared"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
type Config struct {
Command Command
}
type Command struct {
BinPath string
Args []string
// StartTimeout is the timeout to wait for the plugin to say it
// has started successfully.
StartTimeout time.Duration
}
var client *plugin.Client
var gRPCClient plugin.ClientProtocol
var libheifplugin shared.Libheif
var currentConfig Config
func init() {
// Needed to serialize the image interface.
gob.Register(&image.YCbCr{})
gob.Register(&image.RGBA64{})
gob.Register(&image.RGBA{})
gob.Register(&image.Gray{})
}
func Init(config Config) error {
if client != nil {
return nil
}
currentConfig = config
return startPlugin()
}
func DeInit() {
client.Kill()
client = nil
gRPCClient.Close()
gRPCClient = nil
libheifplugin = nil
}
func startPlugin() error {
var handshakeConfig = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "libheif",
}
// pluginMap is the map of plugins we can dispense.
var pluginMap = map[string]plugin.Plugin{
"libheif": &shared.LibheifPlugin{},
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stdout,
Level: hclog.Debug,
})
client = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
Cmd: exec.Command(currentConfig.Command.BinPath, currentConfig.Command.Args...),
Logger: logger,
StartTimeout: currentConfig.Command.StartTimeout,
})
rpcClient, err := client.Client()
if err != nil {
return err
}
gRPCClient = rpcClient
raw, err := rpcClient.Dispense("libheif")
if err != nil {
return err
}
pluginInstance := raw.(shared.Libheif)
pong, err := pluginInstance.Ping()
if err != nil {
return err
}
if pong != "Pong" {
return errors.New("Wrong ping/pong result")
}
libheifplugin = pluginInstance
return nil
}
func checkPlugin() error {
pong, err := libheifplugin.Ping()
if err != nil {
log.Printf("restarting libheif plugin due to wrong pong result: %s", err.Error())
err = startPlugin()
if err != nil {
log.Printf("could not restart libheif plugin: %s", err.Error())
return err
}
}
if pong != "Pong" {
log.Printf("restarting libheif plugin due to wrong pong result: %s", pong)
err = startPlugin()
if err != nil {
log.Printf("could not restart libheif plugin: %s", err.Error())
return err
}
}
return nil
}
var NotInitializedError = errors.New("libheif was not initialized, you must call the Init() method")
type RenderFileOutputFormat string // The file format to render output as.
const (
RenderFileOutputFormatJPG RenderFileOutputFormat = "jpg" // Render the file as a JPEG file.
RenderFileOutputFormatPNG RenderFileOutputFormat = "png" // Render the file as a PNG file.
)
type RenderOptions struct {
OutputFormat RenderFileOutputFormat // The format to output the image as
MaxFileSize int64 // Only used when OutputFormat RenderFileOutputFormatJPG. The maximum filesize, if jpg is chosen as output format, it will try to lower the quality it until it fits.
OutputQuality int // Only used when OutputFormat RenderFileOutputFormatJPG. Ranges from 1 to 100 inclusive, higher is better. The default is 95.
Progressive bool // Only used when OutputFormat RenderFileOutputFormatJPG and with build tag go_libheif_use_turbojpeg. Will render a progressive jpeg.
}
func RenderFile(data *[]byte, options RenderOptions) (*responses.RenderFile, error) {
if libheifplugin == nil {
return nil, NotInitializedError
}
err := checkPlugin()
if err != nil {
return nil, errors.New("could not check or start plugin")
}
resp, err := libheifplugin.RenderFile(&requests.RenderFile{Data: data, OutputFormat: requests.RenderFileOutputFormat(options.OutputFormat), MaxFileSize: options.MaxFileSize, OutputQuality: options.OutputQuality, Progressive: options.Progressive})
if err != nil {
return nil, err
}
return resp, nil
}
func DecodeImage(r io.Reader) (image.Image, error) {
if libheifplugin == nil {
return nil, NotInitializedError
}
err := checkPlugin()
if err != nil {
return nil, errors.New("could not check or start plugin")
}
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
resp, err := libheifplugin.DecodeImage(&requests.DecodeImage{Data: &data})
if err != nil {
return nil, err
}
return resp.Image, nil
}
func DecodeConfig(r io.Reader) (image.Config, error) {
var config image.Config
if libheifplugin == nil {
return config, NotInitializedError
}
err := checkPlugin()
if err != nil {
return config, errors.New("could not check or start plugin")
}
data, err := io.ReadAll(r)
if err != nil {
return config, err
}
resp, err := libheifplugin.DecodeConfig(&requests.DecodeConfig{Data: &data})
if err != nil {
return config, err
}
return resp.Config, nil
}