home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Demos / FlickeringGrating.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  7.3 KB  |  235 lines  |  [TEXT/MMCC]

  1. /*
  2. FlickeringGrating.c
  3. This demo displays a research-grade visual stimulus. If you just want to know
  4. how to load the clut and display a pattern, then you should start by reading
  5. Grating.c, which is much shorter and simpler.
  6. Copyright (c) 1989-1993 Denis G. Pelli
  7. HISTORY:
  8. 11/89     Lan & Denis wrote it.
  9. 23.1.90    dgp        Use second screen only if available.
  10. 4/9/90    dgp        Changed WindowPtr to CWindowPtr. Made big arrays static. Reduced
  11.                 default memory allocation to 1 megabyte. Use any 8-bit screen,
  12.                 preferably not the main screen.
  13. 4/23/90    dgp        Added optional timing. Centered the image. Left clut entries 0 and 
  14.                 clutSize-1 alone, so background doesn't flash. Asked if ISR Video 
  15.                 Attenuator is present.
  16. 10/11/90 dgp    Added fpu test.
  17. 10/29/90 dgp    Added CenterRectInRect().
  18. 10/30/90 dgp    Changed call to SetLuminances() to SetLuminancesAndRange() so that
  19.                 the range is now kept fixed throughout all the frames, to avoid
  20.                 flashes.
  21. 8/24/91    dgp        Made compatible with THINK C 5.0.
  22.                 If possible, use ReadLuminanceRecord().
  23. 3/10/92    dgp        include mc68881.h
  24. 4/26/92    dgp        RestoreCluts().
  25. 8/27/92    dgp        replace SysEnvirons() by Gestalt()
  26. 12/30/92 dgp    made more like Filter, using a small console so that, if necessary,
  27.                 both the console and the grating will fit on the main monitor.
  28. 2/7/93    dgp        replaced SetOnePixel by SetPixelsQuickly
  29. 7/7/93    dgp        added code for compatibility with Radius PowerView, a SCSI video box.
  30. 9/5/94 dgp removed assumption in printf's that int==short.
  31. 11/17/94 dgp Josh Solomon reported a stack overflow, so I added a calls to StackGrow
  32. and assert.
  33. 1/11/95    dgp    Tidied up various things. Now switch to color mode if using an ISR Attenuator.
  34. 4/11/95 dgp Save and restore the display depth and color mode.
  35. */
  36. #include "VideoToolbox.h"
  37. #include "Luminance.h"
  38. #include <math.h>
  39. #include <assert.h>
  40. #if (THINK_C || THINK_CPLUS)
  41.     #include <console.h>
  42.     #include <profile.h>
  43.     #define PROFILE        0            // optionally, report timing
  44. #endif
  45. #if __MWERKS__
  46.     #include <SIOUX.h>
  47. #endif
  48. #if UNIVERSAL_HEADERS
  49.     #include <LowMem.h>
  50. #else
  51.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  52.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  53. #endif
  54.  
  55. #define SIZE         400            // size of grating, in pixels
  56. #define TMAX        5*67        // duration, in frames, typically at 67 Hz
  57. #define REPETITIONS    1            // Number of times to repeat the animation
  58.  
  59. void main(void);
  60. void FlickeringGrating(void);
  61. typedef struct{
  62.     ColorSpec table[256];
  63. }ColorSpecTable;
  64.  
  65. void main(void)
  66. {
  67.     StackGrow(40000L+2L*SIZE*sizeof(double)+sizeof(LuminanceRecord)+TMAX*sizeof(Ptr));
  68.     Require(gestalt8BitQD);
  69.     FlickeringGrating();
  70. }
  71.     
  72. void FlickeringGrating(void)
  73. {
  74.     register int i;
  75.     int j,tmax,clutSize,error,oldPixelSize,oldIsColor;
  76.     double fX[SIZE],fY[SIZE];
  77.     double LMid,LMin,LMax,dL,a,contrast;
  78.     CWindowPtr window=NULL;
  79.     WindowPtr oldPort=NULL;
  80.     GDHandle device=NULL;
  81.     LuminanceRecord LR,*LP;
  82.     ColorSpecTable *tables[TMAX];    // an array of pointers to ColorSpec tables
  83.     char string[100];
  84.     Rect r;
  85.     Boolean attenuatorInstalled;
  86.     RgnHandle rgn;
  87.     
  88.     assert(StackSpace()>16000);
  89.     #if PROFILE
  90.         InitProfile(200,3);    /* only needed if you want timing info */
  91.         _profile=0;
  92.     #endif
  93.     /* INITIALIZE QuickDraw */
  94.     MaximizeConsoleHeight();
  95.     #if (THINK_C || THINK_CPLUS)
  96.         console_options.title="\pFlickeringGrating";
  97.         console_options.nrows = 5;
  98.         printf("\n");
  99.     #elif __MWERKS__
  100.         SIOUXSettings.rows=5;
  101.         SIOUXSettings.autocloseonquit=0;
  102.         SIOUXSettings.showstatusline=0;
  103.         SIOUXSettings.asktosaveonclose=0;
  104.         printf("\n");
  105.     #else
  106.         InitGraf(&qd.thePort);
  107.         InitFonts();
  108.         InitWindows();
  109.         InitCursor();
  110.     #endif
  111.     printf("\n");    // make sure that oldPort is the console
  112.     GetPort(&oldPort);
  113.     printf("Welcome to FlickeringGrating.\n");
  114.  
  115.     for(i=8;i>=0;i--){
  116.         // look for a screen with 8-bit pixels.
  117.         device=GetScreenDevice(i);
  118.         if(device == NULL)continue;
  119.         if((*(*device)->gdPMap)->pixelSize==8)break;
  120.     }
  121.     do{
  122.         if(GetScreenDevice(1)!=NULL)i=ChooseScreen(i,"Which screen?");
  123.         else i=0;
  124.         device=GetScreenDevice(i);
  125.     }while(device==NULL);
  126.     oldIsColor=TestDeviceAttribute(device,gdDevType);
  127.     oldPixelSize=(*(*device)->gdPMap)->pixelSize;
  128.     if(NewPaletteManager() && (oldPixelSize!=8 || !oldIsColor))
  129.         SetDepth(device,8,1<<gdDevType,1);
  130.     if(device==NULL || (*(*device)->gdPMap)->pixelSize!=8)
  131.         PrintfExit("Sorry, I require 8 bits/pixel.\n");
  132.     sprintf(string,"LuminanceRecord%d.h",i);
  133.     LP=&LR;
  134.     i=ReadLuminanceRecord(string,LP,0);    /* try to read correct file */
  135.     if(i<=0){
  136.         #include "LuminanceRecord1.h"
  137.     }
  138.     attenuatorInstalled=Choose(0,"Have you installed an ISR Video Attenuator?\n",noYes,2);
  139.     printf("Using luminance calibration for screen %d calibrated %s by %s.\n"
  140.         ,(int)LP->screen,LP->date,LP->notes);
  141.     if(!attenuatorInstalled){
  142.         LP->r=0.0;
  143.         LP->g=1.0;
  144.         LP->b=0.0;
  145.     }
  146.     window=GDOpenWindow(device);
  147.     #if PROFILE
  148.         _profile=1;
  149.     #endif
  150.  
  151.     /* load clut with linear gray scale */
  152.     // We'll leave clut entries 0 (white) and clutSize-1 (black) alone,
  153.     // since they are used heavily by Apple's stuff. Window frames
  154.     // will mostly look normal if we leave those two entries alone.
  155.     clutSize=GDClutSize(device);
  156.     SetLuminances(device,LP,1,clutSize-2,LP->LMin,LP->LMax);
  157.         
  158.     /* Display a sinusoid with a gaussian envelope */
  159.     // Compute the image.
  160.     SetPort((WindowPtr)window);
  161.     PmBackColor(1+(long)(0.5+(clutSize-3)*0.5));
  162.     EraseRect(&window->portRect);
  163.     SetRect(&r,0,0,SIZE,SIZE);
  164.     CenterRectInRect(&r,&window->portRect);
  165.     for(i=0;i<SIZE;i++){
  166.         a=(i-SIZE/2)/(SIZE/6.);
  167.         fY[i]=exp(-a*a);
  168.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  169.     }
  170.     for(j=0;j<SIZE;j++){
  171.         unsigned long row[SIZE];
  172.         for(i=0;i<SIZE;i++) row[i]=1+(long)(0.5+(clutSize-3)*0.5*(1.0+fY[j]*fX[i]));
  173.         SetPixelsQuickly(r.left,j+r.top,row,SIZE);
  174.     }
  175.     // These bits of conditional code force the Radius PowerView
  176.     // to update the screen from the video buffer in memory.
  177.     #if 0    // No good; causes undesired color translation.
  178.         oldDevice=GetGDevice();
  179.         SetGDevice(device);
  180.         CopyBits((BitMap *)*((CGrafPtr)window)->portPixMap
  181.             ,(BitMap *)*((CGrafPtr)window)->portPixMap
  182.             ,&r,&r,srcCopy,NULL);
  183.         SetGDevice(oldDevice);
  184.     #endif
  185.     #if 1    // Ok, but I don't actually want to scroll.
  186.         rgn=NewRgn();
  187.         ScrollRect(&r,0,1,rgn);
  188.         DisposeRgn(rgn);
  189.     #endif
  190.     // Allocate ColorSpec tables, one for each frame
  191.     for(i=0;i<TMAX;i++){
  192.         tables[i]=(ColorSpecTable *)NewPtr(sizeof(ColorSpecTable));
  193.         if(tables[i]==NULL){
  194.             //printf("StackSpace() %ld\n",StackSpace());
  195.             printf("Only room for %d lookup tables (one per frame) ... continuing.\n",i);
  196.             break;
  197.         }
  198.     }
  199.     tmax=i;
  200.     // Compute lookup tables.
  201.     LMid=(LP->LMax+LP->LMin)/2.0;
  202.     contrast=(LP->LMax-LP->LMin)/(LP->LMax+LP->LMin);
  203.     LMax=LMid*(1.0+contrast);
  204.     LMin=LMid*(1.0-contrast);
  205.     for(i=0;i<tmax;i++){
  206.         a=6.0*(i-tmax/2)/tmax;
  207.         dL=LMid*contrast*exp(-a*a)*sin((i-tmax/2)*(2.0*PI*3.0*0.015));
  208.         SetLuminancesAndRange(NULL,LP,1,clutSize-2,LMid-dL,LMid+dL,LMin,LMax);
  209.         *tables[i]= *(ColorSpecTable *)LP->table;
  210.     }
  211.     printf("Now displaying the animation ...\n");
  212.     for(j=0;j<REPETITIONS;j++){
  213.         for(i=0;i<tmax;i++){
  214.             // This is a thinly disguished call to GDSetEntries.
  215.             LoadLuminances(device,(LuminanceRecord *) tables[i],1,clutSize-2);
  216.         }
  217.     }
  218.     #if PROFILE
  219.         _profile=0;
  220.     #endif
  221.     for(i=0;i<tmax;i++)DisposPtr((Ptr)tables[i]);
  222.     SetPort(oldPort);
  223.     GDDisposeWindow(window);
  224.     if(NewPaletteManager())
  225.         error=SetDepth(device,oldPixelSize,1<<gdDevType,oldIsColor);    // restore
  226.     else RestoreCluts();
  227.     #if !PROFILE
  228.         #if (THINK_C || THINK_CPLUS)
  229.             abort();
  230.         #elif __MWERKS__
  231.             SIOUXSettings.autocloseonquit=1;
  232.         #endif
  233.     #endif
  234. }
  235.