]> git.imager.perl.org - imager.git/blob - t/t021sixteen.t
44e46f631168b83e3bbf6debae04cc80f028bd78
[imager.git] / t / t021sixteen.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 85;
4
5 BEGIN { use_ok(Imager=>qw(:all :handy)) }
6
7 init_log("testout/t021sixteen.log", 1);
8
9 require "t/testtools.pl";
10
11 use Imager::Color::Float;
12
13 my $im_g = Imager::i_img_16_new(100, 101, 1);
14
15 is(Imager::i_img_getchannels($im_g), 1, "1 channel image channel count");
16 ok(Imager::i_img_getmask($im_g) & 1, "1 channel image mask");
17 ok(!Imager::i_img_virtual($im_g), "shouldn't be marked virtual");
18 is(Imager::i_img_bits($im_g), 16, "1 channel image has bits == 16");
19 is(Imager::i_img_type($im_g), 0, "1 channel image isn't direct");
20
21 my @ginfo = i_img_info($im_g);
22 is($ginfo[0], 100, "1 channel image width");
23 is($ginfo[1], 101, "1 channel image height");
24
25 undef $im_g;
26
27 my $im_rgb = Imager::i_img_16_new(100, 101, 3);
28
29 is(Imager::i_img_getchannels($im_rgb), 3, "3 channel image channel count");
30 ok((Imager::i_img_getmask($im_rgb) & 7) == 7, "3 channel image mask");
31 is(Imager::i_img_bits($im_rgb), 16, "3 channel image bits");
32 is(Imager::i_img_type($im_rgb), 0, "3 channel image type");
33
34 my $redf = NCF(1, 0, 0);
35 my $greenf = NCF(0, 1, 0);
36 my $bluef = NCF(0, 0, 1);
37
38 # fill with red
39 for my $y (0..101) {
40   Imager::i_plinf($im_rgb, 0, $y, ($redf) x 100);
41 }
42 pass("fill with red");
43 # basic sanity
44 test_colorf_gpix($im_rgb, 0,  0,   $redf);
45 test_colorf_gpix($im_rgb, 99, 0,   $redf);
46 test_colorf_gpix($im_rgb, 0,  100, $redf);
47 test_colorf_gpix($im_rgb, 99, 100, $redf);
48 test_colorf_glin($im_rgb, 0,  0,   ($redf) x 100);
49 test_colorf_glin($im_rgb, 0,  100, ($redf) x 100);
50
51 Imager::i_plinf($im_rgb, 20, 1, ($greenf) x 60);
52 test_colorf_glin($im_rgb, 0, 1, 
53                  ($redf) x 20, ($greenf) x 60, ($redf) x 20);
54
55 # basic OO tests
56 my $oo16img = Imager->new(xsize=>200, ysize=>201, bits=>16);
57 ok($oo16img, "make a 16-bit oo image");
58 is($oo16img->bits,  16, "test bits");
59
60 # make sure of error handling
61 ok(!Imager->new(xsize=>0, ysize=>1, bits=>16),
62     "fail to create a 0 pixel wide image");
63 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
64        "and correct error message");
65
66 ok(!Imager->new(xsize=>1, ysize=>0, bits=>16),
67     "fail to create a 0 pixel high image");
68 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
69        "and correct error message");
70
71 ok(!Imager->new(xsize=>-1, ysize=>1, bits=>16),
72     "fail to create a negative width image");
73 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
74        "and correct error message");
75
76 ok(!Imager->new(xsize=>1, ysize=>-1, bits=>16),
77     "fail to create a negative height image");
78 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
79        "and correct error message");
80
81 ok(!Imager->new(xsize=>-1, ysize=>-1, bits=>16),
82     "fail to create a negative width/height image");
83 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
84        "and correct error message");
85
86 ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>0),
87     "fail to create a zero channel image");
88 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
89        "and correct error message");
90 ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>5),
91     "fail to create a five channel image");
92 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
93        "and correct error message");
94
95 {
96   # https://rt.cpan.org/Ticket/Display.html?id=8213
97   # check for handling of memory allocation of very large images
98   # only test this on 32-bit machines - on a 64-bit machine it may
99   # result in trying to allocate 4Gb of memory, which is unfriendly at
100   # least and may result in running out of memory, causing a different
101   # type of exit
102  SKIP: {
103     use Config;
104     $Config{intsize} == 4
105       or skip("don't want to allocate 4Gb", 8);
106     my $uint_range = 256 ** $Config{intsize};
107     print "# range $uint_range\n";
108     my $dim1 = int(sqrt($uint_range/2))+1;
109     
110     my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1, bits=>16);
111     is($im_b, undef, "integer overflow check - 1 channel");
112     
113     $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1, bits=>16);
114     ok($im_b, "but same width ok");
115     $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1, bits=>16);
116     ok($im_b, "but same height ok");
117     cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
118            "check the error message");
119
120     # do a similar test with a 3 channel image, so we're sure we catch
121     # the same case where the third dimension causes the overflow
122     my $dim3 = int(sqrt($uint_range / 3 / 2))+1;
123     
124     $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3, bits=>16);
125     is($im_b, undef, "integer overflow check - 3 channel");
126     
127     $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3, bits=>16);
128     ok($im_b, "but same width ok");
129     $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3, bits=>16);
130     ok($im_b, "but same height ok");
131
132     cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
133            "check the error message");
134
135     # check we can allocate a scanline, unlike double images the scanline
136     # in the image itself is smaller than a line of i_fcolor
137     # divide by 2 to get to int range, by 2 for 2 bytes/pixel, by 3 to 
138     # fit the image allocation in, but for the floats to overflow
139     my $dim4 = $uint_range / 2 / 2 / 3;
140     my $im_o = Imager->new(xsize=>$dim4, ysize=>1, channels=>1, bits=>16);
141     is($im_o, undef, "integer overflow check - scanline");
142     cmp_ok(Imager->errstr, '=~',
143            qr/integer overflow calculating scanline allocation/,
144            "check error message");
145   }
146 }
147
148 { # check the channel mask function
149   
150   my $im = Imager->new(xsize => 10, ysize=>10, bits=>16);
151
152   mask_tests($im, 1.0/65535);
153 }