]> git.imager.perl.org - imager.git/blob - t/t66paste.t
test image file limits are thread localized
[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 -d "testout" or mkdir "testout";
11
12 Imager::init('log'=>'testout/t66paste.log');
13
14 # the original smoke tests
15 my $img=Imager->new() || die "unable to create image object\n";
16
17 ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "load test img");
18
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");
21
22 ok($img->paste(img=>$nimg, top=>30, left=>30), "paste it")
23   or print "# ", $img->errstr, "\n";;
24
25 ok($img->write(type=>'pnm',file=>'testout/t66.ppm'), "save it")
26   or print "# ", $img->errstr, "\n";
27
28 # more stringent tests
29 {
30   my $src = Imager->new(xsize => 100, ysize => 110);
31   $src->box(filled=>1, color=>'FF0000');
32
33   $src->box(filled=>1, color=>'0000FF', xmin => 20, ymin=>20,
34             xmax=>79, ymax=>79);
35
36   my $targ = Imager->new(xsize => 100, ysize => 110);
37   $targ->box(filled=>1, color =>'00FFFF');
38   $targ->box(filled=>1, color=>'00FF00', xmin=>20, ymin=>20, xmax=>79,
39              ymax=>79);
40   my $work = $targ->copy;
41   ok($work->paste(src=>$src, left => 15, top => 10), "paste whole image");
42   # build comparison image
43   my $cmp = $targ->copy;
44   $cmp->box(filled=>1, xmin=>15, ymin => 10, color=>'FF0000');
45   $cmp->box(filled=>1, xmin=>35, ymin => 30, xmax=>94, ymax=>89, 
46             color=>'0000FF');
47
48   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
49      "compare pasted and expected");
50
51   $work = $targ->copy;
52   ok($work->paste(src=>$src, left=>2, top=>7, src_minx => 10, src_miny => 15),
53      "paste from inside src");
54   $cmp = $targ->copy;
55   $cmp->box(filled=>1, xmin=>2, ymin=>7, xmax=>91, ymax=>101, color=>'FF0000');
56   $cmp->box(filled=>1, xmin=>12, ymin=>12, xmax=>71, ymax=>71, 
57             color=>'0000FF');
58   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
59      "compare pasted and expected");
60
61   # paste part source
62   $work = $targ->copy;
63   ok($work->paste(src=>$src, left=>15, top=>20, 
64                   src_minx=>10, src_miny=>15, src_maxx=>80, src_maxy =>70),
65      "paste src cropped all sides");
66   $cmp = $targ->copy;
67   $cmp->box(filled=>1, xmin=>15, ymin=>20, xmax=>84, ymax=>74, 
68             color=>'FF0000');
69   $cmp->box(filled=>1, xmin=>25, ymin=>25, xmax=>84, ymax=>74,
70             color=>'0000FF');
71   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
72      "compare pasted and expected");
73
74   # go by width instead
75   $work = $targ->copy;
76   ok($work->paste(src=>$src, left=>15, top=>20,
77                   src_minx=>10, src_miny => 15, width => 70, height => 55),
78      "same but specify width/height instead");
79   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
80      "compare pasted and expected");
81
82   # use src_coords
83   $work = $targ->copy;
84   ok($work->paste(src=>$src, left => 15, top => 20,
85                   src_coords => [ 10, 15, 80, 70 ]),
86      "using src_coords");
87   is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
88      "compare pasted and expected");
89
90   {
91     # Issue #18712
92     # supplying just src_maxx would set the internal maxy to undef
93     # supplying just src_maxy would be ignored
94     # src_maxy (or it's derived value) was being bounds checked against 
95     # the image width instead of the image height
96     $work = $targ->copy;
97     my @warns;
98     local $SIG{__WARN__} = sub { push @warns, "@_"; print "# @_"; };
99     
100     ok($work->paste(src=>$src, left => 15, top => 20,
101                     src_maxx => 50),
102        "paste with just src_maxx");
103     ok(!@warns, "shouldn't warn");
104     my $cmp = $targ->copy;
105     $cmp->box(filled=>1, color => 'FF0000', xmin => 15, ymin => 20,
106               xmax => 64, ymax => 109);
107     $cmp->box(filled=>1, color => '0000FF', xmin => 35, ymin => 40,
108               xmax => 64, ymax => 99);
109     is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
110        "check correctly pasted");
111
112     $work = $targ->copy;
113     @warns = ();
114     ok($work->paste(src=>$src, left=>15, top=>20,
115                     src_maxy => 60),
116        "paste with just src_maxy");
117     ok(!@warns, "shouldn't warn");
118     $cmp = $targ->copy;
119     $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
120               xmax => 99, ymax => 79);
121     $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 40,
122               xmax => 94, ymax => 79);
123     is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
124        "check pasted correctly");
125
126     $work = $targ->copy;
127     @warns = ();
128     ok($work->paste(src=>$src, left=>15, top=>20,
129                     src_miny => 20, src_maxy => 105),
130        "paste with src_maxy > source width");
131
132     $cmp = $targ->copy;
133     $cmp->box(filled => 1, color => 'FF0000', xmin => 15, ymin => 20,
134               ymax => 104);
135     $cmp->box(filled => 1, color => '0000FF', xmin => 35, ymin => 20,
136               xmax => 94, ymax => 79);
137     is(Imager::i_img_diff($work->{IMG}, $cmp->{IMG}), 0,
138        "check pasted correctly");
139   }
140 }
141
142 { # https://rt.cpan.org/Ticket/Display.html?id=30908
143   # we now adapt the source channels to the target
144   # check each combination works as expected
145
146   # various source images
147   my $src1 = Imager->new(xsize => 50, ysize => 50, channels => 1);
148   my $g_grey_full = Imager::Color->new(128, 255, 0, 0);
149   my $g_white_50 = Imager::Color->new(255, 128, 0, 0);
150   $src1->box(filled => 1, xmax => 24, color => $g_grey_full);
151
152   my $src2 = Imager->new(xsize => 50, ysize => 50, channels => 2);
153   $src2->box(filled => 1, xmax => 24, color => $g_grey_full);
154   $src2->box(filled => 1, xmin => 25, color => $g_white_50);
155
156   my $c_red_full = Imager::Color->new(255, 0, 0);
157   my $c_blue_full = Imager::Color->new(0, 0, 255);
158   my $src3 = Imager->new(xsize => 50, ysize => 50, channels => 3);
159   $src3->box(filled => 1, xmax => 24, color => $c_red_full);
160   $src3->box(filled => 1, xmin => 25, color => $c_blue_full);
161
162   my $c_green_50 = Imager::Color->new(0, 255, 0, 127);
163   my $src4 = Imager->new(xsize => 50, ysize => 50, channels => 4);
164   $src4->box(filled => 1, xmax => 24, color => $c_blue_full);
165   $src4->box(filled => 1, xmin => 25, color => $c_green_50);
166
167   my @left_box = ( box => [ 25, 25, 49, 74 ] );
168   my @right_box = ( box => [ 50, 25, 74, 74 ] );
169
170   { # 1 channel output
171     my $base = Imager->new(xsize => 100, ysize => 100, channels => 1);
172     $base->box(filled => 1, color => Imager::Color->new(64, 255, 0, 0));
173
174     my $work = $base->copy;
175     ok($work->paste(left => 25, top => 25, src => $src1), "paste 1 to 1");
176     my $comp = $base->copy;
177     $comp->box(filled => 1, color => $g_grey_full, @left_box);
178     $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
179     is_image($work, $comp, "compare paste target to expected");
180
181     $work = $base->copy;
182     ok($work->paste(left => 25, top => 25, src => $src2), "paste 2 to 1");
183     $comp = $base->copy;
184     $comp->box(filled => 1, @left_box, color => $g_grey_full);
185     $comp->box(filled => 1, @right_box, color => [ 128, 0, 0, 0 ]);
186     is_image($work, $comp, "compare paste target to expected");
187
188     $work = $base->copy;
189     ok($work->paste(left => 25, top => 25, src => $src3), "paste 3 to 1");
190      $comp = $base->copy;
191     $comp->box(filled => 1, @left_box, color => [ 57, 255, 0, 0 ]);
192     $comp->box(filled => 1, @right_box, color => [ 18, 255, 0, 0 ]);
193     is_image($work, $comp, "compare paste target to expected");
194
195     $work = $base->copy;
196     ok($work->paste(left => 25, top => 25, src => $src4), "paste 4 to 1");
197     $comp = $base->copy;
198     $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
199     $comp->box(filled => 1, color => [ 90, 255, 0, 0 ], @right_box);
200     is_image($work, $comp, "compare paste target to expected");
201   }
202
203   { # 2 channel output
204     my $base = Imager->new(xsize => 100, ysize => 100, channels => 2);
205     $base->box(filled => 1, color => [ 128, 128, 0, 0 ]);
206     
207     my $work = $base->copy;
208     ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 2");
209     my $comp = $base->copy;
210     $comp->box(filled => 1, color => $g_grey_full, @left_box);
211     $comp->box(filled => 1, color => [ 0, 255, 0, 0 ], @right_box);
212     is_image($work, $comp, "compare paste target to expected");
213
214     $work = $base->copy;
215     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 2");
216     $comp = $base->copy;
217     $comp->box(filled => 1, color => $g_grey_full, @left_box);
218     $comp->box(filled => 1, color => $g_white_50, @right_box);
219     is_image($work, $comp, "compare paste target to expected");
220
221     $work = $base->copy;
222     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 2");
223     $comp = $base->copy;
224     $comp->box(filled => 1, color => [ 57, 255, 0, 0 ], @left_box);
225     $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @right_box);
226     is_image($work, $comp, "compare paste target to expected");
227
228     $work = $base->copy;
229     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 2");
230     $comp = $base->copy;
231     $comp->box(filled => 1, color => [ 18, 255, 0, 0 ], @left_box);
232     $comp->box(filled => 1, color => [ 180, 127, 0, 0 ], @right_box);
233     is_image($work, $comp, "compare paste target to expected");
234   }
235
236   { # 3 channel output
237     my $base = Imager->new(xsize => 100, ysize => 100, channels => 3);
238     $base->box(filled => 1, color => [ 128, 255, 0, 0 ]);
239     
240     my $work = $base->copy;
241     ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 3");
242     my $comp = $base->copy;
243     $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
244     $comp->box(filled => 1, color => [ 0, 0, 0, 0 ], @right_box);
245     is_image($work, $comp, "compare paste target to expected");
246
247     $work = $base->copy;
248     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 3");
249     $comp = $base->copy;
250     $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
251     $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @right_box);
252     is_image($work, $comp, "compare paste target to expected");
253
254     $work = $base->copy;
255     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 3");
256     $comp = $base->copy;
257     $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
258     $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
259     is_image($work, $comp, "compare paste target to expected");
260
261     $work = $base->copy;
262     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 3");
263     $comp = $base->copy;
264     $comp->box(filled => 1, color => [ 0, 0, 255 ], @left_box);
265     $comp->box(filled => 1, color => [ 0, 127, 0 ], @right_box);
266     is_image($work, $comp, "compare paste target to expected");
267   }
268
269   { # 4 channel output
270     my $base = Imager->new(xsize => 100, ysize => 100, channels => 4);
271     $base->box(filled => 1, color => [ 128, 255, 64, 128 ]);
272     
273     my $work = $base->copy;
274     ok($work->paste(top => 25, left => 25, src => $src1), "paste 1 to 4");
275     my $comp = $base->copy;
276     $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
277     $comp->box(filled => 1, color => [ 0, 0, 0, 255 ], @right_box);
278     is_image($work, $comp, "compare paste target to expected");
279
280     $work = $base->copy;
281     ok($work->paste(top => 25, left => 25, src => $src2), "paste 2 to 4");
282     $comp = $base->copy;
283     $comp->box(filled => 1, color => [ 128, 128, 128, 255 ], @left_box);
284     $comp->box(filled => 1, color => [ 255, 255, 255, 128 ], @right_box);
285     is_image($work, $comp, "compare paste target to expected");
286
287     $work = $base->copy;
288     ok($work->paste(top => 25, left => 25, src => $src3), "paste 3 to 4");
289     $comp = $base->copy;
290     $comp->box(filled => 1, color => [ 255, 0, 0 ], @left_box);
291     $comp->box(filled => 1, color => [ 0, 0, 255 ], @right_box);
292     is_image($work, $comp, "compare paste target to expected");
293
294     $work = $base->copy;
295     ok($work->paste(top => 25, left => 25, src => $src4), "paste 4 to 4");
296     $comp = $base->copy;
297     $comp->box(filled => 1, color => $c_blue_full, @left_box);
298     $comp->box(filled => 1, color => $c_green_50, @right_box);
299     is_image($work, $comp, "compare paste target to expected");
300   }
301 }