-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpixel_iterator.go
60 lines (45 loc) · 1.13 KB
/
pixel_iterator.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
package canvas
/*
#include <wand/MagickWand.h>
static PixelWand* get_pixel_wands_at(PixelWand** pixel_wands, size_t position) {
return pixel_wands[position];
}
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
type PixelIterator struct {
iterator *C.PixelIterator
}
func (self *PixelIterator) NextRow() (row []*Pixel) {
pixelCount := C.size_t(0)
pixels := C.PixelGetNextIteratorRow(self.iterator, &pixelCount)
if pixels == nil {
return nil
}
for i := 0; i < int(pixelCount); i++ {
wand := C.get_pixel_wands_at(pixels, C.size_t(i))
row = append(row, &Pixel{wand: wand})
}
return
}
func (self *PixelIterator) Sync() error {
if C.PixelSyncIterator(self.iterator) == C.MagickFalse {
return fmt.Errorf("cannot sync iterator: %s", self.Error())
}
return nil
}
func (self *PixelIterator) Error() error {
var exception C.ExceptionType
ptr := C.PixelGetIteratorException(self.iterator, &exception)
message := C.GoString(ptr)
C.PixelClearIteratorException(self.iterator)
C.MagickRelinquishMemory(unsafe.Pointer(ptr))
return errors.New(message)
}
func (self *PixelIterator) Destroy() {
C.DestroyPixelIterator(self.iterator)
}