home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*/
- /* */
- /* AddFrameToMovie. */
- /* by John Wang */
- /* */
- /* Description: This program demonstrates adding a frame to the end */
- /* of an existing movie. */
- /* */
- /* Version: 1.0 Completed 12/2/91 */
- /* 1.1 Fixed error where ind was assumed to be 0 based */
- /* rather than 1 based. And, added code to update */
- /* movie rather than require a flatten movie for */
- /* saving the new edited movie. */
- /* */
- /*--------------------------------------------------------------------------*/
-
- #include <GestaltEqu.h>
- #include "Movies.h"
-
- #define Gestalttest 0xA1AD
- #define NoTrap 0xA89F
-
- #define appleID 128
- #define appleMenu 0
- #define aboutMeCommand 1
-
- #define fileID 129
- #define openCommand 1
- #define flattenCommand 2
- #define closeCommand 3
- #define quitCommand 5
-
- #define aboutMeDLOG 128
- #define okButton 1
-
- #define MAXWINDOWS 5
-
- /*------------------------------------------------------*/
- /* Global Variables. */
- /*------------------------------------------------------*/
-
- Boolean DoneFlag = FALSE;
- MenuHandle mymenu0, mymenu1;
- Boolean playingMovie[MAXWINDOWS];
- Movie myMovie[MAXWINDOWS];
- WindowPtr myWindow[MAXWINDOWS];
- int startlocation;
-
- /*------------------------------------------------------*/
- /* getMovieFromFile(). */
- /*------------------------------------------------------*/
-
- Movie getMovieFromFile()
- {
- OSErr err;
- StandardFileReply reply;
- Point where = {200, 50};
- SFTypeList types;
- short movieResRefNum;
- short actualResId;
- Movie theMovie;
- long i, maxCompressionSize;
- Track myVideoTrack;
- Media myVideoMedia;
- OSType myMediaType;
- Boolean done = FALSE;
- GWorldPtr myGWorld;
- Rect movieBounds;
- GDHandle oldGDevice;
- CGrafPtr oldPort;
- Handle compressedData;
- ImageDescriptionHandle imageDescH;
- TimeValue sampleDuration;
-
- types[0] = 'MooV';
- StandardGetFilePreview(nil, 1, types, &reply);
- if (!reply.sfGood) return((Movie) 0);
-
- err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsWrPerm);
- if (GetMoviesError()) return((Movie) 0);
- if (err) return ((Movie) 0);
- actualResId = 0;
-
- err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (StringPtr) 0, newMovieActive, (Boolean *) 0);
- if (GetMoviesError()) return((Movie) 0);
- if (err) return ((Movie) 0);
-
- /* This is where I add a frame to the end of the movie's video track. */
-
- for (i=1; ((i<=GetMovieTrackCount(theMovie)) && (!done)); i++) {
- myVideoTrack = GetMovieIndTrack(theMovie, i);
- myVideoMedia = GetTrackMedia(myVideoTrack);
- GetMediaHandlerDescription(myVideoMedia, &myMediaType, nil, nil);
- if (myMediaType == VideoMediaType)
- done = TRUE;
- }
- if (done == FALSE)
- DebugStr("\PMovie contains no video tracks.");
- else {
- if (BeginMediaEdits(myVideoMedia))
- DebugStr("\PBeginMediaEdits failed.");
-
- GetMovieBox(theMovie, &movieBounds);
- if (NewGWorld(&myGWorld, 1, &movieBounds, nil, nil, 0))
- DebugStr("\PNewGWorld failed.");
- GetGWorld(&oldPort, &oldGDevice);
- SetGWorld(myGWorld, nil);
- LockPixels(GetGWorldPixMap(myGWorld));
- EraseRect(&movieBounds);
- MoveTo(10,10);
- DrawString("\PHi there, John!!!");
- imageDescH = (ImageDescriptionHandle) NewHandle(4);
-
- if (GetMaxCompressionSize(GetGWorldPixMap(myGWorld),
- &movieBounds,
- 1,
- codecNormalQuality,
- 'raw ',
- anyCodec,
- &maxCompressionSize))
- DebugStr("\PCompressImage.");
- compressedData = NewHandle(maxCompressionSize);
- if (compressedData == nil)
- DebugStr("\PCould not allocate compressedData block");
- MoveHHi(compressedData);
- HLock(compressedData);
- if (CompressImage(GetGWorldPixMap(myGWorld),
- &movieBounds,
- codecNormalQuality,
- 'raw ',
- imageDescH,
- StripAddress(*compressedData)))
- DebugStr("\PCompressImage.");
-
- if (AddMediaSample(myVideoMedia,
- compressedData,
- 0,
- (**imageDescH).dataSize,
- GetMediaTimeScale(myVideoMedia),
- (SampleDescriptionHandle) imageDescH,
- 1,
- 0,
- &sampleDuration))
- DebugStr("\PInsertMediaIntoTracks.");
-
- if (InsertMediaIntoTrack(myVideoTrack,
- GetTrackDuration(myVideoTrack),
- sampleDuration,
- GetMediaTimeScale(myVideoMedia),
- 0x00010000))
- DebugStr("\PInsertMediaIntoTracks.");
-
- if (EndMediaEdits(myVideoMedia))
- DebugStr("\PEndMediaEdits failed.");
- SetGWorld(oldPort, oldGDevice);
- HUnlock(compressedData);
- DisposeGWorld(myGWorld);
- DisposeHandle(compressedData);
- DisposeHandle(imageDescH);
- }
-
- if (err = UpdateMovieResource(theMovie, movieResRefNum, actualResId, nil))
- DebugStr("\PUpdateResource failed.");
-
- /* This is the end of the code for adding the one frame to the movie. */
-
- err = CloseMovieFile(movieResRefNum);
- if (GetMoviesError()) return((Movie) 0);
- if (err) return ((Movie) 0);
-
- return (theMovie);
- }
-
- /*------------------------------------------------------*/
- /* playMovie(). */
- /*------------------------------------------------------*/
-
- OSErr playMovie(int index)
- {
- Rect movieBounds;
-
- GetMovieBox(myMovie[index], &movieBounds);
- OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
- if (movieBounds.right < 40) movieBounds.right = 40;
- if (movieBounds.bottom < 20) movieBounds.bottom = 20;
- SetMovieBox(myMovie[index], &movieBounds);
- OffsetRect(&movieBounds, startlocation, startlocation);
- myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, TRUE, 0L);
- startlocation += 50;
- if (startlocation > 300) startlocation = 50;
-
- SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
- if (GetMoviesError()) DebugStr("\PSetMovieGWorld error.");
-
- /* Uncomment these lines if you want to pre load the movie into ram.
- GotoBeginningOfMovie(myMovie[index]);
- if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
- GetMovieDuration(myMovie[index]),
- 0) != noErr)
- DebugStr("\PNot enough memory to load movie into ram.");
- */
- SetMovieRate(myMovie[index], 0x00010000);
-
- }
-
- /*------------------------------------------------------*/
- /* flatten(). */
- /*------------------------------------------------------*/
-
- short flatten(int index)
- {
- StandardFileReply reply;
- OSErr theErr = noErr;
-
- StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
- if (!reply.sfGood) return;
-
- if (theErr = GetMoviesError())
- DebugStr("\PCall Before FlattenMovies failed.");
-
- FlattenMovie(myMovie[index],
- flattenAddMovieToDataFork,
- &reply.sfFile,
- 'JWJW',
- 0,
- createMovieFileDeleteCurFile,
- nil,
- nil);
-
- if (theErr = GetMoviesError())
- DebugStr("\PFlattenMovies failed.");
-
- return(theErr);
- }
-
- /*------------------------------------------------------*/
- /* showAboutMeDialog() */
- /*------------------------------------------------------*/
-
- void showAboutMeDialog()
- {
- GrafPtr savePort;
- DialogPtr theDialog;
- short itemHit;
-
- GetPort(&savePort);
- theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
- SetPort(theDialog);
-
- do {
- ModalDialog(nil, &itemHit);
- } while (itemHit != okButton);
-
- CloseDialog(theDialog);
-
- SetPort(savePort);
- return;
- }
-
- /*------------------------------------------------------*/
- /* init(). */
- /*------------------------------------------------------*/
-
- void init()
- {
- OSErr err;
- int i;
- long QDfeature, OSfeature;
-
- /* Initialize Managaer. */
- InitGraf(&qd.thePort);
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitDialogs(nil);
- InitCursor();
-
- /* Set up menus. */
- mymenu0 = GetMenu(appleID);
- AddResMenu(mymenu0, 'DRVR');
- InsertMenu(mymenu0,0);
- mymenu1 = GetMenu(fileID);
- InsertMenu(mymenu1,0);
- DrawMenuBar();
-
- /* Set up variables. */
- startlocation = 50;
- for (i = 0; i < MAXWINDOWS; i++) {
- playingMovie[i] = FALSE;
- myWindow[i] = nil;
- }
-
- /* Use Gestalt to find if QuickDraw and QuickTime is available. */
- if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
- err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
- if (err)
- ExitToShell();
- err = Gestalt(gestaltSystemVersion, &OSfeature);
- if (err)
- ExitToShell();
- if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
- ExitToShell();
- err = Gestalt(gestaltQuickTime, &QDfeature);
- if (err)
- ExitToShell();
- } else
- ExitToShell();
-
- /* Open QuickTime last. */
- err = EnterMovies();
- if (err)
- ExitToShell();
- }
-
- /*------------------------------------------------------*/
- /* finish(). */
- /*------------------------------------------------------*/
-
- void finish()
- {
- ExitMovies();
- ExitToShell();
- }
-
- /*------------------------------------------------------*/
- /* doOpenCommand(). */
- /*------------------------------------------------------*/
-
- void doOpenCommand()
- {
- int i, useThisIndex;
-
- useThisIndex = -1;
-
- /* Search for the first window that is nil. */
- for (i = MAXWINDOWS-1; i >= 0; i--)
- if (myWindow[i] == nil)
- useThisIndex = i;
-
- /* If index = -1, then it means that there are no windows avaiable. */
- if (useThisIndex != -1) {
- myMovie[useThisIndex] = getMovieFromFile();
- if (myMovie[useThisIndex] != 0) {
- playMovie(useThisIndex);
- playingMovie[useThisIndex] = TRUE;
- }
- }
- }
-
- /*------------------------------------------------------*/
- /* doFlattenCommand(). */
- /*------------------------------------------------------*/
-
- void doFlattenCommand()
- {
- int i, useThisIndex;
- WindowPtr myTempWindow;
-
- /* Flatten movie that is currently selected. */
- myTempWindow = FrontWindow();
- if (myTempWindow == nil) return;
- for (i = 0; i < MAXWINDOWS; i++)
- if (myWindow[i] == myTempWindow)
- flatten(i);
- }
-
- /*------------------------------------------------------*/
- /* doCloseCommand(). */
- /*------------------------------------------------------*/
-
- void doCloseCommand()
- {
- int i, useThisIndex;
- WindowPtr myTempWindow;
-
- /* Close selected window. */
- myTempWindow = FrontWindow();
- if (myTempWindow == nil) return;
- for (i = 0; i < MAXWINDOWS; i++)
- if (myWindow[i] == myTempWindow) {
- DisposeMovie(myMovie[i]);
- DisposeWindow(myTempWindow);
- playingMovie[i] = FALSE;
- myWindow[i] = nil;
- }
- }
-
- /*------------------------------------------------------*/
- /* doCommand(). */
- /*------------------------------------------------------*/
-
- void doCommand(mResult)
- long mResult;
- {
- int theMenu, theItem;
- char daName[256];
- GrafPtr savePort;
-
- theItem = LoWord(mResult);
- theMenu = HiWord(mResult);
-
- switch (theMenu) {
- case appleID:
- if (theItem == aboutMeCommand)
- showAboutMeDialog();
- else {
- GetItem(mymenu0, theItem, daName);
- GetPort(&savePort);
- (void) OpenDeskAcc(daName);
- SetPort(savePort);
- }
- break;
-
- case fileID:
- switch (theItem) {
- case openCommand:
- doOpenCommand();
- break;
- case flattenCommand:
- doFlattenCommand();
- break;
- case closeCommand:
- doCloseCommand();
- break;
- case quitCommand:
- DoneFlag = TRUE;
- break;
- default:
- break;
- }
- break;
- }
- HiliteMenu(0);
- return;
- }
-
- /*------------------------------------------------------*/
- /* playMovies(). */
- /*------------------------------------------------------*/
-
- playMovies()
- {
- int i;
-
- for (i = 0; i < MAXWINDOWS; i++)
- if (playingMovie[i] == TRUE) {
- if (IsMovieDone(myMovie[i]) == FALSE)
- MoviesTask(myMovie[i], DoTheRightThing);
- }
- }
-
- /*------------------------------------------------------*/
- /* main(). */
- /*------------------------------------------------------*/
-
- main()
- {
- int i;
- char key;
- Boolean track;
- long growResult;
- EventRecord myEvent;
- WindowPtr whichWindow;
- int yieldTime;
-
-
- init();
- yieldTime = 0;
- for ( ;; ) {
-
- /* We can't just do ExitToShell because we must cann ExitMovies. */
- if (DoneFlag)
- finish();
-
- /* Play movies which are active. */
- playMovies();
-
- if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
- switch (myEvent.what) {
- case mouseDown:
- switch (FindWindow(myEvent.where, &whichWindow)) {
- case inSysWindow:
- SystemClick(&myEvent, whichWindow);
- break;
- case inMenuBar:
- doCommand(MenuSelect(myEvent.where));
- break;
- case inContent:
- SelectWindow(whichWindow);
- break;
- case inDrag:
- DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
- break;
- case inGrow:
- break;
- case inGoAway:
- track = TrackGoAway (whichWindow, myEvent.where);
- if (track) doCloseCommand();
- break;
- case inZoomIn:
- break;
- case inZoomOut:
- break;
- default:
- break;
- }
- break;
- case keyDown:
- case autoKey:
- key = myEvent.message & charCodeMask;
- if ( myEvent.modifiers & cmdKey )
- if ( myEvent.what == keyDown )
- doCommand(MenuKey(key));
- break;
- case updateEvt:
- for (i = 0; i < MAXWINDOWS; i++)
- if ((WindowPtr) myEvent.message == myWindow[i]) {
- BeginUpdate((WindowPtr) myWindow[i]);
- EndUpdate((WindowPtr) myWindow[i]);
- }
- break;
- case diskEvt:
- break;
- case activateEvt:
- break;
- case app4Evt:
- break;
- default:
- break;
- }
- }
- }
- }