/
waaw.go
135 lines (108 loc) · 3.26 KB
/
waaw.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
package bot
import (
"fmt"
"gopkg.in/gographics/imagick.v3/imagick"
)
type mirrorDirection string
const (
mirrorDirectionVertical mirrorDirection = "vertical"
mirrorDirectionHorizontal mirrorDirection = "horizontal"
)
func mirrorImage(wand *imagick.MagickWand, direction mirrorDirection, flipped bool) ([]*imagick.MagickWand, error) {
var desiredGravity imagick.GravityType
if direction == mirrorDirectionHorizontal {
if flipped {
desiredGravity = imagick.GRAVITY_EAST
} else {
desiredGravity = imagick.GRAVITY_WEST
}
} else {
if flipped {
desiredGravity = imagick.GRAVITY_SOUTH
} else {
desiredGravity = imagick.GRAVITY_NORTH
}
}
err := wand.SetImageGravity(desiredGravity)
if err != nil {
return nil, fmt.Errorf("error setting gravity: %w", err)
}
var half *imagick.MagickWand
var xOffset, yOffset int
if direction == mirrorDirectionHorizontal {
half = wand.Clone()
err = half.CropImageToTiles("50%x100%")
if err != nil {
return nil, fmt.Errorf("error cropping image half: %w", err)
}
err = half.FlopImage()
if err != nil {
return nil, fmt.Errorf("error flipping image: %w", err)
}
if flipped {
xOffset = 0
yOffset = 0
} else {
xOffset = int(half.GetImageWidth())
yOffset = 0
}
} else {
half = wand.Clone()
err = half.CropImageToTiles("100%x50%")
if err != nil {
return nil, fmt.Errorf("error cropping image half: %w", err)
}
err = half.FlipImage()
if err != nil {
return nil, fmt.Errorf("error flipping image: %w", err)
}
if flipped {
xOffset = 0
yOffset = 0
} else {
xOffset = 0
yOffset = int(half.GetImageHeight())
}
}
err = wand.CompositeImage(half, imagick.COMPOSITE_OP_ATOP, true, xOffset, yOffset)
if err != nil {
return nil, fmt.Errorf("error compositing image: %w", err)
}
return []*imagick.MagickWand{wand}, nil
}
type WaawArgs struct {
ImageURL string `default:"" description:"URL to the image to process. Leave blank to automatically attempt to find an image."`
}
func (args WaawArgs) GetImageURL() string {
return args.ImageURL
}
func Waaw(wand *imagick.MagickWand, args WaawArgs) ([]*imagick.MagickWand, error) {
return mirrorImage(wand, mirrorDirectionHorizontal, true)
}
type HaahArgs struct {
ImageURL string `default:"" description:"URL to the image to process. Leave blank to automatically attempt to find an image."`
}
func (args HaahArgs) GetImageURL() string {
return args.ImageURL
}
func Haah(wand *imagick.MagickWand, args HaahArgs) ([]*imagick.MagickWand, error) {
return mirrorImage(wand, mirrorDirectionHorizontal, false)
}
type WoowArgs struct {
ImageURL string `default:"" description:"URL to the image to process. Leave blank to automatically attempt to find an image."`
}
func (args WoowArgs) GetImageURL() string {
return args.ImageURL
}
func Woow(wand *imagick.MagickWand, args WoowArgs) ([]*imagick.MagickWand, error) {
return mirrorImage(wand, mirrorDirectionVertical, false)
}
type HoohArgs struct {
ImageURL string `default:"" description:"URL to the image to process. Leave blank to automatically attempt to find an image."`
}
func (args HoohArgs) GetImageURL() string {
return args.ImageURL
}
func Hooh(wand *imagick.MagickWand, args HoohArgs) ([]*imagick.MagickWand, error) {
return mirrorImage(wand, mirrorDirectionVertical, true)
}