summaryrefslogtreecommitdiff
path: root/layers.py
blob: ceaef1e5c6631b06e247007d216bc97283115ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from PIL import Image
from latLng import latLng

class layer:
	"""template for a GPS image layer"""
	tl=latLng()
	br=latLng()
	pixel=(0,0)
	def __init__(self,file,ll1,ll2):
		try:
			self.image=Image.open(file)
		except:
			print "gps layer: failed to open", file
		try:
			self.tl.parse(ll1)
			self.br.parse(ll2)
			self.pixsize=latLng(abs(self.tl.lat-self.br.lat)/self.image.size[1],abs(self.tl.lng-self.br.lng)/self.image.size[0])
		except:
			print "gps layer: failed to parse", file
	def checkcoord(self,pos):
		p=self.findpixel(pos)
		if p!=self.pixel:
			self.pixel=p
			return self.setcoord(p)
		else:
			return None
	def findpixel(self,pos):
		return (int((pos.lng-self.tl.lng)/self.pixsize.lng),int((pos.lat-self.br.lat)/self.pixsize.lat))
	def setcoord(self,pos):
		"""to be overwritten:
		gets a messages when values change"
		returns None otherwise"""
		return None
		
class trigger():
	"""a generic trigger - 
	id can be anything - 
	i.e. a string or an integer"""
	def __init__(self,id,command,param):
		self.id=id
		self.command=(command,param)
	def check(self,id):
		if id==self.id:
			return command
		else:
			return None
		
class indexlayer(layer):
	"""generates gps triggers from an index colour image
	triggers when colour changes"""
	triggers=[]
	colour=-1
	def setcoord(self,pos):
		result=None
		#210 35  5 185
		c=self.image.getpixel(pos)			
		if c!=self.colour:
			self.colour=c
			print "indexlayer: new colour",c
			for t in self.triggers:
				if c==t.id:
					result=t.command
		return result
		
class scalelayer(layer):
	"""generates a varying signal based on interpolating a greyscale image
	uses sub pixel position"""
	def findpixel(self,pos):
		#float version
		return ((pos.lng-self.tl.lng)/self.pixsize.lng,(pos.lat-self.br.lat)/self.pixsize.lat)
	def setcommand(self,command):
		self.command=command
	def setcoord(self,pos):
		px=int(pos[0])
		py=int(pos[1])
		c=float(self.image.getpixel((px,py)))/255.0
		c1=float(self.image.getpixel((px+1,py)))/255.0
		c2=float(self.image.getpixel((px,py+1)))/255.0
		c3=float(self.image.getpixel((px+1,py+1)))/255.0
		xf=pos[0]-px
		yf=pos[1]-py
		#lerp sub-pixel value
		return (self.command,(((c*(1.0-xf))+(c1*xf))*(1.0-yf))+(((c2*(1.0-xf))+(c3*xf))*yf))