]> git.imager.perl.org - imager.git/blob - t/t67convert.t
fe89401600bf358259e0a8aae255a3f985ffa8cf
[imager.git] / t / t67convert.t
1 #!perl -w
2 use strict;
3 use Imager qw(:all :handy);
4 use Test::More tests => 27;
5 use Imager::Test qw(test_colorf_gpix is_fcolor1 is_fcolor3);
6
7 Imager::init("log"=>'testout/t67convert.log');
8
9 my $imbase = Imager::ImgRaw::new(200,300,3);
10
11 # first a basic test, make sure the basic things happen ok
12 # make a 1 channel image from the above (black) image
13 # but with 1 as the 'extra' value
14 SKIP:
15 {
16   my $im_white = i_convert($imbase, [ [ 0, 0, 0, 1 ] ]);
17   skip("convert to white failed", 3)
18     unless ok($im_white, "convert to white");
19
20   my ($w, $h, $ch) = i_img_info($im_white);
21
22   # the output image should now have one channel
23   is($ch, 1, "one channel image now");
24   # should have the same width and height
25   ok($w == 200 && $h == 300, "check converted size is the same");
26
27   # should be a white image now, let's check
28   my $c = Imager::i_get_pixel($im_white, 20, 20);
29   my @c = $c->rgba;
30   print "# @c\n";
31   is($c[0], 255, "check image is white");
32 }
33
34 # test the highlevel interface
35 # currently this requires visual inspection of the output files
36 my $im = Imager->new;
37 SKIP:
38 {
39   skip("could not load scale.ppm", 3)
40     unless $im->read(file=>'testimg/scale.ppm');
41   my $out = $im->convert(preset=>'gray');
42   ok($out, "convert preset gray");
43   ok($out->write(file=>'testout/t67_gray.ppm', type=>'pnm'),
44     "save grey image");
45   $out = $im->convert(preset=>'blue');
46   ok($out, "convert preset blue");
47
48   ok($out->write(file=>'testout/t67_blue.ppm', type=>'pnm'),
49      "save blue image");
50 }
51
52 # test against 16-bit/sample images
53 {
54  SKIP:
55   {
56     my $imbase16 = Imager::i_img_16_new(200, 200, 3);
57
58     my $im16targ = i_convert($imbase16, [ [ 0, 0, 0, 1 ],
59                                           [ 0, 0, 0, 0 ],
60                                           [ 0, 0, 0, 0 ] ]);
61     ok($im16targ, "convert 16/bit sample image")
62       or skip("could not convert 16-bit image", 2);
63
64     # image should still be 16-bit
65     is(Imager::i_img_bits($im16targ), 16, "Image still 16-bit/sample");
66
67     # make sure that it's roughly red
68     test_colorf_gpix($im16targ, 0, 0, NCF(1, 0, 0), 0.001, "image roughly red");
69   }
70  SKIP:
71   {
72     my $imbase16 = Imager->new(xsize => 10, ysize => 10, bits => 16);
73     ok($imbase16->setpixel
74        (x => 5, y => 2, color => Imager::Color::Float->new(0.1, 0.2, 0.3)),
75        "set a sample pixel");
76     my $c1 = $imbase16->getpixel(x => 5, y => 2, type => "float");
77     is_fcolor3($c1, 0.1, 0.2, 0.3, "check it was set")
78       or print "#", join(",", $c1->rgba), "\n";
79     
80     my $targ16 = $imbase16->convert(matrix => [ [ 0.05, 0.15, 0.01, 0.5 ] ]);
81     ok($targ16, "convert another 16/bit sample image")
82       or skip("could not convert", 3);
83     is($targ16->getchannels, 1, "convert should be 1 channel");
84     is($targ16->bits, 16, "and 16-bits");
85     my $c = $targ16->getpixel(x => 5, y => 2, type => "float");
86     is_fcolor1($c, 0.538, 1/32768, "check grey value");
87   }
88 }
89
90 # test against palette based images
91 my $impal = Imager::i_img_pal_new(200, 300, 3, 256);
92 my $black = NC(0, 0, 0);
93 my $blackindex = Imager::i_addcolors($impal, $black);
94 ok($blackindex, "add black to paletted");
95 for my $y (0..299) {
96   Imager::i_ppal($impal, 0, $y, ($blackindex) x 200);
97 }
98
99 SKIP:
100 {
101   my $impalout = i_convert($impal, [ [ 0, 0, 0, 0 ],
102                                      [ 0, 0, 0, 1 ],
103                                      [ 0, 0, 0, 0 ] ]);
104   skip("could not convert paletted", 3)
105     unless ok($impalout, "convert paletted");
106   is(Imager::i_img_type($impalout), 1, "image still paletted");
107   is(Imager::i_colorcount($impalout), 1, "still only one colour");
108   my $c = Imager::i_getcolors($impalout, $blackindex);
109   ok($c, "get color from palette");
110   my @ch = $c->rgba;
111   print "# @ch\n";
112   ok($ch[0] == 0 && $ch[1] == 255 && $ch[2] == 0, 
113      "colour is as expected");
114 }
115
116 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
117   # methods that return a new image should warn in void context
118   my $warning;
119   local $SIG{__WARN__} = 
120     sub { 
121       $warning = "@_";
122       my $printed = $warning;
123       $printed =~ s/\n$//;
124       $printed =~ s/\n/\n\#/g; 
125       print "# ",$printed, "\n";
126     };
127   my $img = Imager->new(xsize=>10, ysize=>10);
128   $img->convert(preset=>"grey");
129   cmp_ok($warning, '=~', 'void', "correct warning");
130   cmp_ok($warning, '=~', 't67convert\\.t', "correct file");
131 }
132
133 { # http://rt.cpan.org/NoAuth/Bug.html?id=28492
134   # convert() doesn't preserve image sample size
135   my $im = Imager->new(xsize => 20, ysize => 20, channels => 3, 
136                        bits => 'double');
137   is($im->bits, 'double', 'check source bits');
138   my $conv = $im->convert(preset => 'grey');
139   is($conv->bits, 'double', 'make sure result has extra bits');
140 }