in reply to Why is Tk::Animation so slow?
#!/usr/bin/perl =head1 DESCRIPTION This program demonstrates how to load a large GIF animation in Perl Tk without having to wait forever. Thank you vr! Question: Why is Tk::Animation so slow? https://www.perlmonks.org/index.pl?node_id=11110499 Answer: Apparently...something horribly unoptimized happens inside Tk. https://www.perlmonks.org/index.pl?node_id=11110521 https://www.perlmonks.org/index.pl?node_id=11110545 =head1 SYNOPSIS On first run it downloads and saves a 28MB animated GIF from archive.org to the current directory. Subsequent runs use the local image. The image is Comet ISON grazing the sun taken with the Large Angle Coronagraph on NASA/ESA's Solar Heliospheric Observatory. =cut use autodie; use strict; use warnings; use HTTP::Tiny; use MIME::Base64; use Time::HiRes 'time'; use Imager ':handy'; use Tk; use Tk::Animation; use Tk::Photo; my $file = 'ghost_anim.gif'; my $path; if (-e $file) { print "Loading image $file \n"; open my $fh, '<', $file; $path = join '', <$fh> } else { my $http = 'http://web.archive.org/web/20131202114602if_/'. 'http://science.nasa.gov/media/medialibrary/2013/12/01/'. $file; print "Downloading image $http \n"; $path = HTTP::Tiny->new->get($http)->{content}; die 'Download failed!' unless $path; open my $fh, '>', $file; $fh->binmode; print $fh $path } my $size = length $path; my $mw = MainWindow->new; $mw->configure(-title => 'Comet ISON via SOHO LASCO C3'); my $top = $mw->Frame->pack; my $backdrop; my $start = time; my @frames = map { if ($backdrop) { # not 1st? my $mask = $_->copy; my @mask_plt = map NC(255, 255, 255), $_->getcolors; my $trans_idx = $_->tags(name => 'gif_trans_index'); my $left = $_->tags(name => 'gif_left'); my $top = $_->tags(name => 'gif_top'); $mask_plt[$trans_idx] = NC(0, 0, 0) if defined $trans_idx; $mask->setcolors(colors => \@mask_plt); $backdrop->compose( src => $_, mask => $mask, tx => $left, ty => $top ) } else { $backdrop = $_->to_rgb8 } $backdrop->write(data => \my $data, type => 'bmp'); $mw->Photo(-data => encode_base64($data)) } Imager->read_multi(data => $path); my $image = $mw->Animation; $image->add_frame(@frames); print join ' ', "\nImager & Tk::Animation took", time - $start, "seconds for $size bytes \n"; $top->Label(-image => $image)->pack; $image->start_animation; MainLoop
|
|---|