summaryrefslogtreecommitdiffstats
path: root/Mac/Contrib/ImageHelpers/MovieUtils.py
blob: 8250a82cbbea8a065d6c781ee28f15eb53955fec (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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from Carbon import Qt
from Carbon import QuickTime
import macfs
from Carbon import Qd
from Carbon.QuickDraw import srcCopy
from ExtPixMapWrapper import ExtPixMapWrapper
from Carbon.Qdoffs import *
import ImageMac
import W



	
def GetFrames(m):
	frameCount=0
	theTime=0
	type=QuickTime.VideoMediaType
	#type='MPEG'
	flags=QuickTime.nextTimeMediaSample
	flags=flags+QuickTime.nextTimeEdgeOK
	
	while theTime>=0:
		(theTime,duration)=m.GetMovieNextInterestingTime(flags,1,type,theTime,0)
		#print "theTime ",theTime,"   duration ",duration
		frameCount=frameCount+1
		flags = QuickTime.nextTimeMediaSample
	
	
	return frameCount-1
	
def GetMovieFromOpenFile():
	fss, ok = macfs.StandardGetFile(QuickTime.MovieFileType)
	mov = None
	if  ok:
		movieResRef = Qt.OpenMovieFile(fss, 1)
		mov, d1, d2 = Qt.NewMovieFromFile(movieResRef, 0, QuickTime.newMovieActive)
		
	return mov


class ExtMovie:
	def __init__(self,mov):
		
		self.frames=0
		self.frameArray=[]
		self.movie=mov
		self._countFrames()
		r=self.movie.GetMovieBox()
		self.myRect=(0,0,r[2]-r[0],r[3]-r[1])
		self.movie.SetMovieBox(self.myRect)
		self.pm=ExtPixMapWrapper()
		self.pm.left=0
		self.pm.top=0
		self.pm.right=r[2]-r[0]
		self.pm.bottom=r[3]-r[1]
		self.gw=NewGWorld(32,self.myRect,None,None,0)
		self.movie.SetMovieGWorld(self.gw.as_GrafPtr(), self.gw.GetGWorldDevice())
		self.GotoFrame(0)
			
	def _countFrames(self):
		#deve contare il numero di frame, creare un array con i tempi per ogni frame
		theTime=0
		#type=QuickTime.VIDEO_TYPE
		type=QuickTime.VideoMediaType
		flags=QuickTime.nextTimeMediaSample+QuickTime.nextTimeEdgeOK
		
		while theTime>=0:
			(theTime,duration)=self.movie.GetMovieNextInterestingTime(flags,1,type,theTime,0)
			self.frameArray.append((theTime,duration))
			flags = QuickTime.nextTimeMediaSample
			self.frames=self.frames+1	
		
		
	
	def GotoFrame(self,n):
		if n<=self.frames:
			self.curFrame=n
			(port,device)=GetGWorld()
			SetGWorld(self.gw.as_GrafPtr(),None)
			(self.now,self.duration)=self.frameArray[n]
			self.movie.SetMovieTimeValue(self.now)
			pixmap=self.gw.GetGWorldPixMap()
			
			if not LockPixels(pixmap):
				print "not locked"
			else:
			
				#Qd.EraseRect(self.myRect)
				#this draws the frame inside the current gworld
				self.movie.MoviesTask(0)
				#this puts it in the buffer pixmap 
				self.pm.grab(0,0,self.myRect[2],self.myRect[3])
				UnlockPixels(pixmap)
				#self.im=self.pm.toImage()
			SetGWorld(port,device)
		
	def NextFrame(self):
		self.curFrame=self.curFrame+1
		if self.curFrame>self.frames:
			self.curFrame=0
		self.GotoFrame(self.curFrame)
	
	def isLastFrame():
		return self.curFrame==self.frames
		
		
	def GetImage(self):
		return self.pm.toImage()
		
	def GetImageN(self,n):
		self.GotoFrame(n)
		return self.pm.toImage()
		
	def GetNumeric(self):
		return self.pm.toNumeric()
		
	def GetNumericN(self,n):
		self.GotoFrame(n)
		return self.pm.toNumeric()
		
	def Blit(self,destRect):
		Qd.RGBForeColor( (0,0,0) )
		Qd.RGBBackColor((65535, 65535, 65535))
		
		#Qd.MoveTo(10,10)
		#Qd.LineTo(200,150)
		Qd.CopyBits(self.gw.portBits,Qd.GetPort().portBits,self.myRect,destRect,srcCopy,None)

class MovieWin(W.Window):
	
	def __init__(self,eMovie,title="MovieWin"):
		self.ExtMovie=eMovie		
		
def test():
	import ImageFilter
	from MLab import max
	from MLab import min
	from Numeric import *
	Qt.EnterMovies()
	m=GetMovieFromOpenFile()
	em=ExtMovie(m)
	print "Total frames:",em.frames,"  Current frame:",em.curFrame	
	#ImageMac.showImage(em.GetImage(),"frame 0",1)
	#em.GotoFrame(500)
	#ImageMac.showImage(em.GetImage().filter(ImageFilter.SMOOTH),"frame 500",2)
	#ImageMac.showImage(em.GetImageN(1000),"frame 1000",2)
	#r=array(((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0)))
	#g=array(((0,0,0,0),(0,1,0,0),(0,0,0,0),(0,0,0,0)))
	#b=array(((0,0,0,0),(0,0,0,0),(0,0,1,0),(0,0,0,0)))
	#bw=array(((0.3086,0.6094,0.0820,0)))
	#r2=array(((1,0,0,0)))
	#ImageMac.showNumeric(em.GetNumericN(0),"frame 0",1)
	#print em.GetNumericN(500).shape
	#print "original (1,1)",em.GetNumericN(0)[100,100]
	#print "product shape ",innerproduct(em.GetNumericN(0),r).shape
	#print "product (1,1) ",innerproduct(em.GetNumericN(0),r)[100,100]
	
	#ImageMac.showNumeric(ImageMac.BWImage(em.GetNumericN(50)))
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(500),r),"frame 500r",2)
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(500),g),"frame 500g",2)
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(500),b),"frame 500b",2)
	
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(500),r2),"frame 500r2",2)
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(10),bw),"frame 0bw",1)
	#ImageMac.showNumeric(innerproduct(em.GetNumericN(400),bw),"frame 10bw",1)
	#colordif=(em.GetNumericN(100)-em.GetNumericN(10))+(255,255,255,255)
	#colordif=colordif/2
	#ImageMac.showNumeric(colordif,"colordif",1)
	#ImageMac.showNumeric(ImageMac.BWImage(colordif),"bwcolordif",1)
	ilut=arange(0,256)
	#ilut[118]=255
	#ilut[119]=255
	#ilut[120]=255
	ilut[121]=255
	ilut[122]=255
	ilut[123]=255
	ilut[124]=255
	ilut[125]=255
	ilut[126]=255
	ilut[127]=255
	ilut[128]=255
	ilut[129]=255
	#ilut[130]=255
	#ilut[131]=255
	#ilut[132]=255
	mlut=ones(256)
	mlut[118]=0
	mlut[119]=0
	mlut[120]=0
	mlut[121]=0
	mlut[122]=0
	mlut[123]=0
	mlut[124]=0
	mlut[125]=0
	mlut[126]=0
	mlut[127]=0
	mlut[128]=0
	mlut[129]=0
	mlut[130]=0
	mlut[131]=0
	mlut[132]=0
	
	ImageMac.showImage(em.GetImageN(100),"provaImg",2)
	ImageMac.showNumeric(em.GetNumericN(100),"provaNum",2)
	ImageMac.showImage(em.GetImageN(100).filter(ImageFilter.SMOOTH),"frame 500",2)
	#image=ImageMac.BWImage(em.GetNumericN(100))
	#ImageMac.showNumeric(image)
	
	
	
	
	
	
	
	
	#difimage=abs(image-ImageMac.BWImage(em.GetNumericN(10)))
	#ImageMac.PlotHisto(difimage,32)
	#ImageMac.showNumeric(difimage)
	#difimage=127+(image-ImageMac.BWImage(em.GetNumericN(10)))/2
	#ImageMac.PlotHisto(difimage,32)
	#ImageMac.showNumeric(difimage)
	#fimage=ImageMac.Filter3x3(16.0,(1,1,1,1,8,1,1,1,1),difimage)
	#ImageMac.showNumeric(fimage)
	#difimage2=choose(fimage.astype(UnsignedInt8),ilut)
	#ImageMac.showNumeric(difimage2)
	
	#(r,g,b,a)=ImageMac.SplitBands(em.GetNumericN(10))
	#ImageMac.showNumeric(r,"r")
	#ImageMac.showNumeric(g,"g")
	#ImageMac.showNumeric(b,"b")
	#ImageMac.showNumeric(a,"a")
	#bwdif=abs(((innerproduct(em.GetNumericN(400),bw)-innerproduct(em.GetNumericN(10),bw))+255)/2)
	#ImageMac.showNumeric(bwdif,"frame diff/bw",1)
	#ImageMac.PlotHisto(bwdif)
	#ImageMac.showNumeric(choose(bwdif.astype(UnsignedInt8),ilut),"frame diff/bw",1)
	#ImageMac.PlotHisto(choose(bwdif.astype(UnsignedInt8),ilut))
	#bwimage=ImageMac.BWImage(em.GetNumericN(100))
	#ImageMac.showNumeric((ImageMac.BWImage(em.GetNumericN(90))+ImageMac.BWImage(em.GetNumericN(110))+ImageMac.BWImage(em.GetNumericN(130))+ImageMac.BWImage(em.GetNumericN(150))+ImageMac.BWImage(em.GetNumericN(170)))/5)
	#bwdif=abs(((bwimage-ImageMac.BWImage(em.GetNumericN(10)))+255)/2)
	#ImageMac.showNumeric(bwimage,"original frame",1)
	#ImageMac.showNumeric(bwdif,"frame diff/bw",1)
	#ImageMac.PlotHisto(bwdif)
	#ImageMac.showNumeric(choose(bwdif.astype(UnsignedInt8),ilut),"frame diff/bw",1)
	#mmask=choose(bwdif.astype(UnsignedInt8),mlut)
	#ImageMac.showNumeric(255-255*mmask,"frame diff/bw",1)
	#mmask.shape=bwimage.shape
	#ImageMac.showNumeric(mmask*bwimage,"frame diff/bw",1)
	
	#ImageMac.showNumeric((innerproduct(em.GetNumericN(300),bw)-innerproduct(em.GetNumericN(0),bw)),"frame diff/bw",1)
	#ImageMac.showNumeric((innerproduct(em.GetNumericN(400)-em.GetNumericN(10),bw)),"frame diff2/bw",1)
	#cdif=em.GetNumericN(400)-em.GetNumericN(10)
	#ImageMac.showNumeric(,"frame diff2/bw",1)
	
	#ImageMac.showNumeric(innerproduct(cdif,r),"frame 500r",1)
	#ImageMac.showNumeric(innerproduct(cdif,g),"frame 500g",1)
	#ImageMac.showNumeric(innerproduct(cdif,b),"frame 500b",1)
def test2():
	Qt.EnterMovies()
	m=GetMovieFromOpenFile()
	if m==None:
		print "no movie opened"
	else:
		em=ExtMovie(m)
		print "Total frames: ",em.frames,"  Current frame:",em.curFrame
		ImageMac.showImage(em.GetImage(),"frame 0",1)

if __name__ == '__main__':
	test2()