-
Notifications
You must be signed in to change notification settings - Fork 35
/
list.go
205 lines (185 loc) · 4 KB
/
list.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
package magick
// #include <magick/api.h>
import "C"
func (im *Image) IsOrphan() bool {
return im.image.next == nil && im.image.previous == nil
}
func (im *Image) NFrames() int {
if im.parent != nil {
return 1
}
return int(C.GetImageListLength(im.image))
}
func (im *Image) FrameIndex() int {
return int(C.GetImageIndexInList(im.image))
}
func (im *Image) Duration() int {
if im.parent != nil {
return im.Delay()
}
duration := 0
for cur := C.GetFirstImageInList(im.image); cur != nil; cur = (*C.Image)(cur.next) {
duration += int(cur.delay)
}
return duration
}
func (im *Image) Frame(idx int) (*Image, error) {
if im.IsOrphan() && idx == 0 {
return im, nil
}
image := C.GetImageFromList(im.image, magickLong(idx))
if image == nil {
return nil, ErrNoSuchFrame
}
return newImage(image, im), nil
}
func (im *Image) First() *Image {
first, _ := im.Frame(0)
return first
}
func (im *Image) Last() *Image {
last, _ := im.Frame(im.NFrames() - 1)
return last
}
func (im *Image) Prev() *Image {
prev := C.GetPreviousImageInList(im.image)
if prev == nil {
return nil
}
return newImage(prev, im)
}
func (im *Image) Next() *Image {
next := C.GetNextImageInList(im.image)
if next == nil {
return nil
}
return newImage(next, im)
}
func (im *Image) Reverse() {
C.ReverseImageList(&im.image)
}
/*func (im *Image) InsertAfter(image *Image) error {
cloned, err := image.Clone()
if err != nil {
return err
}
first := cloned.First()
first.prev = im
if im.next != nil {
im.next.prev = cloned.Last()
}
im.next = first
return nil
}
func (im *Image) InsertBefore(image *Image) error {
cloned, err := image.CloneAll()
if err != nil {
return err
}
last := cloned.Last()
last.next = im
if im.prev != nil {
im.prev.next = cloned.First()
}
image.prev = last
return nil
}
*/
func (im *Image) Prepend(image *Image) error {
// TODO: Don't clone, refcount
cloned, err := image.Clone()
if err != nil {
return err
}
dontFree(cloned)
C.PrependImageToList(&im.image, cloned.image)
return nil
}
func (im *Image) Append(image *Image) error {
// TODO: Don't clone, refcount
cloned, err := image.Clone()
if err != nil {
return err
}
dontFree(cloned)
C.AppendImageToList(&im.image, cloned.image)
return nil
}
func (im *Image) RemoveIndex(idx int) bool {
if frame, _ := im.Frame(idx); frame != nil {
return frame.Remove()
}
return false
}
func (im *Image) RemoveFirst() bool {
return im.RemoveIndex(0)
}
func (im *Image) RemoveLast() bool {
return im.RemoveIndex(im.NFrames() - 1)
}
func (im *Image) Remove() bool {
if im.parent != nil && !im.parent.IsOrphan() {
// Don't use DeleteImageFromList, since it calls DestroyImage,
// bypassing our refcounting.
if im.image == im.parent.image {
im.parent.image = (*C.Image)(im.parent.image.next)
}
if p := (*C.Image)(im.image.previous); p != nil {
p.next = im.image.next
}
if n := (*C.Image)(im.image.next); n != nil {
n.previous = im.image.previous
}
im.image.previous = nil
im.image.next = nil
im.parent = nil
// im already holds a reference, remove the reference that
// the parent had.
unrefImage(im.image)
return true
}
return false
}
func (im *Image) Apply(f func(*Image) (*Image, error)) (*Image, error) {
if im.IsOrphan() || im.parent != nil {
return f(im)
}
var ex C.ExceptionInfo
C.GetExceptionInfo(&ex)
defer C.DestroyExceptionInfo(&ex)
cur, free := im.coalescedCImage(&ex)
if cur == nil {
return nil, exError(&ex, "coalescing")
}
buf := &Image{
parent: im,
image: cur,
}
first, err := f(buf)
if err != nil {
return nil, err
}
prev := first.image
var res *Image
for cur := (*C.Image)(im.image.next); cur != nil; cur = (*C.Image)(cur.next) {
buf.image = cur
res, err = f(buf)
if err != nil {
break
}
dontFree(res)
img := res.image
if img != cur {
prev.next = (*C.struct__Image)(img)
img.previous = (*C.struct__Image)(prev)
}
prev = img
}
if free && (first.image != cur || err != nil) {
C.DestroyImageList(cur)
}
if err != nil {
return nil, err
}
return first, nil
}