#!/usr/bin/perl
#
# Reduce the resolution of an image.
#
# MWR 8/25/2023

$debug = 1;

$input = "./globe_east.jpg";
$output = "./output_lowres.jpg";
$factor = 32;

$tempfile = "./downsize.tmp";

# figure out the original size
$cmd = "identify $input";
$ret = exec_cmd($cmd);
@words = split(/\s+/, $ret);
$size = $words[2];
@size_words = split(/x/, $size);
$width = $size_words[0];
$height = $size_words[1];
if ($debug > 0) {
  printf " width %6d  height %6d \n", $width, $height;
}

# shrink it by $factor
$small_width = int($width / $factor);
$small_height = int($height / $factor);
$geom_string = sprintf "%dx%d", $small_width, $small_height;
$cmd = sprintf "convert $input -geometry %s $tempfile ", $geom_string;
$ret = exec_cmd($cmd);

# now expand back to the initial size
$geom_string = sprintf "%dx%d", $width, $height;
$cmd = sprintf "convert $tempfile -geometry %s $output ", $geom_string;
$ret = exec_cmd($cmd);



exit 0;

$cmd = "convert -geometry 64x64 $input $output ";
$ret = exec_cmd($cmd);




exit 0;







##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


