#!/usr/bin/env python

#   watermark - insert a watermark into an image
#   Copyright (C) 2006 Gopal V <gopalv82 -at- yahoo.com>
#
#    This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import math
from gimpfu import *
import sys, os, math

def do_watermark(image, drawable, watermark, position="rb", opacity=33.0):
	
	pdb.gimp_selection_clear(image)
	
	x = pdb.gimp_drawable_width(drawable)
	y = pdb.gimp_drawable_height(drawable)
	wm_img = pdb.gimp_file_load(watermark, watermark)
	wm_drawable = pdb.gimp_image_get_active_drawable(wm_img)
	wm_layer = pdb.gimp_layer_new_from_drawable(wm_drawable,image)
	pdb.gimp_image_add_layer(image, wm_layer, -1)
	pdb.gimp_layer_set_opacity(wm_layer, opacity)
	wx = pdb.gimp_drawable_width(wm_drawable)
	wy = pdb.gimp_drawable_height(wm_drawable)

	nx = 0
	ny = 0

	if position[0] == 'r':
		nx = x - wx
	else:
		nx = 0
	
	if position[1] == 'b':
		ny = y - wy
	else:
		ny = 0
	
	#print  "(%d, %d) ~ (%d, %d) = (%d, %d)" % (x, y, wx, wy, nx, ny)
	
	pdb.gimp_layer_translate(wm_layer, nx, ny)

	pdb.gimp_image_flatten(image)

	pdb.gimp_image_delete(wm_img)
	
	return

register(
    "python_fu_watermark",
    "Add a watermark to an image",
    "Add a watermark to an image",
    "Gopal V",
    "Gopal V",
    "2006",
    "<Image>/Python-Fu/Generic/Watermark...",
    "RGB*",
    [
		(PF_FILE, "watermark", "Watermark image:", "watermark.png"),
		(PF_RADIO, "position", "Position:", 'rb', \
					(("Left Top","lt"), ("Right Top", "rt"), \
					  ("Left Bottom", "lb"), ("Right Bottom", "rb"))),
		(PF_SLIDER, "opacity", "Opacity:",33.0, (0,100,1))
    ],
    [],
    do_watermark)


def do_batch_watermark(filename, watermark, newfilename, position, opacity):
	image = pdb.gimp_file_load(filename, filename)
	drawable = pdb.gimp_image_get_active_drawable(image)
	do_watermark(image, drawable, watermark, position, opacity)
	drawable = pdb.gimp_image_get_active_drawable(image)
	pdb.gimp_file_save( 
			image, 
			drawable,
			newfilename,
			newfilename)

register(
    "python_fu_batch_watermark",
    "Add a watermark to an image (in batch mode)",
    "Add a watermark to an image (in batch mode)",
    "Gopal V",
    "Gopal V",
    "2006",
    "<Toolbox>/", # None
    "RGB*",
    [
		(PF_FILE, "filename", "Original image:", "image.png"),
		(PF_FILE, "watermark", "Watermark image:", "watermark.png"),
		(PF_FILE, "newfilename", "Watermarked image:", "image_wm.png"),
		(PF_RADIO, "position", "Position:", 'rb', \
					(("Left Top","lt"), ("Right Top", "rt"), \
					  ("Left Bottom", "lb"), ("Right Bottom", "rb"))),
		(PF_SLIDER, "opacity", "Opacity:",33.0, (0,100,1))
    ],
    [],
    do_batch_watermark)

main()
