]> git.imager.perl.org - imager.git/blob - t/t82inline.t
the PERL_INITIALIZE_IMAGER_PERL_CALLBACKS was checking the wrong version number
[imager.git] / t / t82inline.t
1 #!perl -w
2 #
3 # this tests both the Inline interface and the API
4 use strict;
5 use Test::More;
6 eval "require Inline::C;";
7 plan skip_all => "Inline required for testing API" if $@;
8
9 eval "require Parse::RecDescent;";
10 plan skip_all => "Could not load Parse::RecDescent" if $@;
11
12 use Cwd 'getcwd';
13 plan skip_all => "Inline won't work in directories with spaces"
14   if getcwd() =~ / /;
15
16 plan skip_all => "perl 5.005_04, 5.005_05 too buggy"
17   if $] =~ /^5\.005_0[45]$/;
18
19 -d "testout" or mkdir "testout";
20
21 plan tests => 9;
22 require Inline;
23 Inline->import(with => 'Imager');
24 Inline->import("FORCE"); # force rebuild
25
26 Inline->bind(C => <<'EOS');
27 #include <math.h>
28
29 int pixel_count(Imager::ImgRaw im) {
30   return im->xsize * im->ysize;
31 }
32
33 int count_color(Imager::ImgRaw im, Imager::Color c) {
34   int count = 0, x, y, chan;
35   i_color read_c;
36
37   for (x = 0; x < im->xsize; ++x) {
38     for (y = 0; y < im->ysize; ++y) {
39       int match = 1;
40       i_gpix(im, x, y, &read_c);
41       for (chan = 0; chan < im->channels; ++chan) {
42         if (read_c.channel[chan] != c->channel[chan]) {
43           match = 0;
44           break;
45         }
46       }
47       if (match)
48         ++count;
49     }
50   }
51
52   return count;
53 }
54
55 Imager make_10x10() {
56   i_img *im = i_img_8_new(10, 10, 3);
57   i_color c;
58   c.channel[0] = c.channel[1] = c.channel[2] = 255;
59   i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &c);
60
61   return im;
62 }
63
64 /* tests that all of the APIs are visible - most of them anyway */
65 Imager do_lots(Imager src) {
66   i_img *im = i_img_8_new(100, 100, 3);
67   i_img *fill_im = i_img_8_new(5, 5, 3);
68   i_img *testim;
69   i_color red, blue, green, black, temp_color;
70   i_fcolor redf, bluef;
71   i_fill_t *hatch, *fhatch_fill;
72   i_fill_t *im_fill;
73   i_fill_t *solid_fill, *fsolid_fill;
74   i_fill_t *fount_fill;
75   void *block;
76   double matrix[9] = /* 30 degree rotation */
77     {
78       0.866025,  -0.5,      0, 
79       0.5,       0.866025,  0, 
80       0,         0,         1,      
81     };
82   i_fountain_seg fseg;
83   i_img_tags tags;
84   int entry;
85   double temp_double;
86
87   red.channel[0] = 255; red.channel[1] = 0; red.channel[2] = 0;
88   red.channel[3] = 255;
89   blue.channel[0] = 0; blue.channel[1] = 0; blue.channel[2] = 255;
90   blue.channel[3] = 255;
91   green.channel[0] = 0; green.channel[1] = 255; green.channel[2] = 0;
92   green.channel[3] = 255;
93   black.channel[0] = black.channel[1] = black.channel[2] = 0;
94   black.channel[3] = 255;
95   hatch = i_new_fill_hatch(&red, &blue, 0, 1, NULL, 0, 0);
96
97   i_box(im, 0, 0, 9, 9, &red);
98   i_box_filled(im, 10, 0, 19, 9, &blue);
99   i_box_cfill(im, 20, 0, 29, 9, hatch);
100
101   /* make an image fill, and try it */
102   i_box_cfill(fill_im, 0, 0, 4, 4, hatch);
103   im_fill = i_new_fill_image(fill_im, matrix, 2, 2, 0);
104
105   i_box_cfill(im, 30, 0, 39, 9, im_fill);
106
107   /* make a solid fill and try it */
108   solid_fill = i_new_fill_solid(&red, 0);
109   i_box_cfill(im, 40, 0, 49, 9, solid_fill);
110
111   /* floating fills */
112   redf.channel[0] = 1.0; redf.channel[1] = 0; redf.channel[2] = 0;
113   redf.channel[3] = 1.0;
114   bluef.channel[0] = 0; bluef.channel[1] = 0; bluef.channel[2] = 1.0;
115   bluef.channel[3] = 1.0;
116
117   fsolid_fill = i_new_fill_solidf(&redf, 0);
118   i_box_cfill(im, 50, 0, 59, 9, fsolid_fill);
119  
120   fhatch_fill = i_new_fill_hatchf(&redf, &bluef, 0, 2, NULL, 0, 0);
121   i_box_cfill(im, 60, 0, 69, 9, fhatch_fill);
122
123   /* fountain fill */
124   fseg.start = 0;
125   fseg.middle = 0.5;
126   fseg.end = 1.0;
127   fseg.c[0] = redf;
128   fseg.c[1] = bluef;
129   fseg.type = i_fst_linear;
130   fseg.color = i_fc_hue_down;
131   fount_fill = i_new_fill_fount(70, 0, 80, 0, i_ft_linear, i_fr_triangle, 0, i_fts_none, 1, 1, &fseg);
132
133   i_box_cfill(im, 70, 0, 79, 9, fount_fill);
134
135   i_line(im, 0, 10, 10, 15, &blue, 1);
136   i_line_aa(im, 0, 19, 10, 15, &red, 1);
137   
138   i_arc(im, 15, 15, 4, 45, 160, &blue);
139   i_arc_aa(im, 25, 15, 4, 75, 280, &red);
140   i_arc_cfill(im, 35, 15, 4, 0, 215, hatch);
141   i_arc_aa_cfill(im, 45, 15, 4, 30, 210, hatch);
142   i_circle_aa(im, 55, 15, 4, &red);
143   
144   i_box(im, 61, 11, 68, 18, &red);
145   i_flood_fill(im, 65, 15, &blue);
146   i_box(im, 71, 11, 78, 18, &red);
147   i_flood_cfill(im, 75, 15, hatch);
148
149   i_box_filled(im, 1, 21, 9, 24, &red);
150   i_box_filled(im, 1, 25, 9, 29, &blue);
151   i_flood_fill_border(im, 5, 25, &green, &black);
152
153   i_box_filled(im, 11, 21, 19, 24, &red);
154   i_box_filled(im, 11, 25, 19, 29, &blue);
155   i_flood_cfill_border(im, 15, 25, hatch, &black);
156
157   i_fill_destroy(fount_fill);
158   i_fill_destroy(fhatch_fill);
159   i_fill_destroy(solid_fill);
160   i_fill_destroy(fsolid_fill);
161   i_fill_destroy(hatch);
162   i_fill_destroy(im_fill);
163   i_img_destroy(fill_im);
164
165   /* make sure we can make each image type */
166   testim = i_img_16_new(100, 100, 3);
167   i_img_destroy(testim);
168   testim = i_img_double_new(100, 100, 3);
169   i_img_destroy(testim);
170   testim = i_img_pal_new(100, 100, 3, 256);
171   i_img_destroy(testim);
172   testim = i_sametype(im, 50, 50);
173   i_img_destroy(testim);
174   testim = i_sametype_chans(im, 50, 50, 4);
175   i_img_destroy(testim);
176
177   i_clear_error();
178   i_push_error(0, "Hello");
179   i_push_errorf(0, "%s", "World");
180
181   /* make sure tags create/destroy work */
182   i_tags_new(&tags);
183   i_tags_destroy(&tags);  
184
185   block = mymalloc(20);
186   block = myrealloc(block, 50);
187   myfree(block);
188
189   i_tags_set(&im->tags, "lots_string", "foo", -1);
190   i_tags_setn(&im->tags, "lots_number", 101);
191
192   if (!i_tags_find(&im->tags, "lots_number", 0, &entry)) {
193     i_push_error(0, "lots_number tag not found");
194     i_img_destroy(im);
195     return NULL;
196   }
197   i_tags_delete(&im->tags, entry);
198
199   /* these won't delete anything, but it makes sure the macros and function
200      pointers are correct */
201   i_tags_delbyname(&im->tags, "unknown");
202   i_tags_delbycode(&im->tags, 501);
203   i_tags_set_float(&im->tags, "lots_float", 0, 3.14);
204   if (!i_tags_get_float(&im->tags, "lots_float", 0, &temp_double)) {
205     i_push_error(0, "lots_float not found");
206     i_img_destroy(im);
207     return NULL;
208   }
209   if (fabs(temp_double - 3.14) > 0.001) {
210     i_push_errorf(0, "lots_float incorrect %g", temp_double);
211     i_img_destroy(im);
212     return NULL;
213   }
214   i_tags_set_float2(&im->tags, "lots_float2", 0, 100 * sqrt(2.0), 5);
215   if (!i_tags_get_int(&im->tags, "lots_float2", 0, &entry)) {
216     i_push_error(0, "lots_float2 not found as int");
217     i_img_destroy(im);
218     return NULL;
219   }
220   if (entry != 141) { 
221     i_push_errorf(0, "lots_float2 unexpected value %d", entry);
222     i_img_destroy(im);
223     return NULL;
224   }
225
226   i_tags_set_color(&im->tags, "lots_color", 0, &red);
227   if (!i_tags_get_color(&im->tags, "lots_color", 0, &temp_color)) {
228     i_push_error(0, "lots_color not found as color");
229     i_img_destroy(im);
230     return NULL;
231   }
232     
233   return im;
234 }
235
236 EOS
237
238 my $im = Imager->new(xsize=>50, ysize=>50);
239 is(pixel_count($im), 2500, "pixel_count");
240
241 my $black = Imager::Color->new(0,0,0);
242 is(count_color($im, $black), 2500, "count_color black on black image");
243
244 my $im2 = make_10x10();
245 my $white = Imager::Color->new(255, 255, 255);
246 is(count_color($im2, $white), 100, "check new image white count");
247 ok($im2->box(filled=>1, xmin=>1, ymin=>1, xmax => 8, ymax=>8, color=>$black),
248    "try new image");
249 is(count_color($im2, $black), 64, "check modified black count");
250 is(count_color($im2, $white), 36, "check modified white count");
251
252 my $im3 = do_lots($im2);
253 ok($im3, "do_lots()")
254   or print "# ", Imager->_error_as_msg, "\n";
255 ok($im3->write(file=>'testout/t82lots.ppm'), "write t82lots.ppm");
256
257 { # RT #24992
258   # the T_IMAGER_FULL_IMAGE typemap entry was returning a blessed
259   # hash with an extra ref, causing memory leaks
260
261   my $im = make_10x10();
262   my $im2 = Imager->new(xsize => 10, ysize => 10);
263   require B;
264   my $imb = B::svref_2object($im);
265   my $im2b = B::svref_2object($im2);
266   is ($imb->REFCNT, $im2b->REFCNT, 
267       "check refcnt of imager object hash between normal and typemap generated");
268 }