#!/usr/bin/env python

software_license="""
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
"""

import math, os, random
from gimpfu import *

RANDOM_SEED = 12345
IMAGE_SIZES = [ 50, 100, 200, 300, 320, 333, 400, 480, 500, 600, 640, 800, 999, 1024, 2048, 4000 ] 
STRIPE_SIZES = [ 3, 5, 10, 25 ]
CHECKER_SIZES = [ 3, 5, 10 ]
RANDOM_RANGE = [ 30, 50, 70, 100 ]
TEST_PATTERNS = ["Small Squares", "Leopard", "One Small Step...", "Rocks"]

### c is the bit depth
def random_color():
	c = 255
	return (random.choice(range(c)),random.choice(range(c)),random.choice(range(c)))

class TestImage:
	def __init__(self, x, y, path):
		self.x = x
		self.y = y
		self.type = "none"
		self.path = path
		self.filename = str(RANDOM_SEED) + "-" + self.type + "-" + str(x) + "x" + str(y) + ".bmp"
		img = gimp.Image(x, y, RGB)
		img.disable_undo()
		self.img = img
		l = gimp.Layer(img, "drawlayer", x, y, RGB_IMAGE, 100, NORMAL_MODE)	
		l.fill(WHITE_FILL)
		img.add_layer(l, 0)
		self.layer = l
		
	### Saves it and clears it
	def save(self):
		gimp.pdb.gimp_file_save(self.img, self.layer, os.path.join(self.path, self.filename), os.path.join(self.path, self.filename))
		self.layer.fill(WHITE_FILL)

	def make_all(self):
		self.create_filled()
		self.create_stripes()
		self.create_check()
		self.create_noise()
		self.create_gradient()
		self.create_patterns()

	def update_type(self, type):
		self.type = type
		self.filename = self.type + "-" + str(self.x) + "x" + str(self.y) + ".bmp"
		print "Generating:",self.type,"- Size:",str(self.x) + "x" + str(self.y)

	def create_filled(self):
		self.update_type("solid-filled")
		gimp.set_foreground(random_color())
		self.layer.fill(FOREGROUND_FILL)
		self.save()

	def create_stripes(self):
		### We'll make a few different line widths
		for csx in STRIPE_SIZES:
			gimp.set_foreground(random_color())
			for x in range( int((self.x / csx) / 2)):
				self.update_type("stripe" + str(csx))
				### Select squares
				pdb.gimp_rect_select(self.img, 2 * x * csx, 0, csx, self.y, 2, False, 0)
				### Fill square
				pdb.gimp_edit_bucket_fill(self.layer, 0, 0, 100, 255, True, 0, 0)	
				
			### Save image
			self.save()
		pdb.gimp_selection_none(self.img)

	def create_check(self):
		### We'll make a few different checker sizes
		for csx in CHECKER_SIZES:
			for csy in CHECKER_SIZES:
				self.update_type("check" + str(csx) + "x" + str(csy))
				for i in range(int((self.x / csx) / 2 )):
					for j in range(int(self.y / csy)):	
						gimp.set_foreground(random_color())
						### Select squares
						if (j & 1):
							pdb.gimp_rect_select(self.img, ((2 * i) + 1) * csx, j * csy, csx, csy, 2, False, 0)
						else:
							pdb.gimp_rect_select(self.img, 2 * i * csx, j * csy, csx, csy, 2, False, 0)
						### Fill square
						pdb.gimp_edit_bucket_fill(self.layer, 0, 0, 100, 255, True, 0, 0)	
				
				### Save image
				self.save()
		pdb.gimp_selection_none(self.img)

	def create_gradient(self):
		self.update_type("gradient-linear")
		### Set the Gradient to FG->BG
		gimp.set_foreground(random_color())
		gimp.set_background((255, 255, 255))
		### Render the gradient
		pdb.gimp_edit_blend(self.layer, 0, 0, 0, 100, 0, 0, False, False, 1, 0, True, 0, int(self.y / 2), self.x, int(self.y / 2))
		### Save image
		self.save()
		
		self.update_type("gradient-radial")
		### Set the Gradient to FG->BG
		gimp.set_foreground(random_color())
		gimp.set_background((255, 255, 255))
		### Render the gradient
		pdb.gimp_edit_blend(self.layer, 0, 0, 2, 100, 0, 0, False, False, 1, 0, True, int(self.x / 2), int(self.y / 2), self.x, int(self.y / 2))
		### Save image
		self.save()

		self.update_type("gradient-spiral")
		### Set the Gradient to FG->BG
		gimp.set_foreground(random_color())
		gimp.set_background((255, 255, 255))
		### Render the gradient
		pdb.gimp_edit_blend(self.layer, 0, 0, 9, 100, 0, 0, False, False, 1, 0, True, int(self.x / 2), int(self.y / 2), int(self.x / 2), int((3 * self.y) / 4))
		### Save image
		self.save()

	def create_noise(self):
		for n in RANDOM_RANGE:
			self.update_type("noise" + str(n))
			pdb.plug_in_randomize_hurl(self.img, self.layer, n, 1, False, RANDOM_SEED)

			### Save image
			self.save()

	def create_patterns(self):
		for p in TEST_PATTERNS:
			self.update_type("pattern-" + p)
			pdb.gimp_context_set_pattern(p)
			self.layer.fill(PATTERN_FILL)
			self.save()

def create_test_data(path):
	### Make the results reproducible
	random.seed(RANDOM_SEED)
	rstate = random.getstate()
		
	print "\nGenerating test data for random seed:",RANDOM_SEED	

	for x in IMAGE_SIZES:
		for y in IMAGE_SIZES:
			### Set the state back
			random.setstate(rstate)		
			### Create the images at this size
			c = TestImage(x, y, path)
			c.make_all()
	print "\n Done!"

register(
	"ece2140",
	"Create ECE2140 Test Data",
	"Create ECE2140 Test Data",
	"Joseph Jezak",
	"Joseph Jezak",
	"2006",
	"<Toolbox>/Xtns/Languages/Python-Fu/_ECE2140",
	"",
	[
		(PF_STRING, "path", "Please provide the full path for the final data:", os.getcwd() + '/')
	],
	[],
	create_test_data
	)

main()
