forked from gosexy/canvas
/
canvas.go
794 lines (604 loc) · 21.7 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
/*
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./_include
#include <wand/magick_wand.h>
char *MagickGetPropertyName(char **properties, size_t index) {
return properties[index];
}
*/
import "C"
import (
"fmt"
"math"
"os"
"strconv"
"strings"
"unsafe"
)
// MagickWand constants
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)
)
// 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
}
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)
}
status := C.MagickReadImage(self.wand, C.CString(filename))
if status == C.MagickFalse {
return fmt.Errorf(`Could not open image "%s": %s`, filename, self.Error())
}
self.filename = filename
return nil
}
// Reads an image or image sequence from a blob.
func (self Canvas) OpenBlob(blob []byte, length uint) error {
if C.MagickReadImageBlob(self.wand, unsafe.Pointer(&blob[0]), C.size_t(length)) == 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 fmt.Errorf("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.ulong
var i C.ulong
var value *C.char
var key *C.char
data := make(map[string]string)
properties := C.MagickGetImageProperties(self.wand, C.CString("*"), &n)
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 fmt.Errorf(message)
}
// Associates a metadata key with its value.
func (self Canvas) SetMetadata(key string, value string) error {
success := C.MagickSetImageProperty(self.wand, C.CString(key), C.CString(value))
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 {
if C.MagickSigmoidalContrastImage(self.wand, magickBoolean(sharpen), C.double(alpha), C.double(beta)) == 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 {
if C.MagickContrastImage(self.wand, magickBoolean(sharpen)) == 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
}
// Creates a centered thumbnail of the canvas.
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 {
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.OverCompositeOp, C.long(x), C.long(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
}
success := C.MagickWriteImage(self.wand, C.CString(filename))
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.ulong(width), C.ulong(height), C.GaussianFilter, C.double(1.0))
if success == C.MagickFalse {
return fmt.Errorf("Could not resize: %s", self.Error())
}
return 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.ulong(width), C.ulong(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.ulong(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 {
if C.PixelSetColor(self.bg, C.CString(color)) == C.MagickFalse {
return fmt.Errorf("Could not set pixel color: %s", self.Error())
}
if C.MagickSetImageBackgroundColor(self.wand, self.bg) == 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) {
C.PixelSetColor(self.fill, C.CString(color))
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) {
C.PixelSetColor(self.stroke, C.CString(color))
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.wand != nil {
C.DestroyMagickWand(self.wand)
self.wand = nil
}
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
}
return fmt.Errorf("Nothing to destroy")
}
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.ulong(width), C.ulong(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.ulong(width), C.ulong(height), C.long(x), C.long(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.ulong(width), C.ulong(height), C.long(x), C.long(y))
if success == C.MagickFalse {
return fmt.Errorf("Could not crop: %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
}
// Sets the format of a particular image
func (self Canvas) SetFormat(format string) error {
if C.MagickSetImageFormat(self.wand, C.CString(format)) != C.MagickFalse {
return fmt.Errorf("Coluld not set format : %s", self.Error())
}
return nil
}
// Implements direct to memory image formats. It returns the image as a blob
func (self Canvas) Blob(length *uint) []byte {
ptr := unsafe.Pointer(C.MagickGetImageBlob(self.wand, (*C.size_t) (unsafe.Pointer(length))))
data := C.GoBytes(ptr, C.int(*length))
C.MagickRelinquishMemory(ptr)
return data
}
// Returns a new canvas object.
func New() *Canvas {
self := &Canvas{}
self.wand = C.NewMagickWand()
self.fg = C.NewPixelWand()
self.bg = C.NewPixelWand()
self.fill = C.NewPixelWand()
self.stroke = C.NewPixelWand()
self.drawing = C.NewDrawingWand()
//self.SetColor("#ffffff")
self.SetBackgroundColor("none")
self.SetStrokeColor("#ffffff")
self.SetStrokeAntialias(true)
self.SetStrokeWidth(1.0)
self.SetStrokeOpacity(1.0)
self.SetStrokeLineCap(STROKE_ROUND_CAP)
self.SetStrokeLineJoin(STROKE_ROUND_JOIN)
//self.SetFillRule(FILL_EVEN_ODD_RULE)
self.SetFillColor("#888888")
return self
}