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