home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / ReaderMouse / WorkFunctions.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-21  |  11.0 KB  |  449 lines  |  [TEXT/CWIE]

  1. #include <SpeechSynthesis.h>
  2. #include "MyHeaders.h"
  3. //#include "MySGStuff.h"
  4. #include "WorkFunctions.h"
  5. #include "LetterFind.h"
  6. #include "MyUtils.h"
  7. #include "MyCaptureAppShell.h"
  8. #include <math.h>
  9.  
  10. GWorldPtr    gWorkGWorldPtr =0;
  11. long    gGraphicLevel = 4;
  12.  
  13. #define SIGN(x)(((x)<0)?(-1.0):(1.0))
  14.  
  15. void UpdateWindow (WindowPtr inWindow)
  16. {
  17.  
  18. //    GrafPtr        savePort;
  19. //    GDHandle    saveGD;
  20.     Rect        copyRect = {0,0,0,0};
  21.     
  22. //    GetPort(&savePort);
  23. //    saveGD = GetGDevice();
  24. //    SetPort((GrafPort*)gWorkGWorldPtr);
  25.  
  26.     LockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  27.  
  28.     copyRect.right = gWorkGWorldPtr->portRect.right - gWorkGWorldPtr->portRect.left;
  29.     copyRect.bottom = gWorkGWorldPtr->portRect.bottom - gWorkGWorldPtr->portRect.top;
  30.     
  31.     CopyBits ((BitMap*)*GetGWorldPixMap(gWorkGWorldPtr), &inWindow->portBits, 
  32.                 ©Rect, ©Rect, srcCopy, 0);
  33.  
  34.     UnlockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  35.  
  36. //    SetPort(savePort);
  37. //    SetGDevice(saveGD);
  38.     
  39. }
  40.  
  41. void CommonRect(Rect *toRect, Rect *fromRect)
  42. {
  43.     short toWidth, toHeight, fromWidth, fromHeight;
  44.     float temp;
  45.     
  46.     toWidth = toRect->right - toRect->left;
  47.     toHeight = toRect->bottom - toRect->top;
  48.     fromWidth = fromRect->right - fromRect->left;
  49.     fromHeight = fromRect->bottom - fromRect->top;
  50.     
  51.     if (fromHeight > toHeight)
  52.     {
  53.         temp = (fromHeight - toHeight)/2.0;
  54.         fromRect->top += ceil(temp);
  55.         fromRect->bottom -= floor(temp);
  56.     }    
  57.     else
  58.     {
  59.         temp = (toHeight - fromHeight)/2.0;
  60.         toRect->top += ceil(temp);
  61.         toRect->bottom -= floor(temp);
  62.     }
  63.     if (fromWidth > toWidth)
  64.     {
  65.         temp = (fromWidth - toWidth)/2.0;
  66.         fromRect->left += ceil(temp);
  67.         fromRect->right -= floor(temp);
  68.     }    
  69.     else
  70.     {
  71.         temp = (toWidth - fromWidth)/2.0;
  72.         toRect->left += ceil(temp);
  73.         toRect->right -= floor(temp);
  74.     }
  75. }
  76.  
  77. void CopyGWorldToWindow (GWorldPtr inGWorld, WindowPtr inWindow)
  78. {
  79.     Rect    gWorldBounds;
  80.     Rect    windowBounds;
  81.     
  82.     RECT_EQUAL(gWorldBounds, inGWorld->portRect);
  83.     RECT_EQUAL(windowBounds, inWindow->portRect);
  84.     
  85.     CommonRect(&windowBounds, &gWorldBounds);
  86.     
  87.     LockPixels(GetGWorldPixMap(inGWorld));
  88.  
  89.     CopyBits ((BitMap*)*GetGWorldPixMap(inGWorld), &inWindow->portBits, 
  90.                 &gWorldBounds, &windowBounds, srcCopy, 0);
  91.  
  92.     UnlockPixels(GetGWorldPixMap(inGWorld));
  93. }
  94.  
  95.  
  96.  
  97. void DrawChosenLetterFromGFonts(GWorldPtr inGWorld, LetterData *letter)
  98. {
  99.     Rect        letterRect = {0,0,0,0};
  100.     Rect        outRect;
  101.     FontData    *theFont;
  102.     
  103.     theFont = &FONT_DATA(letter->font, letter->pointSize);
  104.     
  105.     letterRect.left = theFont->kernelOffset[letter->letter];
  106.     letterRect.right = letterRect.left + theFont->kernelWidth[letter->letter] - 1;
  107.     letterRect.bottom = theFont->maxHeight;
  108.     
  109.     outRect.top = letter->y;
  110.     outRect.left = letter->x;
  111.     outRect.right = outRect.left + theFont->kernelWidth[letter->letter] - 1;
  112.     outRect.bottom = outRect.top + theFont->maxHeight;
  113.     
  114.     LockPixels(GetGWorldPixMap(inGWorld));
  115.     LockPixels(GetGWorldPixMap(theFont->kernelBitmap));
  116.     
  117.     CopyBits (    (BitMap*)*GetGWorldPixMap(theFont->kernelBitmap), 
  118.                 (BitMap*)*GetGWorldPixMap(inGWorld), 
  119.                 &letterRect, &outRect, srcCopy, 0);
  120.  
  121.     UnlockPixels(GetGWorldPixMap(theFont->kernelBitmap));
  122.     UnlockPixels(GetGWorldPixMap(inGWorld));
  123. }
  124.  
  125. void DrawChosenLetter(GWorldPtr inGWorld, LetterData *letter)
  126. {
  127.     GrafPtr        savePort;
  128.     GDHandle    saveGD;
  129.     Rect        boundsRect = {0,0,0,0};
  130.     FontInfo    theFontInfo;
  131.     
  132.     // Save the drawing environment.
  133.     GetPort(&savePort);
  134.     saveGD = GetGDevice();
  135.  
  136.     SetPort((GrafPort*)inGWorld);
  137.     TextFont(gFonts[letter->font]);
  138.     TextSize(gPointSizes[letter->pointSize]);
  139.     GetFontInfo(&theFontInfo);
  140.     MoveTo(letter->x,letter->y+theFontInfo.ascent);
  141.  
  142.     ForeColor(yellowColor);
  143.     DrawChar(gLetters[letter->letter]);
  144.  
  145.  
  146.     SetPort(savePort);
  147.     SetGDevice(saveGD);
  148. }
  149.  
  150. void DrawChosenString(WindowPtr inWindow, WordData *inWord)
  151. {
  152.     GrafPtr        savePort;
  153.     GDHandle    saveGD;
  154.     FontInfo    theFontInfo;
  155.     Str255        theString;
  156.     
  157.     // Save the drawing environment.
  158.     GetPort(&savePort);
  159.     saveGD = GetGDevice();
  160.  
  161.     CopyWordToPascalString(theString, inWord);
  162.  
  163.     SetPort((GrafPort*)inWindow);
  164.     TextFont(gFonts[inWord->font]);
  165.     TextSize(gPointSizes[inWord->pointSize]);
  166.     GetFontInfo(&theFontInfo);
  167.     MoveTo(inWord->wordBounds.left,inWord->wordBounds.top+theFontInfo.ascent);
  168.  
  169.     PenMode(patOr);
  170.     DrawString(theString);
  171.  
  172.  
  173.     SetPort(savePort);
  174.     SetGDevice(saveGD);
  175. }
  176.  
  177.  
  178. void CopyGWorldToWork (GWorldPtr inGWorld)
  179. {
  180.     Rect        copyRect = {0,0,0,0};
  181.     
  182.     LockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  183.     LockPixels(GetGWorldPixMap(inGWorld));
  184.  
  185.     copyRect.right = inGWorld->portRect.right - inGWorld->portRect.left;
  186.     copyRect.bottom = inGWorld->portRect.bottom - inGWorld->portRect.top;
  187.     
  188.     CopyBits (    (BitMap*)*GetGWorldPixMap(inGWorld), 
  189.                 (BitMap*)*GetGWorldPixMap(gWorkGWorldPtr),
  190.                 ©Rect, ©Rect, srcCopy, 0);
  191.  
  192.     UnlockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  193.     UnlockPixels(GetGWorldPixMap(inGWorld));
  194. }
  195. void CopySearchedGWorldToWork (GWorldPtr inGWorld, LetterData *letter)
  196. {
  197.     Rect    copyRect = {0,0,0,0};
  198.     Rect    *portRect;
  199.     short    midY,midX,kernelHeight,kernelWidth;
  200.     FontData *theFont;
  201.     
  202.     LockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  203.     LockPixels(GetGWorldPixMap(inGWorld));
  204.     
  205.     portRect = &(inGWorld->portRect);
  206.     midY = (portRect->bottom - portRect->top)/2;
  207.     midX = (portRect->right - portRect->left)/2;
  208.     theFont = &(FONT_DATA(letter->font,letter->pointSize));
  209.     kernelHeight = theFont->maxHeight;
  210.     kernelWidth = theFont->kernelWidth[letter->letter];
  211.     copyRect.top = MAX(portRect->top, midY-kernelHeight);
  212.     copyRect.bottom = MIN(portRect->bottom, midY+kernelHeight);
  213.     copyRect.left = MAX(portRect->left, midX-kernelWidth);
  214.     copyRect.right = MIN(portRect->right, midX+kernelWidth);
  215.     
  216.     CopyBits (    (BitMap*)*GetGWorldPixMap(inGWorld), 
  217.                 (BitMap*)*GetGWorldPixMap(gWorkGWorldPtr),
  218.                 ©Rect, ©Rect, srcCopy, 0);
  219.  
  220.     UnlockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  221.     UnlockPixels(GetGWorldPixMap(inGWorld));
  222. }
  223. void DrawFontsIntoWorkGWorld(void)
  224. {
  225.     // first copy all the font gworlds to the window to make sure we're doing it right.
  226.     Rect    srcRect = {0,0,0,0};
  227.     Rect    destRect = {0,0,0,0};
  228.     int        right;
  229.     short    font,pointSize;
  230.     GWorldPtr theGWorld;
  231.     
  232.     LockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  233.     for(font=0; font<NUM_FONTS; font++)
  234.         for(pointSize=0; pointSize<NUM_POINT_SIZES; pointSize++)
  235.         {
  236.             theGWorld = FONT_DATA(font,pointSize).kernelBitmap;
  237.             
  238.             LockPixels(GetGWorldPixMap(theGWorld));
  239.             
  240.             
  241.             srcRect.bottom = theGWorld->portRect.bottom - theGWorld->portRect.top;
  242.             destRect.bottom = srcRect.bottom + destRect.top;
  243.             destRect.right = theGWorld->portRect.right - theGWorld->portRect.left;
  244.             right = gWorkGWorldPtr->portRect.right - gWorkGWorldPtr->portRect.left;
  245.             if (destRect.right > right) destRect.right = right;
  246.             srcRect.right = destRect.right;
  247.             
  248.             CopyBits (    (BitMap*)*GetGWorldPixMap(theGWorld), 
  249.                         (BitMap*)*GetGWorldPixMap(gWorkGWorldPtr), 
  250.                         &srcRect, &destRect, srcCopy, 0);
  251.  
  252.             UnlockPixels(GetGWorldPixMap(theGWorld));
  253.             
  254.             destRect.top = destRect.bottom;
  255.         }
  256.     UnlockPixels(GetGWorldPixMap(gWorkGWorldPtr));
  257. }
  258.  
  259. void EraseGWorld(GWorldPtr inGWorld)
  260. {
  261.     GrafPtr        savePort;
  262.     GDHandle    saveGD;
  263.     
  264.     // Save the drawing environment.
  265.     GetPort(&savePort);
  266.     saveGD = GetGDevice();
  267.  
  268.     SetPort((GrafPort*)inGWorld);
  269.  
  270.     EraseRect(&(inGWorld->portRect));
  271.  
  272.     SetPort(savePort);
  273.     SetGDevice(saveGD);
  274. }
  275. /*
  276. void HogProcessor (WindowPtr inWindow)
  277. {
  278.     WindowInfoHandle    myWindowInfo;
  279.     long    theStartTime, theEndTime, theNumberOfFrames, theFrameRate; // timing stuff
  280.  
  281.     EventRecord     myEvent;
  282.     long            sleepTime = 0;
  283.  
  284.  
  285.  
  286.     if(inWindow == nil) return; // Sequencer won't be open.
  287.     
  288.     myWindowInfo = (WindowInfoHandle) GetWRefCon(inWindow);
  289.  
  290.     if ((**myWindowInfo).theSG == nil) return; // no sequence grabber
  291.     
  292.     theNumberOfFrames = 0;
  293.             
  294.     theStartTime = TickCount();
  295.             
  296.     while (!Button())
  297.     {    
  298.         WaitNextEvent(everyEvent, &myEvent, sleepTime, nil);
  299.     
  300.         // let the Sequence grabber update    
  301.         SGIdle((**myWindowInfo).theSG);
  302.             
  303.         // operate on results
  304.         DoWorkOnImageData(gWorkGWorldPtr);
  305.         
  306.         // copy results to window.
  307.         if (gGraphicLevel > 1) UpdateWindow (inWindow);
  308.         
  309.         theNumberOfFrames++;
  310.     }
  311.     
  312.     theEndTime = TickCount();
  313.     
  314.     theFrameRate = theNumberOfFrames / ((theEndTime - theStartTime)/60);
  315.     
  316.     ReportWarning("\pFrameRate = ", theFrameRate); //hack
  317.     
  318. }
  319.  
  320. void ProcessMovie (WindowPtr inWindow)
  321. {
  322.     short    movieRefNum;
  323.     OSErr    err;
  324.     Movie    movie;
  325.     short    resID;
  326.     short    pixelDepth = 16;
  327.     Rect    boundsRect = {0,0,240,320};
  328.     
  329.     GWorldPtr    movieGWorld;
  330.     
  331.     // frame by frame variables.
  332.     OSType        mediaType = 'eyes';
  333.     TimeValue    currentTime, nextTime, frameDurration;
  334.     Fixed        rate;
  335.     PicHandle    framePic;
  336.     Rect        picFrame;
  337.     
  338.     StandardFileReply    reply;
  339.     SFTypeList             typeList;
  340.     
  341.     GrafPtr        savePort;
  342.     GDHandle    saveGD;
  343.     
  344.     char        frame = 0;
  345.  
  346.     long    theStartTime, theEndTime, theNumberOfFrames, theFrameRate; // timing stuff
  347.  
  348.  
  349.     // Get file Spec;
  350.     typeList[0] = 'MooV';
  351.     StandardGetFile(nil, 1, typeList,&reply);
  352.     if (!reply.sfGood) return; // cancel
  353.     
  354.     // Open Movie file
  355.     err = OpenMovieFile (&reply.sfFile, &movieRefNum, fsRdPerm);
  356.     
  357.     resID = 0; // get first movie in file;
  358.     err = NewMovieFromFile (&movie, movieRefNum, &resID, nil, 0, nil);
  359.         
  360.     //GetMovieNaturalBoundsRect(movie,&boundsRect);
  361.     // or is it GetMovieBox(movie,&boundsRect)
  362.     GetMovieBox(movie,&boundsRect);
  363.     
  364.     // Create GWorld for movie.
  365.     if (gWorkGWorldPtr != nil)
  366.     {
  367.         // get size and bit depth from this gWorld.
  368.         pixelDepth = gWorkGWorldPtr->portPixMap[0]->pixelSize;
  369. //        boundsRect.right = gWorkGWorldPtr->portRect.right - gWorkGWorldPtr->portRect.left;
  370. //        boundsRect.bottom = gWorkGWorldPtr->portRect.bottom - gWorkGWorldPtr->portRect.top;
  371.     }
  372.     
  373.     err = NewGWorld(&movieGWorld, pixelDepth, &boundsRect, nil, nil, 0);    
  374.                                         
  375.     SetMovieGWorld(movie, movieGWorld, nil);
  376.     
  377.     // Set up the drawing environment.
  378.     GetPort(&savePort);
  379.     saveGD = GetGDevice();
  380.     SetPort((GrafPort*)movieGWorld);
  381.  
  382.  
  383.     // Now go through the movies frames one by one.
  384.     
  385.     // Find first frame.
  386.     
  387.     rate = 1; // not sure what this value is yet.
  388.     GoToBeginningOfMovie (movie);
  389.     nextTime = GetMovieTime (movie, nil);
  390.  
  391.     // timing stuff
  392.     theNumberOfFrames = 0;    
  393.     theStartTime = TickCount();
  394.  
  395.     while (nextTime != -1)
  396.     {
  397.         theNumberOfFrames++;
  398.         frame++;
  399.         currentTime = nextTime;
  400.         
  401.         framePic = GetMoviePict (movie, currentTime);
  402.         
  403.         // Draw with the pic's size to prevent scaling. Different frames can be different
  404.         // sizes. <grin> Especially if you paste pictures into a movie.
  405.         RECT_EQUAL(picFrame,framePic[0]->picFrame);
  406.         DrawPicture(framePic, &picFrame);
  407.         KillPicture(framePic);
  408.                 
  409.         
  410.         if (inWindow != nil)
  411.         {
  412.             LockPixels(GetGWorldPixMap(movieGWorld));
  413.             
  414.             CopyBits ((BitMap*)*GetGWorldPixMap(movieGWorld), &inWindow->portBits, 
  415.                         &boundsRect, &boundsRect, srcCopy, 0);
  416.  
  417.             UnlockPixels(GetGWorldPixMap(movieGWorld));
  418.         }
  419.  
  420.         DoWorkOnImageData (movieGWorld);
  421.         
  422.         UpdateWindow(inWindow);
  423.     
  424.         GetMovieNextInterestingTime (movie, nextTimeMediaSample, 1, &mediaType, currentTime,
  425.                                     rate, &nextTime, &frameDurration);
  426.  
  427.         // temporary so that we can take pictures of the processing.
  428.         //    Get the event.
  429.         //WaitNextEvent(everyEvent, &myEvent, yieldTime, nil);
  430.  
  431.  
  432.     }
  433.     theEndTime = TickCount();
  434.     theFrameRate = theNumberOfFrames / ((theEndTime - theStartTime)/60);
  435.     ReportWarning("\pFrameRate = ", theFrameRate); //hack
  436.  
  437.     // restore graphics environment.
  438.     SetPort(savePort);
  439.     SetGDevice(saveGD);
  440.  
  441.     DisposeGWorld (movieGWorld);
  442.     
  443.     // Close Movie file
  444.     err = CloseMovieFile (movieRefNum);
  445. }
  446.  
  447. */
  448.  
  449.