3 # this tests both the Inline interface and the API
7 eval "require Inline::C;";
8 plan skip_all => "Inline required for testing API" if $@;
11 plan skip_all => "Inline won't work in directories with spaces"
16 Inline->import(with => 'Imager');
18 Inline->bind(C => <<'EOS');
21 int pixel_count(Imager::ImgRaw im) {
22 return im->xsize * im->ysize;
25 int count_color(Imager::ImgRaw im, Imager::Color c) {
26 int count = 0, x, y, chan;
29 for (x = 0; x < im->xsize; ++x) {
30 for (y = 0; y < im->ysize; ++y) {
32 i_gpix(im, x, y, &read_c);
33 for (chan = 0; chan < im->channels; ++chan) {
34 if (read_c.channel[chan] != c->channel[chan]) {
48 i_img *im = i_img_8_new(10, 10, 3);
50 c.channel[0] = c.channel[1] = c.channel[2] = 255;
51 i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &c);
56 /* tests that all of the APIs are visible - most of them anyway */
57 Imager do_lots(Imager src) {
58 i_img *im = i_img_8_new(100, 100, 3);
59 i_img *fill_im = i_img_8_new(5, 5, 3);
61 i_color red, blue, temp_color;
63 i_fill_t *hatch, *fhatch_fill;
65 i_fill_t *solid_fill, *fsolid_fill;
68 double matrix[9] = /* 30 degree rotation */
79 red.channel[0] = 255; red.channel[1] = 0; red.channel[2] = 0;
81 blue.channel[0] = 0; blue.channel[1] = 0; blue.channel[2] = 255;
82 blue.channel[3] = 255;
83 hatch = i_new_fill_hatch(&red, &blue, 0, 1, NULL, 0, 0);
85 i_box(im, 0, 0, 9, 9, &red);
86 i_box_filled(im, 10, 0, 19, 9, &blue);
87 i_box_cfill(im, 20, 0, 29, 9, hatch);
89 /* make an image fill, and try it */
90 i_box_cfill(fill_im, 0, 0, 4, 4, hatch);
91 im_fill = i_new_fill_image(fill_im, matrix, 2, 2, 0);
93 i_box_cfill(im, 30, 0, 39, 9, im_fill);
95 /* make a solid fill and try it */
96 solid_fill = i_new_fill_solid(&red, 0);
97 i_box_cfill(im, 40, 0, 49, 9, solid_fill);
100 redf.channel[0] = 1.0; redf.channel[1] = 0; redf.channel[2] = 0;
101 redf.channel[3] = 1.0;
102 bluef.channel[0] = 0; bluef.channel[1] = 0; bluef.channel[2] = 1.0;
103 bluef.channel[3] = 1.0;
105 fsolid_fill = i_new_fill_solidf(&redf, 0);
106 i_box_cfill(im, 50, 0, 59, 9, fsolid_fill);
108 fhatch_fill = i_new_fill_hatchf(&redf, &bluef, 0, 2, NULL, 0, 0);
109 i_box_cfill(im, 60, 0, 69, 9, fhatch_fill);
117 fseg.type = i_fst_linear;
118 fseg.color = i_fc_hue_down;
119 fount_fill = i_new_fill_fount(70, 0, 80, 0, i_ft_linear, i_fr_triangle, 0, i_fts_none, 1, 1, &fseg);
121 i_box_cfill(im, 70, 0, 79, 9, fount_fill);
123 i_line(im, 0, 10, 10, 15, &blue, 1);
124 i_line_aa(im, 0, 19, 10, 15, &red, 1);
126 i_arc(im, 15, 15, 4, 45, 160, &blue);
127 i_arc_aa(im, 25, 15, 4, 75, 280, &red);
128 i_arc_cfill(im, 35, 15, 4, 0, 215, hatch);
129 i_arc_aa_cfill(im, 45, 15, 4, 30, 210, hatch);
130 i_circle_aa(im, 55, 15, 4, &red);
132 i_box(im, 61, 11, 68, 18, &red);
133 i_flood_fill(im, 65, 15, &blue);
134 i_box(im, 71, 11, 78, 18, &red);
135 i_flood_cfill(im, 75, 15, hatch);
137 i_fill_destroy(fount_fill);
138 i_fill_destroy(fhatch_fill);
139 i_fill_destroy(solid_fill);
140 i_fill_destroy(fsolid_fill);
141 i_fill_destroy(hatch);
142 i_fill_destroy(im_fill);
143 i_img_destroy(fill_im);
145 /* make sure we can make each image type */
146 testim = i_img_16_new(100, 100, 3);
147 i_img_destroy(testim);
148 testim = i_img_double_new(100, 100, 3);
149 i_img_destroy(testim);
150 testim = i_img_pal_new(100, 100, 3, 256);
151 i_img_destroy(testim);
152 testim = i_sametype(im, 50, 50);
153 i_img_destroy(testim);
154 testim = i_sametype_chans(im, 50, 50, 4);
155 i_img_destroy(testim);
158 i_push_error(0, "Hello");
159 i_push_errorf(0, "%s", "World");
161 /* make sure tags create/destroy work */
163 i_tags_destroy(&tags);
165 block = mymalloc(20);
166 block = myrealloc(block, 50);
169 i_tags_set(&im->tags, "lots_string", "foo", -1);
170 i_tags_setn(&im->tags, "lots_number", 101);
172 if (!i_tags_find(&im->tags, "lots_number", 0, &entry)) {
173 i_push_error(0, "lots_number tag not found");
177 i_tags_delete(&im->tags, entry);
179 /* these won't delete anything, but it makes sure the macros and function
180 pointers are correct */
181 i_tags_delbyname(&im->tags, "unknown");
182 i_tags_delbycode(&im->tags, 501);
183 i_tags_set_float(&im->tags, "lots_float", 0, 3.14);
184 if (!i_tags_get_float(&im->tags, "lots_float", 0, &temp_double)) {
185 i_push_error(0, "lots_float not found");
189 if (fabs(temp_double - 3.14) > 0.001) {
190 i_push_errorf(0, "lots_float incorrect %g", temp_double);
194 i_tags_set_float2(&im->tags, "lots_float2", 0, 100 * sqrt(2.0), 5);
195 if (!i_tags_get_int(&im->tags, "lots_float2", 0, &entry)) {
196 i_push_error(0, "lots_float2 not found as int");
201 i_push_errorf(0, "lots_float2 unexpected value %d", entry);
206 i_tags_set_color(&im->tags, "lots_color", 0, &red);
207 if (!i_tags_get_color(&im->tags, "lots_color", 0, &temp_color)) {
208 i_push_error(0, "lots_color not found as color");
218 my $im = Imager->new(xsize=>50, ysize=>50);
219 is(pixel_count($im), 2500, "pixel_count");
221 my $black = Imager::Color->new(0,0,0);
222 is(count_color($im, $black), 2500, "count_color black on black image");
224 my $im2 = make_10x10();
225 my $white = Imager::Color->new(255, 255, 255);
226 is(count_color($im2, $white), 100, "check new image white count");
227 ok($im2->box(filled=>1, xmin=>1, ymin=>1, xmax => 8, ymax=>8, color=>$black),
229 is(count_color($im2, $black), 64, "check modified black count");
230 is(count_color($im2, $white), 36, "check modified white count");
232 my $im3 = do_lots($im2);
233 ok($im3, "do_lots()")
234 or print "# ", Imager->_error_as_msg, "\n";
235 ok($im3->write(file=>'testout/t82lots.ppm'), "write t82lots.ppm");