forked from davidbyttow/govips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
713 lines (612 loc) · 15.9 KB
/
transform.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
package vips
// #cgo pkg-config: vips
// #include "bridge.h"
import "C"
import (
"bytes"
"io"
"io/ioutil"
"math"
"os"
)
// InputParams are options when importing an image from file or buffer
type InputParams struct {
InputFile string
Reader io.Reader
Image *ImageRef
}
// TransformParams are parameters for the transformation
type TransformParams struct {
PadStrategy Extend
ResizeStrategy ResizeStrategy
CropAnchor Anchor
ReductionSampler Kernel
EnlargementInterpolator Interpolator
ZoomX int
ZoomY int
Invert bool
BlurSigma float64
Flip FlipDirection
Width Scalar
Height Scalar
CropOffsetX Scalar
CropOffsetY Scalar
}
// Transform handles single image transformations
type Transform struct {
input *InputParams
tx *TransformParams
export *ExportParams
targetWidth int
targetHeight int
cropOffsetX int
cropOffsetY int
source []byte
}
// NewTransform constructs a new transform for execution
func NewTransform() *Transform {
return &Transform{
input: &InputParams{},
tx: &TransformParams{
ResizeStrategy: ResizeStrategyAuto,
CropAnchor: AnchorAuto,
ReductionSampler: KernelLanczos3,
EnlargementInterpolator: InterpolateBicubic,
},
export: &ExportParams{
Format: ImageTypeUnknown,
Quality: 90,
Interpretation: InterpretationSRGB,
},
}
}
// Image sets the image to operate on
func (t *Transform) Image(image *ImageRef) *Transform {
t.input.Image = image
return t
}
// LoadFile loads a file into the transform
func (t *Transform) LoadFile(file string) *Transform {
t.input.Reader = LazyOpen(file)
return t
}
// LoadBuffer loads a buffer into the transform
func (t *Transform) LoadBuffer(buf []byte) *Transform {
t.input.Reader = bytes.NewBuffer(buf)
return t
}
// Load loads a buffer into the transform
func (t *Transform) Load(reader io.Reader) *Transform {
t.input.Reader = reader
return t
}
// Output outputs the transform to a buffer and closes it
func (t *Transform) Output(writer io.Writer) *Transform {
t.export.Writer = writer
return t
}
// OutputBytes outputs the transform to a buffer and closes it
func (t *Transform) OutputBytes() *Transform {
t.export.Writer = nil
return t
}
// OutputFile outputs the transform to a file and closes it
func (t *Transform) OutputFile(file string) *Transform {
t.export.Writer = LazyCreate(file)
return t
}
// Zoom an image by repeating pixels. This is fast nearest-neighbour zoom.
func (t *Transform) Zoom(x, y int) *Transform {
t.tx.ZoomX = x
t.tx.ZoomY = y
return t
}
// Anchor sets the anchor for cropping
func (t *Transform) Anchor(anchor Anchor) *Transform {
t.tx.CropAnchor = anchor
return t
}
// CropOffsetX sets the target offset from the crop position
func (t *Transform) CropOffsetX(x int) *Transform {
t.tx.CropOffsetX.SetInt(x)
return t
}
// CropOffsetY sets the target offset from the crop position
func (t *Transform) CropOffsetY(y int) *Transform {
t.tx.CropOffsetY.SetInt(y)
return t
}
// CropRelativeOffsetX sets the target offset from the crop position
func (t *Transform) CropRelativeOffsetX(x float64) *Transform {
t.tx.CropOffsetX.SetScale(x)
return t
}
// CropRelativeOffsetY sets the target offset from the crop position
func (t *Transform) CropRelativeOffsetY(y float64) *Transform {
t.tx.CropOffsetY.SetScale(y)
return t
}
// Kernel sets the sampling kernel for the transform when down-scaling. Defaults to lancosz3
func (t *Transform) Kernel(kernel Kernel) *Transform {
t.tx.ReductionSampler = kernel
return t
}
// Interpolator sets the resampling interpolator when upscaling, defaults to bicubic
func (t *Transform) Interpolator(interp Interpolator) *Transform {
t.tx.EnlargementInterpolator = interp
return t
}
// ResizeStrategy sets the strategy when resizing an image
func (t *Transform) ResizeStrategy(strategy ResizeStrategy) *Transform {
t.tx.ResizeStrategy = strategy
return t
}
// PadStrategy sets the strategy when the image must be padded to maintain aspect ratoi
func (t *Transform) PadStrategy(strategy Extend) *Transform {
t.tx.PadStrategy = strategy
return t
}
// Invert inverts the image color
func (t *Transform) Invert() *Transform {
t.tx.Invert = true
return t
}
// Flip flips the image horizontally or vertically
func (t *Transform) Flip(flip FlipDirection) *Transform {
t.tx.Flip = flip
return t
}
// GaussBlur applies a gaussian blur to the image
func (t *Transform) GaussBlur(sigma float64) *Transform {
t.tx.BlurSigma = sigma
return t
}
// Embed this image appropriately if resized according to a new aspect ratio
func (t *Transform) Embed(extend Extend) *Transform {
t.tx.ResizeStrategy = ResizeStrategyEmbed
t.tx.PadStrategy = extend
return t
}
// Crop an image, width and height must be equal to or less than image size
func (t *Transform) Crop(anchor Anchor) *Transform {
t.tx.ResizeStrategy = ResizeStrategyCrop
return t
}
// Stretch an image without maintaining aspect ratio
func (t *Transform) Stretch() *Transform {
t.tx.ResizeStrategy = ResizeStrategyCrop
return t
}
// ScaleWidth scales the image by its width proportionally
func (t *Transform) ScaleWidth(scale float64) *Transform {
t.tx.Width.SetScale(scale)
return t
}
// ScaleHeight scales the height of the image proportionally
func (t *Transform) ScaleHeight(scale float64) *Transform {
t.tx.Height.SetScale(scale)
return t
}
// Scale the image
func (t *Transform) Scale(scale float64) *Transform {
t.tx.Width.SetScale(scale)
t.tx.Height.SetScale(scale)
return t
}
// ResizeWidth resizes the image to the given width, maintaining aspect ratio
func (t *Transform) ResizeWidth(width int) *Transform {
t.tx.Width.SetInt(width)
return t
}
// ResizeHeight resizes the image to the given height, maintaining aspect ratio
func (t *Transform) ResizeHeight(height int) *Transform {
t.tx.Height.SetInt(height)
return t
}
// Resize resizes the image to the given width and height
func (t *Transform) Resize(width, height int) *Transform {
t.tx.Width.SetInt(width)
t.tx.Height.SetInt(height)
return t
}
// Format sets the image format of the input image when exporting. Defaults to JPEG
func (t *Transform) Format(format ImageType) *Transform {
t.export.Format = format
return t
}
// Quality sets the quality value for image formats that support it
func (t *Transform) Quality(quality int) *Transform {
t.export.Quality = quality
return t
}
// Compression sets the compression value for image formats that support it
func (t *Transform) Compression(compression int) *Transform {
t.export.Compression = compression
return t
}
// Lossless uses lossless compression for image formats that support both lossy and lossless e.g. webp
func (t *Transform) Lossless() *Transform {
t.export.Lossless = true
return t
}
// StripMetadata strips ICC profile and metadata from the image
func (t *Transform) StripMetadata() *Transform {
t.export.StripProfile = true
t.export.StripMetadata = true
return t
}
// BackgroundColor sets the background color of the image when a transparent
// image is flattened
func (t *Transform) BackgroundColor(color Color) *Transform {
t.export.BackgroundColor = &color
return t
}
// Apply loads the image, applies the transform, and exports it according
// to the parameters specified
func (t *Transform) Apply() ([]byte, error) {
defer ShutdownThread()
defer func() {
t.source = nil
}()
startupIfNeeded()
input, imageType, err := t.importImage()
if err != nil {
return nil, err
}
transformed, err := t.transform(input, imageType)
if err != nil {
return nil, err
}
return t.exportImage(transformed, imageType)
}
func (t *Transform) importImage() (*C.VipsImage, ImageType, error) {
if t.input.Image != nil {
return t.input.Image.image, t.input.Image.Format(), nil
}
if t.input.Reader == nil {
panic("no input source specified")
}
var err error
t.source, err = ioutil.ReadAll(t.input.Reader)
if err != nil {
return nil, ImageTypeUnknown, nil
}
return vipsLoadFromBuffer(t.source)
}
func (t *Transform) exportImage(image *C.VipsImage, imageType ImageType) ([]byte, error) {
if t.export.Format == ImageTypeUnknown {
t.export.Format = imageType
}
defer C.g_object_unref(C.gpointer(image))
buf, err := vipsExportBuffer(image, t.export)
if err != nil {
return nil, err
}
if t.export.Writer != nil {
_, err = t.export.Writer.Write(buf)
if err != nil {
return buf, err
}
}
return buf, err
}
type Blackboard struct {
*TransformParams
image *C.VipsImage
imageType ImageType
aspectRatio float64
targetWidth int
targetHeight int
targetScale float64
cropOffsetX int
cropOffsetY int
}
func NewBlackboard(image *C.VipsImage, imageType ImageType, p *TransformParams) *Blackboard {
bb := &Blackboard{
TransformParams: p,
image: image,
imageType: imageType,
}
imageWidth := int(image.Xsize)
imageHeight := int(image.Ysize)
bb.aspectRatio = ratio(imageWidth, imageHeight)
bb.cropOffsetX = p.CropOffsetX.GetRounded(imageWidth)
bb.cropOffsetY = p.CropOffsetY.GetRounded(imageHeight)
if p.Width.Value == 0 && p.Height.Value == 0 {
return bb
}
bb.targetWidth = p.Width.GetRounded(imageWidth)
bb.targetHeight = p.Height.GetRounded(imageHeight)
switch {
case bb.targetWidth > 0 && bb.targetHeight > 0:
// Nothing to do
case bb.targetWidth > 0:
bb.targetHeight = roundFloat(ratio(bb.targetWidth, imageWidth) * float64(imageHeight))
case bb.targetHeight > 0:
bb.targetWidth = roundFloat(ratio(bb.targetHeight, imageHeight) * float64(imageWidth))
}
if p.Width.Relative && p.Height.Relative {
sx, sy := p.Width.Value, p.Height.Value
if sx == 0 {
sx = sy
} else if sy == 0 {
sy = sx
}
if sx == sy {
bb.targetScale = sx
}
}
return bb
}
func (bb *Blackboard) Width() int {
return int(bb.image.Xsize)
}
func (bb *Blackboard) Height() int {
return int(bb.image.Ysize)
}
func (t *Transform) transform(image *C.VipsImage, imageType ImageType) (*C.VipsImage, error) {
bb := NewBlackboard(image, imageType, t.tx)
if err := resize(bb); err != nil {
return image, err
}
if err := postProcess(bb); err != nil {
return image, err
}
return bb.image, nil
}
func resize(bb *Blackboard) error {
var err error
kernel := bb.ReductionSampler
// Check for the simple scale down cases
if bb.targetScale != 0 {
bb.image, err = vipsResize(bb.image, bb.targetScale, bb.targetScale, kernel)
if err != nil {
return err
}
}
if bb.targetHeight == 0 && bb.targetWidth == 0 {
return nil
}
shrinkX := ratio(bb.Width(), bb.targetWidth)
shrinkY := ratio(bb.Height(), bb.targetHeight)
cropMode := bb.ResizeStrategy == ResizeStrategyCrop
stretchMode := bb.ResizeStrategy == ResizeStrategyStretch
if !stretchMode {
if shrinkX > 0 && shrinkY > 0 {
if cropMode {
shrinkX = math.Min(shrinkX, shrinkY)
} else {
shrinkX = math.Max(shrinkX, shrinkY)
}
} else {
if cropMode {
shrinkX = math.Min(shrinkX, shrinkY)
} else {
shrinkX = math.Max(shrinkX, shrinkY)
}
}
shrinkY = shrinkX
}
if shrinkX != 1 || shrinkY != 1 {
bb.image, err = vipsResize(bb.image, 1.0/shrinkX, 1.0/shrinkY, kernel)
if err != nil {
return err
}
// If stretching then we're done.
if stretchMode {
return nil
}
}
// Crop if necessary
if cropMode {
if err := maybeCrop(bb); err != nil {
return err
}
}
if err := maybeEmbed(bb); err != nil {
return err
}
return nil
}
func maybeCrop(bb *Blackboard) error {
var err error
imageW, imageH := bb.Width(), bb.Height()
if bb.targetWidth >= imageW && bb.targetHeight >= imageH {
return nil
}
width := minInt(bb.targetWidth, imageW)
height := minInt(bb.targetHeight, imageH)
left, top := 0, 0
middleX := (imageW - bb.targetWidth + 1) >> 1
middleY := (imageH - bb.targetHeight + 1) >> 1
if bb.cropOffsetX != 0 || bb.cropOffsetY != 0 {
if bb.cropOffsetX >= 0 {
left = middleX + minInt(bb.cropOffsetX, middleX)
} else {
left = middleX - maxInt(bb.cropOffsetX, middleX)
}
if bb.cropOffsetY >= 0 {
top = middleY + minInt(bb.cropOffsetY, middleY)
} else {
top = middleY - maxInt(bb.cropOffsetY, middleY)
}
} else {
switch bb.CropAnchor {
case AnchorTop:
left = middleX
case AnchorBottom:
left = middleX
top = imageH - bb.targetHeight
case AnchorRight:
left = imageW - bb.targetWidth
top = middleY
case AnchorLeft:
top = middleY
case AnchorTopRight:
left = imageW - bb.targetWidth
case AnchorTopLeft:
case AnchorBottomRight:
left = imageW - bb.targetWidth
top = imageH - bb.targetHeight
case AnchorBottomLeft:
top = imageH - bb.targetHeight
default:
left = middleX
top = middleY
}
}
left = maxInt(left, 0)
top = maxInt(top, 0)
if left+width > imageW {
width = imageW - left
bb.targetWidth = width
}
if top+height > imageH {
height = imageH - top
bb.targetHeight = height
}
bb.image, err = vipsExtractArea(bb.image, left, top, width, height)
return err
}
func maybeEmbed(bb *Blackboard) error {
var err error
imageW, imageH := bb.Width(), bb.Height()
// Now we might need to embed to match the target dimensions
if bb.targetWidth > imageW || bb.targetHeight > imageH {
var left, top int
width, height := imageW, imageH
if bb.targetWidth > imageW {
width = bb.targetWidth
left = (bb.targetWidth - imageW) >> 1
}
if bb.targetHeight > imageH {
height = bb.targetHeight
top = (bb.targetHeight - imageH) >> 1
}
bb.image, err = vipsEmbed(bb.image, left, top, width, height, bb.PadStrategy)
if err != nil {
return err
}
}
return nil
}
func postProcess(bb *Blackboard) error {
var err error
if bb.ZoomX > 0 || bb.ZoomY > 0 {
bb.image, err = vipsZoom(bb.image, bb.ZoomX, bb.ZoomY)
if err != nil {
return err
}
}
if bb.Flip != FlipNone {
var err error
switch bb.Flip {
case FlipHorizontal:
bb.image, err = vipsFlip(bb.image, DirectionHorizontal)
case FlipVertical:
bb.image, err = vipsFlip(bb.image, DirectionVertical)
case FlipBoth:
bb.image, err = vipsFlip(bb.image, DirectionHorizontal)
if err == nil {
bb.image, err = vipsFlip(bb.image, DirectionVertical)
}
}
if err != nil {
return err
}
}
if bb.Invert {
bb.image, err = vipsInvert(bb.image)
if err != nil {
return err
}
}
if bb.BlurSigma > 0 {
bb.image, err = vipsGaussianBlur(bb.image, bb.BlurSigma)
if err != nil {
return err
}
}
return nil
}
func minInt(a, b int) int {
return int(math.Min(float64(a), float64(b)))
}
func maxInt(a, b int) int {
return int(math.Max(float64(a), float64(b)))
}
func ratio(x, y int) float64 {
if x == y {
return 1
}
return float64(x) / float64(y)
}
func roundFloat(f float64) int {
if f < 0 {
return int(math.Ceil(f - 0.5))
}
return int(math.Floor(f + 0.5))
}
// LazyFile is a lazy reader or writer
// TODO(d): Move this to AF
type LazyFile struct {
name string
file *os.File
}
func LazyOpen(file string) io.Reader {
return &LazyFile{name: file}
}
func LazyCreate(file string) io.Writer {
return &LazyFile{name: file}
}
func (r *LazyFile) Read(p []byte) (n int, err error) {
if r.file == nil {
f, err := os.Open(r.name)
if err != nil {
return 0, err
}
r.file = f
}
return r.file.Read(p)
}
func (r *LazyFile) Close() error {
if r.file != nil {
r.file.Close()
r.file = nil
}
return nil
}
func (r *LazyFile) Write(p []byte) (n int, err error) {
if r.file == nil {
f, err := os.Create(r.name)
if err != nil {
return 0, err
}
r.file = f
}
return r.file.Write(p)
}
type Scalar struct {
Value float64
Relative bool
}
func (s *Scalar) SetInt(value int) {
s.Set(float64(value))
}
func (s *Scalar) Set(value float64) {
s.Value = value
s.Relative = false
}
func (s *Scalar) SetScale(f float64) {
s.Value = f
s.Relative = true
}
func (s *Scalar) Get(base int) float64 {
if s.Relative {
return s.Value * float64(base)
}
return s.Value
}
func (s *Scalar) GetRounded(base int) int {
return roundFloat(s.Get(base))
}