#!/usr/bin/env python

from gimpfu import *
from os import sep
from math import ceil


def contact (cols, rows, width, height, spacing, keep_ratio, images, directory):
    """Creates  new width x height gimp image(s), and import several other
     disk images into it, like a "contact" copy of negatives 
    """
    image_names = []
    image_rotate = []
    i = 0
    images_ = images.split (",")
    if len (images_) == 1:
        images_ = images.split ()
    
    for name in images_:
        name_ = name.strip()
        image_rotate.append (0)
        if name_.isdigit() and len (name_) == 1:
            image_rotate [i - 1] = int(name_)
        else:
            image_names.append (name_ )
            image_rotate.append (0)
            i += 1
    layer_width = float (width - spacing) / cols  - spacing
    layer_height = float (height - spacing) / rows - spacing           
        
    imgs = []
    image_counter = 0
    for img_n in xrange (int (ceil (float (len (image_names)) / (cols * rows)))):
        if image_counter >= len (image_names):
            break
        
        imgs.append (pdb.gimp_image_new (width, height, RGB))
        img = imgs[img_n]
        
        

        background = pdb.gimp_layer_new (img, width, height, RGB, "background", 100, NORMAL_MODE)
        img.add_layer (background, 0)
        background.fill (BACKGROUND_FILL)
    
        
        layers = []
        i = 0
        for x in xrange (cols):
            for y in xrange (rows):
                if image_counter >= len (image_names):
                    break
                try:
                    thumb_img = pdb.gimp_file_load (directory + sep + image_names [image_counter],
                                                    image_names [image_counter]
                                                   )
                except:
                    print "contact.py - Image %s failed to load" %image_names [image_counter]
                    image_counter += 1
                    continue
                if len (thumb_img.layers) > 1:
                    thumb_img.flatten ()
                if image_rotate[image_counter]:
                    pdb.gimp_image_rotate (thumb_img, image_rotate [image_counter] - 1)
                if keep_ratio:
                    if (float (layer_width) / layer_height <= 
                        float (thumb_img.width) / thumb_img.height):
                        thumb_img.scale (layer_width, thumb_img.height / (float (thumb_img.width) / layer_width))
                    else:
                        thumb_img.scale ( thumb_img.width / (float (thumb_img.height) / layer_height), layer_height)
                else:
                    thumb_img.scale (layer_width, layer_height)
                
                drw = pdb.gimp_layer_new_from_drawable (thumb_img.layers[0], img)
                layers.append (drw)
                img.add_layer (drw, -1)
                i += 1     
                drw.set_offsets (spacing + x * (layer_width + spacing), spacing + y * (layer_height + spacing))
                pdb.gimp_drawable_set_name (drw, image_names [image_counter])
                image_counter += 1
                
        pdb.gimp_display_new (img)
    pdb.gimp_displays_flush()




register(
         "contact",
         "Contact Filter",
         "Tile up many images on disk on a large one",
         "Joao S. O. Bueno",
         "(k) All rites reversed - JS",
         "2003",
         "<Toolbox>/Xtns/Python-Fu/contact",
         "*",
         [
                (PF_INT, "cols", "How many cols of images", 2),
                (PF_INT, "rows", "How many rows of images", 2),
                (PF_INT, "width", "Image Width",1754),
                (PF_INT, "height", "Image Width",1240),
                (PF_INT,  "spacing", "Spacing between images", 80),
                (PF_TOGGLE, "keep_ratio", "Keep aspect ratio", 1),
                (PF_STRING, "images", "List of images separated by spaces.\n Put a \"1\" on the list to rotate previous image", ""),
                (PF_FILE, "directory", "Directory were to pick image files from", ".")
         ],
         [],
         contact)

main()
