3 use Test::More tests => 107;
5 BEGIN { use_ok(Imager=>qw(:all :handy)) }
7 -d "testout" or mkdir "testout";
9 Imager->open_log(log => "testout/t021sixteen.log");
11 use Imager::Color::Float;
12 use Imager::Test qw(test_image is_image image_bounds_checks test_colorf_gpix
13 test_colorf_glin mask_tests is_color3);
15 my $im_g = Imager::i_img_16_new(100, 101, 1);
17 is(Imager::i_img_getchannels($im_g), 1, "1 channel image channel count");
18 ok(Imager::i_img_getmask($im_g) & 1, "1 channel image mask");
19 ok(!Imager::i_img_virtual($im_g), "shouldn't be marked virtual");
20 is(Imager::i_img_bits($im_g), 16, "1 channel image has bits == 16");
21 is(Imager::i_img_type($im_g), 0, "1 channel image isn't direct");
23 my @ginfo = i_img_info($im_g);
24 is($ginfo[0], 100, "1 channel image width");
25 is($ginfo[1], 101, "1 channel image height");
29 my $im_rgb = Imager::i_img_16_new(100, 101, 3);
31 is(Imager::i_img_getchannels($im_rgb), 3, "3 channel image channel count");
32 ok((Imager::i_img_getmask($im_rgb) & 7) == 7, "3 channel image mask");
33 is(Imager::i_img_bits($im_rgb), 16, "3 channel image bits");
34 is(Imager::i_img_type($im_rgb), 0, "3 channel image type");
36 my $redf = NCF(1, 0, 0);
37 my $greenf = NCF(0, 1, 0);
38 my $bluef = NCF(0, 0, 1);
42 Imager::i_plinf($im_rgb, 0, $y, ($redf) x 100);
44 pass("fill with red");
46 test_colorf_gpix($im_rgb, 0, 0, $redf, 0, "top-left");
47 test_colorf_gpix($im_rgb, 99, 0, $redf, 0, "top-right");
48 test_colorf_gpix($im_rgb, 0, 100, $redf, 0, "bottom left");
49 test_colorf_gpix($im_rgb, 99, 100, $redf, 0, "bottom right");
50 test_colorf_glin($im_rgb, 0, 0, [ ($redf) x 100 ], "first line");
51 test_colorf_glin($im_rgb, 0, 100, [ ($redf) x 100 ], "last line");
53 Imager::i_plinf($im_rgb, 20, 1, ($greenf) x 60);
54 test_colorf_glin($im_rgb, 0, 1,
55 [ ($redf) x 20, ($greenf) x 60, ($redf) x 20 ],
56 "added some green in the middle");
59 is(Imager::i_gsamp_bits($im_rgb, 18, 22, 1, 16, \@samples, 0, 0 .. 2), 12,
60 "i_gsamp_bits all channels - count")
61 or print "# ", Imager->_error_as_msg(), "\n";
62 is_deeply(\@samples, [ 65535, 0, 0, 65535, 0, 0,
63 0, 65535, 0, 0, 65535, 0 ],
64 "check samples retrieved");
66 is(Imager::i_gsamp_bits($im_rgb, 18, 22, 1, 16, \@samples, 0, 0, 2), 8,
67 "i_gsamp_bits some channels - count")
68 or print "# ", Imager->_error_as_msg(), "\n";
69 is_deeply(\@samples, [ 65535, 0, 65535, 0,
71 "check samples retrieved");
73 is(Imager::i_gsamp_bits($im_rgb, 18, 22, 1, 16, \@samples, 0, 0, 3), undef,
74 "i_gsamp_bits fail bad channel");
75 is(Imager->_error_as_msg(), 'No channel 3 in this image', 'check message');
77 is(Imager::i_gsamp_bits($im_rgb, 18, 22, 1, 17, \@samples, 0, 0, 2), 8,
78 "i_gsamp_bits succeed high bits");
79 is($samples[0], 131071, "check correct with high bits");
81 # write some samples back
89 is(Imager::i_psamp_bits($im_rgb, 18, 2, 16, [ 0 .. 2 ], \@wr_samples),
90 12, "write 16-bit samples")
91 or print "# ", Imager->_error_as_msg(), "\n";
93 is(Imager::i_gsamp_bits($im_rgb, 18, 22, 2, 16, \@samples, 0, 0 .. 2), 12,
95 or print "# ", Imager->_error_as_msg(), "\n";
96 is_deeply(\@samples, \@wr_samples, "check they match");
97 my $c = Imager::i_get_pixel($im_rgb, 18, 2);
98 is_color3($c, 0, 0, 255, "check it write to the right places");
102 my $oo16img = Imager->new(xsize=>200, ysize=>201, bits=>16);
103 ok($oo16img, "make a 16-bit oo image");
104 is($oo16img->bits, 16, "test bits");
105 isnt($oo16img->is_bilevel, "should not be considered mono");
106 # make sure of error handling
107 ok(!Imager->new(xsize=>0, ysize=>1, bits=>16),
108 "fail to create a 0 pixel wide image");
109 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
110 "and correct error message");
112 ok(!Imager->new(xsize=>1, ysize=>0, bits=>16),
113 "fail to create a 0 pixel high image");
114 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
115 "and correct error message");
117 ok(!Imager->new(xsize=>-1, ysize=>1, bits=>16),
118 "fail to create a negative width image");
119 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
120 "and correct error message");
122 ok(!Imager->new(xsize=>1, ysize=>-1, bits=>16),
123 "fail to create a negative height image");
124 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
125 "and correct error message");
127 ok(!Imager->new(xsize=>-1, ysize=>-1, bits=>16),
128 "fail to create a negative width/height image");
129 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
130 "and correct error message");
132 ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>0),
133 "fail to create a zero channel image");
134 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
135 "and correct error message");
136 ok(!Imager->new(xsize=>1, ysize=>1, bits=>16, channels=>5),
137 "fail to create a five channel image");
138 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
139 "and correct error message");
142 # https://rt.cpan.org/Ticket/Display.html?id=8213
143 # check for handling of memory allocation of very large images
144 # only test this on 32-bit machines - on a 64-bit machine it may
145 # result in trying to allocate 4Gb of memory, which is unfriendly at
146 # least and may result in running out of memory, causing a different
150 $Config{ptrsize} == 4
151 or skip("don't want to allocate 4Gb", 10);
152 my $uint_range = 256 ** $Config{intsize};
153 print "# range $uint_range\n";
154 my $dim1 = int(sqrt($uint_range/2))+1;
156 my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1, bits=>16);
157 is($im_b, undef, "integer overflow check - 1 channel");
159 $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1, bits=>16);
160 ok($im_b, "but same width ok");
161 $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1, bits=>16);
162 ok($im_b, "but same height ok");
163 cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
164 "check the error message");
166 # do a similar test with a 3 channel image, so we're sure we catch
167 # the same case where the third dimension causes the overflow
168 my $dim3 = int(sqrt($uint_range / 3 / 2))+1;
170 $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3, bits=>16);
171 is($im_b, undef, "integer overflow check - 3 channel");
173 $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3, bits=>16);
174 ok($im_b, "but same width ok");
175 $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3, bits=>16);
176 ok($im_b, "but same height ok");
178 cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
179 "check the error message");
181 # check we can allocate a scanline, unlike double images the scanline
182 # in the image itself is smaller than a line of i_fcolor
183 # divide by 2 to get to int range, by 2 for 2 bytes/pixel, by 3 to
184 # fit the image allocation in, but for the floats to overflow
185 my $dim4 = $uint_range / 2 / 2 / 3;
186 my $im_o = Imager->new(xsize=>$dim4, ysize=>1, channels=>1, bits=>16);
187 is($im_o, undef, "integer overflow check - scanline");
188 cmp_ok(Imager->errstr, '=~',
189 qr/integer overflow calculating scanline allocation/,
190 "check error message");
194 { # check the channel mask function
196 my $im = Imager->new(xsize => 10, ysize=>10, bits=>16);
198 mask_tests($im, 1.0/65535);
202 my $im = test_image();
203 my $im16 = $im->to_rgb16;
204 print "# check conversion to 16 bit\n";
205 is($im16->bits, 16, "check bits");
206 is_image($im, $im16, "check image data matches");
209 { # empty image handling
210 my $im = Imager->new;
211 ok($im, "make empty image");
212 ok(!$im->to_rgb16, "convert empty image to 16-bit");
213 is($im->errstr, "empty input image", "check message");
217 my $im = Imager->new(xsize => 10, ysize => 10, bits => 16);
218 image_bounds_checks($im);
222 my $im = Imager->new(xsize => 10, ysize => 10, bits => 16, channels => 3);
223 my @wr_samples = map int(rand 65536), 1..30;
224 is($im->setsamples('y' => 1, data => \@wr_samples, type => '16bit'),
225 30, "write 16-bit to OO image")
226 or print "# ", $im->errstr, "\n";
228 is($im->getsamples(y => 1, target => \@samples, type => '16bit'),
229 30, "read 16-bit from OO image")
230 or print "# ", $im->errstr, "\n";
231 is_deeply(\@wr_samples, \@samples, "check it matches");
236 unless ($ENV{IMAGER_KEEP_FILES}) {
237 unlink "testout/t021sixteen.log";