import sys, os, site
import unittest
import logging

if __name__=='__main__':
	pythonPaths = ['../../extern/Imaging-1.1.5b2/PIL',
		'../../extern/Imaging-1.1.5b2/libImaging']
	# Add locations for PIL
	for p in pythonPaths:
		ap = os.path.abspath( p )
		if os.path.exists( ap ):
			site.addsitedir( ap )
		else:
			print 'ERROR: Could not find "%s".  Please edit "pythonPaths" in this script for your environment.' % ap
			sys.exit(1)
	print 'sys.path', sys.path
import ImageFont, ImageDraw, ImageColor, Image


_log = logging.getLogger( __name__ )


def save( image, name ):
	'''
	Save an image so I can see it. Feel free to comment this out.
	'''
	#return
	fname = 'zzz_'+name+'.png'
	_log.debug('saving test image "%s"', fname )
	image.save( fname )


def checkPixelColor( image, xy, expectColor, message=None ):
	'''
	assert that pixel x,y in 'image' is of the proper color.
	'''
	pixel = image.getpixel( xy )
	if isinstance( expectColor, str):
		expectedValue = ImageColor.getcolor( expectColor, image.mode)
	else:
		expectedValue = expectColor
#	print 'pixel', pixel, 'expect', expectedValue
	assert pixel == expectedValue, '%s : Expected color "%s" %s at point %s, but was actually %s.' \
		% (message, expectColor, expectedValue, xy, pixel)


def checkPixelPalette( image, xy, expectColorName, message=None ):
	'''
	assert that pixel x,y in 'image' is of the proper color.
	'''
	expectedRGB = ImageColor.getcolor( expectColorName, image.mode)

	paletteIndex = image.getpixel( xy )
#	print 'image.palette.palette=',image.palette.palette
	actualRGB = ( 	image.palette.palette[ paletteIndex*3 ],
					image.palette.palette[ paletteIndex*3 + 1],
					image.palette.palette[ paletteIndex*3 + 2] )
	assert expectedRGB == actualRGB, '%s : Expected color "%s" %s at point %s, but was actually palette index %s, rgb=%s.' \
		% (message, expectColorName, expectedRGB, xy, paletteIndex, actualRGB)


class MyPalette:
	def __init__(self):
		self._pal = []
		
	def addrgb( self, r, g, b ):
		index = len(self._pal) / 3
		self._pal.extend( [r, g, b] )
		return index
		
		
class ImageDrawTest( unittest.TestCase ):
	'''
	Test Python Imaging LIbrary.
	'''
	def testDrawRGB(self):
		'''
		Test PIL drawing functions in RGB mode
		'''
		mode = 'RGB'

		im = Image.new( mode, (100, 100), 'lightgrey' )
		try :
			draw = ImageDraw.Draw( im )
			checkPixelColor( im, (10,10), 'lightgrey', 'background should be grey' )
			
			# Check line and background colors
			draw.line( (50,0,  50,100), fill='darkblue')
			checkPixelColor( im, (50,50), 'darkblue', 'vertical line should be blue' )
			
			# Draw some lines for checking alignment
			draw.line( (0,50, 100,50), fill='orange')
			checkPixelColor( im, (50,50), 'orange', 'horizontal line should be orange' )
	
			draw.text( (50,45), 'hello', fill='red' )
			checkPixelColor( im, (50,50), 'red', 'text should be red' )

		finally:			
			save( im, 'testDrawRGB')
		
		
	def testDrawPaletteIndex(self):
		'''
		Test PIL drawing functions in Palette mode
		'''
		mode = 'P'
		
		pal = MyPalette()
		white 		= pal.addrgb(255, 255, 255)
		lightgrey 	= pal.addrgb(200, 200, 200)
		black 		= pal.addrgb(0, 0, 0)
		blue 		= pal.addrgb(0, 0, 255)
		green 		= pal.addrgb(0, 255, 0)
		red 		= pal.addrgb(255, 0, 0)

#		print pal._pal
		self.assertEquals( pal._pal, [255, 255, 255, 200, 200, 200, 0, 0, 0, 0, 0, 255, 0, 255, 0, 255, 0, 0] )
		self.assertEquals( white, 0 )
		self.assertEquals( lightgrey, 1 )
		self.assertEquals( black, 2 )
		self.assertEquals( blue, 3 )
		self.assertEquals( green, 4 )
		self.assertEquals( red, 5 )
		
		im = Image.new( mode, (100, 100), lightgrey )
		try:
			im.putpalette( pal._pal )
			checkPixelColor( im, (10,10), lightgrey, 'background should be grey' )
	
			draw = ImageDraw.Draw( im )
	
			draw.line( (50,0,  50,100), fill=blue )
			checkPixelColor( im, (50,50), blue, 'vertical line should be blue' )
	
			# Draw some lines for checking alignment
			draw.line( (0,50, 100,50), fill=green)
			checkPixelColor( im, (50,50), green, 'horizontal line should be green' )
	
			draw.text( (50,45), 'bye bye', red )
			_log.info( 'testDrawPaletteIndex: NOTE: I am not checking text color since it wasn"t working before.' )
			checkPixelColor( im, (50,50), red, 'text should be red' )
		finally:
			save( im, 'testDrawPalette')



if __name__=='__main__':

	testSuite = unittest.defaultTestLoader.loadTestsFromName( __name__ )
 	unittest.TextTestRunner(verbosity=2).run(testSuite)
