/
pixel_iterator.go
197 lines (172 loc) · 5.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
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
// Copyright 2013 Herbert G. Fischer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package imagick
/*
#include <wand/MagickWand.h>
static PixelWand* get_pw_at(PixelWand** pws, size_t pos) {
return pws[pos];
}
*/
import "C"
import (
"runtime"
"sync"
"sync/atomic"
"unsafe"
)
type PixelIterator struct {
pi *C.PixelIterator
init sync.Once
}
func newPixelIterator(cpi *C.PixelIterator) *PixelIterator {
pi := &PixelIterator{pi: cpi}
runtime.SetFinalizer(pi, Destroy)
pi.IncreaseCount()
return pi
}
// Returns a new pixel iterator
//
// mw: the magick wand to iterate on
//
func (mw *MagickWand) NewPixelIterator() *PixelIterator {
ret := newPixelIterator(C.NewPixelIterator(mw.mw))
runtime.KeepAlive(mw)
return ret
}
// Returns a new pixel iterator
//
// mw: the magick wand to iterate on
// x, y, cols, rows: there values define the perimeter of a region of pixels
//
func (mw *MagickWand) NewPixelRegionIterator(x, y int, width, height uint) *PixelIterator {
ret := newPixelIterator(C.NewPixelRegionIterator(mw.mw, C.ssize_t(x), C.ssize_t(y), C.size_t(width), C.size_t(height)))
runtime.KeepAlive(mw)
return ret
}
// Clear resources associated with a PixelIterator.
func (pi *PixelIterator) Clear() {
C.ClearPixelIterator(pi.pi)
runtime.KeepAlive(pi)
}
// Makes an exact copy of the specified iterator.
func (pi *PixelIterator) Clone() *PixelIterator {
ret := newPixelIterator(C.ClonePixelIterator(pi.pi))
runtime.KeepAlive(pi)
return ret
}
// Deallocates resources associated with a PixelIterator.
func (pi *PixelIterator) Destroy() {
if pi.pi == nil {
return
}
pi.init.Do(func() {
pi.pi = C.DestroyPixelIterator(pi.pi)
relinquishMemory(unsafe.Pointer(pi.pi))
runtime.SetFinalizer(pi, nil)
pi.pi = nil
pi.DecreaseCount()
})
}
// Returns true if the iterator is verified as a pixel iterator.
func (pi *PixelIterator) IsVerified() bool {
if pi.pi == nil {
return false
}
ret := 1 == C.IsPixelIterator(pi.pi)
runtime.KeepAlive(pi)
return ret
}
// Increase PixelIterator ref counter and set according "can`t be terminated status"
func (pi *PixelIterator) IncreaseCount() {
atomic.AddInt64(&pixelIteratorCounter, int64(1))
unsetCanTerminate()
}
// Decrease DrawingWand ref counter and set according "can be terminated status"
func (pi *PixelIterator) DecreaseCount() {
atomic.AddInt64(&pixelIteratorCounter, int64(-1))
setCanTerminate()
}
// Returns the current row as an array of pixel wands from the pixel iterator.
func (pi *PixelIterator) GetCurrentIteratorRow() (pws []*PixelWand) {
num := C.size_t(0)
pp := C.PixelGetCurrentIteratorRow(pi.pi, &num)
if pp == nil {
return
}
for i := 0; i < int(num); i++ {
cpw := C.get_pw_at(pp, C.size_t(i))
// PixelWand is a private pointer, borrowed from the pixel
// iterator. We don't want to reference count it. It will
// get destroyed by C API when PixelIterator is destroyed.
pws = append(pws, &PixelWand{pw: cpw})
}
runtime.KeepAlive(pi)
return
}
// Returns the current pixel iterator row.
func (pi *PixelIterator) GetIteratorRow() int {
ret := int(C.PixelGetIteratorRow(pi.pi))
runtime.KeepAlive(pi)
return ret
}
// Returns the next row as an array of pixel wands from the pixel iterator.
func (pi *PixelIterator) GetNextIteratorRow() (pws []*PixelWand) {
num := C.size_t(0)
pp := C.PixelGetNextIteratorRow(pi.pi, &num)
if pp == nil {
return
}
for i := 0; i < int(num); i++ {
cpw := C.get_pw_at(pp, C.size_t(i))
// PixelWand is a private pointer, borrowed from the pixel
// iterator. We don't want to reference count it. It will
// get destroyed by C API when PixelIterator is destroyed.
pws = append(pws, &PixelWand{pw: cpw})
}
runtime.KeepAlive(pi)
return
}
// Returns the previous row as an array of pixel wands from the pixel iterator.
func (pi *PixelIterator) GetPreviousIteratorRow() (pws []*PixelWand) {
num := C.size_t(0)
pp := C.PixelGetPreviousIteratorRow(pi.pi, &num)
if pp == nil {
return
}
for i := 0; i < int(num); i++ {
cpw := C.get_pw_at(pp, C.size_t(i))
// PixelWand is a private pointer, borrowed from the pixel
// iterator. We don't want to reference count it. It will
// get destroyed by C API when PixelIterator is destroyed.
pws = append(pws, &PixelWand{pw: cpw})
}
runtime.KeepAlive(pi)
return
}
// Resets the pixel iterator. Use it in conjunction with GetNextIteratorRow()
// to iterate over all the pixels in a pixel container.
func (pi *PixelIterator) Reset() {
C.PixelResetIterator(pi.pi)
runtime.KeepAlive(pi)
}
// Sets the pixel iterator to the first pixel row.
func (pi *PixelIterator) SetFirstIteratorRow() {
C.PixelSetFirstIteratorRow(pi.pi)
runtime.KeepAlive(pi)
}
// Set the pixel iterator row.
func (pi *PixelIterator) SetIteratorRow(row int) error {
ok := C.PixelSetIteratorRow(pi.pi, C.ssize_t(row))
return pi.getLastErrorIfFailed(ok)
}
// Sets the pixel iterator to the last pixel row.
func (pi *PixelIterator) SetLastIteratorRow() {
C.PixelSetLastIteratorRow(pi.pi)
runtime.KeepAlive(pi)
}
// Syncs the pixel iterator.
func (pi *PixelIterator) SyncIterator() error {
ok := C.PixelSyncIterator(pi.pi)
return pi.getLastErrorIfFailed(ok)
}