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