#!/usr/bin/perl -w
# by Dobrica Pavlinusic <dpavlin@rot13.org> 2001-06-14
#
# rotate jpeg using jpegtran with GTK interface
use Gtk;
use strict;
init Gtk;
set_locale Gtk;
my $false = 0;
my $true = 1;
my $xv_pic=".xvpics/0610_011.jpg";
$xv_pic = $ARGV[0] if ($ARGV[0]);
my ($w,$h,$cols) = (0,0,0);
my @xv_pix;
open(XV,$xv_pic) || die "$xv_pic: $!";
my $l=<XV>; chomp $l;
die "$xv_pic not XV thumbnail file ($l)" if ($l!~m/^P7 332$/);
while(<XV>) {
chomp;
next if (/^#/);
if (/^(\d+) (\d+) (\d+)$/) {
($w,$h,$cols) = ($1,$2,$3);
last;
}
print STDERR "--$_--\n";
}
warn "$xv_pic number of colors is not 255 (strange)" if ($cols != 255);
my $buf; # tmp buffer for read;
# read pixels
read XV,$buf,($w*$h);
@xv_pix=unpack("C*",$buf);
#/* width height ncolors chars_per_pixel */
my @xpm_0= ( "$w $h 256 2" );
my @xpm_180= ( "$w $h 256 2" );
my @xpm_90= ( "$h $w 256 2" );
my @xpm_270= ( "$h $w 256 2" );
# Generate 332 colour-cube colourmap
my @xpm_colmap;
for (my $i=0; $i<256; $i++) {
push(@xpm_colmap,sprintf ("%02x c #%02x%02x%02x",$i,
(255*(($i&(7<<5))>>5))/7,
(255*(($i&(7<<2))>>2))/7,
(255*(($i&(3<<0))>>0))/3
));
}
push @xpm_0,@xpm_colmap;
push @xpm_180,@xpm_colmap;
push @xpm_90,@xpm_colmap;
push @xpm_270,@xpm_colmap;
my @pix_0;
my @pix_180;
my @pix_90;
my @pix_270;
for(my $y=0; $y<$h; $y++) {
for (my $x=0; $x<$w; $x++) {
if (defined($xv_pix[$y*$w+$x])) {
$pix_0[$y*$w+$x] = sprintf("%02x",$xv_pix[$y*$w+$x]);
$pix_180[($h-$y-1)*$w+$x] = sprintf("%02x",$xv_pix[$y*$w+$x]);
$pix_270[($w-$x-1)*$h+$y] = sprintf("%02x",$xv_pix[$y*$w+$x]);
$pix_90[$x*$h+$y] = sprintf("%02x",$xv_pix[$y*$w+$x]);
} else {
print STDERR "undef pixel $x,$y!\n";
}
}
}
# fill in pixels now
my $tmp;
# portrait
my $lw=$w*2;
$tmp=join('',@pix_0); while ($tmp=~s/^(.{$lw})//) { push(@xpm_0,$1); }
$tmp=join('',@pix_180); while ($tmp=~s/^(.{$lw})//) { push(@xpm_180,$1); }
# landscape
$lw=$h*2;
$tmp=join('',@pix_90); while ($tmp=~s/^(.{$lw})//) { push(@xpm_90,$1); }
$tmp=join('',@pix_270); while ($tmp=~s/^(.{$lw})//) { push(@xpm_270,$1); }
my $window;
my ($pixmapwid_0, $pixmapwid_180, $pixmapwid_90, $pixmapwid_270);
my ($button_0, $button_180, $button_90, $button_270);
my ($pixmap_0, $pixmap_180, $pixmap_90, $pixmap_270);
my $mask;
my $style;
$window = new Gtk::Window( "toplevel" );
$window->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } );
$window->border_width( 10 );
$window->show();
my $hbox = new Gtk::HBox( $true, 0 );
# now for the pixmap from gdk
$style = $window->get_style()->bg( 'normal' );
( $pixmap_0, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_0 );
# a pixmap widget to contain the pixmap
$pixmapwid_0 = new Gtk::Pixmap( $pixmap_0, $mask );
$pixmapwid_0->show();
# a button to contain the pixmap widget
$button_0 = new Gtk::Button();
$button_0->add( $pixmapwid_0 );
$button_0->show();
$button_0->signal_connect( "clicked", sub { print( "button 0 clicked\n" ); } );
# rotate 180
( $pixmap_180, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_180 );
$pixmapwid_180 = new Gtk::Pixmap( $pixmap_180, $mask );
$pixmapwid_180->show();
$button_180 = new Gtk::Button();
$button_180->add( $pixmapwid_180 );
$button_180->show();
$button_180->signal_connect( "clicked", sub { print( "button 180 clicked\n" ); } );
# rotate 90
( $pixmap_90, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_90 );
$pixmapwid_90 = new Gtk::Pixmap( $pixmap_90, $mask );
$pixmapwid_90->show();
$button_90 = new Gtk::Button();
$button_90->add( $pixmapwid_90 );
$button_90->show();
$button_90->signal_connect( "clicked", sub { print( "button 90 clicked\n" ); } );
# rotate 270
( $pixmap_270, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_270 );
$pixmapwid_270 = new Gtk::Pixmap( $pixmap_270, $mask );
$pixmapwid_270->show();
$button_270 = new Gtk::Button();
$button_270->add( $pixmapwid_270 );
$button_270->show();
$button_270->signal_connect( "clicked", sub { print( "button 270 clicked\n" ); } );
# pack, show, draw
$hbox->pack_start( $button_0, $false, $false, 0 );
$hbox->pack_start( $button_90, $false, $false, 0 );
$hbox->pack_start( $button_180, $false, $false, 0 );
$hbox->pack_start( $button_270, $false, $false, 0 );
$hbox->show();
$window->add($hbox);
main Gtk;
exit( 0 );