/
image.go
299 lines (217 loc) · 5.22 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
package native
// to consider... https://github.com/corona10/goimghdr
import (
"bufio"
"bytes"
"errors"
"image"
"image/gif"
"image/jpeg"
"image/png"
_ "log"
"github.com/aaronland/go-mimetypes"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/segment"
"github.com/anthonynsimon/bild/transform"
iiifconfig "github.com/go-iiif/go-iiif/v6/config"
iiifimage "github.com/go-iiif/go-iiif/v6/image"
iiifsource "github.com/go-iiif/go-iiif/v6/source"
"github.com/muesli/smartcrop"
"github.com/muesli/smartcrop/nfnt"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
type NativeImage struct {
iiifimage.Image
config *iiifconfig.Config
source iiifsource.Source
source_id string
id string
img image.Image
format string
}
type NativeDimensions struct {
iiifimage.Dimensions
bounds image.Rectangle
}
func (d *NativeDimensions) Height() int {
return d.bounds.Max.Y
}
func (d *NativeDimensions) Width() int {
return d.bounds.Max.X
}
func (im *NativeImage) Update(body []byte) error {
img, fmt, err := decodeImageBytes(body)
if err != nil {
return err
}
im.img = img
im.format = fmt
return nil
}
func (im *NativeImage) Body() []byte {
body, _ := encodeImage(im.img, im.format)
return body
}
func (im *NativeImage) Format() string {
return im.format
}
func (im *NativeImage) ContentType() string {
format := im.Format()
t := mimetypes.TypesByExtension(format)
return t[0]
}
func (im *NativeImage) Identifier() string {
return im.id
}
func (im *NativeImage) Rename(id string) error {
im.id = id
return nil
}
func (im *NativeImage) Dimensions() (iiifimage.Dimensions, error) {
dims := &NativeDimensions{
bounds: im.img.Bounds(),
}
return dims, nil
}
func (im *NativeImage) Transform(t *iiifimage.Transformation) error {
if t.Region != "full" {
rgi, err := t.RegionInstructions(im)
if err != nil {
return err
}
si, err := t.SizeInstructions(im)
if err != nil {
return err
}
if rgi.SmartCrop {
resizer := nfnt.NewDefaultResizer()
analyzer := smartcrop.NewAnalyzer(resizer)
width := si.Width
height := si.Height
topCrop, err := analyzer.FindBestCrop(im.img, width, height)
if err != nil {
return err
}
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
cropped := im.img.(SubImager).SubImage(topCrop)
if cropped.Bounds().Dx() != width || cropped.Bounds().Dy() != height {
cropped = resizer.Resize(cropped, uint(width), uint(height))
}
im.img = cropped
} else {
x1 := rgi.X
y1 := rgi.Y
x2 := x1 + rgi.Width
y2 := y1 + rgi.Height
bounds := image.Rect(x1, y1, x2, y2)
img := transform.Crop(im.img, bounds)
// Crop returns a new image which contains the intersection between the rect and
// the image provided as params. Only the intersection is returned. If a rect larger
// than the image is provided, no fill is done to the 'empty' area.
// https://godoc.org/github.com/anthonynsimon/bild/transform#Crop
im.img = img
}
}
if t.Size != "max" && t.Size != "full" {
si, err := t.SizeInstructions(im)
if err != nil {
return err
}
w := si.Width
h := si.Height
// https://github.com/anthonynsimon/bild/#resize-resampling-filters
// https://godoc.org/github.com/anthonynsimon/bild/transform#ResampleFilter
img := transform.Resize(im.img, w, h, transform.Lanczos)
im.img = img
}
ri, err := t.RotationInstructions(im)
if err != nil {
return nil
}
// auto-rotate checks... are they necessary in a plain-vanilla Go context?
if ri.Angle > 0.0 {
angle := float64(ri.Angle)
img := transform.Rotate(im.img, angle, nil)
im.img = img
}
if ri.Flip {
img := transform.FlipH(im.img)
im.img = img
}
switch t.Quality {
case "color", "default":
// do nothing.
case "gray":
img := effect.Grayscale(im.img)
im.img = img
case "bitonal":
img := segment.Threshold(im.img, 128)
im.img = img
default:
// this should be trapped above
}
fi, err := t.FormatInstructions(im)
if err != nil {
return err
}
encode := false
if fi.Format != im.format {
encode = true
// sigh... computers, amirite?
if fi.Format == "jpg" && im.format == "jpeg" {
encode = false
}
}
if encode {
body, err := encodeImage(im.img, fi.Format)
if err != nil {
return err
}
img, format, err := decodeImageBytes(body)
if err != nil {
return err
}
im.img = img
im.format = format
}
err = iiifimage.ApplyCustomTransformations(t, im)
if err != nil {
return err
}
return nil
}
func decodeImageBytes(body []byte) (image.Image, string, error) {
buf := bytes.NewBuffer(body)
return image.Decode(buf)
}
func encodeImage(im image.Image, format string) ([]byte, error) {
var b bytes.Buffer
wr := bufio.NewWriter(&b)
var err error
switch format {
case "bmp":
bmp.Encode(wr, im)
case "jpg", "jpeg":
opts := jpeg.Options{Quality: 100}
err = jpeg.Encode(wr, im, &opts)
case "png":
err = png.Encode(wr, im)
case "gif":
opts := gif.Options{}
err = gif.Encode(wr, im, &opts)
case "tiff":
opts := tiff.Options{}
err = tiff.Encode(wr, im, &opts)
default:
err = errors.New("Unsupported encoding")
}
if err != nil {
return nil, err
}
wr.Flush()
return b.Bytes(), nil
}