Debian Codegraph @

t/450-api/100-inline.t

raw
1 #!perl -w
2 #
3 # this tests both the Inline interface and the API
4 use strict;
5 use Test::More;
6 use Imager::Test qw(is_color3 is_color4);
7 eval "require Inline::C;";
8 plan skip_all => "Inline required for testing API" if $@;
9
10 eval "require Parse::RecDescent;";
11 plan skip_all => "Could not load Parse::RecDescent" if $@;
12
13 use Cwd 'getcwd';
14 plan skip_all => "Inline won't work in directories with spaces"
15 if getcwd() =~ / /;
16
17 plan skip_all => "perl 5.005_04, 5.005_05 too buggy"
18 if $] =~ /^5\.005_0[45]$/;
19
20 -d "testout" or mkdir "testout";
21
22 Imager->open_log(log => "testout/100-inline.log", loglevel => 4);
23
24 print STDERR "Inline version $Inline::VERSION\n";
25
26 require Inline;
27 Inline->import(with => 'Imager');
28 Inline->import("FORCE"); # force rebuild
29 #Inline->import(C => Config => OPTIMIZE => "-g");
30
31 Inline->bind(C => <<'EOS');
32 #include <math.h>
33
34 int pixel_count(Imager::ImgRaw im) {
35 return im->xsize * im->ysize;
36 }
37
38 int count_color(Imager::ImgRaw im, Imager::Color c) {
39 int count = 0, x, y, chan;
40 i_color read_c;
41
42 for (x = 0; x < im->xsize; ++x) {
43 for (y = 0; y < im->ysize; ++y) {
44 int match = 1;
45 i_gpix(im, x, y, &read_c);
46 for (chan = 0; chan < im->channels; ++chan) {
47 if (read_c.channel[chan] != c->channel[chan]) {
48 match = 0;
49 break;
50 }
51 }
52 if (match)
53 ++count;
54 }
55 }
56
57 return count;
58 }
59
60 Imager make_10x10() {
61 i_img *im = i_img_8_new(10, 10, 3);
62 i_color c;
63 c.channel[0] = c.channel[1] = c.channel[2] = 255;
64 i_box_filled(im, 0, 0, im->xsize-1, im->ysize-1, &c);
65
66 return im;
67 }
68
69 /* tests that all of the APIs are visible - most of them anyway */
70 Imager do_lots(Imager src) {
71 i_img *im = i_img_8_new(100, 100, 3);
72 i_img *fill_im = i_img_8_new(5, 5, 3);
73 i_img *testim;
74 i_color red, blue, green, black, temp_color;
75 i_fcolor redf, bluef;
76 i_fill_t *hatch, *fhatch_fill;
77 i_fill_t *im_fill;
78 i_fill_t *solid_fill, *fsolid_fill;
79 i_fill_t *fount_fill;
80 void *block;
81 double matrix[9] = /* 30 degree rotation */
82 {
83 0.866025, -0.5, 0,
84 0.5, 0.866025, 0,
85 0, 0, 1,
86 };
87 i_fountain_seg fseg;
88 i_img_tags tags;
89 int entry;
90 double temp_double;
91
92 red.channel[0] = 255; red.channel[1] = 0; red.channel[2] = 0;
93 red.channel[3] = 255;
94 blue.channel[0] = 0; blue.channel[1] = 0; blue.channel[2] = 255;
95 blue.channel[3] = 255;
96 green.channel[0] = 0; green.channel[1] = 255; green.channel[2] = 0;
97 green.channel[3] = 255;
98 black.channel[0] = black.channel[1] = black.channel[2] = 0;
99 black.channel[3] = 255;
100 hatch = i_new_fill_hatch(&red, &blue, 0, 1, NULL, 0, 0);
101
102 i_box(im, 0, 0, 9, 9, &red);
103 i_box_filled(im, 10, 0, 19, 9, &blue);
104 i_box_cfill(im, 20, 0, 29, 9, hatch);
105
106 /* make an image fill, and try it */
107 i_box_cfill(fill_im, 0, 0, 4, 4, hatch);
108 im_fill = i_new_fill_image(fill_im, matrix, 2, 2, 0);
109
110 i_box_cfill(im, 30, 0, 39, 9, im_fill);
111
112 /* make a solid fill and try it */
113 solid_fill = i_new_fill_solid(&red, 0);
114 i_box_cfill(im, 40, 0, 49, 9, solid_fill);
115
116 /* floating fills */
117 redf.channel[0] = 1.0; redf.channel[1] = 0; redf.channel[2] = 0;
118 redf.channel[3] = 1.0;
119 bluef.channel[0] = 0; bluef.channel[1] = 0; bluef.channel[2] = 1.0;
120 bluef.channel[3] = 1.0;
121
122 fsolid_fill = i_new_fill_solidf(&redf, 0);
123 i_box_cfill(im, 50, 0, 59, 9, fsolid_fill);
124
125 fhatch_fill = i_new_fill_hatchf(&redf, &bluef, 0, 2, NULL, 0, 0);
126 i_box_cfill(im, 60, 0, 69, 9, fhatch_fill);
127
128 /* fountain fill */
129 fseg.start = 0;
130 fseg.middle = 0.5;
131 fseg.end = 1.0;
132 fseg.c[0] = redf;
133 fseg.c[1] = bluef;
134 fseg.type = i_fst_linear;
135 fseg.color = i_fc_hue_down;
136 fount_fill = i_new_fill_fount(70, 0, 80, 0, i_ft_linear, i_fr_triangle, 0, i_fts_none, 1, 1, &fseg);
137
138 i_box_cfill(im, 70, 0, 79, 9, fount_fill);
139
140 i_line(im, 0, 10, 10, 15, &blue, 1);
141 i_line_aa(im, 0, 19, 10, 15, &red, 1);
142
143 i_arc(im, 15, 15, 4, 45, 160, &blue);
144 i_arc_aa(im, 25, 15, 4, 75, 280, &red);
145 i_arc_cfill(im, 35, 15, 4, 0, 215, hatch);
146 i_arc_aa_cfill(im, 45, 15, 4, 30, 210, hatch);
147 i_circle_aa(im, 55, 15, 4, &red);
148
149 i_box(im, 61, 11, 68, 18, &red);
150 i_flood_fill(im, 65, 15, &blue);
151 i_box(im, 71, 11, 78, 18, &red);
152 i_flood_cfill(im, 75, 15, hatch);
153
154 i_box_filled(im, 1, 21, 9, 24, &red);
155 i_box_filled(im, 1, 25, 9, 29, &blue);
156 i_flood_fill_border(im, 5, 25, &green, &black);
157
158 i_box_filled(im, 11, 21, 19, 24, &red);
159 i_box_filled(im, 11, 25, 19, 29, &blue);
160 i_flood_cfill_border(im, 15, 25, hatch, &black);
161
162 {
163 double x[3];
164 double y[3];
165 i_polygon_t poly;
166 x[0] = 55;
167 y[0] = 25;
168 x[1] = 55;
169 y[1] = 50;
170 x[2] = 70;
171 y[2] = 50;
172 i_poly_aa_m(im, 3, x, y, i_pfm_evenodd, &red);
173 x[2] = 40;
174 i_poly_aa_cfill_m(im, 3, x, y, i_pfm_evenodd, hatch);
175 y[0] = 65;
176 poly.x = x;
177 poly.y = y;
178 poly.count = 3;
179 i_poly_poly_aa(im, 1, &poly, i_pfm_nonzero, &green);
180 x[2] = 70;
181 i_poly_poly_aa_cfill(im, 1, &poly, i_pfm_nonzero, hatch);
182 }
183
184 i_fill_destroy(fount_fill);
185 i_fill_destroy(fhatch_fill);
186 i_fill_destroy(solid_fill);
187 i_fill_destroy(fsolid_fill);
188 i_fill_destroy(hatch);
189 i_fill_destroy(im_fill);
190 i_img_destroy(fill_im);
191
192 /* make sure we can make each image type */
193 testim = i_img_16_new(100, 100, 3);
194 i_img_destroy(testim);
195 testim = i_img_double_new(100, 100, 3);
196 i_img_destroy(testim);
197 testim = i_img_pal_new(100, 100, 3, 256);
198 i_img_destroy(testim);
199 testim = i_sametype(im, 50, 50);
200 i_img_destroy(testim);
201 testim = i_sametype_chans(im, 50, 50, 4);
202 i_img_destroy(testim);
203
204 i_clear_error();
205 i_push_error(0, "Hello");
206 i_push_errorf(0, "%s", "World");
207
208 /* make sure tags create/destroy work */
209 i_tags_new(&tags);
210 i_tags_destroy(&tags);
211
212 block = mymalloc(20);
213 block = myrealloc(block, 50);
214 myfree(block);
215
216 i_tags_set(&im->tags, "lots_string", "foo", -1);
217 i_tags_setn(&im->tags, "lots_number", 101);
218
219 if (!i_tags_find(&im->tags, "lots_number", 0, &entry)) {
220 i_push_error(0, "lots_number tag not found");
221 i_img_destroy(im);
222 return NULL;
223 }
224 i_tags_delete(&im->tags, entry);
225
226 /* these won't delete anything, but it makes sure the macros and function
227 pointers are correct */
228 i_tags_delbyname(&im->tags, "unknown");
229 i_tags_delbycode(&im->tags, 501);
230 i_tags_set_float(&im->tags, "lots_float", 0, 3.14);
231 if (!i_tags_get_float(&im->tags, "lots_float", 0, &temp_double)) {
232 i_push_error(0, "lots_float not found");
233 i_img_destroy(im);
234 return NULL;
235 }
236 if (fabs(temp_double - 3.14) > 0.001) {
237 i_push_errorf(0, "lots_float incorrect %g", temp_double);
238 i_img_destroy(im);
239 return NULL;
240 }
241 i_tags_set_float2(&im->tags, "lots_float2", 0, 100 * sqrt(2.0), 5);
242 if (!i_tags_get_int(&im->tags, "lots_float2", 0, &entry)) {
243 i_push_error(0, "lots_float2 not found as int");
244 i_img_destroy(im);
245 return NULL;
246 }
247 if (entry != 141) {
248 i_push_errorf(0, "lots_float2 unexpected value %d", entry);
249 i_img_destroy(im);
250 return NULL;
251 }
252
253 i_tags_set_color(&im->tags, "lots_color", 0, &red);
254 if (!i_tags_get_color(&im->tags, "lots_color", 0, &temp_color)) {
255 i_push_error(0, "lots_color not found as color");
256 i_img_destroy(im);
257 return NULL;
258 }
259
260 return im;
261 }
262
263 void
264 io_fd(int fd) {
265 Imager::IO io = io_new_fd(fd);
266 i_io_write(io, "test", 4);
267 i_io_close(io);
268 io_glue_destroy(io);
269 }
270
271 int
272 io_bufchain_test() {
273 Imager::IO io = io_new_bufchain();
274 unsigned char *result;
275 size_t size;
276 if (i_io_write(io, "test2", 5) != 5) {
277 fprintf(stderr, "write failed\n");
278 return 0;
279 }
280 if (!i_io_flush(io)) {
281 fprintf(stderr, "flush failed\n");
282 return 0;
283 }
284 if (i_io_close(io) != 0) {
285 fprintf(stderr, "close failed\n");
286 return 0;
287 }
288 size = io_slurp(io, &result);
289 if (size != 5) {
290 fprintf(stderr, "wrong size\n");
291 return 0;
292 }
293 if (memcmp(result, "test2", 5)) {
294 fprintf(stderr, "data mismatch\n");
295 return 0;
296 }
297 if (i_io_seek(io, 0, 0) != 0) {
298 fprintf(stderr, "seek failure\n");
299 return 0;
300 }
301 myfree(result);
302 io_glue_destroy(io);
303
304 return 1;
305 }
306
307 const char *
308 io_buffer_test(SV *in) {
309 STRLEN len;
310 const char *in_str = SvPV(in, len);
311 static char buf[100];
312 Imager::IO io = io_new_buffer(in_str, len, NULL, NULL);
313 ssize_t read_size;
314
315 read_size = i_io_read(io, buf, sizeof(buf)-1);
316 io_glue_destroy(io);
317 if (read_size < 0 || read_size >= sizeof(buf)) {
318 return "";
319 }
320
321 buf[read_size] = '\0';
322
323 return buf;
324 }
325
326 const char *
327 io_peekn_test(SV *in) {
328 STRLEN len;
329 const char *in_str = SvPV(in, len);
330 static char buf[100];
331 Imager::IO io = io_new_buffer(in_str, len, NULL, NULL);
332 ssize_t read_size;
333
334 read_size = i_io_peekn(io, buf, sizeof(buf)-1);
335 io_glue_destroy(io);
336 if (read_size < 0 || read_size >= sizeof(buf)) {
337 return "";
338 }
339
340 buf[read_size] = '\0';
341
342 return buf;
343 }
344
345 const char *
346 io_gets_test(SV *in) {
347 STRLEN len;
348 const char *in_str = SvPV(in, len);
349 static char buf[100];
350 Imager::IO io = io_new_buffer(in_str, len, NULL, NULL);
351 ssize_t read_size;
352
353 read_size = i_io_gets(io, buf, sizeof(buf), 's');
354 io_glue_destroy(io);
355 if (read_size < 0 || read_size >= sizeof(buf)) {
356 return "";
357 }
358
359 return buf;
360 }
361
362 int
363 io_getc_test(SV *in) {
364 STRLEN len;
365 const char *in_str = SvPV(in, len);
366 static char buf[100];
367 Imager::IO io = io_new_buffer(in_str, len, NULL, NULL);
368 int result;
369
370 result = i_io_getc(io);
371 io_glue_destroy(io);
372
373 return result;
374 }
375
376 int
377 io_peekc_test(SV *in) {
378 STRLEN len;
379 const char *in_str = SvPV(in, len);
380 static char buf[100];
381 Imager::IO io = io_new_buffer(in_str, len, NULL, NULL);
382 int result;
383
384 i_io_set_buffered(io, 0);
385
386 result = i_io_peekc(io);
387 io_glue_destroy(io);
388
389 return result;
390 }
391
392
393
394 int
395 test_render_color(Imager work_8) {
396 i_render *r8;
397 i_color c;
398 unsigned char render_coverage[3];
399
400 render_coverage[0] = 0;
401 render_coverage[1] = 128;
402 render_coverage[2] = 255;
403
404 r8 = i_render_new(work_8, 10);
405 c.channel[0] = 128;
406 c.channel[1] = 255;
407 c.channel[2] = 0;
408 c.channel[3] = 255;
409 i_render_color(r8, 0, 0, sizeof(render_coverage), render_coverage, &c);
410
411 c.channel[3] = 128;
412 i_render_color(r8, 0, 1, sizeof(render_coverage), render_coverage, &c);
413
414 c.channel[3] = 0;
415 i_render_color(r8, 0, 2, sizeof(render_coverage), render_coverage, &c);
416
417 i_render_delete(r8);
418
419 return 1;
420 }
421
422 int
423 raw_psamp(Imager im, int chan_count) {
424 static i_sample_t samps[] = { 0, 127, 255 };
425
426 i_clear_error();
427 return i_psamp(im, 0, 1, 0, samps, NULL, chan_count);
428 }
429
430 int
431 raw_psampf(Imager im, int chan_count) {
432 static i_fsample_t samps[] = { 0, 0.5, 1.0 };
433
434 i_clear_error();
435 return i_psampf(im, 0, 1, 0, samps, NULL, chan_count);
436 }
437
438 int
439 test_mutex() {
440 i_mutex_t m;
441
442 m = i_mutex_new();
443 i_mutex_lock(m);
444 i_mutex_unlock(m);
445 i_mutex_destroy(m);
446
447 return 1;
448 }
449
450 int
451 test_slots() {
452 im_slot_t slot = im_context_slot_new(NULL);
453
454 if (im_context_slot_get(aIMCTX, slot)) {
455 fprintf(stderr, "slots should default to NULL\n");
456 return 0;
457 }
458 if (!im_context_slot_set(aIMCTX, slot, &slot)) {
459 fprintf(stderr, "set slot failed\n");
460 return 0;
461 }
462
463 if (im_context_slot_get(aIMCTX, slot) != &slot) {
464 fprintf(stderr, "get slot didn't match\n");
465 return 0;
466 }
467
468 return 1;
469 }
470
471 int
472 color_channels(Imager im) {
473 return i_img_color_channels(im);
474 }
475
476 int
477 color_model(Imager im) {
478 return (int)i_img_color_model(im);
479 }
480
481 int
482 alpha_channel(Imager im) {
483 int channel;
484 if (!i_img_alpha_channel(im, &channel))
485 channel = -1;
486
487 return channel;
488 }
489
490 int
491 tags_get_int(Imager im, const char *name) {
492 int val;
493 if (i_tags_get_int(&im->tags, name, 0, &val))
494 return val;
495 else
496 return -1;
497 }
498
499 int
500 tags_is_int(Imager im, const char *name) {
501 int index;
502 i_img_tag *tag;
503 if (!i_tags_find(&im->tags, name, 0, &index))
504 return 0;
505 tag = im->tags.tags + index;
506 if (tag->data)
507 return 0;
508 return 1;
509 }
510
511 int
512 decode_exif(Imager im, SV *data) {
513 SvGETMAGIC(data);
514 if (!SvOK(data)) return 0;
515 STRLEN len;
516 const char *pv = SvPV(data, len);
517 i_clear_error();
518
519 return im_decode_exif(im, (const unsigned char *)pv, len);
520 }
521
522 int
523 raw_ppal_one(Imager im, i_img_dim x, i_img_dim y, int index) {
524 i_palidx pindex = index;
525 return i_ppal(im, x, x+1, y, &pindex);
526 }
527
528 int
529 raw_gpix(Imager im, i_img_dim x, i_img_dim y, Imager::Color val) {
530 return i_gpix(im, x, y, val);
531 }
532
533 int
534 raw_glin_one(Imager im, i_img_dim x, i_img_dim y, Imager::Color val) {
535 return i_glin(im, x, x+1, y, val);
536 }
537
538 int
539 raw_gsamp_one(Imager im, i_img_dim x, i_img_dim y, SV *sv) {
540 i_sample_t samps[MAXCHANNELS];
541 int count = i_gsamp(im, x, x+1, y, samps, NULL, im->channels);
542 sv_setpvn(sv, samps, count);
543 return count;
544 }
545
546 EOS
547
548 my $im = Imager->new(xsize=>50, ysize=>50);
549 is(pixel_count($im), 2500, "pixel_count");
550
551 my $black = Imager::Color->new(0,0,0);
552 is(count_color($im, $black), 2500, "count_color black on black image");
553
554 my $im2 = make_10x10();
555 my $white = Imager::Color->new(255, 255, 255);
556 is(count_color($im2, $white), 100, "check new image white count");
557 ok($im2->box(filled=>1, xmin=>1, ymin=>1, xmax => 8, ymax=>8, color=>$black),
558 "try new image");
559 is(count_color($im2, $black), 64, "check modified black count");
560 is(count_color($im2, $white), 36, "check modified white count");
561
562 my $im3 = do_lots($im2);
563 ok($im3, "do_lots()")
564 or print "# ", Imager->_error_as_msg, "\n";
565 ok($im3->write(file=>'testout/t82lots.ppm'), "write t82lots.ppm");
566
567 { # RT #24992
568 # the T_IMAGER_FULL_IMAGE typemap entry was returning a blessed
569 # hash with an extra ref, causing memory leaks
570
571 my $im = make_10x10();
572 my $im2 = Imager->new(xsize => 10, ysize => 10);
573 require B;
574 my $imb = B::svref_2object($im);
575 my $im2b = B::svref_2object($im2);
576 is ($imb->REFCNT, $im2b->REFCNT,
577 "check refcnt of imager object hash between normal and typemap generated");
578 }
579
580 SKIP:
581 {
582 my $fd_filename = "testout/t82fd.txt";
583 {
584 open my $fh, ">",$fd_filename
585 or skip("Can't create file: $!", 1);
586 io_fd(fileno($fh));
587 $fh->close;
588 }
589 {
590 open my $fh, "<", $fd_filename
591 or skip("Can't open file: $!", 1);
592 my $data = <$fh>;
593 is($data, "test", "make sure data written to fd");
594 }
595 unlink $fd_filename;
596 }
597
598 ok(io_bufchain_test(), "check bufchain functions");
599
600 is(io_buffer_test("test3"), "test3", "check io_new_buffer() and i_io_read");
601
602 is(io_peekn_test("test5"), "test5", "check i_io_peekn");
603
604 is(io_gets_test("test"), "tes", "check i_io_gets()");
605
606 is(io_getc_test("ABC"), ord "A", "check i_io_getc(_imp)?");
607
608 is(io_getc_test("XYZ"), ord "X", "check i_io_peekc(_imp)?");
609
610 for my $bits (8, 16) {
611 print "# bits: $bits\n";
612
613 # the floating point processing is a little more accurate
614 my $bump = $bits == 16 ? 1 : 0;
615 {
616 my $im = Imager->new(xsize => 10, ysize => 10, channels => 3, bits => $bits);
617 ok($im->box(filled => 1, color => '#808080'), "fill work image with gray");
618 ok(test_render_color($im),
619 "call render_color on 3 channel image");
620 is_color3($im->getpixel(x => 0, y => 0), 128, 128, 128,
621 "check zero coverage, alpha 255 color, bits $bits");
622 is_color3($im->getpixel(x => 1, y => 0), 128, 191+$bump, 63+$bump,
623 "check 128 coverage, alpha 255 color, bits $bits");
624 is_color3($im->getpixel(x => 2, y => 0), 128, 255, 0,
625 "check 255 coverage, alpha 255 color, bits $bits");
626
627 is_color3($im->getpixel(x => 0, y => 1), 128, 128, 128,
628 "check zero coverage, alpha 128 color, bits $bits");
629 is_color3($im->getpixel(x => 1, y => 1), 128, 159+$bump, 95+$bump,
630 "check 128 coverage, alpha 128 color, bits $bits");
631 is_color3($im->getpixel(x => 2, y => 1), 128, 191+$bump, 63+$bump,
632 "check 255 coverage, alpha 128 color, bits $bits");
633
634 is_color3($im->getpixel(x => 0, y => 2), 128, 128, 128,
635 "check zero coverage, alpha 0 color, bits $bits");
636 is_color3($im->getpixel(x => 1, y => 2), 128, 128, 128,
637 "check 128 coverage, alpha 0 color, bits $bits");
638 is_color3($im->getpixel(x => 2, y => 2), 128, 128, 128,
639 "check 255 coverage, alpha 0 color, bits $bits");
640 }
641 {
642 my $im = Imager->new(xsize => 10, ysize => 10, channels => 4, bits => $bits);
643 ok($im->box(filled => 1, color => '#808080'), "fill work image with opaque gray");
644 ok(test_render_color($im),
645 "call render_color on 4 channel image");
646 is_color4($im->getpixel(x => 0, y => 0), 128, 128, 128, 255,
647 "check zero coverage, alpha 255 color, bits $bits");
648 is_color4($im->getpixel(x => 1, y => 0), 128, 191+$bump, 63+$bump, 255,
649 "check 128 coverage, alpha 255 color, bits $bits");
650 is_color4($im->getpixel(x => 2, y => 0), 128, 255, 0, 255,
651 "check 255 coverage, alpha 255 color, bits $bits");
652
653 is_color4($im->getpixel(x => 0, y => 1), 128, 128, 128, 255,
654 "check zero coverage, alpha 128 color, bits $bits");
655 is_color4($im->getpixel(x => 1, y => 1), 128, 159+$bump, 95+$bump, 255,
656 "check 128 coverage, alpha 128 color, bits $bits");
657 is_color4($im->getpixel(x => 2, y => 1), 128, 191+$bump, 63+$bump, 255,
658 "check 255 coverage, alpha 128 color, bits $bits");
659
660 is_color4($im->getpixel(x => 0, y => 2), 128, 128, 128, 255,
661 "check zero coverage, alpha 0 color, bits $bits");
662 is_color4($im->getpixel(x => 1, y => 2), 128, 128, 128, 255,
663 "check 128 coverage, alpha 0 color, bits $bits");
664 is_color4($im->getpixel(x => 2, y => 2), 128, 128, 128, 255,
665 "check 255 coverage, alpha 0 color, bits $bits");
666 }
667
668 {
669 my $im = Imager->new(xsize => 10, ysize => 10, channels => 4, bits => $bits);
670 ok($im->box(filled => 1, color => Imager::Color->new(128, 128, 128, 64)), "fill work image with translucent gray");
671 ok(test_render_color($im),
672 "call render_color on 4 channel image");
673 is_color4($im->getpixel(x => 0, y => 0), 128, 128, 128, 64,
674 "check zero coverage, alpha 255 color, bits $bits");
675 is_color4($im->getpixel(x => 1, y => 0), 128, 230, 25+$bump, 159+$bump,
676 "check 128 coverage, alpha 255 color, bits $bits");
677 is_color4($im->getpixel(x => 2, y => 0), 128, 255, 0, 255,
678 "check 255 coverage, alpha 255 color, bits $bits");
679
680 is_color4($im->getpixel(x => 0, y => 1), 128, 128, 128, 64,
681 "check zero coverage, alpha 128 color, bits $bits");
682 is_color4($im->getpixel(x => 1, y => 1), 129-$bump, 202-$bump, 55, 111+$bump,
683 "check 128 coverage, alpha 128 color, bits $bits");
684 is_color4($im->getpixel(x => 2, y => 1), 128, 230, 25+$bump, 159+$bump,
685 "check 255 coverage, alpha 128 color, bits $bits");
686
687 is_color4($im->getpixel(x => 0, y => 2), 128, 128, 128, 64,
688 "check zero coverage, alpha 0 color, bits $bits");
689 is_color4($im->getpixel(x => 1, y => 2), 128, 128, 128, 64,
690 "check 128 coverage, alpha 0 color, bits $bits");
691 is_color4($im->getpixel(x => 2, y => 2), 128, 128, 128, 64,
692 "check 255 coverage, alpha 0 color, bits $bits");
693 }
694 }
695
696 {
697 my $im = Imager->new(xsize => 10, ysize => 10);
698 is(raw_psamp($im, 4), -1, "bad channel list (4) for psamp should fail");
699 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
700 "check message");
701 is(raw_psamp($im, 0), -1, "bad channel list (0) for psamp should fail");
702 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
703 "check message");
704 is(raw_psampf($im, 4), -1, "bad channel list (4) for psampf should fail");
705 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
706 "check message");
707 is(raw_psampf($im, 0), -1, "bad channel list (0) for psampf should fail");
708 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
709 "check message");
710 }
711
712 {
713 my $im = Imager->new(xsize => 10, ysize => 10, bits => 16);
714 is(raw_psamp($im, 4), -1, "bad channel list (4) for psamp should fail (16-bit)");
715 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
716 "check message");
717 is(raw_psamp($im, 0), -1, "bad channel list (0) for psamp should fail (16-bit)");
718 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
719 "check message");
720 is(raw_psampf($im, 4), -1, "bad channel list (4) for psampf should fail (16-bit)");
721 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
722 "check message");
723 is(raw_psampf($im, 0), -1, "bad channel list (0) for psampf should fail (16-bit)");
724 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
725 "check message");
726 }
727
728 {
729 my $im = Imager->new(xsize => 10, ysize => 10, bits => 'double');
730 is(raw_psamp($im, 4), -1, "bad channel list (4) for psamp should fail (double)");
731 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
732 "check message");
733 is(raw_psamp($im, 0), -1,, "bad channel list (0) for psamp should fail (double)");
734 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
735 "check message");
736 is(raw_psampf($im, 4), -1, "bad channel list (4) for psampf should fail (double)");
737 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
738 "check message");
739 is(raw_psampf($im, 0), -1, "bad channel list (0) for psampf should fail (double)");
740 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
741 "check message");
742 }
743
744 {
745 my $im = Imager->new(xsize => 10, ysize => 10, type => "paletted");
746 is(raw_psamp($im, 4), -1, "bad channel list (4) for psamp should fail (paletted)");
747 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
748 "check message");
749 is(raw_psamp($im, 0), -1, "bad channel list (0) for psamp should fail (paletted)");
750 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
751 "check message");
752 is(raw_psampf($im, 4), -1, "bad channel list (4) for psampf should fail (paletted)");
753 is(_get_error(), "chan_count 4 out of range, must be >0, <= channels",
754 "check message");
755 is(raw_psampf($im, 0), -1, "bad channel list (0) for psampf should fail (paletted)");
756 is(_get_error(), "chan_count 0 out of range, must be >0, <= channels",
757 "check message");
758 is($im->type, "paletted", "make sure we kept the image type");
759 }
760
761 {
762 my $rgb = Imager->new(xsize => 10, ysize => 10);
763 is(color_model($rgb), 3, "check i_img_color_model() api");
764 is(color_channels($rgb), 3, "check i_img_color_channels() api");
765 is(alpha_channel($rgb), -1, "check i_img_alpha_channel() api");
766 }
767
768 for my $bad_index (1, 255) {
769 my $im = Imager->new(xsize => 2, ysize => 2, type => "paletted");
770 ok($im, "make paletted image $bad_index");
771 ok(defined $im->addcolors(colors => [ [ 10, 20, 30 ] ]),
772 "add just one color $bad_index");
773 ok(raw_ppal_one($im, 0, 0, 0), "set a valid index $bad_index");
774 ok(raw_ppal_one($im, 1, 0, $bad_index), "set a bad index $bad_index");
775 my $c = Imager::Color->new( 20, 30, 40 );
776 is(raw_gpix($im, 1, 0, $c), -1, "read the bad pixel $bad_index");
777 is_color3($c, 0, 0, 0, "gpix: bad index should be black $bad_index");
778 $c = Imager::Color->new( 20, 30, 40 );
779 is(raw_glin_one($im, 1, 0, $c), 1, "read via glin $bad_index");
780 is_color3($c, 0, 0, 0, "glin: bad index should be black $bad_index");
781 my $samps;
782 is(raw_gsamp_one($im, 1, 0, $samps), 3, "correct sample count $bad_index");
783 is(unpack("H*", $samps), "000000", "gsamp: bad index black $bad_index");
784 }
785
786 ok(test_mutex(), "call mutex APIs");
787
788 ok(test_slots(), "call slot APIs");
789
790 {
791 use POSIX qw(UINT_MAX INT_MAX INT_MIN);
792 my $im = Imager->new(xsize => 1, ysize => 1);
793 $im->addtag(name => "intmax", value => INT_MAX);
794 $im->addtag(name => "intmin", value => INT_MIN);
795 $im->addtag(name => "uintmax", value => UINT_MAX);
796 is(tags_get_int($im, "intmax"), INT_MAX);
797 ok(tags_is_int($im, "intmax"), "intmax stored as int");
798 is(tags_get_int($im, "intmin"), INT_MIN);
799 ok(tags_is_int($im, "intmin"), "intmin stored as int");
800 is(tags_get_int($im, "uintmax"), -1);
801 ok(!tags_is_int($im, "uintmax"), "uintmax not stored as int");
802 }
803
804 {
805 my $im = Imager->new(xsize => 1, ysize => 1);
806 my $ok = decode_exif($im, "not exif");
807 ok(!$ok, "fail to parse non-exif");
808 my @tags = grep $_->[0] =~ /^exif_/, $im->tags;
809 is_deeply(\@tags, [], "decode_exif obviously not exif");
810
811 ($im, $ok) = do_one_exif("t/data/exifgood.bin");
812 is($im->tags(name => "exif_make"), "Canon", "valid exif model");
813 is($im->tags(name => "exif_artist"), "Tony Cook", "valid exif artist");
814
815 ($im, $ok) = do_one_exif("t/data/exifbadoff.bin");
816 ok(!$ok, "fail to load exif with bad offset");
817
818 ($im, $ok) = do_one_exif("t/data/exifbadderoff.bin");
819 ok(!$ok, "fail to load exif with very large offset");
820
821 ($im, $ok) = do_one_exif("t/data/exifbadifdoff1.bin");
822 ok(!$ok, "fail to load exif with bad exif ifd offset");
823
824 ($im, $ok) = do_one_exif("t/data/exifbadifdstart.bin");
825 ok(!$ok, "fail to load exif with bad exif ifd0 start offset");
826
827 ($im, $ok) = do_one_exif("t/data/exifbadifdstart2.bin");
828 ok(!$ok, "fail to load exif with bad exif exififd start offset");
829
830 ($im, $ok) = do_one_exif("t/data/exifbad0ascii.bin");
831 ok($ok, "load exif with zero length ascii")
832 or diag(Imager->_error_as_msg);
833 is($im->tags(name => "exif_image_description"), "",
834 "read invalid zero length ascii as zero length string");
835
836 ($im, $ok) = do_one_exif("t/data/exifattack.bin");
837 ok($ok, "load exif with zero length ascii (POC)")
838 or diag(Imager->_error_as_msg);
839 is($im->tags(name => "exif_make"), "",
840 "read invalid zero length ascii as zero length string (POC)");
841
842 ($im, $ok) = do_one_exif("t/data/exifbadascoff.bin");
843 ok(!$ok, "fail to load exif with ascii bad offset")
844 or diag(Imager->_error_as_msg);
845
846 ($im, $ok) = do_one_exif("t/data/exif32overcnt.bin");
847 ok(!$ok, "fail to load exif with 32-bit overflow count")
848 or diag(Imager->_error_as_msg);
849
850 ($im, $ok) = do_one_exif("t/data/exif32oversz.bin");
851 ok(!$ok, "fail to load exif with 32-bit overflow sz")
852 or diag(Imager->_error_as_msg);
853 }
854
855 sub do_one_exif {
856 my ($exif_name) = @_;
857
858 open my $exif_fh, "<", $exif_name
859 or die "Cannot open $exif_name: $!";
860 binmode $exif_fh;
861 my $exif_data = do { local $/; <$exif_fh> };
862 close $exif_fh;
863 $im = Imager->new(xsize => 1, ysize => 1);
864 Imager::i_log_entry("Testing $exif_name", 2);
865 my $ok = decode_exif($im, $exif_data);
866
867 return ($im, $ok);
868 }
869
870 done_testing();
871
872 Imager->close_log();
873
874 sub _get_error {
875 my @errors = Imager::i_errors();
876 return join(": ", map $_->[0], @errors);
877 }