1  #!/bin/sh -- # A comment mentioning perl
2eval 'exec perl -S $0 ${1+"$@"}'
3        if 0;
4#
5# slide.pl: amusing example slideshow program for use with x11vnc -rawfb mode.
6#
7# E.g. x11vnc -rawfb map:/tmp/foo@640x480x32:ff/ff00/ff0000 -pipeinput slide.pl
8#
9# requires: jpegtopnm(1), (maybe LSB too).
10#
11
12@jpegs = qw(
13	dr_fun_new.jpg	canon.jpg	go_microsoft.jpg	jonathan2.jpg
14	michelle1.jpg	novm.jpg	photo-008.jpg		presrange.jpg
15);
16
17# Or:
18#	@jpegs = @ARGV;
19#	@jpegs = <*.jpg>;
20
21# this is x11vnc's -rawfb value:
22if ($ENV{X11VNC_RAWFB_STR} =~ m,:(.*)@(\d+)x(\d+)x(\d+),) {
23	$fb = $1;	# filename
24	$W = $2;	# width
25	$H = $3;	# height
26} else {
27	die "No usable X11VNC_RAWFB_STR\n";
28}
29
30open(FB, ">$fb") || die "$!";
31
32# make a solid background:
33$ones = "\377" x ($W * 4);
34$grey = "\340" x ($W * 4);
35for ($y = 0; $y < $H; $y++) {
36	print FB $grey;
37}
38
39# this is rather slow with many jpegs... oh well.
40foreach $pic (@jpegs) {
41	print STDERR "loading '$pic'	please wait ...\n";
42	open(JPEG, "jpegtopnm '$pic' 2>/dev/null|") || die "$!";
43	while (<JPEG>) {
44		next if /^P\d/;
45		if (/^(\d+)\s+(\d+)\s*$/) {
46			$Jpeg{$pic}{w} = $1;
47			$Jpeg{$pic}{h} = $2;
48		}
49		last if /^255$/;
50	}
51	$data = '';
52	while (<JPEG>) {
53		$data .= $_;
54	}
55	close(JPEG);
56
57	# need to put in a 4th 0 byte after RGB for 32bpp. 24bpp doesn't work.
58	# (MSB might be other way around).
59
60	$new = '';
61	for ($l = 0; $l < int(length($data)/3); $l++) {
62		$new .= substr($data, $l * 3, 3) . "\0";
63	}
64	$Jpeg{$pic}{data} = $new;
65	$data = ''; $new = '';
66
67	if ($pic eq $jpegs[0]) {
68		showpic(0);
69	}
70}
71
72$N = scalar(@jpegs);
73print STDERR "\nFinished loading $N images. Click Button or Spacebar for next.\n";
74$I = 0;
75
76while (<>) {
77	# read the next user input event, watch for button press or spacebar:
78	###last if /^Keysym.* [qQ] /;
79	next unless /^(Pointer.*ButtonPress|Keysym.*space.*KeyPress)/;
80	$I = ($I + 1) % $N;
81	showpic($I);
82}
83
84sub showpic {
85	my($i) = @_;
86
87	my $pic = $jpegs[$i];
88	my $h = $Jpeg{$pic}{h};
89	my $w = $Jpeg{$pic}{w};
90
91	my $dy = int(($H - $h)/2);
92	my $dx = int(($W - $w)/2);
93
94	print STDERR "showing pic $i: $pic\t$w x $h +$dy+$dx\n";
95
96	# clear screen:
97	seek(FB, 0, 0);
98	for ($y = 0; $y < $H; $y++) {
99		print FB $ones;
100	}
101
102	# insert new picture:
103	for ($y = 0; $y < $h; $y++) {
104		seek(FB, (($y + $dy) * $W + $dx) * 4, 0);
105		$line = substr($Jpeg{$pic}{data}, $y * $w * 4, $w * 4);
106		print FB $line;
107	}
108}
109
110close(FB);
111###unlink($fb);	# this (probably) won't kill x11vnc
112print STDERR "$0 done.\n";
113