Added extra parameters to rubthrough so only a subimage of
[imager.git] / t / t69rubthru.t
CommitLineData
faa9b3e7
TC
1#!perl -w
2
3BEGIN { $| = 1; print "1..23\n"; }
4END {print "not ok 1\n" unless $loaded;}
5use Imager qw(:all :handy);
6$loaded = 1;
7print "ok 1\n";
8init_log("testout/t69rubthru.log", 1);
9
71dc4a83
AMH
10my $src_height = 80;
11my $src_width = 80;
12
faa9b3e7
TC
13# raw interface
14my $targ = Imager::ImgRaw::new(100, 100, 3);
71dc4a83 15my $src = Imager::ImgRaw::new($src_height, $src_width, 4);
faa9b3e7
TC
16my $halfred = NC(255, 0, 0, 128);
17i_box_filled($src, 20, 20, 60, 60, $halfred);
71dc4a83 18i_rubthru($targ, $src, 10, 10, 0, 0, $src_width, $src_height) or print "not ";
faa9b3e7
TC
19print "ok 2\n";
20my $c = Imager::i_get_pixel($targ, 10, 10) or print "not ";
21print "ok 3\n";
22color_cmp($c, NC(0, 0, 0)) == 0 or print "not ";
23print "ok 4\n";
24$c = Imager::i_get_pixel($targ, 30, 30) or print "not ";
25print "ok 5\n";
26color_cmp($c, NC(128, 0, 0)) == 0 or print "not ";
27print "ok 6\n";
28
29my $black = NC(0, 0, 0);
30# reset the target and try a grey+alpha source
31i_box_filled($targ, 0, 0, 100, 100, $black);
71dc4a83 32my $gsrc = Imager::ImgRaw::new($src_width, $src_height, 2);
faa9b3e7
TC
33my $halfwhite = NC(255, 128, 0);
34i_box_filled($gsrc, 20, 20, 60, 60, $halfwhite);
71dc4a83 35i_rubthru($targ, $gsrc, 10, 10, 0, 0, $src_width, $src_height) or print "not ";
faa9b3e7
TC
36print "ok 7\n";
37$c = Imager::i_get_pixel($targ, 15, 15) or print "not ";
38print "ok 8\n";
39color_cmp($c, NC(0, 0, 0)) == 0 or print "not ";
40print "ok 9\n";
41$c = Imager::i_get_pixel($targ, 30, 30) or print "not ";
42print "ok 10\n";
43color_cmp($c, NC(128, 128, 128)) == 0 or print "not ";
44print "ok 11\n";
45
46# try grey target and grey alpha source
47my $gtarg = Imager::ImgRaw::new(100, 100, 1);
71dc4a83 48i_rubthru($gtarg, $gsrc, 10, 10, 0, 0, $src_width, $src_height) or print "not ";
faa9b3e7
TC
49print "ok 12\n";
50$c = Imager::i_get_pixel($gtarg, 10, 10) or print "not ";
51print "ok 13\n";
52($c->rgba)[0] == 0 or print "not ";
53print "ok 14\n";
54(Imager::i_get_pixel($gtarg, 30, 30)->rgba)[0] == 128 or print "not ";
55print "ok 15\n";
56
57# an attempt rub a 4 channel image over 1 channel should fail
71dc4a83 58i_rubthru($gtarg, $src, 10, 10, 0, 0, $src_width, $src_height) and print "not ";
faa9b3e7
TC
59print "ok 16\n";
60
61# simple test for 16-bit/sample images
62my $targ16 = Imager::i_img_16_new(100, 100, 3);
71dc4a83 63i_rubthru($targ16, $src, 10, 10, 0, 0, $src_width, $src_height) or print "not ";
faa9b3e7
TC
64print "ok 17\n";
65$c = Imager::i_get_pixel($targ16, 30, 30) or print "not ";
66print "ok 18\n";
67color_cmp($c, NC(128, 0, 0)) == 0 or print "not ";
68print "ok 19\n";
69
70# check the OO interface
71my $ootarg = Imager->new(xsize=>100, ysize=>100);
72my $oosrc = Imager->new(xsize=>80, ysize=>80, channels=>4);
71dc4a83 73$oosrc->box(color=>$halfred, xmin=>20, ymin=>20, xmax=>60, ymax=>60,
faa9b3e7
TC
74 filled=>1);
75$ootarg->rubthrough(src=>$oosrc, tx=>10, ty=>10) or print "not ";
76print "ok 20\n";
77color_cmp(Imager::i_get_pixel($ootarg->{IMG}, 10, 10), NC(0, 0, 0)) == 0
78 or print "not ";
79print "ok 21\n";
80color_cmp(Imager::i_get_pixel($ootarg->{IMG}, 30, 30), NC(128, 0, 0)) == 0
81 or print "not ";
82print "ok 22\n";
83
84# make sure we fail as expected
85my $oogtarg = Imager->new(xsize=>100, ysize=>100, channels=>1);
86$oogtarg->rubthrough(src=>$oosrc) and print "not ";
87print "ok 23\n";
88
89
90sub color_cmp {
91 my ($l, $r) = @_;
92 my @l = $l->rgba;
93 my @r = $r->rgba;
94 print "# (",join(",", @l[0..2]),") <=> (",join(",", @r[0..2]),")\n";
95 return $l[0] <=> $r[0]
96 || $l[1] <=> $r[1]
97 || $l[2] <=> $r[2];
98}
99
100