Skip to content

Commit

Permalink
also test singleframed codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
dk committed Nov 3, 2023
1 parent 28cd6bc commit 202d869
Showing 1 changed file with 94 additions and 12 deletions.
106 changes: 94 additions & 12 deletions t/Image/Loader.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ use Prima::sys::Test qw(noX11);
use Test::More;
use Prima::Image::Loader;

my @codecs;
my @names;

for ( @{ Prima::Image->codecs }) {
next unless $_->{canLoad} && $_->{canSave} && $_->{canSaveMultiple} && $_->{canLoadMultiple} && $_->{canSaveStream} && $_->{canLoadStream};
push @codecs, $_->{codecID};
push @names, $_->{fileShortType};
}
plan skip_all => 'No multiframe-capable codecs' unless @codecs;

sub ix($) {
Prima::Image->new(
Expand All @@ -24,7 +15,89 @@ sub ix($) {
}
my @ix = map { ix chr } 0..2;

sub test_codec
sub test_singleframe_codec
{
my ($codecID, $name) = @_;

my $buf1 = '';
open F, "+>", \ $buf1 or die $!;

my $ix = $ix[1];
my $ok = Prima::Image->save( \*F, images => [$ix], codecID => $codecID);
ok( $ok, "$name: traditional save".( $ok ? '' : ":$@"));

my $buf2 = '';
open G, "+>", \ $buf2 or die $!;

my ($s,$err) = Prima::Image::Saver->new( \*G, codecID => $codecID, frames => 1 ) ;
ok( $s, "$name: open_save".($s ? '' : ":$err"));

($ok,$err) = $s->save($ix);
ok($ok, "$name: save frame ".($ok ? '' : ":$err"));
undef $s;
close G;
ok( $buf1 eq $buf2, "$name: traditional and framed saves produce identical results");

seek(F,0,0);
my @nx = Prima::Image->load(\*F, loadAll => 1, loadExtras => 1 );
is( scalar(@nx), 1, "$name: traditional load returned 1 frame");
is( $nx[0] && $nx[0]->{extras} && $nx[0]->{extras}->{frames}, 1, "$name: extras.frames is okay");
$_->type(im::Byte) for grep { defined } @nx;
is($nx[0]->pixel(0,0), 1, "$name/loadAll: image is loaded correctly") if $nx[0];

seek(F,0,0);
@nx = Prima::Image->load(\*F);
$_->type(im::Byte) for grep { defined } @nx;
is( scalar(@nx), 1, "$name/index: noindex loaded ok");
is($nx[0]->pixel(0,0), 1, "$name/index: image is loaded correctly") if $nx[0];

seek(F,0,0);
@nx = Prima::Image->load(\*F, index => 0);
$_->type(im::Byte) for grep { defined } @nx;
is( scalar(@nx), 1, "$name/index: index 0 loaded ok");
is($nx[0]->pixel(0,0), 1, "$name/index: image is loaded correctly") if $nx[0];

my ($lx,$i);
$i = Prima::Image->new;
seek(F,0,0);
($ok,$err) = $i->load(\*F, index => 0);
$i->type(im::Byte);
ok( $ok, "$name/inplace: index 0 loaded ok");
is($i->pixel(0,0), 1, "$name/inplace: image is loaded correctly");

seek(F,0,0);
@nx = Prima::Image->load(\*F, map => [0]);
$_->type(im::Byte) for grep { defined } @nx;
is( scalar(@nx), 1, "$name/map: index 0 loaded ok");
is($nx[0]->pixel(0,0), 1, "$name/map: image 0 is loaded correctly") if $nx[0];

$i = Prima::Image->new;
seek(F,0,0);
@nx = $i->load(\*F, map => [], loadExtras => 1, wantFrames => 1);
$_->type(im::Byte) for grep { defined } @nx;
is(scalar(@nx), 0, "$name: traditional null load request is ok");
is($i->{extras}->{frames}, 1, "$name: traditional null load request says 1 frame");

seek(F,0,0);
($lx,$err) = Prima::Image::Loader->new(\*F);
ok( $lx, "$name: loader created".($ok?'':":$err"));

is($lx->frames, 1, "$name/loader: null load request says 1 frame");

ok(!$lx->eof, "$name/loader: lx(0) is not eof");
($i,$err) = $lx->next;
$i->type(im::Byte) if $i;
ok( $i, "$name/loader: image loaded".($i?'':":$err"));
is($i->pixel(0,0), 1, "$name/loader: image is loaded correctly")
if $i;
ok($lx->eof,"$name/loader: lx(1) is eof");
($i,$err) = $lx->next;
ok(!$i, "$name/loader: image(1) is null");

close F;
}

sub test_multiframe_codec
{
my ($codecID, $name) = @_;

Expand Down Expand Up @@ -116,9 +189,18 @@ sub test_codec
close F;
}

for ( my $i = 0; $i < @codecs; $i++) {
test_codec($codecs[$i], $names[$i]);
my $tests_run = 0;
for ( @{ Prima::Image->codecs }) {
next unless $_->{canSaveStream} && $_->{canLoadStream};
if ( $_->{canSaveMultiple} && $_->{canLoadMultiple} ) {
test_multiframe_codec($_->{codecID}, $_->{fileShortType});
$tests_run++;
} else {
test_singleframe_codec($_->{codecID}, $_->{fileShortType});
$tests_run++;
}
}

plan skip_all => 'no codecs to test' unless $tests_run;

done_testing;

0 comments on commit 202d869

Please sign in to comment.