3 use Test::More tests => 54;
6 use Imager::Test qw(is_image);
10 Imager::init('log'=>'testout/t66paste.log');
12 # the original smoke tests
13 my $img=Imager->new() || die "unable to create image object\n";
15 ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img");
17 my $nimg=Imager->new() or die "Unable to create image object\n";
18 ok($nimg->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img again");
20 ok($img->paste(img=>$nimg, top=>30, left=>30), "paste it")
21 or print "# ", $img->errstr, "\n";;
23 ok($img->write(type=>'pnm',file=>'testout/t66.ppm'), "save it")
24 or print "# ", $img->errstr, "\n";
26 # more stringent tests
28 my $src = Imager->new(xsize => 100, ysize => 110);
29 $src->box(filled=>1, color=>'FF0000');
31 $src->box(filled=>1, color=>'0000FF', xmin => 20, ymin=>20,
34 my $targ = Imager->new(xsize => 100, ysize => 110);
35 $targ->box(filled=>1, color =>'00FFFF');
36 $targ->box(filled=>1, color=>'00FF00', xmin=>20, ymin=>20, xmax=>79,
38 my $work = $targ->copy;
39 ok($work->paste(src=>$src, left => 15, top => 10), "paste whole image");
40 # build comparison image
41 my $cmp = $targ->copy;
42 $cmp->box(filled=>1, xmin=>15, ymin => 10, color=>'FF0000');
43 $cmp->box(filled=>1, xmin=>35, ymin => 30, xmax=>94, ymax=>89,
46 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
47 "compare pasted and expected");
50 ok($work->paste(src=>$src, left=>2, top=>7, src_minx => 10, src_miny => 15),
51 "paste from inside src");
53 $cmp->box(filled=>1, xmin=>2, ymin=>7, xmax=>91, ymax=>101, color=>'FF0000');
54 $cmp->box(filled=>1, xmin=>12, ymin=>12, xmax=>71, ymax=>71,
56 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
57 "compare pasted and expected");
61 ok($work->paste(src=>$src, left=>15, top=>20,
62 src_minx=>10, src_miny=>15, src_maxx=>80, src_maxy =>70),
63 "paste src cropped all sides");
65 $cmp->box(filled=>1, xmin=>15, ymin=>20, xmax=>84, ymax=>74,
67 $cmp->box(filled=>1, xmin=>25, ymin=>25, xmax=>84, ymax=>74,
69 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
70 "compare pasted and expected");
74 ok($work->paste(src=>$src, left=>15, top=>20,
75 src_minx=>10, src_miny => 15, width => 70, height => 55),
76 "same but specify width/height instead");
77 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
78 "compare pasted and expected");
82 ok($work->paste(src=>$src, left => 15, top => 20,
83 src_coords => [ 10, 15, 80, 70 ]),
85 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
86 "compare pasted and expected");
90 # supplying just src_maxx would set the internal maxy to undef
91 # supplying just src_maxy would be ignored
92 # src_maxy (or it's derived value) was being bounds checked against
93 # the image width instead of the image height
96 local $SIG{__WARN__} = sub { push @warns, "@_"; print "# @_"; };
98 ok($work->paste(src=>$src, left => 15, top => 20,
100 "paste with just src_maxx");
101 ok(!@warns, "shouldn't warn");
102 my $cmp = $targ->copy;
103 $cmp->box(filled=>1, color => 'FF0000', xmin => 15, ymin => 20,
104 xmax => 64, ymax => 109);
105 $cmp->box(filled=>1, color => '0000FF', xmin => 35, ymin => 40,
106 xmax => 64, ymax => 99);
107 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
108 "check correctly pasted");
112 ok($work->paste(src=>$src, left=>15, top=>20,
114 "paste with just src_maxy");
115 ok(!@warns, "shouldn't warn");
117 $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
118 xmax => 99, ymax => 79);
119 $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 40,
120 xmax => 94, ymax => 79);
121 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
122 "check pasted correctly");
126 ok($work->paste(src=>$src, left=>15, top=>20,
127 src_miny => 20, src_maxy => 105),
128 "paste with src_maxy > source width");
131 $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
133 $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 20,
134 xmax => 94, ymax => 79);
135 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
136 "check pasted correctly");
140 { # https://rt.cpan.org/Ticket/Display.html?id=30908
141 # we now adapt the source channels to the target
142 # check each combination works as expected
144 # various source images
145 my $src1 = Imager->new(xsize => 50, ysize => 50, channels => 1);
146 my $g_grey_full = Imager::Color->new(128, 255, 0, 0);
147 my $g_white_50 = Imager::Color->new(255, 128, 0, 0);
148 $src1->box(filled => 1, xmax => 24, color => $g_grey_full);
150 my $src2 = Imager->new(xsize => 50, ysize => 50, channels => 2);
151 $src2->box(filled => 1, xmax => 24, color => $g_grey_full);
152 $src2->box(filled => 1, xmin => 25, color => $g_white_50);
154 my $c_red_full = Imager::Color->new(255, 0, 0);
155 my $c_blue_full = Imager::Color->new(0, 0, 255);
156 my $src3 = Imager->new(xsize => 50, ysize => 50, channels => 3);
157 $src3->box(filled => 1, xmax => 24, color => $c_red_full);
158 $src3->box(filled => 1, xmin => 25, color => $c_blue_full);
160 my $c_green_50 = Imager::Color->new(0, 255, 0, 127);
161 my $src4 = Imager->new(xsize => 50, ysize => 50, channels => 4);
162 $src4->box(filled => 1, xmax => 24, color => $c_blue_full);
163 $src4->box(filled => 1, xmin => 25, color => $c_green_50);
165 my @left_box = ( box => [ 25, 25, 49, 74 ] );
166 my @right_box = ( box => [ 50, 25, 74, 74 ] );
169 my $base = Imager->new(xsize => 100, ysize => 100, channels => 1);
170 $base->box(filled => 1, color => Imager::Color->new(64, 255, 0, 0));
172 my $work = $base->copy;
173 ok($work->paste(left => 25, top => 25, src => $src1), "paste 1 to 1");
174 my $comp = $base->copy;
175 $comp->box(filled => 1, color => $g_grey_full, @left_box);
176 $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
177 is_image($work, $comp, "compare paste target to expected");
180 ok($work->paste(left => 25, top => 25, src => $src2), "paste 2 to 1");
182 $comp->box(filled => 1, @left_box, color => $g_grey_full);
183 $comp->box(filled => 1, @right_box, color => [ 128, 0, 0, 0 ]);
184 is_image($work, $comp, "compare paste target to expected");
187 ok($work->paste(left => 25, top => 25, src => $src3), "paste 3 to 1");
189 $comp->box(filled => 1, @left_box, color => [ 57, 255, 0, 0 ]);
190 $comp->box(filled => 1, @right_box, color => [ 18, 255, 0, 0 ]);
191 is_image($work, $comp, "compare paste target to expected");
194 ok($work->paste(left => 25, top => 25, src => $src4), "paste 4 to 1");
196 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
197 $comp->box(filled => 1, color => [ 90, 255, 0, 0 ], @right_box);
198 is_image($work, $comp, "compare paste target to expected");
202 my $base = Imager->new(xsize => 100, ysize => 100, channels => 2);
203 $base->box(filled => 1, color => [ 128, 128, 0, 0 ]);
205 my $work = $base->copy;
206 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 2");
207 my $comp = $base->copy;
208 $comp->box(filled => 1, color => $g_grey_full, @left_box);
209 $comp->box(filled => 1, color => [ 0, 255, 0, 0 ], @right_box);
210 is_image($work, $comp, "compare paste target to expected");
213 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 2");
215 $comp->box(filled => 1, color => $g_grey_full, @left_box);
216 $comp->box(filled => 1, color => $g_white_50, @right_box);
217 is_image($work, $comp, "compare paste target to expected");
220 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 2");
222 $comp->box(filled => 1, color => [ 57, 255, 0, 0 ], @left_box);
223 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @right_box);
224 is_image($work, $comp, "compare paste target to expected");
227 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 2");
229 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
230 $comp->box(filled => 1, color => [ 180, 127, 0, 0 ], @right_box);
231 is_image($work, $comp, "compare paste target to expected");
235 my $base = Imager->new(xsize => 100, ysize => 100, channels => 3);
236 $base->box(filled => 1, color => [ 128, 255, 0, 0 ]);
238 my $work = $base->copy;
239 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 3");
240 my $comp = $base->copy;
241 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
242 $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
243 is_image($work, $comp, "compare paste target to expected");
246 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 3");
248 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
249 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @right_box);
250 is_image($work, $comp, "compare paste target to expected");
253 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 3");
255 $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
256 $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
257 is_image($work, $comp, "compare paste target to expected");
260 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 3");
262 $comp->box(filled => 1, color => [ 0, 0, 255 ], @left_box);
263 $comp->box(filled => 1, color => [ 0, 127, 0 ], @right_box);
264 is_image($work, $comp, "compare paste target to expected");
268 my $base = Imager->new(xsize => 100, ysize => 100, channels => 4);
269 $base->box(filled => 1, color => [ 128, 255, 64, 128 ]);
271 my $work = $base->copy;
272 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 4");
273 my $comp = $base->copy;
274 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
275 $comp->box(filled => 1, color => [ 0, 0, 0, 255 ], @right_box);
276 is_image($work, $comp, "compare paste target to expected");
279 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 4");
281 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
282 $comp->box(filled => 1, color => [ 255, 255, 255, 128 ], @right_box);
283 is_image($work, $comp, "compare paste target to expected");
286 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 4");
288 $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
289 $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
290 is_image($work, $comp, "compare paste target to expected");
293 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 4");
295 $comp->box(filled => 1, color => $c_blue_full, @left_box);
296 $comp->box(filled => 1, color => $c_green_50, @right_box);
297 is_image($work, $comp, "compare paste target to expected");