Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Gtk2 Simple Image Fade

by zentara (Archbishop)
on Jan 19, 2009 at 17:43 UTC ( [id://737347]=sourcecode: print w/replies, xml ) Need Help??
Category: GUI Programming
Author/Contact Info zentara of perlmonks
Description: A very simple demo of fading between images. The one thing to note, is that composite on a pixbuf will be cumulative, so notice I reset the base pixbuf to it's original on every timer call. There may be better ways around the problem.

Also I chose an alpha increment and timer speed to be effective, yet not too cpu intensive.

Additionally note that I read in the images to be the same scale. In a more complex "slideshow", you will have to deal with the effects of compositing files of different sizes.

#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 '-init';
use LWP::Simple;


# cheap webget, no error reporting
if( ! -e 'earth.jpg' and ! -e 'moon.jpg'){

   print "downloading test images, please wait\n";
     foreach my $localfile('earth.jpg','moon.jpg'){
     mirror( "http://zentara.net/$localfile", $localfile );
  }
}


my $mw = Gtk2::Window->new;
$mw->signal_connect('destroy', sub { Gtk2->main_quit });

my $vp = Gtk2::Viewport->new(undef, undef);
$mw->add($vp);

my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale ('earth.jpg', 6
+00,600,0);
my $pixbufa = $pixbuf->copy();
#my ($x, $y) = ($pixbufa->get_width, $pixbufa->get_height);
#print "$x $y\n";
my $pixbuf1 = Gtk2::Gdk::Pixbuf->new_from_file_at_scale ('moon.jpg', 6
+00,600,0);
my $pixbufb = $pixbuf1->copy();
#my ($x1, $y1) = ($pixbufb->get_width, $pixbufb->get_height);
#print "$x1 $y1\n";

#$pixbuf1->composite ($pixbuf, $dest_x, $dest_y, $dest_width, $dest_he
+ight,
#       $offset_x, $offset_y, $scale_x, $scale_y, $interp_type, $overa
+ll_alpha)

# start with base image
my $toggle = 5;
my $alpha =  0;
$pixbufb->composite ($pixbufa, 0, 0, 600, 600,
       0, 0, 1, 1, 'nearest', $alpha);  # 0 <= alpha <= 255

my $image = Gtk2::Image->new_from_pixbuf ($pixbufa);
$vp->add($image);

$mw->set_title("fade demo");
$mw->show_all();

my $id = Glib::Timeout->add (100, \&sequence);

Gtk2->main;

sub sequence{
   
     $alpha += $toggle;
     print "$alpha\n"; 
     if ($alpha == 255){ $toggle = -5 }  
     if ($alpha == 0){ $toggle = 5 }  
   
    $pixbufb->composite ($pixbufa, 0, 0, 600, 600,
           0, 0, 1, 1, 'nearest', $alpha);  # 0 <= alpha <= 255
    
    $image->set_from_pixbuf ($pixbufa);

    Gtk2->main_iteration while Gtk2->events_pending;
    
    # prevents accumulation of pixbuf changes
    $pixbufa = $pixbuf->copy(); 
     
 return 1;
}
Replies are listed 'Best First'.
Re: Gtk2 Simple Image Fade
by zentara (Archbishop) on Jan 21, 2009 at 18:07 UTC
    Here is an updated version that will cycle thru any legal graphic file in the pwd, fading from one image to the next, slideshow fashion. It will read each file on every cycle thru the list, so you may want to store the pixbufs in a hash so they only are read once.
    #!/usr/bin/perl use strict; use warnings; use Gtk2 '-init'; # a list to get most of the standard graphic formats into a glob my @exts = qw(wbmp wmf jpeg jpg bmp gif pcx png pnm pbm pgm ppm tga targa xbm tiff tif xpm ); @exts = map { $_ = "*.$_" } @exts; #map to setup the glob below my @files = <@exts>; #non recursive if(scalar @files == 0){ die "No photo files found\n" } print "@files\n"; my $mw = Gtk2::Window->new; $mw->signal_connect('destroy', sub { Gtk2->main_quit }); my $vp = Gtk2::Viewport->new(undef, undef); $mw->add($vp); # start with base image my $alpha = 0; my $next = 0; my $pb_base = Gtk2::Gdk::Pixbuf->new_from_file_at_scale ($files[0], 60 +0,600,0); my $pb_base_orig = $pb_base->copy(); #get next pixbuf push (@files,shift(@files)); my $pb_next = Gtk2::Gdk::Pixbuf->new_from_file_at_scale ($files[0], 60 +0,600,0); my $image = Gtk2::Image->new_from_pixbuf ($pb_base); $vp->add($image); $mw->set_title("fade demo"); $mw->show_all(); my $id = Glib::Timeout->add (100, \&sequence); Gtk2->main; sub sequence{ if( $next ){ # set base to be the previous next $pb_base = $pb_next; $pb_base_orig = $pb_base->copy(); $image->set_from_pixbuf ($pb_base); Gtk2->main_iteration while Gtk2->events_pending; #get next pixbuf push (@files,shift(@files)); $pb_next = Gtk2::Gdk::Pixbuf->new_from_file_at_scale ($files[0], + 600,600,0); $next = 0; $alpha = 0; }else{ $alpha += 5; print "$alpha\n"; if ($alpha == 255){ $next = 1; } #$pixbuf1->composite ($pixbuf, $dest_x, $dest_y, $dest_width, $dest +_height, # $offset_x, $offset_y, $scale_x, $scale_y, $interp_type, $o +verall_alpha) $pb_next->composite ($pb_base, 0, 0, 600, 600, 0, 0, 1, 1, 'nearest', $alpha); # 0 <= alpha <= 255 $image->set_from_pixbuf ($pb_base); } Gtk2->main_iteration while Gtk2->events_pending; # prevents accumulation of pixbuf changes $pb_base = $pb_base_orig->copy(); return 1; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://737347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found