/
compose.im
211 lines (181 loc) · 4.95 KB
/
compose.im
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
#include "imager.h"
#include "imrender.h"
#include "imageri.h"
int
i_compose_mask(i_img *out, i_img *src, i_img *mask,
int out_left, int out_top,
int src_left, int src_top,
int mask_left, int mask_top,
int width, int height,
int combine,
double opacity) {
i_render r;
int dy;
i_fill_combine_f combinef_8;
i_fill_combinef_f combinef_double;
int channel_zero = 0;
i_clear_error();
if (out_left >= out->xsize
|| out_top >= out->ysize
|| src_left >= src->xsize
|| src_top >= src->ysize
|| width <= 0
|| height <= 0
|| out_left + width <= 0
|| out_top + height <= 0
|| src_left + width <= 0
|| src_top + height <= 0
|| mask_left >= mask->xsize
|| mask_top >= mask->ysize
|| mask_left + width <= 0
|| mask_top + height <= 0)
return 0;
if (out_left < 0) {
width = out_left + width;
out_left = 0;
}
if (out_left + width > out->xsize)
width = out->xsize - out_left;
if (out_top < 0) {
height = out_top + height;
out_top = 0;
}
if (out_top + height > out->ysize)
height = out->ysize - out_top;
if (src_left < 0) {
width = src_left + width;
src_left = 0;
}
if (src_left + width > src->xsize)
width = src->xsize - src_left;
if (src_top < 0) {
height = src_top + height;
src_top = 0;
}
if (src_top + height > src->ysize)
height = src->ysize - src_left;
if (mask_left < 0) {
width = mask_left + width;
mask_left = 0;
}
if (mask_left + width > mask->xsize)
width = mask->xsize - mask_left;
if (mask_top < 0) {
height = mask->ysize + height;
mask_top = 0;
}
if (mask_top + height > mask->ysize)
height = mask->xsize - mask_top;
if (opacity > 1.0)
opacity = 1.0;
else if (opacity <= 0)
return 0;
i_get_combine(combine, &combinef_8, &combinef_double);
i_render_init(&r, out, width);
#code out->bits <= 8 && src->bits<= 8 && mask->bits <= 8
IM_COLOR *src_line = mymalloc(sizeof(IM_COLOR) * width);
IM_SAMPLE_T *mask_line = mymalloc(sizeof(IM_SAMPLE_T) * width);
int adapt_channels = out->channels;
if (adapt_channels == 1 || adapt_channels == 3)
++adapt_channels;
for (dy = 0; dy < height; ++dy) {
IM_GLIN(src, src_left, src_left + width, src_top + dy, src_line);
IM_ADAPT_COLORS(adapt_channels, src->channels, src_line, width);
IM_GSAMP(mask, mask_left, mask_left + width, mask_top + dy,
mask_line, &channel_zero, 1);
if (opacity < 1.0) {
int i;
IM_SAMPLE_T *maskp = mask_line;
for (i = 0; i < width; ++i) {
*maskp = IM_ROUND(*maskp * opacity);
++maskp;
}
}
IM_RENDER_LINE(&r, out_left, out_top+dy, width, mask_line, src_line,
IM_SUFFIX(combinef));
}
myfree(src_line);
myfree(mask_line);
#/code
i_render_done(&r);
return 1;
}
int
i_compose(i_img *out, i_img *src,
int out_left, int out_top,
int src_left, int src_top,
int width, int height,
int combine,
double opacity) {
i_render r;
int dy;
i_fill_combine_f combinef_8;
i_fill_combinef_f combinef_double;
i_clear_error();
if (out_left >= out->xsize
|| out_top >= out->ysize
|| src_left >= src->xsize
|| src_top >= src->ysize
|| width <= 0
|| height <= 0
|| out_left + width <= 0
|| out_top + height <= 0
|| src_left + width <= 0
|| src_top + height <= 0)
return 0;
if (out_left < 0) {
width = out_left + width;
out_left = 0;
}
if (out_left + width > out->xsize)
width = out->xsize - out_left;
if (out_top < 0) {
height = out_top + height;
out_top = 0;
}
if (out_top + height > out->ysize)
height = out->ysize - out_top;
if (src_left < 0) {
width = src_left + width;
src_left = 0;
}
if (src_left + width > src->xsize)
width = src->xsize - src_left;
if (src_top < 0) {
height = src_top + height;
src_top = 0;
}
if (src_top + height > src->ysize)
height = src->ysize - src_left;
if (opacity > 1.0)
opacity = 1.0;
else if (opacity <= 0)
return 0;
i_get_combine(combine, &combinef_8, &combinef_double);
i_render_init(&r, out, width);
#code out->bits <= 8 && src->bits <= 8
IM_COLOR *src_line = mymalloc(sizeof(IM_COLOR) * width);
IM_SAMPLE_T *mask_line = NULL;
int adapt_channels = out->channels;
if (opacity != 1.0) {
int i;
IM_SAMPLE_T mask_value = IM_ROUND(opacity * IM_SAMPLE_MAX);
mask_line = mymalloc(sizeof(IM_SAMPLE_T) * width);
for (i = 0; i < width; ++i)
mask_line[i] = mask_value;
}
if (adapt_channels == 1 || adapt_channels == 3)
++adapt_channels;
for (dy = 0; dy < height; ++dy) {
IM_GLIN(src, src_left, src_left + width, src_top + dy, src_line);
IM_ADAPT_COLORS(adapt_channels, src->channels, src_line, width);
IM_RENDER_LINE(&r, out_left, out_top+dy, width, mask_line, src_line,
IM_SUFFIX(combinef));
}
myfree(src_line);
if (mask_line)
myfree(mask_line);
#/code
i_render_done(&r);
return 1;
}