#!/usr/bin/perl  
#Gimp script to make it easy to create wallets, 5x7's and 8x10's

 use Gimp ":auto"; 
 use Gimp::Fu; 

register 
  "ezphoto",                 # fill in name 
  "Create easy photo sheets",  # a small description 
  "Duplicate an image",       # a help text 
  "Kirk Patton",            # Your name 
  "Kirk Patton (c)",        # Your copyright 
  "Oct 16th, 2002",              # Date 
  "<Image>/Perl-Fu/EZ Photo",   # menu path 
  "*",                       # Image types 
  [ 
    [PF_STRING,   "dimentions", "wallet, 5x7, 8x10", "wallet"] 
  ], 
  sub {
      my($img,$layer,$portrate_size) = @_;
      
      my $portrate_ratio = chk_input($portrate_size, $img);

      #Generate a cropped image
      my $new_image = ratio(gimp_image_width($img),gimp_image_height($img), $img, $portrate_ratio);

      #Create new proportional 8.5x11 canvas
      $photo_sheet = create_8x11($new_image,$portrate_size,gimp_image_width($new_image),gimp_image_height($new_image));

      $photo_sheet = place_image($new_image,$photo_sheet,gimp_image_width($new_image),gimp_image_height($new_image));
     # return $new_image;
      return $photo_sheet;
  };
exit main(); 


sub ratio{
    my ($width,$height, $orig_img, $ratio) = @_;
    my $cropped_height;
    #Make sure the width is already less than the height
    chk_dimentions($width, $height);

    if($ratio == 1){$cropped_height = $height;}
    else { $cropped_height = sprintf("%1.f",$width / $ratio); }
    
    #Get drawable of original image
    my $draw = gimp_image_active_drawable($orig_img);

    #Create new image

    my $img = gimp_image_new($width, $cropped_height, RGB);

    #Create new layer
    my $layer = gimp_layer_new($img, $width, $cropped_height, RGB, "Layer 1", 100, NORMAL_MODE); 

    #Add layer to newly created image
    gimp_image_add_layer($img, $layer, -1);

    #Color the new layer
    gimp_edit_fill($layer, BG_IMAGE_FILL);

    #Select portion of original image equal to dimentions of new image
    gimp_rect_select($orig_img,0,0,$width,$cropped_height,ADD,0,0);

    #Copy selection
    gimp_edit_copy($draw);

    #Clear selection
    gimp_selection_none($orig_img);

    #Paste into new cropped image
    my $pasted = gimp_edit_paste($layer,0);

    #Anchor pasted image
    gimp_floating_sel_anchor($pasted);

    return $img;
}

sub chk_dimentions{
    my ($width, $height) = @_;
    if($width >= $height){
	gimp_message("This picture dimentions are reversed.");
	exit main();
    }
    return;
}

sub chk_input{
    my ($user_input, $img) = @_;
    my $ratio = "";
    my $width = gimp_image_width($img);
    my $height = gimp_image_height($img);
    #Check user input
    if($user_input =~ /^wallet$/i)    { 
        if( sprintf("%.3f", $width / $height) == .833){
	    return $ratio = 1;
	}
	else { return $ratio = .833; } 
    }
    elsif($user_input =~ /^5x7$/i) { 
	if( sprintf("%.2f", $width / $height) == .75 ){
	    return $ratio = 1;
	}
	else { return $ratio = .75; }  
}
    elsif($user_input =~ /^8x10$/i) { 
	if( sprintf("%.1f", $width / $height) == .8){
	    return $ratio = 1;
	}
	else {return  $ratio = .8; }
    }
    else{ 
	gimp_message("Portrate size incorrect");
	exit main();
    }
}

sub create_8x11 {
#Create a new area that is proportional in size to an 8x11" piece of paper
    my ($img,$size,$width,$height) = @_;
    my $wratio,$hratio,$sheet_width,$sheet_height;

#Set proper ratio for 8.5x11 based on current image size
    if($size =~ /wallet/){
	$wratio = 3.4;
	$hratio = 3.666;
    }
    elsif($size =~ /5x7/){
	$wratio = 1.7;
	$hratio = 1.57;
    }
    else{
	$wratio = 1.063;
	$hratio = 1.1;
    }

#Compute and round the new image size
    $sheet_width = sprintf("%1d",$width * $wratio);
    $sheet_height = sprintf("%1d",$height * $hratio);

#Create the new canvas
    my $photo_sheet = gimp_image_new($sheet_width, $sheet_height, RGB);

#Create new layer
    my $layer = gimp_layer_new($photo_sheet, $sheet_width, $sheet_height, RGB, "Layer 1", 100, NORMAL_MODE); 
    gimp_image_add_layer($photo_sheet, $layer, -1);

    #Color the new layer
    gimp_edit_fill($layer, BG_IMAGE_FILL);

    return $photo_sheet;
}

sub place_image{
    my ($cropped_img, $photo_sheet, $crop_width, $crop_height) = @_;
#Place image or images evenly spaced on new 8.5x11 canvas

#Get drawable of original image
    my $draw = gimp_image_active_drawable($cropped_img);

#Get active layer of photo sheet
    my $layer = gimp_image_get_active_layer($photo_sheet);

#Copy cropped image to buffer
    gimp_edit_copy($draw);

#Determine number of pics per sheet and place accordingly
    if(sprintf("%.3f",$crop_width / $crop_height) == .833){
        #We have wallets, need to place nine times...
	my $space = 100; # number of pixels to space between pictures
	for($y_loop = 0; $y_loop <= 2; $y_loop++){

	    for($x_loop = 0; $x_loop <= 2;$x_loop++){
		#Generate left and right coordinates for selection
		my $x_left = ($crop_width * $x_loop) + ($space * $x_loop); 
		my $y_left = ($crop_height * $y_loop) + ($space * $y_loop);
		my $x_right = ($crop_width * ($x_loop +1)) + ($space * $x_loop);
		my $y_right = ($crop_height * ($y_loop +1)) + ($space * $y_loop);

		#Make selection
		gimp_rect_select($photo_sheet,$x_left,$y_left,$x_right,$y_right,REPLACE,0,0);
		print "$x_left,$y_left,$x_right,$y_right\n";
                #Paste into new cropped image
		my $pasted = gimp_edit_paste($layer,0);

		#Anchor pasted image
		gimp_floating_sel_anchor($pasted);
	    }
	}
    }

    
return $photo_sheet;
}
