forked from gosexy/canvas
/
canvas.go
662 lines (520 loc) · 17.3 KB
/
canvas.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
/*
Copyright (c) 2012 José Carlos Nieto, http://xiam.menteslibres.org/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package canvas
/*
#cgo LDFLAGS: -lMagickWand -lMagickCore
#cgo CFLAGS: -fopenmp -I/usr/local/include/ImageMagick
#include <wand/magick_wand.h>
char *MagickGetPropertyName(char **properties, size_t index) {
return properties[index];
}
*/
import "C"
import "math"
import "fmt"
import "unsafe"
import "strings"
import "strconv"
var (
STROKE_BUTT_CAP = uint(C.ButtCap)
STROKE_ROUND_CAP = uint(C.RoundCap)
STROKE_SQUARE_CAP = uint(C.SquareCap)
STROKE_MITER_JOIN = uint(C.MiterJoin)
STROKE_ROUND_JOIN = uint(C.RoundJoin)
STROKE_BEVEL_JOIN = uint(C.BevelJoin)
FILL_EVEN_ODD_RULE = uint(C.EvenOddRule)
FILL_NON_ZERO_RULE = uint(C.NonZeroRule)
RAD_TO_DEG = 180 / math.Pi
DEG_TO_RAD = math.Pi / 180
UNDEFINED_ORIENTATION = uint(C.UndefinedOrientation)
TOP_LEFT_ORIENTATION = uint(C.TopLeftOrientation)
TOP_RIGHT_ORIENTATION = uint(C.TopRightOrientation)
BOTTOM_RIGHT_ORIENTATION = uint(C.BottomRightOrientation)
BOTTOM_LEFT_ORIENTATION = uint(C.BottomLeftOrientation)
LEFT_TOP_ORIENTATION = uint(C.LeftTopOrientation)
RIGHT_TOP_ORIENTATION = uint(C.RightTopOrientation)
RIGHT_BOTTOM_ORIENTATION = uint(C.RightBottomOrientation)
LEFT_BOTTOM_ORIENTATION = uint(C.LeftBottomOrientation)
)
type Canvas struct {
wand *C.MagickWand
fg *C.PixelWand
bg *C.PixelWand
drawing *C.DrawingWand
fill *C.PixelWand
stroke *C.PixelWand
filename string
width string
height string
}
// Private: returns wand's hexadecimal color.
func getPixelHexColor(p *C.PixelWand) string {
var rgb [3]float64
rgb[0] = float64(C.PixelGetRed(p))
rgb[1] = float64(C.PixelGetGreen(p))
rgb[2] = float64(C.PixelGetBlue(p))
return fmt.Sprintf("#%02x%02x%02x", int(rgb[0]*255.0), int(rgb[1]*255.0), int(rgb[2]*255.0))
}
// Private: returns MagickTrue or MagickFalse
func magickBoolean(value bool) C.MagickBooleanType {
if value == true {
return C.MagickTrue
}
return C.MagickFalse
}
// Initializes the canvas environment.
func (cv Canvas) Init() {
C.MagickWandGenesis()
}
// Opens an image file, returns true on success.
func (cv Canvas) Open(filename string) bool {
cv.filename = filename
status := C.MagickReadImage(cv.wand, C.CString(cv.filename))
if status == C.MagickFalse {
return false
}
return true
}
// Opens an image from binary, returns true on success.
func (cv Canvas) OpenBlob(data []byte) bool {
cv.filename = ""
status := C.MagickReadImageBlob(cv.wand, unsafe.Pointer(&data[0]), C.size_t(len(data)))
if status == C.MagickFalse {
return false
}
return true
}
// Auto-orientates canvas based on its original image's EXIF metadata
func (cv Canvas) AutoOrientate() bool {
data := cv.Metadata()
orientation, err := strconv.Atoi(data["exif:Orientation"])
if err != nil {
return false
}
switch uint(orientation) {
case TOP_LEFT_ORIENTATION:
// normal
case TOP_RIGHT_ORIENTATION:
cv.Flop()
case BOTTOM_RIGHT_ORIENTATION:
cv.RotateCanvas(math.Pi)
case BOTTOM_LEFT_ORIENTATION:
cv.Flip()
case LEFT_TOP_ORIENTATION:
cv.Flip()
cv.RotateCanvas(-math.Pi / 2)
case RIGHT_TOP_ORIENTATION:
cv.RotateCanvas(-math.Pi / 2)
case RIGHT_BOTTOM_ORIENTATION:
cv.Flop()
cv.RotateCanvas(-math.Pi / 2)
case LEFT_BOTTOM_ORIENTATION:
cv.RotateCanvas(math.Pi / 2)
default:
return false
}
C.MagickSetImageOrientation(cv.wand, (C.OrientationType)(TOP_LEFT_ORIENTATION))
cv.SetMetadata("exif:Orientation", (string)(TOP_LEFT_ORIENTATION))
return true
}
// Returns all metadata keys from the currently loaded image.
func (cv Canvas) Metadata() map[string]string {
var n C.ulong
var i C.ulong
var value *C.char
var key *C.char
data := make(map[string]string)
properties := C.MagickGetImageProperties(cv.wand, C.CString("*"), &n)
for i = 0; i < n; i++ {
key = C.MagickGetPropertyName(properties, C.size_t(i))
value = C.MagickGetImageProperty(cv.wand, key)
data[strings.Trim(C.GoString(key), " ")] = strings.Trim(C.GoString(value), " ")
C.MagickRelinquishMemory(unsafe.Pointer(value))
C.MagickRelinquishMemory(unsafe.Pointer(key))
}
return data
}
func (cv Canvas) Format() string {
format := C.MagickGetImageFormat(cv.wand)
return strings.Trim(C.GoString(format), " ")
}
// Associates a metadata key with its value.
func (cv Canvas) SetMetadata(key string, value string) {
C.MagickSetImageProperty(cv.wand, C.CString(key), C.CString(value))
}
// Creates a horizontal mirror image by reflecting the pixels around the central y-axis.
func (cv Canvas) Flop() bool {
status := C.MagickFlopImage(cv.wand)
if status == C.MagickFalse {
return false
}
return true
}
// Creates a vertical mirror image by reflecting the pixels around the central x-axis.
func (cv Canvas) Flip() bool {
status := C.MagickFlipImage(cv.wand)
if status == C.MagickFalse {
return false
}
return true
}
// Creates a centered thumbnail of the canvas.
func (cv Canvas) Thumbnail(width uint, height uint) bool {
var ratio float64
// Normalizing image.
ratio = math.Min(float64(cv.Width())/float64(width), float64(cv.Height())/float64(height))
if ratio < 1.0 {
// Origin image is smaller than the thumbnail image.
max := uint(math.Max(float64(width), float64(height)))
// Empty replacement buffer with transparent background.
replacement := New()
replacement.SetBackgroundColor("none")
replacement.Blank(max, max)
// Putting original image in the center of the replacement canvas.
replacement.AppendCanvas(cv, int(int(width-cv.Width())/2), int(int(height-cv.Height())/2))
// Replacing wand
C.DestroyMagickWand(cv.wand)
cv.wand = C.CloneMagickWand(replacement.wand)
} else {
// Is bigger, just resizing.
cv.Resize(uint(float64(cv.Width())/ratio), uint(float64(cv.Height())/ratio))
}
// Now we have an image that we can use to crop the thumbnail from.
cv.Crop(int(int(cv.Width()-width)/2), int(int(cv.Height()-height)/2), width, height)
return true
}
// Puts a canvas on top of the current one.
func (cv Canvas) AppendCanvas(source Canvas, x int, y int) bool {
status := C.MagickCompositeImage(cv.wand, source.wand, C.OverCompositeOp, C.long(x), C.long(y))
if status == C.MagickFalse {
return false
}
return true
}
// Rotates the whole canvas.
func (cv Canvas) RotateCanvas(rad float64) {
C.MagickRotateImage(cv.wand, cv.bg, C.double(RAD_TO_DEG*rad))
}
// Returns canvas' width.
func (cv Canvas) Width() uint {
return uint(C.MagickGetImageWidth(cv.wand))
}
// Returns canvas' height.
func (cv Canvas) Height() uint {
return uint(C.MagickGetImageHeight(cv.wand))
}
// Writes canvas to a file, returns true on success.
func (cv Canvas) Write(filename string) bool {
cv.Update()
status := C.MagickWriteImage(cv.wand, C.CString(filename))
if status == C.MagickFalse {
return false
}
return true
}
// Writes canvas to a file, returns true on success.
func (cv Canvas) GetImageBlob() []byte {
cv.Update()
var length C.size_t
data := C.MagickGetImageBlob(cv.wand, &length)
bin := C.GoBytes(unsafe.Pointer(data), C.int(length))
C.MagickRelinquishMemory(unsafe.Pointer(data))
return bin
}
// Changes the size of the canvas, returns true on success.
func (cv Canvas) Resize(width uint, height uint) bool {
status := C.MagickResizeImage(cv.wand, C.ulong(width), C.ulong(height), C.GaussianFilter, C.double(1.0))
if status == C.MagickFalse {
return false
}
return true
}
// Adaptively changes the size of the canvas, returns true on success.
func (cv Canvas) AdaptiveResize(width uint, height uint) bool {
status := C.MagickAdaptiveResizeImage(cv.wand, C.ulong(width), C.ulong(height))
if status == C.MagickFalse {
return false
}
return true
}
// Changes the compression quality of the canvas. Ranges from 1 (lowest) to 100 (highest).
func (cv Canvas) SetQuality(quality uint) bool {
status := C.MagickSetImageCompressionQuality(cv.wand, C.ulong(quality))
if status == C.MagickFalse {
return false
}
return true
}
// Returns the compression quality of the canvas. Ranges from 1 (lowest) to 100 (highest).
func (cv Canvas) Quality() uint {
return uint(C.MagickGetImageCompressionQuality(cv.wand))
}
/*
// Sets canvas's foreground color.
func (cv Canvas) SetColor(color string) (bool) {
status := C.PixelSetColor(cv.fg, C.CString(color))
if status == C.MagickFalse {
return false
}
return true
}
*/
// Sets canvas' background color.
func (cv Canvas) SetBackgroundColor(color string) bool {
C.PixelSetColor(cv.bg, C.CString(color))
status := C.MagickSetImageBackgroundColor(cv.wand, cv.bg)
if status == C.MagickFalse {
return false
}
return true
}
// Returns canvas' background color.
func (cv Canvas) BackgroundColor() string {
return getPixelHexColor(cv.bg)
}
// Sets antialiasing setting for the current drawing stroke.
func (cv Canvas) SetStrokeAntialias(value bool) {
C.DrawSetStrokeAntialias(cv.drawing, magickBoolean(value))
}
// Returns antialiasing setting for the current drawing stroke.
func (cv Canvas) StrokeAntialias() bool {
value := C.DrawGetStrokeAntialias(cv.drawing)
if value == C.MagickTrue {
return true
}
return false
}
// Sets the width of the stroke on the current drawing surface.
func (cv Canvas) SetStrokeWidth(value float64) {
C.DrawSetStrokeWidth(cv.drawing, C.double(value))
}
// Returns the width of the stroke on the current drawing surface.
func (cv Canvas) StrokeWidth() float64 {
return float64(C.DrawGetStrokeWidth(cv.drawing))
}
// Sets the opacity of the stroke on the current drawing surface.
func (cv Canvas) SetStrokeOpacity(value float64) {
C.DrawSetStrokeOpacity(cv.drawing, C.double(value))
}
// Returns the opacity of the stroke on the current drawing surface.
func (cv Canvas) StrokeOpacity() float64 {
return float64(C.DrawGetStrokeOpacity(cv.drawing))
}
// Sets the type of the line cap on the current drawing surface.
func (cv Canvas) SetStrokeLineCap(value uint) {
C.DrawSetStrokeLineCap(cv.drawing, C.LineCap(value))
}
// Returns the type of the line cap on the current drawing surface.
func (cv Canvas) StrokeLineCap() uint {
return uint(C.DrawGetStrokeLineCap(cv.drawing))
}
// Sets the type of the line join on the current drawing surface.
func (cv Canvas) SetStrokeLineJoin(value uint) {
C.DrawSetStrokeLineJoin(cv.drawing, C.LineJoin(value))
}
// Returns the type of the line join on the current drawing surface.
func (cv Canvas) StrokeLineJoin() uint {
return uint(C.DrawGetStrokeLineJoin(cv.drawing))
}
/*
func (cv Canvas) SetFillRule(value int) {
C.DrawSetFillRule(cv.drawing, C.FillRule(value))
}
*/
// Sets the fill color for enclosed areas on the current drawing surface.
func (cv Canvas) SetFillColor(color string) {
C.PixelSetColor(cv.fill, C.CString(color))
C.DrawSetFillColor(cv.drawing, cv.fill)
}
// Returns the fill color for enclosed areas on the current drawing surface.
func (cv Canvas) FillColor() string {
return getPixelHexColor(cv.fill)
}
// Sets the stroke color on the current drawing surface.
func (cv Canvas) SetStrokeColor(color string) {
C.PixelSetColor(cv.stroke, C.CString(color))
C.DrawSetStrokeColor(cv.drawing, cv.stroke)
}
// Returns the stroke color on the current drawing surface.
func (cv Canvas) StrokeColor() string {
return getPixelHexColor(cv.stroke)
}
// Draws a circle over the current drawing surface.
func (cv Canvas) Circle(radius float64) {
C.DrawCircle(cv.drawing, C.double(0), C.double(0), C.double(radius), C.double(0))
}
// Draws a rectangle over the current drawing surface.
func (cv Canvas) Rectangle(x float64, y float64) {
C.DrawRectangle(cv.drawing, C.double(0), C.double(0), C.double(x), C.double(y))
}
// Moves the current coordinate system origin to the specified coordinate.
func (cv Canvas) Translate(x float64, y float64) {
C.DrawTranslate(cv.drawing, C.double(x), C.double(y))
}
// Applies a scaling factor to the units of the current coordinate system.
func (cv Canvas) Scale(x float64, y float64) {
C.DrawScale(cv.drawing, C.double(x), C.double(y))
}
// Draws a line starting on the current coordinate system origin and ending on the specified coordinates.
func (cv Canvas) Line(x float64, y float64) {
C.DrawLine(cv.drawing, C.double(0), C.double(0), C.double(x), C.double(y))
}
/*
func (cv Canvas) Skew(x float64, y float64) {
C.DrawSkewX(cv.drawing, C.double(x))
C.DrawSkewY(cv.drawing, C.double(y))
}
*/
// Applies a rotation of a given angle (in radians) on the current coordinate system.
func (cv Canvas) Rotate(rad float64) {
deg := RAD_TO_DEG * rad
C.DrawRotate(cv.drawing, C.double(deg))
}
// Draws an ellipse centered at the current coordinate system's origin.
func (cv Canvas) Ellipse(a float64, b float64) {
C.DrawEllipse(cv.drawing, C.double(0), C.double(0), C.double(a), C.double(b), 0, 360)
}
// Clones the current drawing surface and stores it in a stack.
func (cv Canvas) PushDrawing() bool {
status := C.PushDrawingWand(cv.drawing)
if status == C.MagickFalse {
return false
}
return true
}
// Destroys the current drawing surface and returns the latest surface that was pushed to the stack.
func (cv Canvas) PopDrawing() bool {
status := C.PopDrawingWand(cv.drawing)
if status == C.MagickFalse {
return false
}
return true
}
// Copies a drawing surface to the canvas.
func (cv Canvas) Update() {
C.MagickDrawImage(cv.wand, cv.drawing)
}
// Destroys canvas.
func (cv Canvas) Destroy() {
if cv.wand != nil {
C.DestroyMagickWand(cv.wand)
}
C.MagickWandTerminus()
}
// Creates an empty canvas of the given dimensions.
func (cv Canvas) Blank(width uint, height uint) bool {
status := C.MagickNewImage(cv.wand, C.ulong(width), C.ulong(height), cv.bg)
if status == C.MagickFalse {
return false
}
return true
}
// Convolves the canvas with a Gaussian function given its standard deviation.
func (cv Canvas) Blur(sigma float64) bool {
status := C.MagickBlurImage(cv.wand, C.double(0), C.double(sigma))
if status == C.MagickFalse {
return false
}
return true
}
// Adaptively blurs the image by blurring less intensely near the edges and more intensely far from edges.
func (cv Canvas) AdaptiveBlur(sigma float64) bool {
status := C.MagickAdaptiveBlurImage(cv.wand, C.double(0), C.double(sigma))
if status == C.MagickFalse {
return false
}
return true
}
// Adds random noise to the canvas.
func (cv Canvas) AddNoise() bool {
status := C.MagickAddNoiseImage(cv.wand, C.GaussianNoise)
if status == C.MagickFalse {
return false
}
return true
}
// Removes a region of a canvas and collapses the canvas to occupy the removed portion.
func (cv Canvas) Chop(x int, y int, width uint, height uint) bool {
status := C.MagickChopImage(cv.wand, C.ulong(width), C.ulong(height), C.long(x), C.long(y))
if status == C.MagickFalse {
return false
}
return true
}
// Extracts a region from the canvas.
func (cv Canvas) Crop(x int, y int, width uint, height uint) bool {
status := C.MagickCropImage(cv.wand, C.ulong(width), C.ulong(height), C.long(x), C.long(y))
if status == C.MagickFalse {
return false
}
return true
}
// Adjusts the canvas's brightness given a factor (-1.0 thru 1.0)
func (cv Canvas) SetBrightness(factor float64) bool {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
status := C.MagickModulateImage(cv.wand, C.double(100+factor*100.0), C.double(100), C.double(100))
if status == C.MagickFalse {
return false
}
return true
}
// Adjusts the canvas's saturation given a factor (-1.0 thru 1.0)
func (cv Canvas) SetSaturation(factor float64) bool {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
status := C.MagickModulateImage(cv.wand, C.double(100), C.double(100+factor*100.0), C.double(100))
if status == C.MagickFalse {
return false
}
return true
}
// Adjusts the canvas's hue given a factor (-1.0 thru 1.0)
func (cv Canvas) SetHue(factor float64) bool {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
status := C.MagickModulateImage(cv.wand, C.double(100), C.double(100), C.double(100+factor*100.0))
if status == C.MagickFalse {
return false
}
return true
}
// Returns a new canvas object.
func New() *Canvas {
cv := &Canvas{}
cv.Init()
cv.wand = C.NewMagickWand()
cv.fg = C.NewPixelWand()
cv.bg = C.NewPixelWand()
cv.fill = C.NewPixelWand()
cv.stroke = C.NewPixelWand()
cv.drawing = C.NewDrawingWand()
//cv.SetColor("#ffffff")
cv.SetBackgroundColor("none")
cv.SetStrokeColor("#ffffff")
cv.SetStrokeAntialias(true)
cv.SetStrokeWidth(1.0)
cv.SetStrokeOpacity(1.0)
cv.SetStrokeLineCap(STROKE_ROUND_CAP)
cv.SetStrokeLineJoin(STROKE_ROUND_JOIN)
//cv.SetFillRule(FILL_EVEN_ODD_RULE)
cv.SetFillColor("#888888")
return cv
}