home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / QT MovieToolBox / other QT code not used yet / AddFrameToMovie.c next >
Encoding:
C/C++ Source or Header  |  1992-09-09  |  13.1 KB  |  532 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        AddFrameToMovie.                                                    */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demonstrates adding a frame to the end    */
  7. /*            of an existing movie.                                            */
  8. /*                                                                            */
  9. /*        Version:        1.0 Completed 12/2/91                                */
  10. /*                        1.1    Fixed error where ind was assumed to be 0 based    */
  11. /*                            rather than 1 based.  And, added code to update    */
  12. /*                            movie rather than require a flatten movie for    */
  13. /*                            saving the new edited movie.                    */
  14. /*                                                                            */
  15. /*--------------------------------------------------------------------------*/
  16.  
  17. #include     <GestaltEqu.h>
  18. #include    "Movies.h"
  19.  
  20. #define Gestalttest        0xA1AD
  21. #define NoTrap            0xA89F
  22.  
  23. #define    appleID            128            
  24. #define    appleMenu        0
  25. #define    aboutMeCommand    1
  26.  
  27. #define    fileID            129
  28. #define openCommand        1
  29. #define    flattenCommand    2
  30. #define closeCommand    3
  31. #define    quitCommand     5
  32.  
  33. #define    aboutMeDLOG        128
  34. #define    okButton        1
  35.  
  36. #define    MAXWINDOWS        5
  37.  
  38. /*------------------------------------------------------*/
  39. /*    Global Variables.                                    */
  40. /*------------------------------------------------------*/
  41.  
  42. Boolean                DoneFlag = FALSE;
  43. MenuHandle            mymenu0, mymenu1;
  44. Boolean                playingMovie[MAXWINDOWS];
  45. Movie                myMovie[MAXWINDOWS];
  46. WindowPtr            myWindow[MAXWINDOWS];
  47. int                    startlocation;
  48.  
  49. /*------------------------------------------------------*/
  50. /*    getMovieFromFile().                                    */
  51. /*------------------------------------------------------*/
  52.  
  53. Movie getMovieFromFile()
  54. {
  55.     OSErr                    err;
  56.     StandardFileReply        reply;
  57.     Point                    where = {200, 50};
  58.     SFTypeList                types;
  59.     short                    movieResRefNum;
  60.     short                    actualResId;
  61.     Movie                    theMovie;
  62.     long                    i, maxCompressionSize;
  63.     Track                    myVideoTrack;
  64.     Media                    myVideoMedia;
  65.     OSType                    myMediaType;
  66.     Boolean                    done = FALSE;
  67.     GWorldPtr                myGWorld;
  68.     Rect                    movieBounds;
  69.     GDHandle                oldGDevice;
  70.     CGrafPtr                oldPort;
  71.     Handle                    compressedData;
  72.     ImageDescriptionHandle    imageDescH;
  73.     TimeValue                sampleDuration;
  74.     
  75.     types[0] = 'MooV';
  76.     StandardGetFilePreview(nil, 1, types, &reply);
  77.     if (!reply.sfGood) return((Movie) 0);
  78.  
  79.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsWrPerm);
  80.     if (GetMoviesError()) return((Movie) 0);
  81.     if (err) return ((Movie) 0);
  82.     actualResId = 0;
  83.  
  84.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (StringPtr) 0, newMovieActive, (Boolean *) 0);
  85.     if (GetMoviesError()) return((Movie) 0);
  86.     if (err) return ((Movie) 0);
  87.  
  88. /*    This is where I add a frame to the end of the movie's video track.    */
  89.  
  90.     for (i=1; ((i<=GetMovieTrackCount(theMovie)) && (!done)); i++) {
  91.         myVideoTrack = GetMovieIndTrack(theMovie, i);
  92.         myVideoMedia = GetTrackMedia(myVideoTrack);
  93.         GetMediaHandlerDescription(myVideoMedia, &myMediaType, nil, nil);
  94.         if (myMediaType == VideoMediaType)
  95.             done = TRUE;
  96.     }
  97.     if (done == FALSE)
  98.         DebugStr("\PMovie contains no video tracks.");
  99.     else {
  100.         if (BeginMediaEdits(myVideoMedia))
  101.             DebugStr("\PBeginMediaEdits failed.");
  102.     
  103.         GetMovieBox(theMovie, &movieBounds);
  104.         if (NewGWorld(&myGWorld, 1, &movieBounds, nil, nil, 0))
  105.             DebugStr("\PNewGWorld failed.");
  106.         GetGWorld(&oldPort, &oldGDevice);
  107.         SetGWorld(myGWorld, nil);
  108.         LockPixels(GetGWorldPixMap(myGWorld));
  109.         EraseRect(&movieBounds);
  110.         MoveTo(10,10);
  111.         DrawString("\PHi there, John!!!");
  112.         imageDescH = (ImageDescriptionHandle) NewHandle(4);
  113.  
  114.         if (GetMaxCompressionSize(GetGWorldPixMap(myGWorld),
  115.                             &movieBounds,
  116.                             1,
  117.                             codecNormalQuality,
  118.                             'raw ',
  119.                             anyCodec,
  120.                             &maxCompressionSize))
  121.             DebugStr("\PCompressImage.");
  122.         compressedData = NewHandle(maxCompressionSize);
  123.         if (compressedData == nil)
  124.             DebugStr("\PCould not allocate compressedData block");
  125.         MoveHHi(compressedData);
  126.         HLock(compressedData);
  127.         if (CompressImage(GetGWorldPixMap(myGWorld),
  128.                             &movieBounds,
  129.                             codecNormalQuality,
  130.                             'raw ',
  131.                             imageDescH,
  132.                             StripAddress(*compressedData)))
  133.             DebugStr("\PCompressImage.");
  134.  
  135.         if (AddMediaSample(myVideoMedia,
  136.                             compressedData,
  137.                             0,
  138.                             (**imageDescH).dataSize,
  139.                             GetMediaTimeScale(myVideoMedia),
  140.                             (SampleDescriptionHandle) imageDescH,
  141.                             1,
  142.                             0,
  143.                             &sampleDuration))
  144.             DebugStr("\PInsertMediaIntoTracks.");
  145.  
  146.         if (InsertMediaIntoTrack(myVideoTrack,
  147.                             GetTrackDuration(myVideoTrack),
  148.                             sampleDuration,
  149.                             GetMediaTimeScale(myVideoMedia),
  150.                             0x00010000))
  151.             DebugStr("\PInsertMediaIntoTracks.");
  152.  
  153.         if (EndMediaEdits(myVideoMedia))
  154.             DebugStr("\PEndMediaEdits failed.");
  155.         SetGWorld(oldPort, oldGDevice);
  156.         HUnlock(compressedData);
  157.         DisposeGWorld(myGWorld);
  158.         DisposeHandle(compressedData);
  159.         DisposeHandle(imageDescH);
  160.     }
  161.     
  162.     if (err = UpdateMovieResource(theMovie, movieResRefNum, actualResId, nil))
  163.         DebugStr("\PUpdateResource failed.");
  164.  
  165. /*    This is the end of the code for adding the one frame to the movie.    */
  166.  
  167.     err = CloseMovieFile(movieResRefNum);
  168.     if (GetMoviesError()) return((Movie) 0);
  169.     if (err) return ((Movie) 0);
  170.  
  171.     return (theMovie);
  172. }
  173.  
  174. /*------------------------------------------------------*/
  175. /*    playMovie().                                            */
  176. /*------------------------------------------------------*/
  177.  
  178. OSErr playMovie(int    index)
  179. {
  180.     Rect            movieBounds;
  181.     
  182.     GetMovieBox(myMovie[index], &movieBounds);
  183.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  184.     if (movieBounds.right < 40) movieBounds.right = 40;
  185.     if (movieBounds.bottom < 20) movieBounds.bottom = 20;
  186.     SetMovieBox(myMovie[index], &movieBounds);
  187.     OffsetRect(&movieBounds, startlocation, startlocation);
  188.     myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, TRUE, 0L);
  189.     startlocation += 50;
  190.     if (startlocation > 300) startlocation = 50;
  191.     
  192.     SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
  193.     if (GetMoviesError()) DebugStr("\PSetMovieGWorld error.");
  194.  
  195. /*    Uncomment these lines if you want to pre load the movie into ram.
  196.     GotoBeginningOfMovie(myMovie[index]);
  197.     if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  198.                                     GetMovieDuration(myMovie[index]),
  199.                                     0) != noErr)
  200.         DebugStr("\PNot enough memory to load movie into ram.");
  201. */
  202.     SetMovieRate(myMovie[index], 0x00010000);
  203.  
  204. }
  205.  
  206. /*------------------------------------------------------*/
  207. /*    flatten().                                            */
  208. /*------------------------------------------------------*/
  209.  
  210. short flatten(int index)
  211. {
  212.     StandardFileReply        reply;
  213.     OSErr       theErr      = noErr;
  214.  
  215.     StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
  216.     if (!reply.sfGood) return;
  217.  
  218.      if (theErr = GetMoviesError()) 
  219.         DebugStr("\PCall Before FlattenMovies failed.");
  220.  
  221.     FlattenMovie(myMovie[index],
  222.                 flattenAddMovieToDataFork,
  223.                 &reply.sfFile,
  224.                 'JWJW',
  225.                 0,
  226.                 createMovieFileDeleteCurFile,
  227.                 nil,
  228.                 nil);
  229.                 
  230.     if (theErr = GetMoviesError()) 
  231.         DebugStr("\PFlattenMovies failed.");
  232.   
  233.     return(theErr);
  234. }
  235.  
  236. /*------------------------------------------------------*/
  237. /*    showAboutMeDialog()                                    */
  238. /*------------------------------------------------------*/
  239.  
  240. void showAboutMeDialog()
  241. {
  242.     GrafPtr     savePort;
  243.     DialogPtr    theDialog;
  244.     short        itemHit;
  245.  
  246.     GetPort(&savePort);
  247.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  248.     SetPort(theDialog);
  249.  
  250.     do {
  251.         ModalDialog(nil, &itemHit);
  252.     } while (itemHit != okButton);
  253.  
  254.     CloseDialog(theDialog);
  255.  
  256.     SetPort(savePort);
  257.     return;
  258. }
  259.  
  260. /*------------------------------------------------------*/
  261. /*    init().                                                */
  262. /*------------------------------------------------------*/
  263.  
  264. void init()
  265. {
  266.     OSErr                err;
  267.     int                    i;
  268.     long                QDfeature, OSfeature;
  269.  
  270.     /*    Initialize Managaer.    */
  271.     InitGraf(&qd.thePort);
  272.     FlushEvents(everyEvent, 0);
  273.     InitWindows();
  274.     InitDialogs(nil);
  275.     InitCursor();
  276.  
  277.     /*    Set up menus.    */
  278.     mymenu0 = GetMenu(appleID);
  279.     AddResMenu(mymenu0, 'DRVR');
  280.     InsertMenu(mymenu0,0);
  281.     mymenu1 = GetMenu(fileID);
  282.     InsertMenu(mymenu1,0);
  283.     DrawMenuBar();
  284.     
  285.     /*    Set up variables.    */
  286.     startlocation = 50;
  287.     for (i = 0; i < MAXWINDOWS; i++) {
  288.         playingMovie[i] = FALSE;
  289.         myWindow[i] = nil;
  290.     }
  291.  
  292.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  293.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  294.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  295.         if (err)
  296.             ExitToShell();
  297.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  298.         if (err)
  299.             ExitToShell();
  300.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  301.             ExitToShell();
  302.         err = Gestalt(gestaltQuickTime, &QDfeature);
  303.         if (err)
  304.             ExitToShell();
  305.     } else
  306.         ExitToShell();
  307.     
  308.     /*    Open QuickTime last.    */
  309.     err = EnterMovies();
  310.     if (err)
  311.         ExitToShell();
  312. }
  313.  
  314. /*------------------------------------------------------*/
  315. /*    finish().                                            */
  316. /*------------------------------------------------------*/
  317.  
  318. void finish()
  319. {
  320.     ExitMovies();
  321.     ExitToShell();
  322. }
  323.  
  324. /*------------------------------------------------------*/
  325. /*    doOpenCommand().                                        */
  326. /*------------------------------------------------------*/
  327.  
  328. void doOpenCommand()
  329. {
  330.     int            i, useThisIndex;
  331.     
  332.     useThisIndex = -1;
  333.     
  334.     /*    Search for the first window that is nil.    */
  335.     for (i = MAXWINDOWS-1; i >= 0; i--)
  336.         if (myWindow[i] == nil)
  337.             useThisIndex = i;
  338.             
  339.     /*    If index = -1, then it means that there are no windows avaiable.    */
  340.     if (useThisIndex != -1) {
  341.         myMovie[useThisIndex] = getMovieFromFile();
  342.         if (myMovie[useThisIndex] != 0) {
  343.             playMovie(useThisIndex);
  344.             playingMovie[useThisIndex] = TRUE;
  345.         }
  346.     }
  347. }
  348.  
  349. /*------------------------------------------------------*/
  350. /*    doFlattenCommand().                                        */
  351. /*------------------------------------------------------*/
  352.  
  353. void doFlattenCommand()
  354. {
  355.     int            i, useThisIndex;
  356.     WindowPtr    myTempWindow;
  357.     
  358.     /*    Flatten movie that is currently selected.    */
  359.     myTempWindow = FrontWindow();
  360.     if (myTempWindow == nil) return;
  361.     for (i = 0; i < MAXWINDOWS; i++)
  362.         if (myWindow[i] == myTempWindow)
  363.             flatten(i);
  364. }
  365.  
  366. /*------------------------------------------------------*/
  367. /*    doCloseCommand().                                        */
  368. /*------------------------------------------------------*/
  369.  
  370. void doCloseCommand()
  371. {
  372.     int        i, useThisIndex;
  373.     WindowPtr    myTempWindow;
  374.  
  375.     /*    Close selected window.    */
  376.     myTempWindow = FrontWindow();
  377.     if (myTempWindow == nil) return;
  378.     for (i = 0; i < MAXWINDOWS; i++)
  379.         if (myWindow[i] == myTempWindow) {
  380.             DisposeMovie(myMovie[i]);
  381.             DisposeWindow(myTempWindow);
  382.             playingMovie[i] = FALSE;
  383.             myWindow[i] = nil;
  384.         }
  385. }
  386.  
  387. /*------------------------------------------------------*/
  388. /*    doCommand().                                        */
  389. /*------------------------------------------------------*/
  390.  
  391. void doCommand(mResult)
  392.     long    mResult;
  393. {
  394.     int                     theMenu, theItem;
  395.     char                    daName[256];
  396.     GrafPtr                 savePort;
  397.  
  398.     theItem = LoWord(mResult);
  399.     theMenu = HiWord(mResult);
  400.     
  401.     switch (theMenu) {
  402.         case appleID:
  403.             if (theItem == aboutMeCommand)
  404.                 showAboutMeDialog();
  405.             else {
  406.                 GetItem(mymenu0, theItem, daName);
  407.                 GetPort(&savePort);
  408.                 (void) OpenDeskAcc(daName);
  409.                 SetPort(savePort);
  410.             }
  411.             break;
  412.  
  413.         case fileID:
  414.             switch (theItem) {
  415.                 case openCommand:
  416.                     doOpenCommand();
  417.                     break;
  418.                 case flattenCommand:
  419.                     doFlattenCommand();
  420.                     break;
  421.                 case closeCommand:
  422.                     doCloseCommand();
  423.                     break;
  424.                 case quitCommand:
  425.                     DoneFlag = TRUE;
  426.                     break;
  427.                 default:
  428.                     break;
  429.                 }
  430.             break;
  431.     }
  432.     HiliteMenu(0);
  433.     return;
  434. }
  435.  
  436. /*------------------------------------------------------*/
  437. /*    playMovies().                                            */
  438. /*------------------------------------------------------*/
  439.  
  440. playMovies()
  441. {
  442.     int        i;
  443.     
  444.     for (i = 0; i < MAXWINDOWS; i++)
  445.         if (playingMovie[i] == TRUE) {
  446.             if (IsMovieDone(myMovie[i]) == FALSE)
  447.                 MoviesTask(myMovie[i], DoTheRightThing);
  448.         }
  449. }
  450.  
  451. /*------------------------------------------------------*/
  452. /*    main().                                                */
  453. /*------------------------------------------------------*/
  454.  
  455. main()
  456. {
  457.     int                    i;
  458.     char            key;
  459.     Boolean            track;
  460.     long            growResult;
  461.     EventRecord     myEvent;
  462.     WindowPtr        whichWindow;
  463.     int                yieldTime;
  464.  
  465.  
  466.     init();
  467.     yieldTime = 0;
  468.     for ( ;; ) {
  469.     
  470.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  471.         if (DoneFlag)
  472.             finish();
  473.             
  474.         /*    Play movies which are active.    */
  475.         playMovies();
  476.  
  477.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  478.             switch (myEvent.what) {
  479.                 case mouseDown:
  480.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  481.                         case inSysWindow:
  482.                             SystemClick(&myEvent, whichWindow);
  483.                             break;
  484.                         case inMenuBar:
  485.                             doCommand(MenuSelect(myEvent.where));
  486.                             break;
  487.                         case inContent:
  488.                             SelectWindow(whichWindow);
  489.                             break;
  490.                         case inDrag:
  491.                             DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  492.                             break;
  493.                         case inGrow:
  494.                             break;
  495.                         case inGoAway:
  496.                             track = TrackGoAway (whichWindow, myEvent.where);
  497.                             if (track)    doCloseCommand();
  498.                             break;
  499.                         case inZoomIn:
  500.                             break;
  501.                         case inZoomOut:
  502.                             break;
  503.                         default:
  504.                             break;
  505.                     }
  506.                     break;
  507.                 case keyDown:
  508.                 case autoKey:
  509.                     key = myEvent.message & charCodeMask;
  510.                     if ( myEvent.modifiers & cmdKey )
  511.                         if ( myEvent.what == keyDown )
  512.                             doCommand(MenuKey(key));
  513.                     break;
  514.                 case updateEvt:
  515.                     for (i = 0; i < MAXWINDOWS; i++)
  516.                         if ((WindowPtr) myEvent.message == myWindow[i]) {
  517.                             BeginUpdate((WindowPtr) myWindow[i]);
  518.                             EndUpdate((WindowPtr) myWindow[i]);
  519.                         }
  520.                     break;
  521.                 case diskEvt:
  522.                     break;
  523.                 case activateEvt:
  524.                     break;
  525.                 case app4Evt:
  526.                     break; 
  527.                 default:
  528.                     break;
  529.             }
  530.         }
  531.     }
  532. }