-
Notifications
You must be signed in to change notification settings - Fork 181
/
main.go
226 lines (177 loc) · 5.24 KB
/
main.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
// Port of http://members.shaw.ca/el.supremo/MagickWand/3dlogo.htm to Go
package main
import (
"os"
"gopkg.in/gographics/imagick.v3/imagick"
)
func main() {
imagick.Initialize()
defer imagick.Terminate()
mw := imagick.NewMagickWand()
pw := imagick.NewPixelWand()
dw := imagick.NewDrawingWand()
if err := mw.SetSize(170, 100); err != nil {
panic(err)
}
if err := mw.ReadImage("xc:black"); err != nil {
panic(err)
}
pw.SetColor("white")
dw.SetFillColor(pw)
dw.Circle(50, 50, 13, 50)
dw.Circle(120, 50, 157, 50)
dw.Rectangle(50, 13, 120, 87)
pw.SetColor("black")
dw.SetFillColor(pw)
dw.Circle(50, 50, 25, 50)
dw.Circle(120, 50, 145, 50)
dw.Rectangle(50, 25, 120, 75)
pw.SetColor("white")
dw.SetFillColor(pw)
dw.Circle(60, 50, 40, 50)
dw.Circle(110, 50, 130, 50)
dw.Rectangle(60, 30, 110, 70)
// Now we draw the Drawing wand on to the Magick Wand
if err := mw.DrawImage(dw); err != nil {
panic(err)
}
if err := mw.GaussianBlurImage(1, 1); err != nil {
panic(err)
}
// Turn the matte of == +matte
if err := mw.SetImageMatte(false); err != nil {
panic(err)
}
if err := mw.WriteImage("logo_mask.png"); err != nil {
panic(err)
}
mw.Destroy()
dw.Destroy()
pw.Destroy()
mw = imagick.NewMagickWand()
pw = imagick.NewPixelWand()
dw = imagick.NewDrawingWand()
mwc := imagick.NewMagickWand()
mw.ReadImage("logo_mask.png")
pw.SetColor("red")
dw.SetFillColor(pw)
dw.Color(0, 0, imagick.PAINT_METHOD_RESET)
mw.DrawImage(dw)
mwc.ReadImage("logo_mask.png")
mwc.SetImageMatte(false)
mw.CompositeImage(mwc, imagick.COMPOSITE_OP_COPY_ALPHA, true, 0, 0)
// Annotate gets all the font information from the drawingwand
// but draws the text on the magickwand
// Get the first available "*Sans*" font
fonts := mw.QueryFonts("*Sans*")
dw.SetFont(fonts[0])
dw.SetFontSize(36)
pw.SetColor("white")
dw.SetFillColor(pw)
pw.SetColor("black")
dw.SetStrokeColor(pw)
dw.SetGravity(imagick.GRAVITY_CENTER)
mw.AnnotateImage(dw, 0, 0, 0, "Ant")
mw.WriteImage("logo_ant.png")
mwc.Destroy()
mw.Destroy()
mw = imagick.NewMagickWand()
if err := mw.ReadImage("logo_ant.png"); err != nil {
panic(err)
}
mwf, err := mw.FxImage("A")
if err != nil {
panic(err)
}
//mw.SetImageMatte(false)
// +matte is the same as -alpha off
mwf.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
mwf.BlurImage(0, 6)
mwf.ShadeImage(true, 110, 30)
mwf.NormalizeImage()
// ant.png -compose Overlay -composite
mwc = imagick.NewMagickWand()
mwc.ReadImage("logo_ant.png")
mwf.CompositeImage(mwc, imagick.COMPOSITE_OP_OVERLAY, true, 0, 0)
mwc.Destroy()
// ant.png -matte -compose Dst_In -composite
mwc = imagick.NewMagickWand()
mwc.ReadImage("logo_ant.png")
// -matte is the same as -alpha on
// I don't understand why the -matte in the command line
// does NOT operate on the image just read in (logo_ant.png in mwc)
// but on the image before it in the list
// It would appear that the -matte affects each wand currently in the
// command list because applying it to both wands gives the same result
mwf.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_SET)
mwf.CompositeImage(mwc, imagick.COMPOSITE_OP_DST_IN, true, 0, 0)
mwf.WriteImage("logo_ant_3D.png")
mw.Destroy()
mwc.Destroy()
mwf.Destroy()
/* Now for the shadow
convert ant_3D.png \( +clone -background navy -shadow 80x4+6+6 \) +swap \
-background none -layers merge +repage ant_3D_shadowed.png
*/
mw = imagick.NewMagickWand()
mw.ReadImage("logo_ant_3D.png")
mwc = mw.Clone()
pw.SetColor("navy")
mwc.SetImageBackgroundColor(pw)
mwc.ShadowImage(80, 4, 6, 6)
// at this point
// mw = ant_3D.png
// mwc = +clone -background navy -shadow 80x4+6+6
// To do the +swap I create a new blank MagickWand and then
// put mwc and mw into it. ImageMagick probably doesn't do it
// this way but it works here and that's good enough for me!
mwf = imagick.NewMagickWand()
mwf.AddImage(mwc)
mwf.AddImage(mw)
mwc.Destroy()
pw.SetColor("none")
mwf.SetImageBackgroundColor(pw)
mwc = mwf.MergeImageLayers(imagick.IMAGE_LAYER_MERGE)
mwc.WriteImage("logo_shadow_3D.png")
mw.Destroy()
mwc.Destroy()
mwf.Destroy()
/*
and now for the fancy background
convert ant_3D_shadowed.png \
\( +clone +repage +matte -fx 'rand()' -shade 120x30 \
-fill grey70 -colorize 60 \
-fill lavender -tint 100 \) -insert 0 \
-flatten ant_3D_bg.jpg
*/
mw = imagick.NewMagickWand()
mw.ReadImage("logo_shadow_3D.png")
mwc = mw.Clone()
// +repage
mwc.ResetImagePage("")
// +matte is the same as -alpha off
mwc.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE)
mwf, err = mwc.FxImage("rand()")
if err != nil {
panic(err)
}
mwf.ShadeImage(true, 120, 30)
pw.SetColor("grey70")
// It seems that this must be a separate pixelwand for Colorize to work!
pwo := imagick.NewPixelWand()
// AHA .. this is how to do a 60% colorize
pwo.SetColor("rgb(60%,60%,60%)")
mwf.ColorizeImage(pw, pwo)
pw.SetColor("lavender")
// and this is a 100% tint
pwo.SetColor("rgb(100%,100%,100%)")
mwf.TintImage(pw, pwo)
mwc.Destroy()
mwc = imagick.NewMagickWand()
mwc.AddImage(mwf)
mwc.AddImage(mw)
mwf = mwc.MergeImageLayers(imagick.IMAGE_LAYER_FLATTEN)
if err := mwf.DisplayImage(os.Getenv("DISPLAY")); err != nil {
panic(err)
}
}