3 use Test::More tests => 60;
6 use Imager::Test qw(is_image);
10 -d "testout" or mkdir "testout";
12 Imager::init('log'=>'testout/t66paste.log');
14 # the original smoke tests
15 my $img=Imager->new() || die "unable to create image object\n";
17 ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img");
19 my $nimg=Imager->new() or die "Unable to create image object\n";
20 ok($nimg->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img again");
22 ok($img->paste(img=>$nimg, top=>30, left=>30), "paste it")
23 or print "# ", $img->errstr, "\n";;
25 ok($img->write(type=>'pnm',file=>'testout/t66.ppm'), "save it")
26 or print "# ", $img->errstr, "\n";
29 my $empty = Imager->new;
30 ok(!$empty->paste(src => $nimg), "paste into empty image");
31 is($empty->errstr, "paste: empty input image",
32 "check error message");
34 ok(!$img->paste(src => $empty), "paste from empty image");
35 is($img->errstr, "paste: empty input image (for src)",
36 "check error message");
38 ok(!$img->paste(), "no source image");
39 is($img->errstr, "no source image");
42 # more stringent tests
44 my $src = Imager->new(xsize => 100, ysize => 110);
45 $src->box(filled=>1, color=>'FF0000');
47 $src->box(filled=>1, color=>'0000FF', xmin => 20, ymin=>20,
50 my $targ = Imager->new(xsize => 100, ysize => 110);
51 $targ->box(filled=>1, color =>'00FFFF');
52 $targ->box(filled=>1, color=>'00FF00', xmin=>20, ymin=>20, xmax=>79,
54 my $work = $targ->copy;
55 ok($work->paste(src=>$src, left => 15, top => 10), "paste whole image");
56 # build comparison image
57 my $cmp = $targ->copy;
58 $cmp->box(filled=>1, xmin=>15, ymin => 10, color=>'FF0000');
59 $cmp->box(filled=>1, xmin=>35, ymin => 30, xmax=>94, ymax=>89,
62 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
63 "compare pasted and expected");
66 ok($work->paste(src=>$src, left=>2, top=>7, src_minx => 10, src_miny => 15),
67 "paste from inside src");
69 $cmp->box(filled=>1, xmin=>2, ymin=>7, xmax=>91, ymax=>101, color=>'FF0000');
70 $cmp->box(filled=>1, xmin=>12, ymin=>12, xmax=>71, ymax=>71,
72 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
73 "compare pasted and expected");
77 ok($work->paste(src=>$src, left=>15, top=>20,
78 src_minx=>10, src_miny=>15, src_maxx=>80, src_maxy =>70),
79 "paste src cropped all sides");
81 $cmp->box(filled=>1, xmin=>15, ymin=>20, xmax=>84, ymax=>74,
83 $cmp->box(filled=>1, xmin=>25, ymin=>25, xmax=>84, ymax=>74,
85 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
86 "compare pasted and expected");
90 ok($work->paste(src=>$src, left=>15, top=>20,
91 src_minx=>10, src_miny => 15, width => 70, height => 55),
92 "same but specify width/height instead");
93 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
94 "compare pasted and expected");
98 ok($work->paste(src=>$src, left => 15, top => 20,
99 src_coords => [ 10, 15, 80, 70 ]),
101 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
102 "compare pasted and expected");
106 # supplying just src_maxx would set the internal maxy to undef
107 # supplying just src_maxy would be ignored
108 # src_maxy (or it's derived value) was being bounds checked against
109 # the image width instead of the image height
112 local $SIG{__WARN__} = sub { push @warns, "@_"; print "# @_"; };
114 ok($work->paste(src=>$src, left => 15, top => 20,
116 "paste with just src_maxx");
117 ok(!@warns, "shouldn't warn");
118 my $cmp = $targ->copy;
119 $cmp->box(filled=>1, color => 'FF0000', xmin => 15, ymin => 20,
120 xmax => 64, ymax => 109);
121 $cmp->box(filled=>1, color => '0000FF', xmin => 35, ymin => 40,
122 xmax => 64, ymax => 99);
123 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
124 "check correctly pasted");
128 ok($work->paste(src=>$src, left=>15, top=>20,
130 "paste with just src_maxy");
131 ok(!@warns, "shouldn't warn");
133 $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
134 xmax => 99, ymax => 79);
135 $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 40,
136 xmax => 94, ymax => 79);
137 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
138 "check pasted correctly");
142 ok($work->paste(src=>$src, left=>15, top=>20,
143 src_miny => 20, src_maxy => 105),
144 "paste with src_maxy > source width");
147 $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
149 $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 20,
150 xmax => 94, ymax => 79);
151 is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
152 "check pasted correctly");
156 { # https://rt.cpan.org/Ticket/Display.html?id=30908
157 # we now adapt the source channels to the target
158 # check each combination works as expected
160 # various source images
161 my $src1 = Imager->new(xsize => 50, ysize => 50, channels => 1);
162 my $g_grey_full = Imager::Color->new(128, 255, 0, 0);
163 my $g_white_50 = Imager::Color->new(255, 128, 0, 0);
164 $src1->box(filled => 1, xmax => 24, color => $g_grey_full);
166 my $src2 = Imager->new(xsize => 50, ysize => 50, channels => 2);
167 $src2->box(filled => 1, xmax => 24, color => $g_grey_full);
168 $src2->box(filled => 1, xmin => 25, color => $g_white_50);
170 my $c_red_full = Imager::Color->new(255, 0, 0);
171 my $c_blue_full = Imager::Color->new(0, 0, 255);
172 my $src3 = Imager->new(xsize => 50, ysize => 50, channels => 3);
173 $src3->box(filled => 1, xmax => 24, color => $c_red_full);
174 $src3->box(filled => 1, xmin => 25, color => $c_blue_full);
176 my $c_green_50 = Imager::Color->new(0, 255, 0, 127);
177 my $src4 = Imager->new(xsize => 50, ysize => 50, channels => 4);
178 $src4->box(filled => 1, xmax => 24, color => $c_blue_full);
179 $src4->box(filled => 1, xmin => 25, color => $c_green_50);
181 my @left_box = ( box => [ 25, 25, 49, 74 ] );
182 my @right_box = ( box => [ 50, 25, 74, 74 ] );
185 my $base = Imager->new(xsize => 100, ysize => 100, channels => 1);
186 $base->box(filled => 1, color => Imager::Color->new(64, 255, 0, 0));
188 my $work = $base->copy;
189 ok($work->paste(left => 25, top => 25, src => $src1), "paste 1 to 1");
190 my $comp = $base->copy;
191 $comp->box(filled => 1, color => $g_grey_full, @left_box);
192 $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
193 is_image($work, $comp, "compare paste target to expected");
196 ok($work->paste(left => 25, top => 25, src => $src2), "paste 2 to 1");
198 $comp->box(filled => 1, @left_box, color => $g_grey_full);
199 $comp->box(filled => 1, @right_box, color => [ 128, 0, 0, 0 ]);
200 is_image($work, $comp, "compare paste target to expected");
203 ok($work->paste(left => 25, top => 25, src => $src3), "paste 3 to 1");
205 $comp->box(filled => 1, @left_box, color => [ 57, 255, 0, 0 ]);
206 $comp->box(filled => 1, @right_box, color => [ 18, 255, 0, 0 ]);
207 is_image($work, $comp, "compare paste target to expected");
210 ok($work->paste(left => 25, top => 25, src => $src4), "paste 4 to 1");
212 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
213 $comp->box(filled => 1, color => [ 90, 255, 0, 0 ], @right_box);
214 is_image($work, $comp, "compare paste target to expected");
218 my $base = Imager->new(xsize => 100, ysize => 100, channels => 2);
219 $base->box(filled => 1, color => [ 128, 128, 0, 0 ]);
221 my $work = $base->copy;
222 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 2");
223 my $comp = $base->copy;
224 $comp->box(filled => 1, color => $g_grey_full, @left_box);
225 $comp->box(filled => 1, color => [ 0, 255, 0, 0 ], @right_box);
226 is_image($work, $comp, "compare paste target to expected");
229 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 2");
231 $comp->box(filled => 1, color => $g_grey_full, @left_box);
232 $comp->box(filled => 1, color => $g_white_50, @right_box);
233 is_image($work, $comp, "compare paste target to expected");
236 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 2");
238 $comp->box(filled => 1, color => [ 57, 255, 0, 0 ], @left_box);
239 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @right_box);
240 is_image($work, $comp, "compare paste target to expected");
243 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 2");
245 $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
246 $comp->box(filled => 1, color => [ 180, 127, 0, 0 ], @right_box);
247 is_image($work, $comp, "compare paste target to expected");
251 my $base = Imager->new(xsize => 100, ysize => 100, channels => 3);
252 $base->box(filled => 1, color => [ 128, 255, 0, 0 ]);
254 my $work = $base->copy;
255 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 3");
256 my $comp = $base->copy;
257 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
258 $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
259 is_image($work, $comp, "compare paste target to expected");
262 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 3");
264 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
265 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @right_box);
266 is_image($work, $comp, "compare paste target to expected");
269 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 3");
271 $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
272 $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
273 is_image($work, $comp, "compare paste target to expected");
276 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 3");
278 $comp->box(filled => 1, color => [ 0, 0, 255 ], @left_box);
279 $comp->box(filled => 1, color => [ 0, 127, 0 ], @right_box);
280 is_image($work, $comp, "compare paste target to expected");
284 my $base = Imager->new(xsize => 100, ysize => 100, channels => 4);
285 $base->box(filled => 1, color => [ 128, 255, 64, 128 ]);
287 my $work = $base->copy;
288 ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 4");
289 my $comp = $base->copy;
290 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
291 $comp->box(filled => 1, color => [ 0, 0, 0, 255 ], @right_box);
292 is_image($work, $comp, "compare paste target to expected");
295 ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 4");
297 $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
298 $comp->box(filled => 1, color => [ 255, 255, 255, 128 ], @right_box);
299 is_image($work, $comp, "compare paste target to expected");
302 ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 4");
304 $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
305 $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
306 is_image($work, $comp, "compare paste target to expected");
309 ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 4");
311 $comp->box(filled => 1, color => $c_blue_full, @left_box);
312 $comp->box(filled => 1, color => $c_green_50, @right_box);
313 is_image($work, $comp, "compare paste target to expected");