/
t82inline.t
266 lines (222 loc) · 7.5 KB
/
t82inline.t
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!perl -w
#
# this tests both the Inline interface and the API
use strict;
use Test::More;
eval "require Inline::C;";
plan skip_all => "Inline required for testing API" if $@;
eval "require Parse::RecDescent;";
plan skip_all => "Could not load Parse::RecDescent" if $@;
use Cwd 'getcwd';
plan skip_all => "Inline won't work in directories with spaces"
if getcwd() =~ / /;
plan skip_all => "perl 5.005_04, 5.005_05 too buggy"
if $] =~ /^5\.005_0[45]$/;
plan tests => 9;
require Inline;
Inline->import(with => 'Imager');
Inline->import("FORCE"); # force rebuild
Inline->bind(C => <<'EOS');
#include <math.h>
int pixel_count(Imager::ImgRaw im) {
return im->xsize * im->ysize;
}
int count_color(Imager::ImgRaw im, Imager::Color c) {
int count = 0, x, y, chan;
i_color read_c;
for (x = 0; x < im->xsize; ++x) {
for (y = 0; y < im->ysize; ++y) {
int match = 1;
i_gpix(im, x, y, &read_c);
for (chan = 0; chan < im->channels; ++chan) {
if (read_c.channel[chan] != c->channel[chan]) {
match = 0;
break;
}
}
if (match)
++count;
}
}
return count;
}
Imager make_10x10() {
i_img *im = i_img_8_new(10, 10, 3);
i_color c;
c.channel[0] = c.channel[1] = c.channel[2] = 255;
i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &c);
return im;
}
/* tests that all of the APIs are visible - most of them anyway */
Imager do_lots(Imager src) {
i_img *im = i_img_8_new(100, 100, 3);
i_img *fill_im = i_img_8_new(5, 5, 3);
i_img *testim;
i_color red, blue, green, black, temp_color;
i_fcolor redf, bluef;
i_fill_t *hatch, *fhatch_fill;
i_fill_t *im_fill;
i_fill_t *solid_fill, *fsolid_fill;
i_fill_t *fount_fill;
void *block;
double matrix[9] = /* 30 degree rotation */
{
0.866025, -0.5, 0,
0.5, 0.866025, 0,
0, 0, 1,
};
i_fountain_seg fseg;
i_img_tags tags;
int entry;
double temp_double;
red.channel[0] = 255; red.channel[1] = 0; red.channel[2] = 0;
red.channel[3] = 255;
blue.channel[0] = 0; blue.channel[1] = 0; blue.channel[2] = 255;
blue.channel[3] = 255;
green.channel[0] = 0; green.channel[1] = 255; green.channel[2] = 0;
green.channel[3] = 255;
black.channel[0] = black.channel[1] = black.channel[2] = 0;
black.channel[3] = 255;
hatch = i_new_fill_hatch(&red, &blue, 0, 1, NULL, 0, 0);
i_box(im, 0, 0, 9, 9, &red);
i_box_filled(im, 10, 0, 19, 9, &blue);
i_box_cfill(im, 20, 0, 29, 9, hatch);
/* make an image fill, and try it */
i_box_cfill(fill_im, 0, 0, 4, 4, hatch);
im_fill = i_new_fill_image(fill_im, matrix, 2, 2, 0);
i_box_cfill(im, 30, 0, 39, 9, im_fill);
/* make a solid fill and try it */
solid_fill = i_new_fill_solid(&red, 0);
i_box_cfill(im, 40, 0, 49, 9, solid_fill);
/* floating fills */
redf.channel[0] = 1.0; redf.channel[1] = 0; redf.channel[2] = 0;
redf.channel[3] = 1.0;
bluef.channel[0] = 0; bluef.channel[1] = 0; bluef.channel[2] = 1.0;
bluef.channel[3] = 1.0;
fsolid_fill = i_new_fill_solidf(&redf, 0);
i_box_cfill(im, 50, 0, 59, 9, fsolid_fill);
fhatch_fill = i_new_fill_hatchf(&redf, &bluef, 0, 2, NULL, 0, 0);
i_box_cfill(im, 60, 0, 69, 9, fhatch_fill);
/* fountain fill */
fseg.start = 0;
fseg.middle = 0.5;
fseg.end = 1.0;
fseg.c[0] = redf;
fseg.c[1] = bluef;
fseg.type = i_fst_linear;
fseg.color = i_fc_hue_down;
fount_fill = i_new_fill_fount(70, 0, 80, 0, i_ft_linear, i_fr_triangle, 0, i_fts_none, 1, 1, &fseg);
i_box_cfill(im, 70, 0, 79, 9, fount_fill);
i_line(im, 0, 10, 10, 15, &blue, 1);
i_line_aa(im, 0, 19, 10, 15, &red, 1);
i_arc(im, 15, 15, 4, 45, 160, &blue);
i_arc_aa(im, 25, 15, 4, 75, 280, &red);
i_arc_cfill(im, 35, 15, 4, 0, 215, hatch);
i_arc_aa_cfill(im, 45, 15, 4, 30, 210, hatch);
i_circle_aa(im, 55, 15, 4, &red);
i_box(im, 61, 11, 68, 18, &red);
i_flood_fill(im, 65, 15, &blue);
i_box(im, 71, 11, 78, 18, &red);
i_flood_cfill(im, 75, 15, hatch);
i_box_filled(im, 1, 21, 9, 24, &red);
i_box_filled(im, 1, 25, 9, 29, &blue);
i_flood_fill_border(im, 5, 25, &green, &black);
i_box_filled(im, 11, 21, 19, 24, &red);
i_box_filled(im, 11, 25, 19, 29, &blue);
i_flood_cfill_border(im, 15, 25, hatch, &black);
i_fill_destroy(fount_fill);
i_fill_destroy(fhatch_fill);
i_fill_destroy(solid_fill);
i_fill_destroy(fsolid_fill);
i_fill_destroy(hatch);
i_fill_destroy(im_fill);
i_img_destroy(fill_im);
/* make sure we can make each image type */
testim = i_img_16_new(100, 100, 3);
i_img_destroy(testim);
testim = i_img_double_new(100, 100, 3);
i_img_destroy(testim);
testim = i_img_pal_new(100, 100, 3, 256);
i_img_destroy(testim);
testim = i_sametype(im, 50, 50);
i_img_destroy(testim);
testim = i_sametype_chans(im, 50, 50, 4);
i_img_destroy(testim);
i_clear_error();
i_push_error(0, "Hello");
i_push_errorf(0, "%s", "World");
/* make sure tags create/destroy work */
i_tags_new(&tags);
i_tags_destroy(&tags);
block = mymalloc(20);
block = myrealloc(block, 50);
myfree(block);
i_tags_set(&im->tags, "lots_string", "foo", -1);
i_tags_setn(&im->tags, "lots_number", 101);
if (!i_tags_find(&im->tags, "lots_number", 0, &entry)) {
i_push_error(0, "lots_number tag not found");
i_img_destroy(im);
return NULL;
}
i_tags_delete(&im->tags, entry);
/* these won't delete anything, but it makes sure the macros and function
pointers are correct */
i_tags_delbyname(&im->tags, "unknown");
i_tags_delbycode(&im->tags, 501);
i_tags_set_float(&im->tags, "lots_float", 0, 3.14);
if (!i_tags_get_float(&im->tags, "lots_float", 0, &temp_double)) {
i_push_error(0, "lots_float not found");
i_img_destroy(im);
return NULL;
}
if (fabs(temp_double - 3.14) > 0.001) {
i_push_errorf(0, "lots_float incorrect %g", temp_double);
i_img_destroy(im);
return NULL;
}
i_tags_set_float2(&im->tags, "lots_float2", 0, 100 * sqrt(2.0), 5);
if (!i_tags_get_int(&im->tags, "lots_float2", 0, &entry)) {
i_push_error(0, "lots_float2 not found as int");
i_img_destroy(im);
return NULL;
}
if (entry != 141) {
i_push_errorf(0, "lots_float2 unexpected value %d", entry);
i_img_destroy(im);
return NULL;
}
i_tags_set_color(&im->tags, "lots_color", 0, &red);
if (!i_tags_get_color(&im->tags, "lots_color", 0, &temp_color)) {
i_push_error(0, "lots_color not found as color");
i_img_destroy(im);
return NULL;
}
return im;
}
EOS
my $im = Imager->new(xsize=>50, ysize=>50);
is(pixel_count($im), 2500, "pixel_count");
my $black = Imager::Color->new(0,0,0);
is(count_color($im, $black), 2500, "count_color black on black image");
my $im2 = make_10x10();
my $white = Imager::Color->new(255, 255, 255);
is(count_color($im2, $white), 100, "check new image white count");
ok($im2->box(filled=>1, xmin=>1, ymin=>1, xmax => 8, ymax=>8, color=>$black),
"try new image");
is(count_color($im2, $black), 64, "check modified black count");
is(count_color($im2, $white), 36, "check modified white count");
my $im3 = do_lots($im2);
ok($im3, "do_lots()")
or print "# ", Imager->_error_as_msg, "\n";
ok($im3->write(file=>'testout/t82lots.ppm'), "write t82lots.ppm");
{ # RT #24992
# the T_IMAGER_FULL_IMAGE typemap entry was returning a blessed
# hash with an extra ref, causing memory leaks
my $im = make_10x10();
my $im2 = Imager->new(xsize => 10, ysize => 10);
require B;
my $imb = B::svref_2object($im);
my $im2b = B::svref_2object($im2);
is ($imb->REFCNT, $im2b->REFCNT,
"check refcnt of imager object hash between normal and typemap generated");
}