#!/usr/bin/python
import os, glob
from gimpfu import *

def load_image(fn):
	img = pdb.gimp_file_load(fn, fn)
	if img.base_type != RGB:
		print "converting to RGB"
		pdb.gimp_convert_rgb(img)
	return img


#duplicate an image.  Screwed for images with alpha for some reason.
def duplicate(img, layer):
	newimg = pdb.gimp_image_new(layer.width, layer.height, img.base_type)
	newlayer = pdb.gimp_layer_new(newimg, layer.width, layer.height, layer.type, "Background", 100, 0)
	pdb.gimp_image_add_layer(newimg, newlayer, 0)

	pdb.gimp_edit_copy(layer)
	pdb.gimp_floating_sel_anchor(pdb.gimp_edit_paste(newlayer, 0))

	return newimg


def smart_enlarge_file(fn, scale_factor, vtile, htile):
	#duplicate() is screwed for images with alpha, so just load the image three times
	img = load_image(fn)
	small = load_image(fn)
	large = load_image(fn)
#	small = duplicate(img, layer)
#	large = duplicate(img, layer)
	limg = smart_enlarge(img, img.layers[0], scale_factor, vtile, htile, small, large)
	
	pdb.gimp_image_delete(img)
	pdb.gimp_image_delete(small)
	return limg


def smart_enlarge(img, layer, scale_factor, vtile, htile, small, large):
	width = layer.width
	height = layer.height

	#pdb.gimp_image_scale(small, width/scale_factor, height/scale_factor)
	pdb.gimp_image_scale(small, width/2, height/2) # using scale_factor as the divisor causes havoc with larger scale_factors and small images
	pdb.gimp_image_scale(small, width, height)

	pdb.gimp_image_scale(large, width*scale_factor, height*scale_factor)

#image, drawable, vtile, htile, noborder, corpus, inmask, outmask, random_weight, map_weight
	pdb['plug-in-resynthesizer'](large, large.layers[0], vtile, htile, 0, img.layers[0], small.layers[0], large.layers[0], 0.5, 1.0)

	return large

def do_smart_enlarge(fn, outputdir, scale_factor, vtile, htile):
	output_fn = os.path.join(outputdir, os.path.splitext(os.path.split(fn)[1])[0]+'.tga')
	if os.path.exists(output_fn):
		print output_fn,"exists, skipping",fn
		return
	print "enlarging",fn,"->",output_fn

	print 'calling smart_enlarge with scale_factor=%s vtile=%s htile=%s'%(scale_factor,vtile,htile)
	#limg = pdb.script_fu_smart_enlarge(img, img.layers[0], 2.0)
	limg = smart_enlarge_file(fn, scale_factor, vtile, htile)
	
	pdb.file_tga_save(limg, limg.layers[0], output_fn, output_fn, 0, 1)
	pdb.gimp_image_delete(limg)
	


def python_fu_batch_smart_enlarge(srcglob, outputdir, scale_factor=2.0, vtile=1, htile=1):
	print "python_fu_batch_smart_enlarge(%s,%s,%s,%s,%s)"%(srcglob, outputdir, scale_factor, vtile, htile)
	inputs = glob.glob(srcglob)
	for infn in inputs:	
		do_smart_enlarge(infn, outputdir, scale_factor, vtile, htile)
	print 'python_fu_batch_smart_enlarge %s -> %s done'%(srcglob, outputdir)

register(
		"python_fu_batch_smart_enlarge",
		"smart enlarge many images",
		"smart enlarge many images",
		"Matthew Mueller",
		"Matthew Mueller",
		"2003",
		"<Toolbox>/Xtns/Python-Fu/Batch smart enlarge...",
		"*",
		[
			(PF_STRING, "srcglob", "Source image glob", os.path.join("orig","*.tga")),
			(PF_STRING, "outputdir", "Output dir", "."),
#SF-ADJUSTMENT "Scaling factor" '(2 1 32 0.1 1.0 2 1)
			(PF_ADJUSTMENT, "scale_factor", "Scaling factor", 2, (1, 32, 0.1, 1.0, 2, 1)),
			(PF_BOOL, "vtile", "Make verticly tilable", 1),
			(PF_BOOL, "htile", "Make horizontally tilable", 1),
		],
		[],
		python_fu_batch_smart_enlarge)


main()

