forked from gosexy/canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.go
1051 lines (776 loc) · 27.5 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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 pkg-config: MagickWand MagickCore
#cgo CFLAGS: -fopenmp -I./_include
#include <wand/MagickWand.h>
char *MagickGetPropertyName(char **properties, size_t index) {
return properties[index];
}
*/
import "C"
import (
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
"unsafe"
)
// Holds a Canvas object
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
quantumRange uint
text *TextProperties
}
func init() {
C.MagickWandGenesis()
}
// 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
}
// Opens an image file, returns nil on success, error otherwise.
func (self *Canvas) Open(filename string) error {
stat, err := os.Stat(filename)
if err != nil {
return err
}
if stat.IsDir() == true {
return fmt.Errorf(`Could not open file "%s": it's a directory!`, filename)
}
cfilename := C.CString(filename)
status := C.MagickReadImage(self.wand, cfilename)
C.free(unsafe.Pointer(cfilename))
if status == C.MagickFalse {
return fmt.Errorf(`Could not open image "%s": %s`, filename, self.Error())
}
self.filename = filename
return nil
}
func (self *Canvas) SetOption(key, value string) error {
ckey := C.CString(key)
cvalue := C.CString(value)
defer C.free(unsafe.Pointer(ckey))
defer C.free(unsafe.Pointer(cvalue))
if C.MagickSetOption(self.wand, ckey, cvalue) == C.MagickFalse {
return fmt.Errorf(`Could not set option "%s" to "%s": %s`, ckey, cvalue, self.Error())
}
return nil
}
func (self *Canvas) SetCaption(content string) error {
ccontent := C.CString("caption:" + content)
defer C.free(unsafe.Pointer(ccontent))
if C.MagickReadImage(self.wand, ccontent) == C.MagickFalse {
return fmt.Errorf(`Could not open image "%s": %s`, content, self.Error())
}
C.MagickDrawImage(self.wand, self.drawing)
return nil
}
func (self *Canvas) DrawAnnotation(content string, width, height uint) error {
ccontent := C.CString("caption:" + content)
defer C.free(unsafe.Pointer(ccontent))
C.DrawAnnotation(self.drawing, 20, 20, (*C.uchar)(unsafe.Pointer(ccontent)))
if C.MagickDrawImage(self.wand, self.drawing) == C.MagickFalse {
return fmt.Errorf(`Could not draw annotation: %s`, self.Error())
}
return nil
}
// Reads an image or image sequence from a blob.
func (self *Canvas) OpenBlob(blob []byte, length uint) error {
status := C.MagickReadImageBlob(self.wand, unsafe.Pointer(&blob[0]), C.size_t(length))
if status == C.MagickFalse {
return fmt.Errorf(`Could not open image from blob: %s`, self.Error())
}
return nil
}
// Auto-orientates canvas based on its original image's EXIF metadata
func (self *Canvas) AutoOrientate() error {
data := self.Metadata()
orientation, err := strconv.Atoi(data["exif:Orientation"])
if err != nil {
return err
}
switch uint(orientation) {
case TOP_LEFT_ORIENTATION:
// normal
case TOP_RIGHT_ORIENTATION:
self.Flop()
case BOTTOM_RIGHT_ORIENTATION:
self.RotateCanvas(math.Pi)
case BOTTOM_LEFT_ORIENTATION:
self.Flip()
case LEFT_TOP_ORIENTATION:
self.Flip()
self.RotateCanvas(-math.Pi / 2)
case RIGHT_TOP_ORIENTATION:
self.RotateCanvas(math.Pi / 2)
case RIGHT_BOTTOM_ORIENTATION:
self.Flop()
self.RotateCanvas(-math.Pi / 2)
case LEFT_BOTTOM_ORIENTATION:
self.RotateCanvas(-math.Pi / 2)
default:
return errors.New("No orientation data found in file.")
}
success := C.MagickSetImageOrientation(self.wand, (C.OrientationType)(TOP_LEFT_ORIENTATION))
if success == C.MagickFalse {
return fmt.Errorf("Could not orientate photo: %s", self.Error())
}
self.SetMetadata("exif:Orientation", (string)(TOP_LEFT_ORIENTATION))
return nil
}
// Returns all metadata keys from the currently loaded image.
func (self *Canvas) Metadata() map[string]string {
var n C.size_t
var i C.size_t
var value *C.char
var key *C.char
data := make(map[string]string)
cplist := C.CString("*")
properties := C.MagickGetImageProperties(self.wand, cplist, &n)
C.free(unsafe.Pointer(cplist))
for i = 0; i < n; i++ {
key = C.MagickGetPropertyName(properties, C.size_t(i))
value = C.MagickGetImageProperty(self.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
}
// Returns the latest error reported by the MagickWand API.
func (self *Canvas) Error() error {
var t C.ExceptionType
ptr := C.MagickGetException(self.wand, &t)
message := C.GoString(ptr)
C.MagickClearException(self.wand)
C.MagickRelinquishMemory(unsafe.Pointer(ptr))
return errors.New(message)
}
// Associates a metadata key with its value.
func (self *Canvas) SetMetadata(key string, value string) error {
ckey := C.CString(key)
cval := C.CString(value)
success := C.MagickSetImageProperty(self.wand, ckey, cval)
C.free(unsafe.Pointer(ckey))
C.free(unsafe.Pointer(cval))
if success == C.MagickFalse {
return fmt.Errorf("Could not set metadata: %s", self.Error())
}
return nil
}
// Creates a horizontal mirror image by reflecting the pixels around the central y-axis.
func (self *Canvas) Flop() error {
success := C.MagickFlopImage(self.wand)
if success == C.MagickFalse {
return fmt.Errorf("Could not flop image: %s", self.Error())
}
return nil
}
// Creates a vertical mirror image by reflecting the pixels around the central x-axis.
func (self *Canvas) Flip() error {
success := C.MagickFlipImage(self.wand)
if success == C.MagickFalse {
return fmt.Errorf("Could not flop image: %s", self.Error())
}
return nil
}
// Adjusts the contrast of an image with a non-linear sigmoidal contrast algorithm. Increase the contrast of the image using a sigmoidal transfer function without saturating highlights or shadows. Contrast indicates how much to increase the contrast (0 is none; 3 is typical; 20 is pushing it); mid-point indicates where midtones fall in the resultant image (0 is white; 50 is middle-gray; 100 is black). Set sharpen to true to increase the image contrast otherwise the contrast is reduced.
func (self *Canvas) SigmoidalContrast(sharpen bool, alpha float64, beta float64) error {
status := C.MagickSigmoidalContrastImage(self.wand, magickBoolean(sharpen), C.double(alpha), C.double(beta))
if status == C.MagickFalse {
return fmt.Errorf("Could not contrast image: %s", self.Error())
}
return nil
}
// Enhances the intensity differences between the lighter and darker elements of the image. Set sharpen to a value other than 0 to increase the image contrast otherwise the contrast is reduced.
func (self *Canvas) Contrast(sharpen bool) error {
status := C.MagickContrastImage(self.wand, magickBoolean(sharpen))
if status == C.MagickFalse {
return fmt.Errorf("Could not contrast image: %s", self.Error())
}
return nil
}
// Clones an image to another canvas
func (self *Canvas) Clone() *Canvas {
clone := New()
clone.SetBackgroundColor("none")
clone.Blank(self.Width(), self.Height())
clone.AppendCanvas(self, 0, 0)
return clone
}
// Converts the current image into a thumbnail of the specified
// width and height preserving ratio. It uses Crop() to clip the
// image to the specified area.
//
// If width or height are bigger than the current image, a centered
// thumbnail will be produced.
//
// Is width and height are smaller than the current image, the image
// will be resized and cropped, if needed.
func (self *Canvas) Thumbnail(width uint, height uint) error {
var ratio float64
// Normalizing image.
ratio = math.Min(float64(self.Width())/float64(width), float64(self.Height())/float64(height))
return self.thumbnail(width, height, ratio)
}
// Creates a thumbnail that fits within the given dimensions
func (self *Canvas) Fit(width uint, height uint) error {
var ratio float64
// Normalizing image.
ratio = math.Max(float64(self.Width())/float64(width), float64(self.Height())/float64(height))
return self.thumbnail(width, height, ratio)
}
func (self *Canvas) thumbnail(width uint, height uint, ratio float64) error {
if ratio < 1.0 {
// Origin image is smaller than the thumbnail image.
// Empty replacement buffer with transparent background.
replacement := New()
replacement.SetBackgroundColor("none")
replacement.Blank(width, height)
// Putting original image in the center of the replacement canvas.
replacement.AppendCanvas(self, int(int(width-self.Width())/2), int(int(height-self.Height())/2))
// Replacing wand
C.DestroyMagickWand(self.wand)
self.wand = C.CloneMagickWand(replacement.wand)
} else {
// Is bigger, just resizing.
err := self.Resize(uint(float64(self.Width())/ratio), uint(float64(self.Height())/ratio))
if err != nil {
return err
}
}
// Now we have an image that we can use to crop the thumbnail from.
err := self.Crop(int(int(self.Width()-width)/2), int(int(self.Height()-height)/2), width, height)
if err != nil {
return err
}
return nil
}
// Puts a canvas on top of the current one.
func (self *Canvas) AppendCanvas(source *Canvas, x int, y int) error {
success := C.MagickCompositeImage(self.wand, source.wand, C.SrcOverCompositeOp, C.ssize_t(x), C.ssize_t(y))
if success == C.MagickFalse {
return fmt.Errorf("Could not append image: %s", self.Error())
}
return nil
}
// Rotates the whole canvas.
func (self *Canvas) RotateCanvas(rad float64) error {
success := C.MagickRotateImage(self.wand, self.bg, C.double(RAD_TO_DEG*rad))
if success == C.MagickFalse {
return fmt.Errorf("Could not rotate image: %s", self.Error())
}
return nil
}
// Returns canvas' width.
func (self *Canvas) Width() uint {
return uint(C.MagickGetImageWidth(self.wand))
}
// Returns canvas' height.
func (self *Canvas) Height() uint {
return uint(C.MagickGetImageHeight(self.wand))
}
// Writes canvas to a file, returns true on success.
func (self *Canvas) Write(filename string) error {
err := self.Update()
if err != nil {
return err
}
cfilename := C.CString(filename)
success := C.MagickWriteImage(self.wand, cfilename)
C.free(unsafe.Pointer(cfilename))
if success == C.MagickFalse {
return fmt.Errorf("Could not write: %s", self.Error())
}
return nil
}
// Changes the size of the canvas, returns true on success.
func (self *Canvas) Resize(width uint, height uint) error {
success := C.MagickResizeImage(self.wand, C.size_t(width), C.size_t(height), C.GaussianFilter, C.double(1.0))
if success == C.MagickFalse {
return fmt.Errorf("Could not resize: %s", self.Error())
}
return nil
}
// Changes the size of the canvas using specified filter and blur, returns true on success.
func (self *Canvas) ResizeWithFilter(width uint, height uint, filter uint, blur float32) error {
if width == 0 && height == 0 {
return errors.New("Please specify at least one of dimensions")
}
if width == 0 || height == 0 {
origHeight := uint(C.MagickGetImageHeight(self.wand))
origWidth := uint(C.MagickGetImageWidth(self.wand))
if width == 0 {
ratio := float32(origHeight) / float32(height)
width = uint(float32(origWidth) / ratio)
}
if height == 0 {
ratio := float32(origWidth) / float32(width)
height = uint(float32(origHeight) / ratio)
}
}
success := C.MagickResizeImage(self.wand, C.size_t(width), C.size_t(height), C.FilterTypes(filter), C.double(blur))
if success == C.MagickFalse {
return fmt.Errorf("Could not resize: %s", self.Error())
}
return nil
}
// Sharpens an image. We convolve the image with a Gaussian operator of the
// given radius and standard deviation (sigma). For reasonable results, the
// radius should be larger than sigma.
// Use a radius of 0 and selects a suitable radius for you.
// You can pass 0 as channel number - to use default channels
func (self *Canvas) SharpenImage(radius float32, sigma float32, channel int) error {
if channel == 0 {
channel = C.DefaultChannels
}
success := C.MagickSharpenImageChannel(self.wand, C.ChannelType(channel), C.double(radius), C.double(sigma))
if success == C.MagickFalse {
return fmt.Errorf("Could not resize: %s", self.Error())
}
return nil
}
func (self *Canvas) GetImageBlob() ([]byte, error) {
return self.Blob()
}
func (self *Canvas) Blob() ([]byte, error) {
var size C.size_t = 0
p := unsafe.Pointer(C.MagickGetImageBlob(self.wand, &size))
if size == 0 {
return nil, errors.New("Could not get image blob.")
}
blob := C.GoBytes(p, C.int(size))
C.MagickRelinquishMemory(p)
return blob, nil
}
// Adaptively changes the size of the canvas, returns true on success.
func (self *Canvas) AdaptiveResize(width uint, height uint) error {
success := C.MagickAdaptiveResizeImage(self.wand, C.size_t(width), C.size_t(height))
if success == C.MagickFalse {
return fmt.Errorf("Could not resize: %s", self.Error())
}
return nil
}
// Changes the compression quality of the canvas. Ranges from 1 (lowest) to 100 (highest).
func (self *Canvas) SetQuality(quality uint) error {
success := C.MagickSetImageCompressionQuality(self.wand, C.size_t(quality))
if success == C.MagickFalse {
return fmt.Errorf("Could not set compression quality: %s", self.Error())
}
return nil
}
// Returns the compression quality of the canvas. Ranges from 1 (lowest) to 100 (highest).
func (self *Canvas) Quality() uint {
return uint(C.MagickGetImageCompressionQuality(self.wand))
}
// Sets canvas's foreground color.
func (self *Canvas) SetColor(color string) bool {
status := C.PixelSetColor(self.fg, C.CString(color))
if status == C.MagickFalse {
return false
}
return true
}
// Sets canvas' background color.
func (self *Canvas) SetBackgroundColor(color string) error {
var status C.MagickBooleanType
ccolor := C.CString(color)
status = C.PixelSetColor(self.bg, ccolor)
C.free(unsafe.Pointer(ccolor))
if status == C.MagickFalse {
return fmt.Errorf("Could not set pixel color: %s", self.Error())
}
status = C.MagickSetImageBackgroundColor(self.wand, self.bg)
if status == C.MagickFalse {
return fmt.Errorf("Could not set background color: %s", self.Error())
}
return nil
}
// Returns canvas' background color.
func (self *Canvas) BackgroundColor() string {
return getPixelHexColor(self.bg)
}
// Sets antialiasing setting for the current drawing stroke.
func (self *Canvas) SetStrokeAntialias(value bool) {
C.DrawSetStrokeAntialias(self.drawing, magickBoolean(value))
}
// Returns antialiasing setting for the current drawing stroke.
func (self *Canvas) StrokeAntialias() bool {
value := C.DrawGetStrokeAntialias(self.drawing)
if value == C.MagickTrue {
return true
}
return false
}
// Sets the width of the stroke on the current drawing surface.
func (self *Canvas) SetStrokeWidth(value float64) {
C.DrawSetStrokeWidth(self.drawing, C.double(value))
}
// Returns the width of the stroke on the current drawing surface.
func (self *Canvas) StrokeWidth() float64 {
return float64(C.DrawGetStrokeWidth(self.drawing))
}
// Sets the opacity of the stroke on the current drawing surface.
func (self *Canvas) SetStrokeOpacity(value float64) {
C.DrawSetStrokeOpacity(self.drawing, C.double(value))
}
// Returns the opacity of the stroke on the current drawing surface.
func (self *Canvas) StrokeOpacity() float64 {
return float64(C.DrawGetStrokeOpacity(self.drawing))
}
// Sets the type of the line cap on the current drawing surface.
func (self *Canvas) SetStrokeLineCap(value uint) {
C.DrawSetStrokeLineCap(self.drawing, C.LineCap(value))
}
// Returns the type of the line cap on the current drawing surface.
func (self *Canvas) StrokeLineCap() uint {
return uint(C.DrawGetStrokeLineCap(self.drawing))
}
// Sets the type of the line join on the current drawing surface.
func (self *Canvas) SetStrokeLineJoin(value uint) {
C.DrawSetStrokeLineJoin(self.drawing, C.LineJoin(value))
}
// Returns the type of the line join on the current drawing surface.
func (self *Canvas) StrokeLineJoin() uint {
return uint(C.DrawGetStrokeLineJoin(self.drawing))
}
/*
func (self *Canvas) SetFillRule(value int) {
C.DrawSetFillRule(self.drawing, C.FillRule(value))
}
*/
// Sets the fill color for enclosed areas on the current drawing surface.
func (self *Canvas) SetFillColor(color string) {
ccolor := C.CString(color)
C.PixelSetColor(self.fill, ccolor)
C.free(unsafe.Pointer(ccolor))
C.DrawSetFillColor(self.drawing, self.fill)
}
// Returns the fill color for enclosed areas on the current drawing surface.
func (self *Canvas) FillColor() string {
return getPixelHexColor(self.fill)
}
// Sets the stroke color on the current drawing surface.
func (self *Canvas) SetStrokeColor(color string) {
ccolor := C.CString(color)
C.PixelSetColor(self.stroke, ccolor)
C.free(unsafe.Pointer(ccolor))
C.DrawSetStrokeColor(self.drawing, self.stroke)
}
// Returns the stroke color on the current drawing surface.
func (self *Canvas) StrokeColor() string {
return getPixelHexColor(self.stroke)
}
// Draws a circle over the current drawing surface.
func (self *Canvas) Circle(radius float64) {
C.DrawCircle(self.drawing, C.double(0), C.double(0), C.double(radius), C.double(0))
}
// Draws a rectangle over the current drawing surface.
func (self *Canvas) Rectangle(x float64, y float64) {
C.DrawRectangle(self.drawing, C.double(0), C.double(0), C.double(x), C.double(y))
}
// Moves the current coordinate system origin to the specified coordinate.
func (self *Canvas) Translate(x float64, y float64) {
C.DrawTranslate(self.drawing, C.double(x), C.double(y))
}
// Applies a scaling factor to the units of the current coordinate system.
func (self *Canvas) Scale(x float64, y float64) {
C.DrawScale(self.drawing, C.double(x), C.double(y))
}
// Draws a line starting on the current coordinate system origin and ending on the specified coordinates.
func (self *Canvas) Line(x float64, y float64) {
C.DrawLine(self.drawing, C.double(0), C.double(0), C.double(x), C.double(y))
}
/*
func (self *Canvas) Skew(x float64, y float64) {
C.DrawSkewX(self.drawing, C.double(x))
C.DrawSkewY(self.drawing, C.double(y))
}
*/
// Applies a rotation of a given angle (in radians) on the current coordinate system.
func (self *Canvas) Rotate(rad float64) {
deg := RAD_TO_DEG * rad
C.DrawRotate(self.drawing, C.double(deg))
}
// Draws an ellipse centered at the current coordinate system's origin.
func (self *Canvas) Ellipse(a float64, b float64) {
C.DrawEllipse(self.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 (self *Canvas) PushDrawing() error {
success := C.PushDrawingWand(self.drawing)
if success == C.MagickFalse {
return fmt.Errorf("Could not push surface: %s", self.Error())
}
return nil
}
// Destroys the current drawing surface and returns the latest surface that was pushed to the stack.
func (self *Canvas) PopDrawing() error {
success := C.PopDrawingWand(self.drawing)
if success == C.MagickFalse {
return fmt.Errorf("Could not pop surface: %s", self.Error())
}
return nil
}
// Copies a drawing surface to the canvas.
func (self *Canvas) Update() error {
success := C.MagickDrawImage(self.wand, self.drawing)
if success == C.MagickFalse {
return fmt.Errorf("Could not update image: %s", self.Error())
}
return nil
}
// Destroys canvas.
func (self *Canvas) Destroy() error {
if self.bg != nil {
C.DestroyPixelWand(self.bg)
self.bg = nil
}
if self.fg != nil {
C.DestroyPixelWand(self.fg)
self.fg = nil
}
if self.stroke != nil {
C.DestroyPixelWand(self.stroke)
self.stroke = nil
}
if self.fill != nil {
C.DestroyPixelWand(self.fill)
self.fill = nil
}
if self.drawing != nil {
C.DestroyDrawingWand(self.drawing)
self.drawing = nil
}
if self.wand == nil {
return errors.New("Nothing to destroy")
} else {
C.DestroyMagickWand(self.wand)
self.wand = nil
}
return nil
}
func Finalize() {
C.MagickWandTerminus()
}
// Creates an empty canvas of the given dimensions.
func (self *Canvas) Blank(width uint, height uint) error {
success := C.MagickNewImage(self.wand, C.size_t(width), C.size_t(height), self.bg)
if success == C.MagickFalse {
return fmt.Errorf("Could not create image: %s", self.Error())
}
return nil
}
// Convolves the canvas with a Gaussian function given its standard deviation.
func (self *Canvas) Blur(sigma float64) error {
success := C.MagickBlurImage(self.wand, C.double(0), C.double(sigma))
if success == C.MagickFalse {
return fmt.Errorf("Could not blur image: %s", self.Error())
}
return nil
}
// Adaptively blurs the image by blurring less intensely near the edges and more intensely far from edges.
func (self *Canvas) AdaptiveBlur(sigma float64) error {
success := C.MagickAdaptiveBlurImage(self.wand, C.double(0), C.double(sigma))
if success == C.MagickFalse {
return fmt.Errorf("Could not blur image: %s", self.Error())
}
return nil
}
// Adds random noise to the canvas.
func (self *Canvas) AddNoise() error {
success := C.MagickAddNoiseImage(self.wand, C.GaussianNoise)
if success == C.MagickFalse {
return fmt.Errorf("Could not add noise: %s", self.Error())
}
return nil
}
// Removes a region of a canvas and collapses the canvas to occupy the removed portion.
func (self *Canvas) Chop(x int, y int, width uint, height uint) error {
success := C.MagickChopImage(self.wand, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y))
if success == C.MagickFalse {
return fmt.Errorf("Could not chop: %s", self.Error())
}
return nil
}
// Extracts a region from the canvas.
func (self *Canvas) Crop(x int, y int, width uint, height uint) error {
success := C.MagickCropImage(self.wand, C.size_t(width), C.size_t(height), C.ssize_t(x), C.ssize_t(y))
if success == C.MagickFalse {
return fmt.Errorf("Could not crop: %s", self.Error())
}
return nil
}
func (self *Canvas) SetSize(width, height uint) error {
if C.MagickSetSize(self.wand, C.size_t(width), C.size_t(height)) == C.MagickFalse {
return fmt.Errorf("Could not set size: %s", self.Error())
}
return nil
}
func (self *Canvas) SetContrast(factor float64) error {
factor = math.Max(-100, factor)
factor = math.Min(100, factor)
success := C.MagickBrightnessContrastImage(self.wand, 0, C.double(factor))
if success == C.MagickFalse {
return fmt.Errorf("Could not set contrast: %s", self.Error())
}
return nil
}
// Adjusts the canvas's brightness given a factor (-1.0 thru 1.0)
func (self *Canvas) SetBrightness(factor float64) error {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
success := C.MagickModulateImage(self.wand, C.double(100+factor*100.0), C.double(100), C.double(100))
if success == C.MagickFalse {
return fmt.Errorf("Could not set brightness: %s", self.Error())
}
return nil
}
// Adjusts the canvas's saturation given a factor (-1.0 thru 1.0)
func (self *Canvas) SetSaturation(factor float64) error {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
success := C.MagickModulateImage(self.wand, C.double(100), C.double(100+factor*100.0), C.double(100))
if success == C.MagickFalse {
return fmt.Errorf("Could not set saturation: %s", self.Error())
}
return nil
}
// Adjusts the canvas's hue given a factor (-1.0 thru 1.0)
func (self *Canvas) SetHue(factor float64) error {
factor = math.Max(-1, factor)
factor = math.Min(1, factor)
success := C.MagickModulateImage(self.wand, C.double(100), C.double(100), C.double(100+factor*100.0))
if success == C.MagickFalse {
return fmt.Errorf("Could not set hue: %s", self.Error())
}
return nil
}
func (self *Canvas) InterlaceScheme() uint {
return uint(C.MagickGetImageInterlaceScheme(self.wand))
}
func (self *Canvas) SetInterlaceScheme(scheme uint) error {
if C.MagickSetImageInterlaceScheme(self.wand, C.InterlaceType(scheme)) == C.MagickFalse {
return fmt.Errorf("Could not set interlace scheme: %s", self.Error())
}
return nil
}
// Sets the format of a particular image
func (self *Canvas) SetFormat(format string) error {
cformat := C.CString(format)
defer C.free(unsafe.Pointer(cformat))
if C.MagickSetImageFormat(self.wand, cformat) == C.MagickFalse {
return fmt.Errorf("Could not set format: %s", self.Error())
}
return nil
}
func (self *Canvas) GetFormat() string {
return self.Format()
}
func (self *Canvas) Format() string {
ptr := C.MagickGetImageFormat(self.wand)
defer C.free(unsafe.Pointer(ptr))
return C.GoString(ptr)
}
func (self *Canvas) Strip() error {
var status C.MagickBooleanType
status = C.MagickStripImage(self.wand)
if status == C.MagickFalse {
return fmt.Errorf("Could not strip: %s", self.Error())
}
return nil
}
func (self *Canvas) SetType(imageType uint) error {
var status C.MagickBooleanType
status = C.MagickSetImageType(self.wand, C.ImageType(imageType))
if status == C.MagickFalse {
return fmt.Errorf("Could not set type: %s", self.Error())
}
return nil
}
func (self *Canvas) Type() uint {
return uint(C.MagickGetImageType(self.wand))
}
func (self *Canvas) SetSepiaTone(threshold float64) error {
threshold = math.Max(0.0, threshold)
threshold = math.Min(100.0, threshold)
threshold = (float64(self.QuantumRange()) * threshold) / 100.0
if C.MagickSepiaToneImage(self.wand, C.double(threshold)) == C.MagickFalse {
return fmt.Errorf("Could not apply sepia effect: %s", self.Error())
}