#!/usr/bin/perl


if    ($#ARGV != 2){
  print "jpghsplit: split jpg image vertically\n";
  print "needs 3 parameters: img-num1 img-num2 image\n";
}
else{
  my $tmpfile = "tmp-img.pnm";
  my $infname = $ARGV[2];
  my $fnum1 = $ARGV[0];
  my $fnum2 = $ARGV[1];
  print "split $infname\n";
  system ("jpegtopnm $infname > $tmpfile");
  
  my $imageinfo = `pnmfile $tmpfile`;
  if ($imageinfo =~ m/\D*(\d*)\D*(\d*)\D*(\d*).*/){
    use integer;
    my $width = $1;
    my $height = $2;
    print "image width=$width height=$height\n";
    my $half = $width/2;
    print "writing img$fnum1.jpg ...\n";
    system ("pnmcut -right $half tmp-img.pnm | pnmtojpeg >img$fnum1.jpg");
    print "writing img$fnum2.jpg ...\n";
    system ("pnmcut -left  $half tmp-img.pnm | pnmtojpeg >img$fnum2.jpg");
  }
  else{
    print "can't get image width/height\n";
  }
  unlink $tmpfile;
}
