1#!/usr/bin/perl
2
3use Imlib2;
4use Gtk;
5init Gtk;
6
7$loaded = 0;
8
9gui();
10parse_args();
11Gtk->main();
12
13sub gui
14{
15
16	$win = new Gtk::Window("toplevel");
17	$win->set_title("Imlib2::Perl Image browser!");
18	$win->set_default_size(500,340);
19	$win->set_policy(1,1,0);
20	$vbox = new Gtk::VBox(0,0);
21	$win->add($vbox);
22	$win->signal_connect("destroy", sub { Gtk->exit(0); });
23	$hbox = new Gtk::HBox(0,0);
24	$hbox->border_width(2);
25	$vbox->pack_start($hbox,1,1,0);
26	$da = new Gtk::DrawingArea();
27	$da->set_events("button_press_mask");
28	$hbox->pack_start($da,1,1,0);
29	Imlib2_Init();
30	$da->signal_connect("expose_event", \&render);
31	$da->signal_connect("button_press_event", \&button_press);
32	$scrolled_win = new Gtk::ScrolledWindow(undef, undef);
33	$scrolled_win->set_policy(-never, -automatic);
34	$hbox->pack_start($scrolled_win, 1, 1, 0);
35
36	$clist = new Gtk::CList(1);
37	$scrolled_win->add($clist);
38	$clist->set_usize(100,100);
39	$clist->set_column_auto_resize(0, 1);
40	$clist->signal_connect('select_row', \&select_clist);
41	$statusbar = new Gtk::Statusbar();
42	$vbox->pack_start($statusbar,0,0,0);
43
44	$popup = new Gtk::Menu();
45
46	$view_full = new Gtk::MenuItem("View Full Size");
47	$view_full->signal_connect("activate", \&view_full);
48	$popup->append($view_full);
49
50	$blur_menu = new Gtk::MenuItem("Blur");
51	$blur_menu->signal_connect("activate", \&blur);
52	$popup->append($blur_menu);
53
54	$sharpen_menu = new Gtk::MenuItem("Sharpen");
55	$sharpen_menu->signal_connect("activate", \&sharpen);
56	$popup->append($sharpen_menu);
57
58	$flip_h = new Gtk::MenuItem("Flip horizontal");
59	$flip_h->signal_connect("activate", sub { Imlib2::flip_horizontal() });
60	$popup->append($flip_h);
61
62	$flip_v = new Gtk::MenuItem("Flip vertical");
63	$flip_v->signal_connect("activate", sub { Imlib2::flip_vertical() });
64	$popup->append($flip_v);
65
66	$flip_d = new Gtk::MenuItem("Flip diagonal");
67	$flip_d->signal_connect("activate", sub { Imlib2::flip_diagonal() });
68	$popup->append($flip_d);
69
70	show_all $popup;
71	show_all $win;
72
73}
74
75sub select_clist {
76
77	($widget,$row,$col,$ev,$data) = @_;
78	$text = $widget->get_text($row,$col);
79	Imlib2::free_image() if $loaded;
80
81	$im = Imlib2::load_image ($path . "/" . $text);
82	if (!$im) {
83		$loaded = 0;
84		$statusbar->push(1,"Could not load image $text");
85		return;
86	}
87	Imlib2::set_context($im);
88	$w = Imlib2::get_width();
89	$h = Imlib2::get_height();
90
91	$statusbar->push(1,"File: $text - Width $w - Height $h");
92
93	if($w > $h) {
94		$mult = ($w / 300);
95		$h = $h/$mult;
96		$w = 300;
97		$da->size($w,$h);
98		$da->window->clear();
99		Imlib2::render_image_on_drawable_at_size(0,0,$w,$h);
100	} else {
101		$mult = ($h / 300);
102		$w = $w/$mult;
103		$h = 300;
104		$da->size($w,$h);
105		$da->window->clear();
106		Imlib2::render_image_on_drawable_at_size(0,0,$w,$h);
107	}
108
109	$loaded = 1 if !$loaded;
110}
111
112
113sub Imlib2_Init
114{
115	 $da->realize();
116         my $cmap = $da->get_colormap()->XCOLORMAP;
117	 my $visual = $da->get_visual()->XVISUAL;
118	 my $display = $da->window->XDISPLAY;
119	 Imlib2::context_set_display($display);
120	 Imlib2::context_set_colormap($cmap);
121	 Imlib2::context_set_visual($visual);
122	 Imlib2::context_set_drawable($da->window->XWINDOW);
123	 return 1;
124}
125
126
127sub render
128{
129
130	Imlib2::render_image_on_drawable_at_size(0,0,$w,$h) if $loaded;
131 	return;
132}
133
134sub button_press
135{
136	($da, $event) = @_;
137	return if ($event->{button} != 3);
138	return if !$loaded;
139	$popup->popup(undef,undef,$event->{button},$event->{time});
140	return;
141}
142
143sub parse_args
144{
145	if (!$ARGV[0]) {
146		$ARGV[0] = "./";
147		$path = $ARGV[0];
148	}
149
150	if($ARGV[0] && -d $ARGV[0]) {
151		$path = $ARGV[0];
152		foreach(`ls -1 $ARGV[0]`) {
153			chomp;
154			#check if its a file to begin with and then if its an image.
155			if (/(bmp|gif|xcf|jpg|jpeg|png|tif|tiff)$/i) {
156				if(-f $path . "/" . $_) {
157					$clist->append($_);
158				}
159			}
160		}
161	} else {
162		die "usage: browser.pl directory\n";
163	}
164}
165
166sub view_full
167{
168	system("./pfeh.pl $path/$text &");
169	return;
170}
171
172sub blur
173{
174	$blur = 0 if !$blur;
175	Imlib2::blur($blur+10);
176	return;
177}
178
179sub sharpen
180{
181	$sharpen = 0 if !$sharpen;
182	Imlib2::sharpen($sharpen+10);
183	return;
184}
185
186