home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Sample Code / Video Digitizer / Easy Video Grabber / BigEasyVideoGrabber.c next >
Encoding:
Text File  |  1994-12-06  |  2.9 KB  |  148 lines  |  [TEXT/MPS ]

  1. /*
  2.   File:            BigEasyVideoGrabber.c
  3.   Contains:        Routines for Using the Video Digitizer.
  4.   Written by:    David Van Brink / QT Engineering
  5.   Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  6.   Change History (most recent first):
  7.   <2>         12/4/94        khs        changed the format of the file to the new look and feel
  8.   <1>         12/18/94    dvb        1.0 Started
  9.   To Do:
  10. */
  11.  
  12.  
  13. // INCLUDES
  14. #include <Memory.h>
  15. #include <OSUtils.h>
  16. #include "BigEasyVideoGrabber.h"
  17.  
  18.  
  19. // FUNCTIONS
  20. EasyVideoGrabber NewEasyVideoGrabber(Rect* outputSize)
  21. /*
  22.  * Gets the default video digitizer,
  23.  * and returns the output size of the video image.
  24.  */
  25. {
  26.     EasyVideoGrabber evg;
  27.     ComponentResult thisError;
  28.  
  29.     evg = (void*)NewPtr(sizeof(EasyVideoGrabberRecord));
  30.     if (!evg)
  31.         goto fail;
  32.  
  33.     evg->sg = 0;
  34.     evg->vc = 0;
  35.  
  36.     evg->sg = OpenDefaultComponent(SeqGrabComponentType, 0);
  37.     if (!evg->sg)
  38.         goto fail;
  39.  
  40.     thisError = SGInitialize(evg->sg);
  41.     if (thisError)
  42.         goto fail;
  43.  
  44.     thisError = SGNewChannel(evg->sg, VideoMediaType, &evg->vc);
  45.     if (thisError)
  46.         goto fail;
  47.  
  48.     if (outputSize)
  49.     {
  50.         thisError = SGGetSrcVideoBounds(evg->vc, outputSize);
  51.         if (thisError)
  52.             goto fail;
  53.     }
  54.  
  55.     thisError = SGSetChannelUsage(evg->vc, seqGrabPreview);
  56.     if (thisError)
  57.         goto fail;
  58.  
  59.     goto goHome;
  60.  
  61. fail:if (evg)
  62.     {
  63.         if (evg->sg)
  64.             CloseComponent(evg->sg);
  65.         DisposePtr((void*)evg);
  66.         evg = 0;
  67.     }
  68.  
  69. goHome:return evg;
  70. }
  71.  
  72.  
  73. Boolean GrabEasyVideoGrabber(EasyVideoGrabber evg,
  74.                              Rect* r)
  75. /*
  76.  * Grab a frame, and draw it in the current port, of size 'r'.
  77.  * You can pass 'nil' for the EasyVideoGrabber, and one
  78.  * will be allocated and disposed, while-u-wait.
  79.  *
  80.  * The boolean returned is 'true' iff a frame was actually captured and drawn.
  81.  *
  82.  * The 'while' loop is a real barnstorming technique. The "Sequence Grab"
  83.  * component does not provide any way to know when a frame has actually
  84.  * been grabbed. Therefore, the Sequence Grabber is simply idled for
  85.  * a few ticks, on the _assumption_ that the video frame rate is high
  86.  * enough that at least one frame is grabbed.
  87.  *
  88.  * While this may seem mildly inelegant, it, in fact, works.
  89.  */
  90. {
  91.     ComponentResult thisError;
  92.     GWorldPtr gw;
  93.     GDHandle gd;
  94.     Boolean localGrabber;
  95.     unsigned long t;
  96.     Boolean result;
  97.  
  98.     result = false;
  99.  
  100.     localGrabber = (evg == 0);
  101.     if (localGrabber)
  102.         evg = NewEasyVideoGrabber(nil);
  103.  
  104.     if (!evg)
  105.         goto goHome;
  106.  
  107.     GetGWorld(&gw, &gd);
  108.  
  109.     thisError = SGSetGWorld(evg->sg, gw, gd);
  110.     if (thisError)
  111.         goto goHome;
  112.  
  113.     thisError = SGSetChannelBounds(evg->vc, r);
  114.     if (thisError)
  115.         goto goHome;
  116.  
  117.     thisError = SGStartPreview(evg->sg);
  118.     if (thisError)
  119.         goto goHome;
  120.  
  121.     t = TickCount() + 8;
  122.     do
  123.     {
  124.         thisError = SGIdle(evg->sg);
  125.         if (thisError)
  126.             goto goHome;
  127.     } while (TickCount() < t);
  128.  
  129.     thisError = SGStop(evg->sg);
  130.  
  131.     if (localGrabber)
  132.         DisposeEasyVideoGrabber(evg);
  133.  
  134.     result = true;
  135.  
  136. goHome:;
  137.     return result;
  138. }
  139.  
  140.  
  141. void DisposeEasyVideoGrabber(EasyVideoGrabber evg)
  142. {
  143.     CloseComponent(evg->sg);
  144.     DisposePtr((void*)evg);
  145. }
  146.  
  147.  
  148.