#!/usr/bin/perl
#
# $Id: show.pl,v 1.2 2000/01/26 23:52:29 tim Exp $
#
# Display a MrSID Image with controls for changing it's size and
# zoom percentage.
# The page this image is displayed in is based on a template
# provided by the owner of the image.

require "config.pl";
require "getInfo.pl";
require "cgi-lib.pl";

# Relative path to the script which will display the image.
$imagescript = "./image_jpeg.pl";

# This script.
$formAction = "show.pl";

# Make the output unbuffered.
$| = 1;

# Get the form data.
$rc = &ReadParse;

if (!(defined ($in{client}) && defined ($in{image}))) {
	# Required params are missing from the request.
	print "Content-type: text/html\n\n";
	print "<html><head><title>MrSID Extension</title></head><body>\n";
	print "<b>ERROR 101</b> = Either a category or an image filename or both ";
	print "are missing from the request.<br>\n";
	print "</body></html>";
	
	exit (0);
}
$client = $in{client};
$image  = $in{image};

# If this is the first time, initialize the information about
# the image.
if (!(defined ($in{levels}))) {
  &SetDefaultValues;
}

# Get the form data into nicer variables.
$levels     = $in{levels};
$level      = $in{level};
$lastlevel  = $in{lastlevel};
$fullheight = $in{fullheight};
$fullwidth  = $in{fullwidth};
$originx    = $in{originx};
$originy    = $in{originy};
$size       = $in{size};

# The x,y of the user's click is adjusted by the origin of the
# image to account for cases when only part of the full image
# is displayed.
$x = (int ($in{"image.x"}) * (2 ** $lastlevel)) + $originx;
$y = (int ($in{"image.y"}) * (2 ** $lastlevel)) + $originy;

$numsizes = scalar (@sizes);

# Some error checking.
if ($size >= $numsizes) { $size = $numsizes - 1; }
if ($size <  0)         { $size = 0; }

# Get the image height & width requested by the user.
($height, $width) = &SizeToHW ($size);

# Figure out the height and width of the image at this
# zoom level.
$scaledheight = int ($fullheight / (2 ** $level)) + 1;
$scaledwidth  = int ($fullwidth  / (2 ** $level)) + 1;

# If the image size (at the current zoom level) is smaller
# than what the user requested, use the image size.  Also,
# change the x & y to keep the image centered.
if ($height > $scaledheight) {
  $height = $scaledheight;
  $y      = int ($fullheight / 2);
}

if ($width  > $scaledwidth) {
  $width = $scaledwidth;
  $x     = int ($fullwidth / 2);
}

# Figure out the origin of the image (the x,y of the
# upper left hand corner).
$originx = $x - (($width  / 2) * (2 ** $level));
$originy = $y - (($height / 2) * (2 ** $level));

# If the origin point is outside the image, re-adjust the
# center of the image so it is not.  This avoids having
# black space around the image.
if ($originx < 0) {
  $x += abs ($originx);
  $originx = 0;
}

if ($originy < 0) {
  $y += abs ($originy);
  $originy = 0;
}

# Figure out the x,y of the lower right hand corner.
$lrx = $x + (($width  / 2) * (2 ** $level));
$lry = $y + (($height / 2) * (2 ** $level));

# If the lower right hand corner is outside the image, re-adjust
# the center of the image so it is not.  This avoids having
# black space around the image.
if ($lrx > $fullwidth) {
  $x -= $lrx - $fullwidth;
}

if ($lry > $fullheight) {
  $y -= $lry - $fullheight;
}

# Output the Page.
&ShowImagePage;

exit (0);
#########################################################
# End of mainline
#########################################################


sub ShowImagePage {

  # Open and send out the beginning of the template.
  $template= "../$client/$imageTemplateFile";

  $rc = open (TEMPLATE, $template);

  if (!$rc) {
    print "Content-Type: text/html\n\n";
    print "unable to open '$templateFile', $!\n";
    exit (0);
  }

  print "Content-Type: text/html\n";
  print "\n";

  while (<TEMPLATE>) {

    # Look for the "IMAGEHERE" string, this marks where the
    # dynamic data goes.
    
    if (/IMAGEHERE/) { last; }
    print;
  }


  # The dynamic portion consists of the image at current size, level & etc.,
  # a selector for the next size, and the next level.  
  # Maintains state between requests.
  # print "<p><center>\n";
  print "<form action=\"$formAction\" method=post>\n";
  print "<input type=hidden name=client     value=$client>\n";
  print "<input type=hidden name=image      value=$image>\n";
  print "<input type=hidden name=levels     value=$levels>\n";
  print "<input type=hidden name=originx    value=$originx>\n";
  print "<input type=hidden name=originy    value=$originy>\n";
  print "<input type=hidden name=lastlevel  value=$level>\n";
  print "<input type=hidden name=fullheight value=$fullheight>\n";
  print "<input type=hidden name=fullwidth  value=$fullwidth>\n";


  # Setup for outputting the lists.
  if ($dropDowns) {
    $begintag = "<option";
    $endtag   = "</option>";
    $thisone  = "selected";
  } else {
    $begintag = "<input type=radio name=level";
    $endtag   = "";
    $thisone  = "checked";
  }

  # HTML
  print "<table border=0 cellpadding=0 cellspacing=0 align=center><tr><td align=center>";
 
  # The image
  $imageref = "$imagescript?client=$client&image=$image&x=$x&y=$y&" .
              "level=$level&width=$width&height=$height";
  
  $percent = int (100 / (2 ** $level));
  
  print "<font size=2 face=verdana,arial,clean>";
  print "<b>$width x $height pixels - ", $percent,
        "% zoom<br>\n";
  print "<table border=2 bgcolor=000000 width=450 cellpadding=4><tr><td align=center>";
  print "<input type=image name=image src=\"$imageref\" border=0 width=\"$width\" height=\"$height\"></b></font>\n";
  

  # Selector for the next resolution (level).
  print "</td></tr><tr><td>";
  print "<table border=0 width=100% cellpadding=3 cellspacing=0 bgcolor=eoeoeo>\n";
  print "<tr><TD ALIGN=\"CENTER\" VALIGN=\"TOP\" bgcolor=eoeoeo>\n";
  print "<font face=verdana,arial,clean size=1>Zoom to\n";

  # Make the default for the next level one less than the
  # current, unless we are at the smallest level.
  $nextlevel = $level;
  if ($nextlevel > 0) { $nextlevel--; }

  if ($dropDowns) { print "<select name=level size=1>\n"; }

  for ($i = 0; $i < $levels; $i++) {
    $percent = int (100 / (2 ** $i));

    print "$begintag value=$i";
    if ($i == $nextlevel) { 
    	print " $thisone"; 
    }
    print "> $percent $endtag\n";
  }

  if ($dropDowns) { 
  	print "</select>\n"; 
  }

  print "(percent)</td><td bgcolor=eoeoeo>\n";

  # Selector for the next Image size.
  print "<font face=verdana,arial,clean size=1>Size\n";

  if ($dropDowns) { 
  	print "<select name=size size=1>\n"; 
  } else { 
  	$begintag = "<input type=radio name=size";
  }

  for ($i = 0; $i < $numsizes; $i++) {
    print "$begintag value=$i";
    if ($i == $size) { 
    	print " $thisone"; 
    }
    print "> $sizes[$i] $endtag\n";
  }

  if ($dropDowns) { 
  	print "</select>\n"; 
  }

  print "(pixels)</td></tr></table>";
  print "</td></tr><tr><td bgcolor=oooooo align=center>";
  print "<font face=verdana,arial,clean size=1 color=ffffff>";
  print "Set the zoom and size then click an area on the image</td></tr></table>";
  print "</td></tr></table>";

  # End of the form.
  print "<table border=0 align=center><tr><td align=center><font size=1 face=verdana,arial,helvetica,clean>";

  print "</form>";
  print "</td></tr></table>";
  print "</td></tr></table>";



  # Now the rest of the template
  while (<TEMPLATE>) { print; }

  close (TEMPLATE);
}


# Set the default values for all needed incoming form data.
# This is only used the first time the image is viewed.
sub SetDefaultValues {

  local ($scaledheight, $scaledwidth);
  local ($filename, $levels, $imgHeight, $imgWidth, $nbands, $version);
  
  ($filename, $levels, $imgHeight, $imgWidth, $nbands, $version) = &GetImageInfo;
  	
  $scaledwidth  = $imgWidth / (2 ** $levels);
  $scaledheight = $imgHeight / (2 ** $levels);

  $in{levels}     = $levels;
  $in{level}      = $in{levels} - 1;
  $in{lastlevel}  = $in{level};
  $in{size}       = $window;
  $in{"image.x"}  = ($imgWidth / 2) / (2 ** $in{level});
  $in{"image.y"}  = ($imgHeight / 2) / (2 ** $in{level});
  $in{originx}    = 0;
  $in{originy}    = 0;
  $in{fullwidth}  = $imgWidth;
  $in{fullheight} = $imgHeight;
}


# Convert a size to a height and width.
sub SizeToHW {
  local ($size) = $_[0];

  $sizes[$size] =~ /(\d+)\s*x\s*(\d+)/;

  ($2, $1);
}
#########################################################
# End of Program
#########################################################
