-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
366 lines (266 loc) · 7.24 KB
/
image.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
package vips
// https://github.com/h2non/bimg
// https://github.com/jcupitt/libvips
import (
"bytes"
"errors"
"fmt"
iiifconfig "github.com/go-iiif/go-iiif/v3/config"
iiifimage "github.com/go-iiif/go-iiif/v3/image"
iiifsource "github.com/go-iiif/go-iiif/v3/source"
"gopkg.in/h2non/bimg.v1"
"image"
"image/gif"
_ "log"
)
type VIPSImage struct {
iiifimage.Image
config *iiifconfig.Config
source iiifsource.Source
source_id string
id string
bimg *bimg.Image
isgif bool
}
type VIPSDimensions struct {
iiifimage.Dimensions
imagesize bimg.ImageSize
}
func (d *VIPSDimensions) Height() int {
return d.imagesize.Height
}
func (d *VIPSDimensions) Width() int {
return d.imagesize.Width
}
/*
See notes in NewVIPSImageFromConfigWithSource - basically getting an image's
dimensions after the we've done the GIF conversion (just see the notes...)
will make bimg/libvips sad so account for that in Dimensions() and create a
pure Go implementation of the Dimensions interface (20160922/thisisaaronland)
*/
type GolangImageDimensions struct {
iiifimage.Dimensions
image image.Image
}
func (dims *GolangImageDimensions) Width() int {
bounds := dims.image.Bounds()
return bounds.Max.X
}
func (dims *GolangImageDimensions) Height() int {
bounds := dims.image.Bounds()
return bounds.Max.Y
}
func (im *VIPSImage) Update(body []byte) error {
bimg := bimg.NewImage(body)
im.bimg = bimg
return nil
}
func (im *VIPSImage) Body() []byte {
return im.bimg.Image()
}
func (im *VIPSImage) Format() string {
return im.bimg.Type()
}
func (im *VIPSImage) ContentType() string {
format := im.Format()
if format == "jpg" || format == "jpeg" {
return "image/jpeg"
} else if format == "png" {
return "image/png"
} else if format == "webp" {
return "image/webp"
} else if format == "svg" {
return "image/svg+xml"
} else if format == "tif" || format == "tiff" {
return "image/tiff"
} else if format == "gif" {
return "image/gif"
} else {
return ""
}
}
func (im *VIPSImage) Identifier() string {
return im.id
}
func (im *VIPSImage) Rename(id string) error {
im.id = id
return nil
}
func (im *VIPSImage) Dimensions() (iiifimage.Dimensions, error) {
// see notes in NewVIPSImageFromConfigWithSource
// ideally this never gets triggered but just in case...
if im.isgif {
buf := bytes.NewBuffer(im.Body())
goimg, err := gif.Decode(buf)
if err != nil {
return nil, err
}
d := GolangImageDimensions{
image: goimg,
}
return &d, nil
}
sz, err := im.bimg.Size()
if err != nil {
return nil, err
}
d := VIPSDimensions{
imagesize: sz,
}
return &d, nil
}
// https://godoc.org/github.com/h2non/bimg#Options
func (im *VIPSImage) Transform(t *iiifimage.Transformation) error {
// https://godoc.org/github.com/h2non/bimg#Options
opts := bimg.Options{}
opts.Quality = 100
if t.Region != "full" {
rgi, err := t.RegionInstructions(im)
if err != nil {
return err
}
if rgi.SmartCrop {
opts.Gravity = bimg.GravitySmart
opts.Crop = true
opts.Width = rgi.Width
opts.Height = rgi.Height
} else {
opts.AreaWidth = rgi.Width
opts.AreaHeight = rgi.Height
opts.Left = rgi.X
opts.Top = rgi.Y
if opts.Top == 0 && opts.Left == 0 {
opts.Top = -1
}
}
/*
We need to do this or libvips will freak out and think it's trying to save
an SVG file which it can't do (20160929/thisisaaronland)
*/
if im.ContentType() == "image/svg+xml" {
opts.Type = bimg.PNG
}
/*
So here's a thing that we need to do because... computers?
(20160910/thisisaaronland)
*/
_, err = im.bimg.Process(opts)
if err != nil {
return err
}
// This is important and without it tiling gets completely
// fubar-ed (20180620/thisisaaronland)
// https://github.com/aaronland/go-iiif/issues/46
opts = bimg.Options{}
opts.Quality = 100
} else {
dims, err := im.Dimensions()
if err != nil {
return err
}
opts.Width = dims.Width() // opts.AreaWidth,
opts.Height = dims.Height() // opts.AreaHeight,
}
if t.Size != "max" && t.Size != "full" {
dims, err := im.Dimensions()
if err != nil {
return err
}
width := dims.Width()
height := dims.Height()
si, err := t.SizeInstructionsWithDimensions(im, width, height)
if err != nil {
return err
}
opts.Width = si.Width
opts.Height = si.Height
opts.Force = si.Force
}
ri, err := t.RotationInstructions(im)
if err != nil {
return nil
}
// Okay, so there are a few things are happening here:
// So apparently we need to explicitly swap height and width when we're auto-rotating
// images based on EXIF orientation... which I guess makes but is kind of annoying but
// so are a lot of things in life...
// But it gets better... if you don't resize the image then all the metadata
// gets preserved including the orientation flags which no longer make any
// sense we've applied automagic orientation rotation hoohah so every application
// that looks at the output (including this tool) will just keep rotating the
// image by n-degrees every time. We could opts.StripMetaData all the images but that
// seems a bit extreme and there is currently no way in bimg, libvips, some other pure-Go
// package to strip or reset the Orientation EXIF tag. Instead we are hijacking the
// IIIF spec to add make "-1" a valid rotation (see compliance/level2.go) which means
// "do not autorotate based on EXIF orientation". As of this writing it does not work
// in combination with other rotation commands (something like "-1,180" or "#180") but
// it probably should... (20180607/thisisaaronland)
// See also: https://github.com/h2non/bimg/issues/179
if ri.NoAutoRotate {
opts.NoAutoRotate = true
} else {
opts.Flip = ri.Flip
opts.Rotate = bimg.Angle(ri.Angle % 360)
}
if !opts.NoAutoRotate {
m, e := im.bimg.Metadata()
if e == nil {
// things that are on their side
// https://magnushoff.com/jpeg-orientation.html
if m.Orientation >= 5 && m.Orientation <= 8 {
w := opts.Width
h := opts.Height
opts.Width = h
opts.Height = w
}
}
}
if t.Quality == "color" || t.Quality == "default" {
// do nothing.
} else if t.Quality == "gray" {
opts.Interpretation = bimg.InterpretationBW
} else if t.Quality == "bitonal" {
opts.Interpretation = bimg.InterpretationBW
} else {
// this should be trapped above
}
fi, err := t.FormatInstructions(im)
if err != nil {
return nil
}
if fi.Format == "jpg" {
opts.Type = bimg.JPEG
} else if fi.Format == "png" {
opts.Type = bimg.PNG
} else if fi.Format == "webp" {
opts.Type = bimg.WEBP
} else if fi.Format == "tif" {
opts.Type = bimg.TIFF
} else if fi.Format == "gif" {
opts.Type = bimg.PNG // see this - we're just going to trick libvips until the very last minute...
} else {
msg := fmt.Sprintf("Unsupported image format '%s'", fi.Format)
return errors.New(msg)
}
_, err = im.bimg.Process(opts)
if err != nil {
return err
}
err = iiifimage.ApplyCustomTransformations(t, im)
if err != nil {
return err
}
// see notes in NewVIPSImageFromConfigWithSource
if fi.Format == "gif" && !im.isgif {
goimg, err := iiifimage.IIIFImageToGolangImage(im)
if err != nil {
return err
}
im.isgif = true
err = iiifimage.GolangImageToIIIFImage(goimg, im)
if err != nil {
return err
}
}
return nil
}