]> git.imager.perl.org - imager.git/blob - t/t66paste.t
only prepend ./ to font filenames when passing them to T1Lib and
[imager.git] / t / t66paste.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 54;
4
5 use Imager;
6 use Imager::Test qw(is_image);
7
8 #$Imager::DEBUG=1;
9
10 Imager::init('log'=>'testout/t66paste.log');
11
12 # the original smoke tests
13 my $img=Imager->new() || die "unable to create image object\n";
14
15 ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img");
16
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");
19
20 ok($img->paste(img=>$nimg, top=>30, left=>30), "paste it")
21   or print "# ", $img->errstr, "\n";;
22
23 ok($img->write(type=>'pnm',file=>'testout/t66.ppm'), "save it")
24   or print "# ", $img->errstr, "\n";
25
26 # more stringent tests
27 {
28   my $src = Imager->new(xsize => 100, ysize => 110);
29   $src->box(filled=>1, color=>'FF0000');
30
31   $src->box(filled=>1, color=>'0000FF', xmin => 20, ymin=>20,
32             xmax=>79, ymax=>79);
33
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,
37              ymax=>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, 
44             color=>'0000FF');
45
46   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
47      "compare pasted and expected");
48
49   $work = $targ->copy;
50   ok($work->paste(src=>$src, left=>2, top=>7, src_minx => 10, src_miny => 15),
51      "paste from inside src");
52   $cmp = $targ->copy;
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, 
55             color=>'0000FF');
56   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
57      "compare pasted and expected");
58
59   # paste part source
60   $work = $targ->copy;
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");
64   $cmp = $targ->copy;
65   $cmp->box(filled=>1, xmin=>15, ymin=>20, xmax=>84, ymax=>74, 
66             color=>'FF0000');
67   $cmp->box(filled=>1, xmin=>25, ymin=>25, xmax=>84, ymax=>74,
68             color=>'0000FF');
69   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
70      "compare pasted and expected");
71
72   # go by width instead
73   $work = $targ->copy;
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");
79
80   # use src_coords
81   $work = $targ->copy;
82   ok($work->paste(src=>$src, left => 15, top => 20,
83                   src_coords => [ 10, 15, 80, 70 ]),
84      "using src_coords");
85   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
86      "compare pasted and expected");
87
88   {
89     # Issue #18712
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
94     $work = $targ->copy;
95     my @warns;
96     local $SIG{__WARN__} = sub { push @warns, "@_"; print "# @_"; };
97     
98     ok($work->paste(src=>$src, left => 15, top => 20,
99                     src_maxx => 50),
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");
109
110     $work = $targ->copy;
111     @warns = ();
112     ok($work->paste(src=>$src, left=>15, top=>20,
113                     src_maxy => 60),
114        "paste with just src_maxy");
115     ok(!@warns, "shouldn't warn");
116     $cmp = $targ->copy;
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");
123
124     $work = $targ->copy;
125     @warns = ();
126     ok($work->paste(src=>$src, left=>15, top=>20,
127                     src_miny => 20, src_maxy => 105),
128        "paste with src_maxy > source width");
129
130     $cmp = $targ->copy;
131     $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
132               ymax => 104);
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");
137   }
138 }
139
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
143
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);
149
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);
153
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);
159
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);
164
165   my @left_box = ( box => [ 25, 25, 49, 74 ] );
166   my @right_box = ( box => [ 50, 25, 74, 74 ] );
167
168   { # 1 channel output
169     my $base = Imager->new(xsize => 100, ysize => 100, channels => 1);
170     $base->box(filled => 1, color => Imager::Color->new(64, 255, 0, 0));
171
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");
178
179     $work = $base->copy;
180     ok($work->paste(left => 25, top => 25, src => $src2), "paste 2 to 1");
181     $comp = $base->copy;
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");
185
186     $work = $base->copy;
187     ok($work->paste(left => 25, top => 25, src => $src3), "paste 3 to 1");
188      $comp = $base->copy;
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");
192
193     $work = $base->copy;
194     ok($work->paste(left => 25, top => 25, src => $src4), "paste 4 to 1");
195     $comp = $base->copy;
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");
199   }
200
201   { # 2 channel output
202     my $base = Imager->new(xsize => 100, ysize => 100, channels => 2);
203     $base->box(filled => 1, color => [ 128, 128, 0, 0 ]);
204     
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");
211
212     $work = $base->copy;
213     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 2");
214     $comp = $base->copy;
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");
218
219     $work = $base->copy;
220     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 2");
221     $comp = $base->copy;
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");
225
226     $work = $base->copy;
227     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 2");
228     $comp = $base->copy;
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");
232   }
233
234   { # 3 channel output
235     my $base = Imager->new(xsize => 100, ysize => 100, channels => 3);
236     $base->box(filled => 1, color => [ 128, 255, 0, 0 ]);
237     
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");
244
245     $work = $base->copy;
246     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 3");
247     $comp = $base->copy;
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");
251
252     $work = $base->copy;
253     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 3");
254     $comp = $base->copy;
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");
258
259     $work = $base->copy;
260     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 3");
261     $comp = $base->copy;
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");
265   }
266
267   { # 4 channel output
268     my $base = Imager->new(xsize => 100, ysize => 100, channels => 4);
269     $base->box(filled => 1, color => [ 128, 255, 64, 128 ]);
270     
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");
277
278     $work = $base->copy;
279     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 4");
280     $comp = $base->copy;
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");
284
285     $work = $base->copy;
286     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 4");
287     $comp = $base->copy;
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");
291
292     $work = $base->copy;
293     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 4");
294     $comp = $base->copy;
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");
298   }
299 }