-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenCanvasArray.py
206 lines (180 loc) · 6.26 KB
/
ScreenCanvasArray.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import copy
from collections import OrderedDict
def simpleCached(cacheSize):
'''
A simple cache. Use like:
@simpleCache(cachesize)
def function:
'''
cache=OrderedDict()
def cacheFunction(function):
def cachedFunction(*args):
tArgs=tuple(args)
if tArgs in cache:
return cache[tArgs]
else:
answer=function(*args)
if len(cache)>cacheSize:
cache.popitem(last = False)
cache.popitem(last = False)
cache.popitem(last = False)
cache.popitem(last = False)
cache.popitem(last = False)
cache[tArgs]=answer
return answer
return cachedFunction
return cacheFunction
class Canvas:
'''
Array Implementation of canvas
All the mod operations that look convoluted are here because
they were considerably faster than loops
'''
def __init__(self,height, width, previousArray=None):
if (previousArray is None):
self._array = [0. for x in xrange(3*width * height)]
else:
self._array = previousArray
self.height=height
self.width=width
self.pendingMappingFunctions=[]
self.startX=[0]
self.startY=[0]
self._heightArr=[height]
self._widthArr=[width]
self._setArray = self._array
self._standardSet = self.__setitem__
self._standardApply = self.mapFunctionApply
self._convertYX = self._getConvertYXPosFunction()
def __getitem__(self, arg):
if self.pendingMappingFunctions:
self.mapFunctionApply()
y,x = arg
pos=self._convertYX(y,x)
return self._array[pos:pos+3]
def getArray(self):
if self.pendingMappingFunctions:
self.mapFunctionApply()
return self._array
def __setitem__(self,arg,value):
if self.pendingMappingFunctions:
self.mapFunctionApply()
y,x = arg
pos=self._convertYX(y,x)
self._setArray[pos],self._setArray[pos+1],self._setArray[pos+2] = value[0],value[1],value[2]
def mapFunction(self,function):
self.pendingMappingFunctions.append(function)
def mapFunctionApply(self):
'''
Optimized for performance
'''
height=self._heightArr[-1]
width=self._widthArr[-1]
getArray=self._array
setArray = self._setArray
convertYX=self._convertYX
pendingFunctions = self.pendingMappingFunctions
for y in xrange(height):
for x in xrange(width):
pos = convertYX(y,x)
r,g,b = getArray[pos:pos+3]
for function in pendingFunctions:
r,g,b= function((r,g,b), y, x)
setArray[pos], setArray[pos+1], setArray[pos+2] = r,g,b
self.pendingMappingFunctions=[]
def translateAndScale(self,x=None,y=None,height=None,width=None):
if (x==None):
x=0
self.startX.append(x)
if (y==None):
y=0
self.startY.append(y)
if (height==None):
height=self._heightArr[-1]
self._heightArr.append(max(1,min(height,self._heightArr[-1])))
if (width==None):
width=self._widthArr[-1]
self._widthArr.append(max(1,min(width,self._widthArr[-1])))
self._convertYX = self._getConvertYXPosFunction()
def restoreTranslateAndScale(self, clear=True):
if clear:
self.eraseNonDisplayable()
self.mapFunctionApply()
self.startX.pop()
self.startY.pop()
self._heightArr.pop()
self._widthArr.pop()
self._convertYX = self._getConvertYXPosFunction()
def getByte(self, y,x):
return _floatToByte(self[y,x])
def _getConvertYXPosFunction(self):
x0s=self.startX
y0s=self.startY
heights=self._heightArr
widths=self._widthArr
length = len(heights)-1
realHeight = self.height
realWidth = self.width
def convertYX(y,x):
for n in xrange(length,-1,-1):
y%=heights[n]
x%=widths[n]
x=(x+x0s[n])
y=(y+y0s[n])
pos = (y*realWidth+x)*3
return pos
def convertYXSimple(y,x):
y%=realHeight
x%=realWidth
pos = (y*realWidth+x)*3
return pos
if len(x0s)>1:
return convertYX
else:
return convertYXSimple
def eraseNonDisplayable(self):
posSet=set()
height=self._heightArr[-1]
width=self._widthArr[-1]
convertYX=self._getConvertYXPosFunction()
x=0
y=0
for i in xrange(height*width):
y%=height
x%=width
pos = convertYX(y,x)
posSet.add(pos)
x+=1
y+=int(x)/width
realHeight = self.height
realWidth = self.width
myArray=self._array
for i in xrange(realHeight*realWidth):
pos = i*3
if pos not in posSet:
myArray[pos], myArray[pos+1], myArray[pos+2] = 0,0,0
def updateArgs(self, otherCanvas):
self._widthArr = copy.deepcopy(otherCanvas._widthArr)
self._heightArr = copy.deepcopy(otherCanvas._heightArr)
self.startX = copy.deepcopy(otherCanvas.startX)
self.startY = copy.deepcopy(otherCanvas.startY)
convertYX=self._convertYX
def _floatToByte(self, f):
return max(min(255,int(f*255+0.5)),0)
#############3
def copyArray(self, otherCanvas):
otherArray = otherCanvas._array
for i in xrange(len(otherArray)):
self._array[i] = otherArray[i]
def __deepcopy__(self,memo):
if self.pendingMappingFunctions:
self.mapFunctionApply()
newColorarray = self._array*1
newone=Canvas(self.height,self.width,newColorarray)
newone.pendingMappingFunctions=[]
newone._convertYX = self._convertYX
newone._widthArr = copy.deepcopy(self._widthArr)
newone._heightArr = copy.deepcopy(self._heightArr)
newone.startX = copy.deepcopy(self.startX)
newone.startY = copy.deepcopy(self.startY)
return newone