int bits
i_channel_list channels
AV *data_av
- int data_offset
- int pixel_count
+ i_img_dim data_offset
+ i_img_dim pixel_count
PREINIT:
STRLEN data_count;
size_t data_used;
data_count = av_len(data_av) + 1;
if (data_offset < 0) {
- croak("data_offset must by non-negative");
+ croak("data_offset must be non-negative");
}
if (data_offset > data_count) {
croak("data_offset greater than number of samples supplied");
RETVAL
undef_neg_int
-i_psamp(im, x, y, channels, data)
+i_psamp(im, x, y, channels, data, offset = 0, width = -1)
Imager::ImgRaw im
i_img_dim x
i_img_dim y
i_channel_list channels
i_sample_list data
+ i_img_dim offset
+ i_img_dim width
PREINIT:
i_img_dim r;
CODE:
- if (data.count % channels.count) {
- croak("channel count and data sample counts don't match");
- }
- r = x + data.count / channels.count;
i_clear_error();
+ if (offset < 0) {
+ i_push_error(0, "offset must be non-negative");
+ XSRETURN_UNDEF;
+ }
+ if (offset > 0) {
+ if (offset > data.count) {
+ i_push_error(0, "offset greater than number of samples supplied");
+ XSRETURN_UNDEF;
+ }
+ data.samples += offset;
+ data.count -= offset;
+ }
+ if (width == -1 ||
+ width * channels.count > data.count) {
+ width = data.count / channels.count;
+ }
+ r = x + width;
RETVAL = i_psamp(im, x, r, y, data.samples, channels.channels, channels.count);
OUTPUT:
RETVAL
undef_neg_int
-i_psampf(im, x, y, channels, data)
+i_psampf(im, x, y, channels, data, offset = 0, width = -1)
Imager::ImgRaw im
i_img_dim x
i_img_dim y
i_channel_list channels
i_fsample_list data
+ i_img_dim offset
+ i_img_dim width
PREINIT:
i_img_dim r;
CODE:
- if (data.count % channels.count) {
- croak("channel count and data sample counts don't match");
- }
- r = x + data.count / channels.count;
i_clear_error();
+ if (offset < 0) {
+ i_push_error(0, "offset must be non-negative");
+ XSRETURN_UNDEF;
+ }
+ if (offset > 0) {
+ if (offset > data.count) {
+ i_push_error(0, "offset greater than number of samples supplied");
+ XSRETURN_UNDEF;
+ }
+ data.samples += offset;
+ data.count -= offset;
+ }
+ if (width == -1 ||
+ width * channels.count > data.count) {
+ width = data.count / channels.count;
+ }
+ r = x + width;
RETVAL = i_psampf(im, x, r, y, data.samples, channels.channels, channels.count);
OUTPUT:
RETVAL
# to make sure we get expected values
use strict;
-use Test::More tests => 329;
+use Test::More tests => 345;
BEGIN { use_ok(Imager => qw(:handy :all)) }
my $psamp_outside_error = "Image position outside of image";
{ # psamp
print "# psamp\n";
- my $imraw = Imager::ImgRaw::new(10, 10, 3);
+ my $imraw = Imager::ImgRaw::new(10, 20, 3);
{
is(Imager::i_psamp($imraw, 0, 2, undef, [ 255, 128, 64 ]), 3,
"i_psamp def channels, 3 samples");
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");
+ is(Imager::i_psamp($imraw, 4, 6, undef, [ 0 .. 18 ], 1), 18,
+ "psamp with offset");
+ is_deeply([ Imager::i_gsamp($imraw, 0, 10, 6, undef) ],
+ [ (0) x 12, 1 .. 18 ],
+ "check result");
+ is(Imager::i_psamp($imraw, 4, 11, undef, [ 0 .. 18 ], 1, 3), 9,
+ "psamp with offset and width");
+ is_deeply([ Imager::i_gsamp($imraw, 0, 10, 11, undef) ],
+ [ (0) x 12, 1 .. 9, (0) x 9 ],
+ "check result");
}
{ # errors we catch
is(Imager::i_psamp($imraw, 6, 8, [ 0, 1, 3 ], [ 255, 128, 32 ]),
"negative y");
is(_get_error(), $psamp_outside_error,
"check error message");
- is(Imager::i_psamp($imraw, 0, 10, undef, [ 0, 0, 0 ]), undef,
+ is(Imager::i_psamp($imraw, 0, 20, undef, [ 0, 0, 0 ]), undef,
"y overflow");
is(_get_error(), $psamp_outside_error,
"check error message");
"check message");
# not the typemap
- ok(!eval { Imager::i_psamp($imraw, 9, 9, undef, [ 1 ]); 1 },
- "sample count mod channel count non-zero");
- like($@, qr/channel count and data sample counts don't match/,
- "check message");
+ is(Imager::i_psamp($imraw, 0, 8, undef, [ (0) x 3 ], -1), undef,
+ "negative offset");
+ is(_get_error(), "offset must be non-negative",
+ "check message");
+
+ is(Imager::i_psamp($imraw, 0, 8, undef, [ (0) x 3 ], 4), undef,
+ "too high offset");
+ is(_get_error(), "offset greater than number of samples supplied",
+ "check message");
}
print "# end psamp tests\n";
}
{ # psampf
print "# psampf\n";
- my $imraw = Imager::ImgRaw::new(10, 10, 3);
+ my $imraw = Imager::ImgRaw::new(10, 20, 3);
{
is(Imager::i_psampf($imraw, 0, 2, undef, [ 1, 0.5, 0.25 ]), 3,
"i_psampf def channels, 3 samples");
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");
+ is(Imager::i_psampf($imraw, 4, 6, undef, [ map $_/254.9, 0 .. 18 ], 1), 18,
+ "psampf with offset");
+ is_deeply([ Imager::i_gsamp($imraw, 0, 10, 6, undef) ],
+ [ (0) x 12, 1 .. 18 ],
+ "check result");
+ is(Imager::i_psampf($imraw, 4, 11, undef, [ map $_/254.9, 0 .. 18 ], 1, 3), 9,
+ "psampf with offset and width");
+ is_deeply([ Imager::i_gsamp($imraw, 0, 10, 11, undef) ],
+ [ (0) x 12, 1 .. 9, (0) x 9 ],
+ "check result");
}
{ # errors we catch
is(Imager::i_psampf($imraw, 6, 8, [ 0, 1, 3 ], [ 1, 0.5, 0.125 ]),
"negative y");
is(_get_error(), $psamp_outside_error,
"check error message");
- is(Imager::i_psampf($imraw, 0, 10, undef, [ 0, 0, 0 ]), undef,
+ is(Imager::i_psampf($imraw, 0, 20, undef, [ 0, 0, 0 ]), undef,
"y overflow");
is(_get_error(), $psamp_outside_error,
"check error message");
"check message");
# not the typemap
- ok(!eval { Imager::i_psampf($imraw, 9, 9, undef, [ 1 ]); 1 },
- "sample count mod channel count non-zero");
- like($@, qr/channel count and data sample counts don't match/,
- "check message");
+ is(Imager::i_psampf($imraw, 0, 8, undef, [ (0) x 3 ], -1), undef,
+ "negative offset");
+ is(_get_error(), "offset must be non-negative",
+ "check message");
+
+ is(Imager::i_psampf($imraw, 0, 8, undef, [ (0) x 3 ], 4), undef,
+ "too high offset");
+ is(_get_error(), "offset greater than number of samples supplied",
+ "check message");
}
print "# end psampf tests\n";
}
is_deeply([ $im->getsamples(y => 5, x => 0) ],
[ (0) x 9, 1 .. 6, (0) x 15 ], "check they were stored");
+ is($im->setsamples(y => 7, x => 3, data => [ 0 .. 18 ], offset => 1), 18,
+ "setsamples offset");
+ is_deeply([ $im->getsamples(y => 7) ],
+ [ (0) x 9, 1 .. 18, (0) x 3 ],
+ "check result");
+
+ is($im->setsamples(y => 8, x => 3, data => [ map $_ / 254.9, 0 .. 18 ],
+ offset => 1, type => 'float'),
+ 18, "setsamples offset (float)");
+ is_deeply([ $im->getsamples(y => 8) ],
+ [ (0) x 9, 1 .. 18, (0) x 3 ],
+ "check result");
+
is_deeply([ $im->setsamples(y => 6, x => 10, data => [ (0) x 3 ]) ],
[], "check out of range result (8bit)");
is($im->errstr, $psamp_outside_error, "check error message");