1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
# by Dobrica Pavlinusic <dpavlin@rot13.org> 2001-06-14 |
4 |
|
|
# |
5 |
|
|
# rotate jpeg using jpegtran with GTK interface |
6 |
|
|
|
7 |
|
|
use Gtk; |
8 |
|
|
use strict; |
9 |
|
|
|
10 |
|
|
init Gtk; |
11 |
|
|
set_locale Gtk; |
12 |
|
|
|
13 |
|
|
my $false = 0; |
14 |
|
|
my $true = 1; |
15 |
|
|
|
16 |
|
|
my $xv_pic=".xvpics/0610_011.jpg"; |
17 |
|
|
|
18 |
|
|
$xv_pic = $ARGV[0] if ($ARGV[0]); |
19 |
|
|
|
20 |
|
|
my ($w,$h,$cols) = (0,0,0); |
21 |
|
|
my @xv_pix; |
22 |
|
|
|
23 |
|
|
open(XV,$xv_pic) || die "$xv_pic: $!"; |
24 |
|
|
my $l=<XV>; chomp $l; |
25 |
|
|
die "$xv_pic not XV thumbnail file ($l)" if ($l!~m/^P7 332$/); |
26 |
|
|
while(<XV>) { |
27 |
|
|
chomp; |
28 |
|
|
next if (/^#/); |
29 |
|
|
if (/^(\d+) (\d+) (\d+)$/) { |
30 |
|
|
($w,$h,$cols) = ($1,$2,$3); |
31 |
|
|
last; |
32 |
|
|
} |
33 |
|
|
print STDERR "--$_--\n"; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
warn "$xv_pic number of colors is not 255 (strange)" if ($cols != 255); |
37 |
|
|
|
38 |
|
|
my $buf; # tmp buffer for read; |
39 |
|
|
|
40 |
|
|
# read pixels |
41 |
|
|
read XV,$buf,($w*$h); |
42 |
|
|
@xv_pix=unpack("C*",$buf); |
43 |
|
|
|
44 |
|
|
#/* width height ncolors chars_per_pixel */ |
45 |
|
|
my @xpm_0= ( "$w $h 256 2" ); |
46 |
|
|
my @xpm_180= ( "$w $h 256 2" ); |
47 |
|
|
my @xpm_90= ( "$h $w 256 2" ); |
48 |
|
|
my @xpm_270= ( "$h $w 256 2" ); |
49 |
|
|
|
50 |
|
|
# Generate 332 colour-cube colourmap |
51 |
|
|
my @xpm_colmap; |
52 |
|
|
for (my $i=0; $i<256; $i++) { |
53 |
|
|
|
54 |
|
|
push(@xpm_colmap,sprintf ("%02x c #%02x%02x%02x",$i, |
55 |
|
|
(255*(($i&(7<<5))>>5))/7, |
56 |
|
|
(255*(($i&(7<<2))>>2))/7, |
57 |
|
|
(255*(($i&(3<<0))>>0))/3 |
58 |
|
|
)); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
push @xpm_0,@xpm_colmap; |
62 |
|
|
push @xpm_180,@xpm_colmap; |
63 |
|
|
push @xpm_90,@xpm_colmap; |
64 |
|
|
push @xpm_270,@xpm_colmap; |
65 |
|
|
|
66 |
|
|
my @pix_0; |
67 |
|
|
my @pix_180; |
68 |
|
|
my @pix_90; |
69 |
|
|
my @pix_270; |
70 |
|
|
|
71 |
|
|
for(my $y=0; $y<$h; $y++) { |
72 |
|
|
for (my $x=0; $x<$w; $x++) { |
73 |
|
|
if (defined($xv_pix[$y*$w+$x])) { |
74 |
|
|
$pix_0[$y*$w+$x] = sprintf("%02x",$xv_pix[$y*$w+$x]); |
75 |
|
|
$pix_180[($h-$y-1)*$w+$x] = sprintf("%02x",$xv_pix[$y*$w+$x]); |
76 |
|
|
$pix_270[($w-$x-1)*$h+$y] = sprintf("%02x",$xv_pix[$y*$w+$x]); |
77 |
|
|
$pix_90[$x*$h+$y] = sprintf("%02x",$xv_pix[$y*$w+$x]); |
78 |
|
|
} else { |
79 |
|
|
print STDERR "undef pixel $x,$y!\n"; |
80 |
|
|
} |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
# fill in pixels now |
86 |
|
|
|
87 |
|
|
my $tmp; |
88 |
|
|
|
89 |
|
|
# portrait |
90 |
|
|
my $lw=$w*2; |
91 |
|
|
$tmp=join('',@pix_0); while ($tmp=~s/^(.{$lw})//) { push(@xpm_0,$1); } |
92 |
|
|
$tmp=join('',@pix_180); while ($tmp=~s/^(.{$lw})//) { push(@xpm_180,$1); } |
93 |
|
|
|
94 |
|
|
# landscape |
95 |
|
|
$lw=$h*2; |
96 |
|
|
$tmp=join('',@pix_90); while ($tmp=~s/^(.{$lw})//) { push(@xpm_90,$1); } |
97 |
|
|
$tmp=join('',@pix_270); while ($tmp=~s/^(.{$lw})//) { push(@xpm_270,$1); } |
98 |
|
|
|
99 |
|
|
my $window; |
100 |
|
|
my ($pixmapwid_0, $pixmapwid_180, $pixmapwid_90, $pixmapwid_270); |
101 |
|
|
my ($button_0, $button_180, $button_90, $button_270); |
102 |
|
|
my ($pixmap_0, $pixmap_180, $pixmap_90, $pixmap_270); |
103 |
|
|
my $mask; |
104 |
|
|
my $style; |
105 |
|
|
|
106 |
|
|
$window = new Gtk::Window( "toplevel" ); |
107 |
|
|
$window->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } ); |
108 |
|
|
$window->border_width( 10 ); |
109 |
|
|
$window->show(); |
110 |
|
|
|
111 |
|
|
my $hbox = new Gtk::HBox( $true, 0 ); |
112 |
|
|
|
113 |
|
|
# now for the pixmap from gdk |
114 |
|
|
$style = $window->get_style()->bg( 'normal' ); |
115 |
|
|
( $pixmap_0, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_0 ); |
116 |
|
|
|
117 |
|
|
# a pixmap widget to contain the pixmap |
118 |
|
|
$pixmapwid_0 = new Gtk::Pixmap( $pixmap_0, $mask ); |
119 |
|
|
$pixmapwid_0->show(); |
120 |
|
|
|
121 |
|
|
# a button to contain the pixmap widget |
122 |
|
|
$button_0 = new Gtk::Button(); |
123 |
|
|
$button_0->add( $pixmapwid_0 ); |
124 |
|
|
$button_0->show(); |
125 |
|
|
$button_0->signal_connect( "clicked", sub { print( "button 0 clicked\n" ); } ); |
126 |
|
|
|
127 |
|
|
# rotate 180 |
128 |
|
|
( $pixmap_180, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_180 ); |
129 |
|
|
|
130 |
|
|
$pixmapwid_180 = new Gtk::Pixmap( $pixmap_180, $mask ); |
131 |
|
|
$pixmapwid_180->show(); |
132 |
|
|
|
133 |
|
|
$button_180 = new Gtk::Button(); |
134 |
|
|
$button_180->add( $pixmapwid_180 ); |
135 |
|
|
$button_180->show(); |
136 |
|
|
$button_180->signal_connect( "clicked", sub { print( "button 180 clicked\n" ); } ); |
137 |
|
|
|
138 |
|
|
# rotate 90 |
139 |
|
|
( $pixmap_90, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_90 ); |
140 |
|
|
$pixmapwid_90 = new Gtk::Pixmap( $pixmap_90, $mask ); |
141 |
|
|
$pixmapwid_90->show(); |
142 |
|
|
|
143 |
|
|
$button_90 = new Gtk::Button(); |
144 |
|
|
$button_90->add( $pixmapwid_90 ); |
145 |
|
|
$button_90->show(); |
146 |
|
|
$button_90->signal_connect( "clicked", sub { print( "button 90 clicked\n" ); } ); |
147 |
|
|
|
148 |
|
|
# rotate 270 |
149 |
|
|
( $pixmap_270, $mask ) = Gtk::Gdk::Pixmap->create_from_xpm_d( $window->window, $style, @xpm_270 ); |
150 |
|
|
$pixmapwid_270 = new Gtk::Pixmap( $pixmap_270, $mask ); |
151 |
|
|
$pixmapwid_270->show(); |
152 |
|
|
|
153 |
|
|
$button_270 = new Gtk::Button(); |
154 |
|
|
$button_270->add( $pixmapwid_270 ); |
155 |
|
|
$button_270->show(); |
156 |
|
|
$button_270->signal_connect( "clicked", sub { print( "button 270 clicked\n" ); } ); |
157 |
|
|
|
158 |
|
|
# pack, show, draw |
159 |
|
|
|
160 |
|
|
$hbox->pack_start( $button_0, $false, $false, 0 ); |
161 |
|
|
$hbox->pack_start( $button_90, $false, $false, 0 ); |
162 |
|
|
$hbox->pack_start( $button_180, $false, $false, 0 ); |
163 |
|
|
$hbox->pack_start( $button_270, $false, $false, 0 ); |
164 |
|
|
|
165 |
|
|
$hbox->show(); |
166 |
|
|
$window->add($hbox); |
167 |
|
|
|
168 |
|
|
main Gtk; |
169 |
|
|
exit( 0 ); |
170 |
|
|
|