#!/usr/bin/perl use warnings; use Tk; use Tk::JPEG; use Tk::Zinc; use MIME::Base64; #my $imagein = shift || '1z-squatch.jpg'; my $imagein = shift || "No Image $!\n" ; my $mw = MainWindow->new(); $mw->idletasks; my $jscreen = $mw->screenwidth - 500; my $kscreen = $mw->screenheight - 500; $mw->geometry( "$jscreen" . "x" . "$kscreen" . "+20+20" ); # Only tested with jpeg. open( PIC, $imagein ) or die print "Need a jpeg \n"; binmode(PIC); my $buffer = ''; while () { $buffer .= $_; } close(PIC); my $image = encode_base64($buffer); my $zinc = $mw->Scrolled( 'Zinc', -scrollbars => 'osoe', -borderwidth => 0 )->pack( -side => 'left', -fill => 'both', -expand => 1 ); my $zoomframe = $mw->Frame()->pack( -side => 'left', -fill => 'y' ); my $label1 = $zoomframe->Label( -text => 'Zoom' )->pack( -side => 'top', -pady => 5 ); my $button1 = $zoomframe->Button( -text => '+', -command => [ \&zoom, 1 ] )->pack( -side => 'top', -pady => 5, -fill => 'x' ); my $button2 = $zoomframe->Button( -text => '-', -command => [ \&zoom, 0 ] )->pack( -side => 'top', -pady => 5, -fill => 'x' ); my $scale = 0; my $radio1 = $zoomframe->Radiobutton( -text => 'Resize', -variable => \$scale, -value => 0, -command => \&reset )->pack( -side => 'top', -pady => 5, -fill => 'x' ); my $radio2 = $zoomframe->Radiobutton( -text => 'Scale', -variable => \$scale, -value => 1, -command => \&reset )->pack( -side => 'top', -pady => 5, -fill => 'x' ); my $percent = '100%'; my $label2 = $zoomframe->Label( -textvariable => \$percent )->pack( -side => 'top', -pady => 5 ); my $factor1 = 4; my $factor2 = 4; my $photo = $zinc->Photo( -data => $image, -format => 'jpeg' ); my $group = $zinc->add( 'group', 1 ); my $realimage = $zinc->add( 'icon', $group, -position => [ 0, 0 ], -image => $photo, -tags => 'image' ); $zinc->configure( -scrollregion => [ $zinc->bbox('image') ] ); MainLoop; sub reset { $factor1 = 4; $factor2 = 4; $percent = '100%'; $zinc->remove('image'); $zinc->xview( 'moveto', 0 ); $zinc->yview( 'moveto', 0 ); $zinc->remove( 'group', 1 ); $group = $zinc->add( 'group', 1 ); $realimage = $zinc->add( 'icon', $group, -position => [ 0, 0 ], -image => $photo, -tags => 'image' ); $zinc->configure( -scrollregion => [ $zinc->bbox('image') ] ); } sub zoom { my ($zoomer) = @_; if ( $zoomer == 0 && $factor1 == 1 ) { return; } $mw->Busy; if ( $scale == 0 ) { if ($realimage) { $zinc->remove('image'); undef($realimage); } if ($zoomimage) { $zoomimage->delete; undef($zoomimage); } if ($subbedimage) { $subbedimage->delete; undef($subbedimage); } $zoomimage = $zinc->Photo; $subbedimage = $zinc->Photo; $zoomimage->blank; $subbedimage->blank; if ( $zoomer == 1 ) { $factor1++; } else { $factor1--; } $zoomimage->copy( $photo, -zoom => $factor1 ); $subbedimage->copy( $zoomimage, -subsample => $factor2 ); $zinc->xview( 'moveto', 0 ); $zinc->yview( 'moveto', 0 ); $realimage = $zinc->add( 'icon', $group, -position => [ 0, 0 ], -image => $subbedimage, -tags => 'image' ); $zinc->configure( -scrollregion => [ $zinc->bbox('image') ] ); } else { if ( $zoomer == 1 ) { $factor1++; } else { $factor1--; } my $newscale = $factor1 / $factor2; $zinc->xview( 'moveto', 0 ); $zinc->yview( 'moveto', 0 ); $zinc->scale( $group, $newscale, $newscale ); $zinc->configure( -scrollregion => [ $zinc->bbox('image') ] ); } $mw->Unbusy; $mw->update; $percent = ( ( $factor1 / $factor2 ) * 100 ) . '%'; }