#!/usr/bin/perl

# May be nesessary to set undo_level to 0, before running this script

use Gimp;
use Gimp::Fu;
#use Gimp::PDL;

$SRC_DIR=" "; #insert full path to the source directory between quotes
opendir(SRC, $SRC_DIR) || die("Unable open directory $SRC_DIR.");
$DEST_DIR=" "; #insert full path to the source directory between quotes
$THMB_DIR="$DEST_DIR/THMB";
while ($FILE=readdir(SRC)){
  if ( $FILE =~ /jpg|JPG/ ) {
    my $image = file_jpeg_load(RUN_INTERACTIVE, "$SRC_DIR/$FILE", "$FILE");
    my $lay = $image->get_active_layer;
    $lay->gauss_rle(2,2,2);
    my $h1=$image->height;
    my $w1=$image->width;
    if ( $w1 > $h1 ) {
      my $w2=600;
      my $h2=$h1*600/$w1;
    }
    else {
      my $h2=600;
      my $w2=$w1*600/$h1;
    }
    $image->scale($w2, $h2);
    $lay->sharpen(30);
    $lay->file_jpeg_save("$DEST_DIR/mp_$FILE", "mp_$FILE", 0.75,0,1,0,"",0,1,0,0);
    $lay->gauss_rle(2,2,2);
    if ( $w2< $h2 ) {
      $h3=175;
      $w3=$w2*175/600;
    }
    else {
      $w3=175;
      $h3=600*175/$w2;
    }
    $image->scale($w3, $h3);
    $lay->sharpen(30);
    $lay->file_jpeg_save("$THMB_DIR/mp_$FILE", "mp_$FILE", 0.75,0,1,0,"",0,1,0,0);

  }
}
