Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame^] | 1 | from Carbon import Qt |
| 2 | from Carbon import QuickTime |
Jack Jansen | d35509a | 2000-09-22 12:46:19 +0000 | [diff] [blame] | 3 | import macfs |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame^] | 4 | from Carbon import Qd |
| 5 | from Carbon.QuickDraw import srcCopy |
Jack Jansen | d35509a | 2000-09-22 12:46:19 +0000 | [diff] [blame] | 6 | from ExtPixMapWrapper import ExtPixMapWrapper |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame^] | 7 | from Carbon.Qdoffs import * |
Jack Jansen | d35509a | 2000-09-22 12:46:19 +0000 | [diff] [blame] | 8 | import ImageMac |
| 9 | import W |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | def GetFrames(m): |
| 15 | frameCount=0 |
| 16 | theTime=0 |
| 17 | type=QuickTime.VideoMediaType |
| 18 | #type='MPEG' |
| 19 | flags=QuickTime.nextTimeMediaSample |
| 20 | flags=flags+QuickTime.nextTimeEdgeOK |
| 21 | |
| 22 | while theTime>=0: |
| 23 | (theTime,duration)=m.GetMovieNextInterestingTime(flags,1,type,theTime,0) |
| 24 | #print "theTime ",theTime," duration ",duration |
| 25 | frameCount=frameCount+1 |
| 26 | flags = QuickTime.nextTimeMediaSample |
| 27 | |
| 28 | |
| 29 | return frameCount-1 |
| 30 | |
| 31 | def GetMovieFromOpenFile(): |
| 32 | fss, ok = macfs.StandardGetFile(QuickTime.MovieFileType) |
| 33 | mov = None |
| 34 | if ok: |
| 35 | movieResRef = Qt.OpenMovieFile(fss, 1) |
| 36 | mov, d1, d2 = Qt.NewMovieFromFile(movieResRef, 0, QuickTime.newMovieActive) |
| 37 | |
| 38 | return mov |
| 39 | |
| 40 | |
| 41 | class ExtMovie: |
| 42 | def __init__(self,mov): |
| 43 | |
| 44 | self.frames=0 |
| 45 | self.frameArray=[] |
| 46 | self.movie=mov |
| 47 | self._countFrames() |
| 48 | r=self.movie.GetMovieBox() |
| 49 | self.myRect=(0,0,r[2]-r[0],r[3]-r[1]) |
| 50 | self.movie.SetMovieBox(self.myRect) |
| 51 | self.pm=ExtPixMapWrapper() |
| 52 | self.pm.left=0 |
| 53 | self.pm.top=0 |
| 54 | self.pm.right=r[2]-r[0] |
| 55 | self.pm.bottom=r[3]-r[1] |
| 56 | self.gw=NewGWorld(32,self.myRect,None,None,0) |
| 57 | self.movie.SetMovieGWorld(self.gw.as_GrafPtr(), self.gw.GetGWorldDevice()) |
| 58 | self.GotoFrame(0) |
| 59 | |
| 60 | def _countFrames(self): |
| 61 | #deve contare il numero di frame, creare un array con i tempi per ogni frame |
| 62 | theTime=0 |
| 63 | #type=QuickTime.VIDEO_TYPE |
| 64 | type=QuickTime.VideoMediaType |
| 65 | flags=QuickTime.nextTimeMediaSample+QuickTime.nextTimeEdgeOK |
| 66 | |
| 67 | while theTime>=0: |
| 68 | (theTime,duration)=self.movie.GetMovieNextInterestingTime(flags,1,type,theTime,0) |
| 69 | self.frameArray.append((theTime,duration)) |
| 70 | flags = QuickTime.nextTimeMediaSample |
| 71 | self.frames=self.frames+1 |
| 72 | |
| 73 | |
| 74 | |
| 75 | def GotoFrame(self,n): |
| 76 | if n<=self.frames: |
| 77 | self.curFrame=n |
| 78 | (port,device)=GetGWorld() |
| 79 | SetGWorld(self.gw.as_GrafPtr(),None) |
| 80 | (self.now,self.duration)=self.frameArray[n] |
| 81 | self.movie.SetMovieTimeValue(self.now) |
| 82 | pixmap=self.gw.GetGWorldPixMap() |
| 83 | |
| 84 | if not LockPixels(pixmap): |
| 85 | print "not locked" |
| 86 | else: |
| 87 | |
| 88 | #Qd.EraseRect(self.myRect) |
| 89 | #this draws the frame inside the current gworld |
| 90 | self.movie.MoviesTask(0) |
| 91 | #this puts it in the buffer pixmap |
| 92 | self.pm.grab(0,0,self.myRect[2],self.myRect[3]) |
| 93 | UnlockPixels(pixmap) |
| 94 | #self.im=self.pm.toImage() |
| 95 | SetGWorld(port,device) |
| 96 | |
| 97 | def NextFrame(self): |
| 98 | self.curFrame=self.curFrame+1 |
| 99 | if self.curFrame>self.frames: |
| 100 | self.curFrame=0 |
| 101 | self.GotoFrame(self.curFrame) |
| 102 | |
| 103 | def isLastFrame(): |
| 104 | return self.curFrame==self.frames |
| 105 | |
| 106 | |
| 107 | def GetImage(self): |
| 108 | return self.pm.toImage() |
| 109 | |
| 110 | def GetImageN(self,n): |
| 111 | self.GotoFrame(n) |
| 112 | return self.pm.toImage() |
| 113 | |
| 114 | def GetNumeric(self): |
| 115 | return self.pm.toNumeric() |
| 116 | |
| 117 | def GetNumericN(self,n): |
| 118 | self.GotoFrame(n) |
| 119 | return self.pm.toNumeric() |
| 120 | |
| 121 | def Blit(self,destRect): |
| 122 | Qd.RGBForeColor( (0,0,0) ) |
| 123 | Qd.RGBBackColor((65535, 65535, 65535)) |
| 124 | |
| 125 | #Qd.MoveTo(10,10) |
| 126 | #Qd.LineTo(200,150) |
| 127 | Qd.CopyBits(self.gw.portBits,Qd.GetPort().portBits,self.myRect,destRect,srcCopy,None) |
| 128 | |
| 129 | class MovieWin(W.Window): |
| 130 | |
| 131 | def __init__(self,eMovie,title="MovieWin"): |
| 132 | self.ExtMovie=eMovie |
| 133 | |
| 134 | def test(): |
| 135 | import ImageFilter |
| 136 | from MLab import max |
| 137 | from MLab import min |
| 138 | from Numeric import * |
| 139 | Qt.EnterMovies() |
| 140 | m=GetMovieFromOpenFile() |
| 141 | em=ExtMovie(m) |
| 142 | print "Total frames:",em.frames," Current frame:",em.curFrame |
| 143 | #ImageMac.showImage(em.GetImage(),"frame 0",1) |
| 144 | #em.GotoFrame(500) |
| 145 | #ImageMac.showImage(em.GetImage().filter(ImageFilter.SMOOTH),"frame 500",2) |
| 146 | #ImageMac.showImage(em.GetImageN(1000),"frame 1000",2) |
| 147 | #r=array(((1,0,0,0),(0,0,0,0),(0,0,0,0),(0,0,0,0))) |
| 148 | #g=array(((0,0,0,0),(0,1,0,0),(0,0,0,0),(0,0,0,0))) |
| 149 | #b=array(((0,0,0,0),(0,0,0,0),(0,0,1,0),(0,0,0,0))) |
| 150 | #bw=array(((0.3086,0.6094,0.0820,0))) |
| 151 | #r2=array(((1,0,0,0))) |
| 152 | #ImageMac.showNumeric(em.GetNumericN(0),"frame 0",1) |
| 153 | #print em.GetNumericN(500).shape |
| 154 | #print "original (1,1)",em.GetNumericN(0)[100,100] |
| 155 | #print "product shape ",innerproduct(em.GetNumericN(0),r).shape |
| 156 | #print "product (1,1) ",innerproduct(em.GetNumericN(0),r)[100,100] |
| 157 | |
| 158 | #ImageMac.showNumeric(ImageMac.BWImage(em.GetNumericN(50))) |
| 159 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(500),r),"frame 500r",2) |
| 160 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(500),g),"frame 500g",2) |
| 161 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(500),b),"frame 500b",2) |
| 162 | |
| 163 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(500),r2),"frame 500r2",2) |
| 164 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(10),bw),"frame 0bw",1) |
| 165 | #ImageMac.showNumeric(innerproduct(em.GetNumericN(400),bw),"frame 10bw",1) |
| 166 | #colordif=(em.GetNumericN(100)-em.GetNumericN(10))+(255,255,255,255) |
| 167 | #colordif=colordif/2 |
| 168 | #ImageMac.showNumeric(colordif,"colordif",1) |
| 169 | #ImageMac.showNumeric(ImageMac.BWImage(colordif),"bwcolordif",1) |
| 170 | ilut=arange(0,256) |
| 171 | #ilut[118]=255 |
| 172 | #ilut[119]=255 |
| 173 | #ilut[120]=255 |
| 174 | ilut[121]=255 |
| 175 | ilut[122]=255 |
| 176 | ilut[123]=255 |
| 177 | ilut[124]=255 |
| 178 | ilut[125]=255 |
| 179 | ilut[126]=255 |
| 180 | ilut[127]=255 |
| 181 | ilut[128]=255 |
| 182 | ilut[129]=255 |
| 183 | #ilut[130]=255 |
| 184 | #ilut[131]=255 |
| 185 | #ilut[132]=255 |
| 186 | mlut=ones(256) |
| 187 | mlut[118]=0 |
| 188 | mlut[119]=0 |
| 189 | mlut[120]=0 |
| 190 | mlut[121]=0 |
| 191 | mlut[122]=0 |
| 192 | mlut[123]=0 |
| 193 | mlut[124]=0 |
| 194 | mlut[125]=0 |
| 195 | mlut[126]=0 |
| 196 | mlut[127]=0 |
| 197 | mlut[128]=0 |
| 198 | mlut[129]=0 |
| 199 | mlut[130]=0 |
| 200 | mlut[131]=0 |
| 201 | mlut[132]=0 |
| 202 | |
| 203 | ImageMac.showImage(em.GetImageN(100),"provaImg",2) |
| 204 | ImageMac.showNumeric(em.GetNumericN(100),"provaNum",2) |
| 205 | ImageMac.showImage(em.GetImageN(100).filter(ImageFilter.SMOOTH),"frame 500",2) |
| 206 | #image=ImageMac.BWImage(em.GetNumericN(100)) |
| 207 | #ImageMac.showNumeric(image) |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | #difimage=abs(image-ImageMac.BWImage(em.GetNumericN(10))) |
| 217 | #ImageMac.PlotHisto(difimage,32) |
| 218 | #ImageMac.showNumeric(difimage) |
| 219 | #difimage=127+(image-ImageMac.BWImage(em.GetNumericN(10)))/2 |
| 220 | #ImageMac.PlotHisto(difimage,32) |
| 221 | #ImageMac.showNumeric(difimage) |
| 222 | #fimage=ImageMac.Filter3x3(16.0,(1,1,1,1,8,1,1,1,1),difimage) |
| 223 | #ImageMac.showNumeric(fimage) |
| 224 | #difimage2=choose(fimage.astype(UnsignedInt8),ilut) |
| 225 | #ImageMac.showNumeric(difimage2) |
| 226 | |
| 227 | #(r,g,b,a)=ImageMac.SplitBands(em.GetNumericN(10)) |
| 228 | #ImageMac.showNumeric(r,"r") |
| 229 | #ImageMac.showNumeric(g,"g") |
| 230 | #ImageMac.showNumeric(b,"b") |
| 231 | #ImageMac.showNumeric(a,"a") |
| 232 | #bwdif=abs(((innerproduct(em.GetNumericN(400),bw)-innerproduct(em.GetNumericN(10),bw))+255)/2) |
| 233 | #ImageMac.showNumeric(bwdif,"frame diff/bw",1) |
| 234 | #ImageMac.PlotHisto(bwdif) |
| 235 | #ImageMac.showNumeric(choose(bwdif.astype(UnsignedInt8),ilut),"frame diff/bw",1) |
| 236 | #ImageMac.PlotHisto(choose(bwdif.astype(UnsignedInt8),ilut)) |
| 237 | #bwimage=ImageMac.BWImage(em.GetNumericN(100)) |
| 238 | #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) |
| 239 | #bwdif=abs(((bwimage-ImageMac.BWImage(em.GetNumericN(10)))+255)/2) |
| 240 | #ImageMac.showNumeric(bwimage,"original frame",1) |
| 241 | #ImageMac.showNumeric(bwdif,"frame diff/bw",1) |
| 242 | #ImageMac.PlotHisto(bwdif) |
| 243 | #ImageMac.showNumeric(choose(bwdif.astype(UnsignedInt8),ilut),"frame diff/bw",1) |
| 244 | #mmask=choose(bwdif.astype(UnsignedInt8),mlut) |
| 245 | #ImageMac.showNumeric(255-255*mmask,"frame diff/bw",1) |
| 246 | #mmask.shape=bwimage.shape |
| 247 | #ImageMac.showNumeric(mmask*bwimage,"frame diff/bw",1) |
| 248 | |
| 249 | #ImageMac.showNumeric((innerproduct(em.GetNumericN(300),bw)-innerproduct(em.GetNumericN(0),bw)),"frame diff/bw",1) |
| 250 | #ImageMac.showNumeric((innerproduct(em.GetNumericN(400)-em.GetNumericN(10),bw)),"frame diff2/bw",1) |
| 251 | #cdif=em.GetNumericN(400)-em.GetNumericN(10) |
| 252 | #ImageMac.showNumeric(,"frame diff2/bw",1) |
| 253 | |
| 254 | #ImageMac.showNumeric(innerproduct(cdif,r),"frame 500r",1) |
| 255 | #ImageMac.showNumeric(innerproduct(cdif,g),"frame 500g",1) |
| 256 | #ImageMac.showNumeric(innerproduct(cdif,b),"frame 500b",1) |
| 257 | def test2(): |
| 258 | Qt.EnterMovies() |
| 259 | m=GetMovieFromOpenFile() |
| 260 | if m==None: |
| 261 | print "no movie opened" |
| 262 | else: |
| 263 | em=ExtMovie(m) |
| 264 | print "Total frames: ",em.frames," Current frame:",em.curFrame |
| 265 | ImageMac.showImage(em.GetImage(),"frame 0",1) |
| 266 | |
| 267 | if __name__ == '__main__': |
| 268 | test2() |
| 269 | |