#!/bin/perl
#
# $Id: frameit.pl,v 1.20 2003/08/07 20:29:19 dlk Exp $
# $Source: /home/dlk/src/frameit/RCS/frameit.pl,v $
#
# (C) Copyright 2000-2003, David L. Kensiski
#
# Take a bunch of Sony Mavica images and wrap HTML frames around them

use strict;
use Getopt::Std;

# Parse cmd args
my %opt;
my $usage = "Usage: $0 [-b base] [-d date] [-k] [-s subtitle] [-t title] [-v] [<jpg|mpg>...]\n";
&getopts("b:d:ks:t:v", \%opt) || die $usage;

# Misc options
my $verbose = $opt{v} || 0;

# Text inserts
my $date = $opt{d} || 'Date';
my $subtitle = $opt{s} || 'Subtitle';
my $title = $opt{t} || 'Title';

# URL base and stuff
die "$0: cannot use -b and -k at the same time\n" if ($opt{b} && $opt{k});
$opt{b} = 'http://www.kensiski.org' if ($opt{k});
my $base = $opt{b} || '';
chop($base) if ($base =~ m|/$|);
my $pixel = '/images/1pxclear.gif';
my $help = '/misc/fhelp.html';

warn "Options:
Verbose:  $verbose
Base:     $base
Title:    $title
Subtitle  $subtitle
Date:     $date\n\n" if ($verbose);

# JPG section
my $jfmt1  = '<A TARGET="right" HREF="%s">' . "\n";
my $jfmt2  = '<IMG SRC="%s" ALT=""><BR>' . "\n";
my $jfmt3e = '(%dK)</A> or <A TARGET="right" HREF="%s">(%dK)</A><BR>' . "\n";
my $jfmt3f = '(%dK)</A><BR>' . "\n";
my $jfmt4  = '%s <P>' . "\n\n";

# MPG section
my $mfmt1 = '<A HREF="%s"><IMG SRC="%s"><BR>' . "\n";
my $mfmt2 = '(%s)</A><BR>' . "\n";
my $mfmt3 = '%s<BR>(%d seconds)<P>' . "\n\n";

# index.html stuff
local *I;
my $ifile = 'index.html';
my $iheader = '<HTML>
<HEAD>
<TITLE>'.$title.'</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function Redir(url) {
  if (top.left) {
    redirRE = /(.*)\?(.*)/;
    if (redirRE.test(url)) {
        NewLoc = RegExp.$2;
        top.left.location = NewLoc;
    }
  }
}
-->
</SCRIPT>
</HEAD>

<FRAMESET ROWS="80,*" onLoad="Redir(self.location);">
  <FRAME SRC="top.html">
  <FRAMESET COLS="200,*">
    <FRAME NAME="left" SRC="all.html">
    <FRAME NAME="right" SRC="http://www.kensiski.org/misc/fhelp.html">
  </FRAMESET>
</FRAMESET>
';

# top.html stuff
local *T;
my $tfile = 'top.html';
my $theader = '<HTML>
<HEAD>
<TITLE>'.$title.'</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (top.location == self.location) {
    top.location.href = "index.html";
}
-->
</SCRIPT>
</HEAD>
<BODY TEXT="#66FFFF" BGCOLOR="#444444" LINK="#FFFFCC" VLINK="#FFFF99"
ALINK="#FF9900">
<CENTER>
<TABLE BORDER="0">
<TR>
<TD ALIGN="center">
<FONT SIZE="6" COLOR="#33CCFF">'.$title.'</FONT><BR>
'.$date.'
</TD>

<TD><IMG SRC="'.$base.$pixel.'" ALT="---" WIDTH="20"></TD>

<TD ALIGN="center">
<FONT COLOR="yellow">
[<A TARGET="left" HREF="all.html">Pictures</A>]
<BR>
[<A TARGET="left" HREF="all.html">Unedited</A>]
[<A TARGET="right" HREF="'.$base.$help.'">HELP</A>]
[<A TARGET="_top" HREF="../index.html">RETURN</A>]
</FONT>
</TD>
</TR>
<TR>
<TD COLSPAN="3" ALIGN="center">
Select from the above for different sets of pictures.
</TD>
</TR>
</TABLE>

</CENTER>
</BODY>
</HTML>
';

# all.html stuff
local *L;
my $lfile = 'all.html';
my $leftopen = 0;
my $lheader = '<HTML>
<HEAD>
<TITLE>'.$title.'</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (top.location == self.location) {
    top.location.href = "index.html?all.html";
}
-->
</SCRIPT>
</HEAD>
<BODY TEXT="#66FFFF" BGCOLOR="#444444" LINK="#FFFFCC" VLINK="#FFFF99"
ALINK="#FF9900">
<CENTER>
<H2><FONT COLOR="#33CCFF">'.$title.'</FONT><BR>
<FONT COLOR="#33FFFF">'.$subtitle.'</FONT></H2>

<P>'.$date.'</P>

';

my $lfooter = '</CENTER>
</BODY>
</HTML>
';


# Get file list
my @files;
if ($#ARGV < 0) {
	@files = sort <*.{jpg,JPG,jpeg,JPEG,mpg,MPG}>;
} else {
	@files=@ARGV;
}

die "$0: nothing to do\n" if ($#files < 0);

# Iterate over files
FILE:
foreach my $file ( @files ) {
	print "Processing $file\n" if ($verbose);

	# Get base filename
	my ($base, $ext);
	if ($file =~ /([^.]*)\.(.*)/) {
		$base = $1;
		$ext = $2;
		print " $base . $ext\n" if ($verbose);
	} else {
		warn "$0: unable to parse $file; skipping\n";
		next FILE;
	}

	# Handle Sony filename format
	my $prefix;
	if ($base =~ /(mvc-[0-9]+)[a-z]/) {
		$prefix = $1;
	} else {
		$prefix = $base;
	}

	# Scale jpgs
	if ($ext =~ /^(jpg|jpeg)$/i) {

		# First create a pnm
		system("djpeg \"$file\" > \"$base.pnm\"");
		my $ssize = (stat $file)[7];

		# Then scrape some numbers out of it
		my ($x, $y);
		my $data = `pnmfile \"$base.pnm\"`;
		if ($data =~ /(\d+) by (\d+)/) {
			($x, $y) = ($1, $2);
		} else {
			warn "$0: unable to get size of $file ($base.pnm); ",
				"skipping\n";
			next FILE;
		}
		my $port = ($x > $y) ? 0 : 1;
		print " size: $x x $y: ", $port ? "portrait\n" : "landscape\n"
			if ($verbose);
		
		# And calculate new x and y (maintain aspect ratio)
		my ($escale, $ex, $ey, $tscale, $tx, $ty);
		$escale = ($port ? $y : $x) / 640;
		$ex = int($x / $escale);
		$ey = int($y / $escale);
		$tscale = ($port ? $y : $x) / 128;
		$tx = int($x / $tscale);
		$ty = int($y / $tscale);

		print " e: $ex x $ey, t: $tx x $ty\n" if ($verbose);

		# Create e-size jpg (if necessary)
		my $esize=0;
		if ($escale != 1) {
			system("pnmscale -xsize $ex -ysize $ey \"$base.pnm\" | cjpeg > \"${prefix}e.${ext}\"");
			$esize = (stat "${prefix}e.${ext}")[7];
		}

		# Create t-size jpg
		system("pnmscale -xsize $tx -ysize $ty \"$base.pnm\" | cjpeg > \"${prefix}t.${ext}\"");
		my $tsize = (stat "${prefix}t.${ext}")[7];

		print " $ssize, $esize, $tsize bytes\n" if ($verbose);

		# Clean up
		unlink("$base.pnm") || warn "$0: unlik $base.pnm: $!\n";

		# Open index file if not already open;
		&openifile if (!$leftopen);

		# Write html
		if ($escale != 1) {
			printf L $jfmt1, "${prefix}e.${ext}";
			printf L $jfmt2, "${prefix}t.${ext}";
			printf L $jfmt3e, $esize/1024, $file, $ssize/1024;
			printf L $jfmt4, $file;
		} else {
			printf L $jfmt1, "${file}";
			printf L $jfmt2, "${prefix}t.${ext}";
			printf L $jfmt3f, $ssize/1024;
			printf L $jfmt4, $file;
		}
	}
	elsif ($ext =~ /^mpg$/i) {

		# Calculate size
		my $size = (stat $file)[7];
		$size /= 1024;
		my $msize = sprintf("%dK", $size);
		if ($size > 1024) {
			$size /= 1024;
			$msize = sprintf("%0.1fM", $size);
		}
		print " $msize\n" if ($verbose);

		# Open index file if not already open;
		&openifile if (!$leftopen);

		# Write html
		printf L $mfmt1, $file, "${prefix}t.jpg";
		printf L $mfmt2, $msize;
		printf L $mfmt3, $file;
	}
	else {
		warn "$0: $file: unknown type; skipping\n";
		print L "<!-- skipped $file -- unknown type -->\n\n";
	}
}

# Wrap up file stuff
if ($leftopen) {
	# Close left file
	warn "Closing $lfile\n" if ($verbose);
	print L $lfooter;
	close L || warn "$0: close $lfile: $!\n";

	# Create index file
	warn "Creating $ifile\n" if ($verbose);
	open(I, ">$ifile") || warn "$0: open $ifile: $!\n";
	print I $iheader;
	close I || warn "$0: close $ifile: $!\n";

	# Create top file
	warn "Creating $tfile\n" if ($verbose);
	open(T, ">$tfile") || warn "$0: open $tfile: $!\n";
	print T $theader;
	close T || warn "$0: close $tfile: $!\n";
}

# Open index file if not already open
sub openifile {
	# Have we opened our index file?
	if (!$leftopen) {
		warn "Opening $ifile\n" if ($verbose);
		open(L, ">$lfile") || die "$0: $lfile: $!\n";
		print L $lheader;
		$leftopen=1;
	}
}
