-
Notifications
You must be signed in to change notification settings - Fork 35
/
bridge.c
93 lines (89 loc) · 2.5 KB
/
bridge.c
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
#include <magick/api.h>
#include "bridge.h"
Image *
bridge_image_func(ImageFunc f, Image *im, ExceptionInfo *ex)
{
return f(im, ex);
}
Image *
bridge_image_data_func(ImageDataFunc f, Image *im, void *data, ExceptionInfo *ex)
{
return f(im, data, ex);
}
Image *
apply_image_func(ImageFunc f, Image *image, void *parent, int is_coalesced, ExceptionInfo *ex)
{
// XXX: f must either return new images or the same images in all calls. If sometimes
// it returns a new one and other times it returns the same image, that will cause either
// a crash or a memory leak. Since currently the C interface is not exposed to package users
// there's no need to inform them about this quirk, but must be taken into account by people
// writing magick code in C.
if (parent || (!image->next && !image->previous)) {
return f(image, ex);
}
Image *coalesced = NULL;
if (!is_coalesced) {
coalesced = CoalesceImages(image, ex);
if (!coalesced) {
return NULL;
}
image = coalesced;
}
Image *ret = f(image, ex);
if (ret) {
Image *prev = ret;
Image *cur;
Image *res;
for (cur = image->next; cur; cur = cur->next) {
res = f(cur, ex);
if (!res) {
DestroyImageList(ret);
ret = NULL;
break;
}
prev->next = res;
res->previous = prev;
prev = res;
}
}
if (coalesced && ret != image) {
DestroyImageList(coalesced);
}
return ret;
}
Image *
apply_image_data_func(ImageDataFunc f, Image *image, void *data, void *parent, int is_coalesced, ExceptionInfo *ex)
{
if (parent || (!image->next && !image->previous)) {
return f(image, data, ex);
}
Image *coalesced = NULL;
if (!is_coalesced) {
coalesced = CoalesceImages(image, ex);
if (!coalesced) {
return NULL;
}
image = coalesced;
}
Image *ret = f(image, data, ex);
if (ret) {
Image *prev = ret;
Image *cur;
Image *res;
for (cur = image->next; cur; cur = cur->next) {
res = f(cur, data, ex);
if (!res) {
DestroyImageList(ret);
ret = NULL;
break;
}
prev->next = res;
res->previous = prev;
prev = res;
}
}
if (coalesced && ret != image) {
DestroyImageList(coalesced);
}
return ret;
}