File: 100-masked.t

package info (click to toggle)
libimager-perl 1.027%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,820 kB
  • sloc: perl: 32,971; ansic: 28,092; makefile: 52; cpp: 4
file content (731 lines) | stat: -rw-r--r-- 27,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#!perl -w
use strict;
use Test::More;
use Imager qw(:all :handy);
use Imager::Test qw(is_color3 is_fcolor3 test_image is_image);

-d "testout" or mkdir "testout";

Imager->open_log(log => "testout/t020masked.log");

my $base_rgb = Imager::ImgRaw::new(100, 100, 3);
# put something in there
my $black = NC(0, 0, 0);
my $red = NC(255, 0, 0);
my $green = NC(0, 255, 0);
my $blue = NC(0, 0, 255);
my $white = NC(255, 255, 255);
my $grey = NC(128, 128, 128);
use Imager::Color::Float;
my $redf = Imager::Color::Float->new(1, 0, 0);
my $greenf = Imager::Color::Float->new(0, 1, 0);
my $bluef = Imager::Color::Float->new(0, 0, 1);
my $greyf = Imager::Color::Float->new(0.5, 0.5, 0.5);
my @cols = ($red, $green, $blue);
for my $y (0..99) {
  Imager::i_plin($base_rgb, 0, $y, ($cols[$y % 3] ) x 100);
}

# first a simple subset image
my $s_rgb = Imager::i_img_masked_new($base_rgb, undef, 25, 25, 50, 50);

is(Imager::i_img_getchannels($s_rgb), 3,
   "1 channel image channel count match");
ok(Imager::i_img_getmask($s_rgb) & 1,
   "1 channel image mask");
ok(Imager::i_img_virtual($s_rgb),
   "1 channel image thinks it isn't virtual");
is(Imager::i_img_bits($s_rgb), 8,
   "1 channel image has bits == 8");
is(Imager::i_img_type($s_rgb), 0, # direct
   "1 channel image is direct");

my @ginfo = i_img_info($s_rgb);
is($ginfo[0], 50, "check width");
is($ginfo[1], 50, "check height");

# sample some pixels through the subset
my $c = Imager::i_get_pixel($s_rgb, 0, 0);
is_color3($c, 0, 255, 0, "check (0,0)");
$c = Imager::i_get_pixel($s_rgb, 49, 49);
# (25+49)%3 = 2
is_color3($c, 0, 0, 255, "check (49,49)");

# try writing to it
for my $y (0..49) {
  Imager::i_plin($s_rgb, 0, $y, ($cols[$y % 3]) x 50);
}
pass("managed to write to it");
# and checking the target image
$c = Imager::i_get_pixel($base_rgb, 25, 25);
is_color3($c, 255, 0, 0, "check (25,25)");
$c = Imager::i_get_pixel($base_rgb, 29, 29);
is_color3($c, 0, 255, 0, "check (29,29)");

undef $s_rgb;

# a basic background
for my $y (0..99) {
  Imager::i_plin($base_rgb, 0, $y, ($red ) x 100);
}
my $mask = Imager::ImgRaw::new(50, 50, 1);
# some venetian blinds
for my $y (4..20) {
  Imager::i_plin($mask, 5, $y*2, ($white) x 40);
}
# with a strip down the middle
for my $y (0..49) {
  Imager::i_plin($mask, 20, $y, ($white) x 8);
}
my $m_rgb = Imager::i_img_masked_new($base_rgb, $mask, 25, 25, 50, 50);
ok($m_rgb, "make masked with mask");
for my $y (0..49) {
  Imager::i_plin($m_rgb, 0, $y, ($green) x 50);
}
my @color_tests =
  (
   [ 25+0,  25+0,  $red ],
   [ 25+19, 25+0,  $red ],
   [ 25+20, 25+0,  $green ],
   [ 25+27, 25+0,  $green ],
   [ 25+28, 25+0,  $red ],
   [ 25+49, 25+0,  $red ],
   [ 25+19, 25+7,  $red ],
   [ 25+19, 25+8,  $green ],
   [ 25+19, 25+9,  $red ],
   [ 25+0,  25+8,  $red ],
   [ 25+4,  25+8,  $red ],
   [ 25+5,  25+8,  $green ],
   [ 25+44, 25+8,  $green ],
   [ 25+45, 25+8,  $red ],
   [ 25+49, 25+49, $red ],
  );
my $test_num = 15;
for my $test (@color_tests) {
  my ($x, $y, $testc) = @$test;
  my ($r, $g, $b) = $testc->rgba;
  my $c = Imager::i_get_pixel($base_rgb, $x, $y);
  is_color3($c, $r, $g, $b, "at ($x, $y)");
}

{
  # tests for the OO versions, fairly simple, since the basic functionality
  # is covered by the low-level interface tests
   
  my $base = Imager->new(xsize=>100, ysize=>100);
  ok($base, "make base OO image");
  $base->box(color=>$blue, filled=>1); # fill it all
  my $mask = Imager->new(xsize=>80, ysize=>80, channels=>1);
  $mask->box(color=>$white, filled=>1, xmin=>5, xmax=>75, ymin=>5, ymax=>75);
  my $m_img = $base->masked(mask=>$mask, left=>5, top=>5);
  ok($m_img, "make masked OO image");
  is($m_img->getwidth, 80, "check width");
  $m_img->box(color=>$green, filled=>1);
  my $c = $m_img->getpixel(x=>0, y=>0);
  is_color3($c, 0, 0, 255, "check (0,0)");
  $c = $m_img->getpixel(x => 5, y => 5);
  is_color3($c, 0, 255, 0, "check (5,5)");

  # older versions destroyed the Imager::ImgRaw object manually in 
  # Imager::DESTROY rather than letting Imager::ImgRaw::DESTROY 
  # destroy the object
  # so we test here by destroying the base and mask objects and trying 
  # to draw to the masked wrapper
  # you may need to test with ElectricFence to trigger the problem
  undef $mask;
  undef $base;
  $m_img->box(color=>$blue, filled=>1);
  pass("didn't crash unreffing base or mask for masked image");
}

# 35.7% cover on maskimg.c up to here

{ # error handling:
  my $base = Imager->new(xsize => 100, ysize => 100);
  ok($base, "make base");
  { #  make masked image subset outside of the base image
    my $masked = $base->masked(left => 100);
    ok (!$masked, "fail to make empty masked");
    is($base->errstr, "subset outside of target image", "check message");
  }
}

{ # size limiting
  my $base = Imager->new(xsize => 10, ysize => 10);
  ok($base, "make base for size limit tests");
  {
    my $masked = $base->masked(left => 5, right => 15);
    ok($masked, "make masked");
    is($masked->getwidth, 5, "check width truncated");
  }
  {
    my $masked = $base->masked(top => 5, bottom => 15);
    ok($masked, "make masked");
    is($masked->getheight, 5, "check height truncated");
  }
}
# 36.7% up to here

$mask = Imager->new(xsize => 80, ysize => 80, channels => 1);
$mask->box(filled => 1, color => $white, xmax => 39, ymax => 39);
$mask->box(fill => { hatch => "check1x1" }, ymin => 40, xmax => 39);

{
  my $base = Imager->new(xsize => 100, ysize => 100, bits => "double");
  ok($base, "base for single pixel tests");
  is($base->type, "direct", "check type");
  my $masked = $base->masked(mask => $mask, left => 1, top => 2);
  my $limited = $base->masked(left => 1, top => 2);

  is($masked->type, "direct", "check masked is same type as base");
  is($limited->type, "direct", "check limited is same type as base");

  {
    # single pixel writes, masked
    {
      ok($masked->setpixel(x => 1, y => 3, color => $green),
	 "set (1,3) in masked (2, 5) in based");
      my $c = $base->getpixel(x => 2, y => 5);
      is_color3($c, 0, 255, 0, "check it wrote through");
      ok($masked->setpixel(x => 45, y => 2, color => $red),
	 "set (45,2) in masked (46,4) in base (no mask)");
    $c = $base->getpixel(x => 46, y => 4);
      is_color3($c, 0, 0, 0, "shouldn't have written through");
    }
    {
      ok($masked->setpixel(x => 2, y => 3, color => $redf),
	 "write float red to (2,3) base(3,5)");
      my $c = $base->getpixel(x => 3, y => 5);
      is_color3($c, 255, 0, 0, "check it wrote through");
      ok($masked->setpixel(x => 45, y => 3, color => $greenf),
	 "set float (45,3) in masked (46,5) in base (no mask)");
      $c = $base->getpixel(x => 46, y => 5);
      is_color3($c, 0, 0, 0, "check it didn't write");
    }
    {
      # write out of range should fail
      is($masked->setpixel(x => 80, y => 0, color => $green), "0 but true",
	 "write 8-bit color out of range");
      is($masked->setpixel(x => 0, y => 80, color => $greenf), "0 but true",
	 "write float color out of range");
    }
  }

  # 46.9

  {
    print "# plin coverage\n";
    {
      $base->box(filled => 1, color => $black);
      # plin masked
      # simple path
      is($masked->setscanline(x => 76, y => 1, pixels => [ ($red, $green) x 3 ]),
	 4, "try to write 6 pixels, but only write 4");
      is_deeply([ $base->getsamples(x => 77, y => 3, width => 4) ],
		[ ( 0 ) x 12 ],
		"check not written through");
      # !simple path
      is($masked->setscanline(x => 4, y => 2, pixels => [ ($red, $green, $blue, $grey) x (72/4) ]),
	 72, "write many pixels (masked)");
      is_deeply([ $base->getsamples(x => 5, y => 4, width => 72) ],
		[ ( (255, 0, 0), (0, 255, 0), (0, 0, 255), (128, 128, 128)) x 9,
		  ( 0, 0, 0 ) x 36 ],
		"check written through to base");
      
      # simple path, due to number of transitions
      is($masked->setscanline(x => 0, y => 40, pixels => [ ($red, $green, $blue, $grey) x 5 ]),
	 20, "try to write 20 pixels, with alternating write through");
      is_deeply([ $base->getsamples(x => 1, y => 42, width => 20) ],
		[ ( (0, 0, 0), (0,255,0), (0,0,0), (128,128,128) ) x 5 ],
		"check correct pixels written through");
    }
    
    {
      $base->box(filled => 1, color => $black);
      # plin, non-masked path
      is($limited->setscanline(x => 4, y => 2, pixels => [ ($red, $green, $blue, $grey) x (72/4) ]),
	 72, "write many pixels (limited)");
      is_deeply([ $base->getsamples(x => 5, y => 4, width => 72) ],
		[ ( (255, 0, 0), (0, 255, 0), (0, 0, 255), (128, 128, 128)) x 18 ],
		"check written through to based");
    }
    
    {
      # draw outside fails
      is($masked->setscanline(x => 80, y => 2, pixels => [ $red, $green ]),
	 0, "check writing no pixels");
    }
  }

  {
    print "# plinf coverage\n";
    {
      $base->box(filled => 1, color => $black);
      # plinf masked
      # simple path
      is($masked->setscanline(x => 76, y => 1, pixels => [ ($redf, $greenf) x 3 ]),
	 4, "try to write 6 pixels, but only write 4");
      is_deeply([ $base->getsamples(x => 77, y => 3, width => 4, type => "float") ],
		[ ( 0 ) x 12 ],
		"check not written through");
      # !simple path
      is($masked->setscanline(x => 4, y => 2, pixels => [ ($redf, $greenf, $bluef, $greyf) x (72/4) ]),
	 72, "write many pixels (masked)");
      is_deeply([ $base->getsamples(x => 5, y => 4, width => 72, type => "float") ],
		[ ( (1, 0, 0), (0, 1, 0), (0, 0, 1), (0.5, 0.5, 0.5)) x 9,
		  ( 0, 0, 0 ) x 36 ],
		"check written through to base");
      
      # simple path, due to number of transitions
      is($masked->setscanline(x => 0, y => 40, pixels => [ ($redf, $greenf, $bluef, $greyf) x 5 ]),
	 20, "try to write 20 pixels, with alternating write through");
      is_deeply([ $base->getsamples(x => 1, y => 42, width => 20, type => "float") ],
		[ ( (0, 0, 0), (0,1,0), (0,0,0), (0.5,0.5,0.5) ) x 5 ],
		"check correct pixels written through");
    }
    
    {
      $base->box(filled => 1, color => $black);
      # plinf, non-masked path
      is($limited->setscanline(x => 4, y => 2, pixels => [ ($redf, $greenf, $bluef, $greyf) x (72/4) ]),
	 72, "write many pixels (limited)");
      is_deeply([ $base->getsamples(x => 5, y => 4, width => 72, type => "float") ],
		[ ( (1, 0, 0), (0, 1, 0), (0, 0, 1), (0.5, 0.5, 0.5)) x 18 ],
		"check written through to based");
    }
    
    {
      # draw outside fails
      is($masked->setscanline(x => 80, y => 2, pixels => [ $redf, $greenf ]),
	 0, "check writing no pixels");
    }
  }
  # 71.4%
  {
    {
      print "# gpix\n";
      # gpix
      $base->box(filled => 1, color => $black);
      ok($base->setpixel(x => 4, y => 10, color => $red),
	 "set base(4,10) to red");
      is_fcolor3($masked->getpixel(x => 3, y => 8),
		 255, 0, 0, "check pixel written");

      # out of range
      is($masked->getpixel(x => -1, y => 1),
	 undef, "check failure to left");
      is($masked->getpixel(x => 0, y => -1),
	 undef, "check failure to top");
      is($masked->getpixel(x => 80, y => 1),
	 undef, "check failure to right");
      is($masked->getpixel(x => 0, y => 80),
	 undef, "check failure to bottom");
    }
    {
      print "# gpixf\n";
      # gpixf
      $base->box(filled => 1, color => $black);
      ok($base->setpixel(x => 4, y => 10, color => $redf),
	 "set base(4,10) to red");
      is_fcolor3($masked->getpixel(x => 3, y => 8, type => "float"),
		 1.0, 0, 0, 0, "check pixel written");

      # out of range
      is($masked->getpixel(x => -1, y => 1, type => "float"),
	 undef, "check failure to left");
      is($masked->getpixel(x => 0, y => -1, type => "float"),
	 undef, "check failure to top");
      is($masked->getpixel(x => 80, y => 1, type => "float"),
	 undef, "check failure to right");
      is($masked->getpixel(x => 0, y => 80, type => "float"),
	 undef, "check failure to bottom");
    }
  }
  # 74.5
  {
    {
      print "# glin\n";
      $base->box(filled => 1, color => $black);
      is($base->setscanline(x => 31, y => 3, 
			    pixels => [ ( $red, $green) x 10 ]),
	 20, "write 20 pixels to base image");
      my @colors = $masked->
	getscanline(x => 30, y => 1, width => 20);
      is(@colors, 20, "check we got right number of colors");
      is_color3($colors[0], 255, 0, 0, "check first pixel");
      is_color3($colors[19], 0, 255, 0, "check last pixel");

      @colors = $masked->getscanline(x => 76, y => 2, width => 10);
      is(@colors, 4, "read line from right edge");
      is_color3($colors[0], 0, 0, 0, "check pixel");

      is_deeply([ $masked->getscanline(x => -1, y => 0, width => 1) ],
	 [], "fail read left of image");
      is_deeply([ $masked->getscanline(x => 0, y => -1, width => 1) ],
	 [], "fail read top of image");
      is_deeply([$masked->getscanline(x => 80, y => 0, width => 1)],
	 [], "fail read right of image");
      is_deeply([$masked->getscanline(x => 0, y => 80, width => 1)],
	 [], "fail read bottom of image");
    }
    {
      print "# glinf\n";
      $base->box(filled => 1, color => $black);
      is($base->setscanline(x => 31, y => 3, 
			    pixels => [ ( $redf, $greenf) x 10 ]),
	 20, "write 20 pixels to base image");
      my @colors = $masked->
	getscanline(x => 30, y => 1, width => 20, type => "float");
      is(@colors, 20, "check we got right number of colors");
      is_fcolor3($colors[0], 1.0, 0, 0, 0, "check first pixel");
      is_fcolor3($colors[19], 0, 1.0, 0, 0, "check last pixel");

      @colors = $masked->
	getscanline(x => 76, y => 2, width => 10, type => "float");
      is(@colors, 4, "read line from right edge");
      is_fcolor3($colors[0], 0, 0, 0, 0, "check pixel");

      is_deeply([ $masked->getscanline(x => -1, y => 0, width => 1, type => "float") ],
	 [], "fail read left of image");
      is_deeply([ $masked->getscanline(x => 0, y => -1, width => 1, type => "float") ],
	 [], "fail read top of image");
      is_deeply([$masked->getscanline(x => 80, y => 0, width => 1, type => "float")],
	 [], "fail read right of image");
      is_deeply([$masked->getscanline(x => 0, y => 80, width => 1, type => "float")],
	 [], "fail read bottom of image");
    }
  }
  # 81.6%
  {
    {
      print "# gsamp\n";
      $base->box(filled => 1, color => $black);
      is($base->setscanline(x => 31, y => 3, 
			    pixels => [ ( $red, $green) x 10 ]),
	 20, "write 20 pixels to base image");
      my @samps = $masked->
	getsamples(x => 30, y => 1, width => 20);
      is(@samps, 60, "check we got right number of samples");
      is_deeply(\@samps,
		[ (255, 0, 0, 0, 255, 0) x 10 ],
		"check it");

      @samps = $masked->
	getsamples(x => 76, y => 2, width => 10);
      is(@samps, 12, "read line from right edge");
      is_deeply(\@samps, [ (0, 0, 0) x 4], "check result");

      is_deeply([ $masked->getsamples(x => -1, y => 0, width => 1) ],
	 [], "fail read left of image");
      is_deeply([ $masked->getsamples(x => 0, y => -1, width => 1) ],
	 [], "fail read top of image");
      is_deeply([$masked->getsamples(x => 80, y => 0, width => 1)],
	 [], "fail read right of image");
      is_deeply([$masked->getsamples(x => 0, y => 80, width => 1)],
	 [], "fail read bottom of image");
    }
    {
      print "# gsampf\n";
      $base->box(filled => 1, color => $black);
      is($base->setscanline(x => 31, y => 3, 
			    pixels => [ ( $redf, $greenf) x 10 ]),
	 20, "write 20 pixels to base image");
      my @samps = $masked->
	getsamples(x => 30, y => 1, width => 20, type => "float");
      is(@samps, 60, "check we got right number of samples");
      is_deeply(\@samps,
		[ (1.0, 0, 0, 0, 1.0, 0) x 10 ],
		"check it");

      @samps = $masked->
	getsamples(x => 76, y => 2, width => 10, type => "float");
      is(@samps, 12, "read line from right edge");
      is_deeply(\@samps, [ (0, 0, 0) x 4], "check result");

      is_deeply([ $masked->getsamples(x => -1, y => 0, width => 1, type => "float") ],
	 [], "fail read left of image");
      is_deeply([ $masked->getsamples(x => 0, y => -1, width => 1, type => "float") ],
	 [], "fail read top of image");
      is_deeply([$masked->getsamples(x => 80, y => 0, width => 1, type => "float")],
	 [], "fail read right of image");
      is_deeply([$masked->getsamples(x => 0, y => 80, width => 1, type => "float")],
	 [], "fail read bottom of image");
    }
  }
  # 86.2%
}

{
  my $base = Imager->new(xsize => 100, ysize => 100, type => "paletted");
  ok($base, "make paletted base");
  is($base->type, "paletted", "check we got paletted");
  is($base->addcolors(colors => [ $black, $red, $green, $blue ]),
     "0 but true",
     "add some colors to base");
  my $masked = $base->masked(mask => $mask, left => 1, top => 2);
  my $limited = $base->masked(left => 1, top => 2);

  is($masked->type, "paletted", "check masked is same type as base");
  is($limited->type, "paletted", "check limited is same type as base");

  {
    # make sure addcolors forwarded
    is($masked->addcolors(colors => [ $grey ]), 4,
       "test addcolors forwarded");
    my @colors = $masked->getcolors();
    is(@colors, 5, "check getcolors forwarded");
    is_color3($colors[1], 255, 0, 0, "check color from palette");
  }

  my ($blacki, $redi, $greeni, $bluei, $greyi) = 0 .. 4;

  { # gpal
    print "# gpal\n";
    $base->box(filled => 1, color => $black);
    is($base->setscanline(x => 0, y => 5, type => "index",
			  pixels => [ ( $redi, $greeni, $bluei, $greyi) x 25 ]),
       100, "write some pixels to base");
    my @indexes = $masked->getscanline(y => 3, type => "index", width => "81");
    is(@indexes, 80, "got 80 indexes");
    is_deeply(\@indexes,
	      [ ( $greeni, $bluei, $greyi, $redi) x 20 ],
	      "check values");

    is_deeply([ $masked->getscanline(x => -1, y => 3, type => "index") ],
	      [], "fail read left of image");
  }
  # 89.8%

  { # ppal, unmasked
    print "# ppal\n";
    $base->box(filled => 1, color => $black);
    is($limited->setscanline(x => 1, y => 1, type => "index",
			     pixels => [ ( $redi, $greeni, $bluei) x 3 ]),
       9, "ppal limited");
    is_deeply([ $base->getscanline(x => 2, y => 3, type => "index", 
				   width => 9) ],
	      [ ( $redi, $greeni, $bluei) x 3 ],
	      "check set in base");
  }
  { # ppal, masked
    $base->box(filled => 1, color => $black);
    is($masked->setscanline(x => 1, y => 2, type => "index",
			    pixels => [ ( $redi, $greeni, $bluei, $greyi) x 12 ]),
       48, "ppal masked");
    is_deeply([ $base->getscanline(x => 0, y => 4, type => "index") ],
	      [ 0, 0,
		( $redi, $greeni, $bluei, $greyi ) x 9,
		$redi, $greeni, $bluei, ( 0 ) x 59 ],
	      "check written");
  }
  {
    # ppal, errors
    is($masked->setscanline(x => -1, y => 0, type => "index",
			    pixels => [ $redi, $bluei ]),
       0, "fail to write ppal");

    is($masked->setscanline(x => 78, y => 0, type => "index",
			   pixels => [ $redi, $bluei, $greeni, $greyi ]),
       2, "write over right side");
  }
}

my $full_mask = Imager->new(xsize => 10, ysize => 10, channels => 1);
$full_mask->box(filled => 1, color => NC(255, 0, 0));

# no mask and mask with full coverage should behave the same
my $psamp_outside_error = "Image position outside of image";
for my $masked (0, 1){ # psamp
  print "# psamp masked: $masked\n";
  my $imback = Imager::ImgRaw::new(20, 20, 3);
  my $mask;
  if ($masked) {
    $mask = $full_mask->{IMG};
  }
  my $imraw = Imager::i_img_masked_new($imback, $mask, 3, 4, 10, 10);
  {
    is(Imager::i_psamp($imraw, 0, 2, undef, [ 255, 128, 64 ]), 3,
       "i_psamp def channels, 3 samples");
    is_color3(Imager::i_get_pixel($imraw, 0, 2), 255, 128, 64,
	      "check color written");
    Imager::i_img_setmask($imraw, 5);
    is(Imager::i_psamp($imraw, 1, 3, undef, [ 64, 128, 192 ]), 3,
       "i_psamp def channels, 3 samples, masked");
    is_color3(Imager::i_get_pixel($imraw, 1, 3), 64, 0, 192,
	      "check color written");
    is(Imager::i_psamp($imraw, 1, 7, [ 0, 1, 2 ], [ 64, 128, 192 ]), 3,
       "i_psamp channels listed, 3 samples, masked");
    is_color3(Imager::i_get_pixel($imraw, 1, 7), 64, 0, 192,
	      "check color written");
    Imager::i_img_setmask($imraw, ~0);
    is(Imager::i_psamp($imraw, 2, 4, [ 0, 1 ], [ 255, 128, 64, 32 ]), 4,
       "i_psamp channels [0, 1], 4 samples");
    is_color3(Imager::i_get_pixel($imraw, 2, 4), 255, 128, 0,
	      "check first color written");
    is_color3(Imager::i_get_pixel($imraw, 3, 4), 64, 32, 0,
	      "check second color written");
    is(Imager::i_psamp($imraw, 0, 5, [ 0, 1, 2 ], [ (128, 63, 32) x 10 ]), 30,
       "write a full row");
    is_deeply([ Imager::i_gsamp($imraw, 0, 10, 5, [ 0, 1, 2 ]) ],
	      [ (128, 63, 32) x 10 ],
	      "check full row");
    is(Imager::i_psamp($imraw, 8, 8, [ 0, 1, 2 ],
		       [ 255, 128, 32, 64, 32, 16, 32, 16, 8 ]),
       6, "i_psamp channels [0, 1, 2], 9 samples, but room for 6");
  }
  { # errors we catch
    is(Imager::i_psamp($imraw, 6, 8, [ 0, 1, 3 ], [ 255, 128, 32 ]),
       undef, "i_psamp channels [0, 1, 3], 3 samples (invalid channel number)");
    is(_get_error(), "No channel 3 in this image",
       "check error message");
    is(Imager::i_psamp($imraw, 6, 8, [ 0, 1, -1 ], [ 255, 128, 32 ]),
       undef, "i_psamp channels [0, 1, -1], 3 samples (invalid channel number)");
    is(_get_error(), "No channel -1 in this image",
       "check error message");
    is(Imager::i_psamp($imraw, 0, -1, undef, [ 0, 0, 0 ]), undef,
       "negative y");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psamp($imraw, 0, 10, undef, [ 0, 0, 0 ]), undef,
       "y overflow");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psamp($imraw, -1, 0, undef, [ 0, 0, 0 ]), undef,
       "negative x");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psamp($imraw, 10, 0, undef, [ 0, 0, 0 ]), undef,
       "x overflow");
    is(_get_error(), $psamp_outside_error, "check error message");
  }
  print "# end psamp tests\n";
}

for my $masked (0, 1) { # psampf
  print "# psampf\n";
  my $imback = Imager::ImgRaw::new(20, 20, 3);
  my $mask;
  if ($masked) {
    $mask = $full_mask->{IMG};
  }
  my $imraw = Imager::i_img_masked_new($imback, $mask, 3, 4, 10, 10);
  {
    is(Imager::i_psampf($imraw, 0, 2, undef, [ 1, 0.5, 0.25 ]), 3,
       "i_psampf def channels, 3 samples");
    is_color3(Imager::i_get_pixel($imraw, 0, 2), 255, 128, 64,
	      "check color written");
    Imager::i_img_setmask($imraw, 5);
    is(Imager::i_psampf($imraw, 1, 3, undef, [ 0.25, 0.5, 0.75 ]), 3,
       "i_psampf def channels, 3 samples, masked");
    is_color3(Imager::i_get_pixel($imraw, 1, 3), 64, 0, 191,
	      "check color written");
    is(Imager::i_psampf($imraw, 1, 7, [ 0, 1, 2 ], [ 0.25, 0.5, 0.75 ]), 3,
       "i_psampf channels listed, 3 samples, masked");
    is_color3(Imager::i_get_pixel($imraw, 1, 7), 64, 0, 191,
	      "check color written");
    Imager::i_img_setmask($imraw, ~0);
    is(Imager::i_psampf($imraw, 2, 4, [ 0, 1 ], [ 1, 0.5, 0.25, 0.125 ]), 4,
       "i_psampf channels [0, 1], 4 samples");
    is_color3(Imager::i_get_pixel($imraw, 2, 4), 255, 128, 0,
	      "check first color written");
    is_color3(Imager::i_get_pixel($imraw, 3, 4), 64, 32, 0,
	      "check second color written");
    is(Imager::i_psampf($imraw, 0, 5, [ 0, 1, 2 ], [ (0.5, 0.25, 0.125) x 10 ]), 30,
       "write a full row");
    is_deeply([ Imager::i_gsamp($imraw, 0, 10, 5, [ 0, 1, 2 ]) ],
	      [ (128, 64, 32) x 10 ],
	      "check full row");
    is(Imager::i_psampf($imraw, 8, 8, [ 0, 1, 2 ],
			[ 1.0, 0.5, 0.125, 0.25, 0.125, 0.0625, 0.125, 0, 1 ]),
       6, "i_psampf channels [0, 1, 2], 9 samples, but room for 6");
  }
  { # errors we catch
    is(Imager::i_psampf($imraw, 6, 8, [ 0, 1, 3 ], [ 1, 0.5, 0.125 ]),
       undef, "i_psampf channels [0, 1, 3], 3 samples (invalid channel number)");
    is(_get_error(), "No channel 3 in this image",
       "check error message");
    is(Imager::i_psampf($imraw, 6, 8, [ 0, 1, -1 ], [ 1, 0.5, 0.125 ]),
       undef, "i_psampf channels [0, 1, -1], 3 samples (invalid channel number)");
    is(_get_error(), "No channel -1 in this image",
       "check error message");
    is(Imager::i_psampf($imraw, 0, -1, undef, [ 0, 0, 0 ]), undef,
       "negative y");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psampf($imraw, 0, 10, undef, [ 0, 0, 0 ]), undef,
       "y overflow");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psampf($imraw, -1, 0, undef, [ 0, 0, 0 ]), undef,
       "negative x");
    is(_get_error(), $psamp_outside_error, "check error message");
    is(Imager::i_psampf($imraw, 10, 0, undef, [ 0, 0, 0 ]), undef,
       "x overflow");
    is(_get_error(), $psamp_outside_error, "check error message");
  }
  print "# end psampf tests\n";
}

{
  my $sub_mask = $full_mask->copy;
  $sub_mask->box(filled => 1, color => NC(0,0,0), xmin => 4, xmax => 6);
  my $base = Imager::ImgRaw::new(20, 20, 3);
  my $masked = Imager::i_img_masked_new($base, $sub_mask->{IMG}, 3, 4, 10, 10);

  is(Imager::i_psamp($masked, 0, 2, undef, [ ( 0, 127, 255) x 10 ]), 30,
     "psamp() to masked image");
  is_deeply([ Imager::i_gsamp($base, 0, 20, 6, undef) ],
	    [ ( 0, 0, 0 ) x 3, # left of mask
	      ( 0, 127, 255 ) x 4, # masked area
	      ( 0, 0, 0 ) x 3, # unmasked area
	      ( 0, 127, 255 ) x 3, # masked area
	      ( 0, 0, 0 ) x 7 ], # right of mask
	    "check values written");
  is(Imager::i_psampf($masked, 0, 2, undef, [ ( 0, 0.5, 1.0) x 10 ]), 30,
     "psampf() to masked image");
  is_deeply([ Imager::i_gsamp($base, 0, 20, 6, undef) ],
	    [ ( 0, 0, 0 ) x 3, # left of mask
	      ( 0, 128, 255 ) x 4, # masked area
	      ( 0, 0, 0 ) x 3, # unmasked area
	      ( 0, 128, 255 ) x 3, # masked area
	      ( 0, 0, 0 ) x 7 ], # right of mask
	    "check values written");
}

{
  my $im = test_image;
  my $m = $im->masked(left => 10, right => 10);
  ok(!$m, "cannot make masked image with no width");
  $m = $im->masked(top => 10, bottom => 10);
  ok(!$m, "cannot make masked image with no height");

  $m = $im->masked(left => 10, right => 9);
  ok(!$m, "cannot make masked image with neg width");
  $m = $im->masked(top => 10, bottom => 9);
  ok(!$m, "cannot make masked image with neg height");

  $m = $im->masked(left => -151);
  ok(!$m, "disallow negative left (after scaling)");
  $m = $im->masked(top => -151);
  ok(!$m, "disallow negative top (after scaling)");

  $m = $im->masked(left => -20, top => -70, right => -10, bottom => -15);
  ok($m, "make masked image based on negative offsets");
  my $cmp = $im->crop(left => 130, top => 80, right => 140, bottom => 135);
  is_image($m, $cmp, "check masked image matches expecteed cropped image");
}

{
  my $empty = Imager->new;
  ok(!$empty->masked, "fail to make a masked image from an empty");
  is($empty->errstr, "masked: empty input image",
    "check error message");
}

Imager->close_log();

unless ($ENV{IMAGER_KEEP_FILES}) {
  unlink "testout/t020masked.log";
}

done_testing();

sub _get_error {
  my @errors = Imager::i_errors();
  return join(": ", map $_->[0], @errors);
}