/
processing.go
82 lines (70 loc) · 1.96 KB
/
processing.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
package defaultprocessing
import (
"context"
"image"
"strings"
"github.com/Falokut/image_processing_service/internal/imageprocessing"
"github.com/anthonynsimon/bild/adjust"
"github.com/anthonynsimon/bild/blur"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/transform"
)
type ImageProcessing struct {
}
func (p ImageProcessing) Resize(_ context.Context, img image.Image, width, height int, resampleMethod string) image.Image {
return transform.Resize(img, width, height, resolveFiter(resampleMethod))
}
func (p ImageProcessing) Crop(_ context.Context, img image.Image, x0, y0, x1, y1 int) image.Image {
return transform.Crop(img, image.Rectangle{
Min: image.Point{X: x0, Y: y0},
Max: image.Point{X: x1, Y: y1}})
}
func (p ImageProcessing) Desaturate(_ context.Context, img image.Image) image.Image {
return effect.Grayscale(img)
}
func (p ImageProcessing) Hue(_ context.Context, img image.Image, hue int) image.Image {
return adjust.Hue(img, hue%imageprocessing.HueMaxValue)
}
type BlurMethod string
const (
Box BlurMethod = "Box"
Gaussian BlurMethod = "Gaussian"
)
func (p ImageProcessing) Blur(_ context.Context, img image.Image, radius float64, method string) image.Image {
blurMethod := resolveBlurMethod(method)
switch blurMethod {
case Gaussian:
return blur.Gaussian(img, radius)
default:
return blur.Box(img, radius)
}
}
func resolveFiter(filter string) transform.ResampleFilter {
switch filter {
case "Lanczos":
return transform.Lanczos
case "CatmullRom":
return transform.CatmullRom
case "MitchellNetravali":
return transform.MitchellNetravali
case "Linear":
return transform.Linear
case "Box":
return transform.Box
case "NearestNeighbor":
return transform.NearestNeighbor
default:
return transform.NearestNeighbor
}
}
func resolveBlurMethod(method string) BlurMethod {
method = strings.ToUpper(method)
switch method {
case "BOX":
return Box
case "GAUSSIAN":
return Gaussian
default:
return Box
}
}