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