home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Utilities / MeasureMTF / MeasureMTF.c next >
Encoding:
C/C++ Source or Header  |  1994-11-04  |  11.2 KB  |  359 lines  |  [TEXT/MMCC]

  1. /*
  2. MeasureMTF.c
  3. Copyright © 1990-1993 Denis G. Pelli
  4. Measures the modulation transfer function of a monitor. Uses drifting gratings,
  5. both horizontal and vertical, of constant temporal frequency. The results
  6. are saved in a KaleidaGraph text file, MTF?.data, where ? stands for the screen
  7. number. The data file includes the dc and second harmonic.
  8. HISTORY:
  9. 10/11/90    dgp    wrote it.
  10. 10/12/90    dgp    added dc and harmonics.
  11. 3/18/91        dgp    updated to work with new PlotXY, but not tested
  12. 4/15/91        dgp Check for NewPaletteManager().
  13. 8/24/91        dgp    Made compatible with THINK C 5.0.
  14. 3/10/92    dgp    include mc68881.h
  15. 8/27/92    dgp    replace SysEnvirons() by Gestalt()
  16. 10/23/92 dgp try to read latest LuminanceRecord
  17. 2/7/93    dgp    updated to use SetPixelsQuickly.
  18. */
  19. #include "VideoToolbox.h"
  20. #include <math.h>
  21. #include <assert.h>
  22. #include "Luminance.h"
  23. #include <Packages.h>
  24. #if (THINK_C || THINK_CPLUS)
  25.     #include <profile.h>    /* for timing */
  26.     #include <console.h>
  27.     #define PROFILE 1
  28. #endif
  29. #define TWO_PI (2.0*PI)
  30. #define MAX(a,b) ((a)>(b)?(a):(b))
  31. #define MIN(a,b) ((a)<(b)?(a):(b))
  32.  
  33. void MeasureMTF(void);
  34. double MeasureContrast(GDHandle device,LuminanceRecord *LP,int tPeriod,double c[10],WindowPtr plotWindow);
  35. double saw(double x);
  36.  
  37. void main(void)
  38. {
  39.     assert(StackSpace()>1000);
  40.     StackGrow(30000);
  41.     Require(gestalt8BitQD);
  42.     MeasureMTF();
  43. }
  44.  
  45. void MeasureMTF(void)
  46. {
  47.     GDHandle device,oldGDHandle;
  48.     CWindowPtr window;
  49.     WindowPtr plotWindow,mtfWindow;
  50.     LuminanceRecord LR;
  51.     char string[100],string2[100],outName[100];
  52.     unsigned long seconds;
  53.     FILE *outfile[2];
  54.     int i,tPeriod,width;
  55.     Rect r,srcRect,dstRect;
  56.     double a,b,f,fNominal,c,L,cOut,cH[10],cV[10];
  57.     PlotXYStyle mtfStyle[2];
  58.     unsigned long row[1048];    // hopefully long enough for any video device
  59.     short oldScreen;
  60.  
  61.     assert(StackSpace()>4000);
  62.     #if (THINK_C || THINK_CPLUS)
  63.         console_options.ncols=100;
  64.         MaximizeConsoleHeight();
  65.         printf("\n");                       /* Initialize QuickDraw */
  66.     #else
  67.         InitGraf(&qd.thePort);
  68.         InitFonts();
  69.         InitWindows();
  70.         InitCursor();
  71.     #endif
  72.     #if PROFILE
  73.         InitProfile(200,1);
  74.         _profile=0;    /* disable profiling for the moment */
  75.     #endif
  76.     printf("Welcome to MeasureMTF.\n");
  77.     #include "LuminanceRecord1.h"                    // read at compile time
  78.     oldScreen=LR.screen;
  79.     if(GetScreenDevice(1)!=NULL){
  80.         printf("Which screen would you like to calibrate (%d):",LR.screen);
  81.         gets(string);
  82.         sscanf(string,"%d",&LR.screen);
  83.     }
  84.     else LR.screen=0;
  85.     sprintf(string,"LuminanceRecord%d.h",LR.screen);
  86.     i=ReadLuminanceRecord(string,&LR,0);        // try to read latest LuminanceRecord
  87.     if(i<1)printf("Warning: couldn't find “%s”. Calibrating screen %d.\n"
  88.         ,string,LR.screen);
  89.     else oldScreen=LR.screen;
  90.  
  91.     mtfStyle[0].continuing=0;
  92.     mtfStyle[0].lineWidth=1;
  93.     mtfStyle[0].symbolWidth=0;
  94.     mtfStyle[0].dash[0]=0;
  95.     mtfStyle[0].dashOffset=0;
  96.     mtfStyle[0].color=blackColor;
  97.     mtfStyle[1]=mtfStyle[0];
  98.     mtfStyle[1].color=blueColor;
  99.     
  100.     GetDateTime(&seconds);
  101.     sprintf(outName,"MTF%d.%s",LR.screen,DatedString(seconds));
  102.     outfile[0]=stdout;
  103.     outfile[1]=fopen(outName,"w");
  104.     if(outfile[1]==NULL) PrintfExit("Sorry, can't create \"%s\".\007\n",outName);
  105.     SetFileInfo(outName,'TEXT','QKPT');    /* for Kaleidagraph */
  106.  
  107.     /* Find device corresponding to the experimental screen. */
  108.     oldGDHandle=GetGDevice();
  109.     device = GetScreenDevice(LR.screen);
  110.     if(NewPaletteManager())
  111.         SetDepth(device,8,1,1);    /* 8-bit pixels, color mode */
  112.     window = GDOpenWindow(device);
  113.     
  114.     if(oldScreen==LR.screen){
  115.         printf("Luminance was calibrated on %s\n",LR.date);
  116.         printf("at %.2f %s by %s\n",LR.LBackground,LR.units,LR.notes);
  117.         printf("Range is %.1f to %.1f %s.\n",LR.LMin,LR.LMax,LR.units);
  118.     }
  119.     L=LR.LBackground;    /* desired mean luminance */
  120.     printf("Enter desired display luminance in cd/m^2 (%.2f):",L);
  121.     fgets(string,sizeof(string)-1,stdin);
  122.     sscanf(string,"%lf", &L);
  123.  
  124.     c=0.9;
  125.     c=MIN(c,MIN((LR.LMax-L)/L,(L-LR.LMin)/L));
  126.     printf("Contrast %.1f\n",c);
  127.     printf("Computing sinusoidal lookup table . . .\n");
  128.     for(i=0;i<256;i++){
  129.         SetLuminance(device,&LR,i,L*(1.0+c*sin(TWO_PI*i/256.0)),L*(1.0-c),L*(1.0+c));
  130.     }
  131.  
  132.     tPeriod=64;                                    /* frames in one temporal period */
  133.     width=window->portRect.right;                /* pixels across screen */
  134.     SetRect(&r,0,0,2*tPeriod,2*tPeriod);
  135.     OffsetRect(&r,64,64);
  136.     plotWindow=NewWindow(NULL,&r,"\pL",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  137.     SetRect(&r,0,0,192,192);
  138.     OffsetRect(&r,640-r.right-64,64);
  139.     mtfWindow=NewWindow(NULL,&r,"\pMTF",1,noGrowDocProc,(WindowPtr) -1L,FALSE,0L);
  140.     MeasureContrast(device,&LR,tPeriod,cH,plotWindow);        /* measure vBlack */
  141.     ffprintf(outfile,
  142.         "notes" "\tcycles/pixel" "\tcycles/screen" 
  143.         "\tHoriz. Grating Gain" "\tVert. Grating Gain" "\tVert./Horiz.");
  144.     fprintf(outfile[0],"\n");
  145.     fprintf(outfile[1],"\tc" "\tcH[0]" "\tcH[1]" "\tcH[2]" "\tcV[0]" "\tcV[1]" "\tcV[2]" 
  146.         "\t(cV[0]-cH[0])/cH[0]" "\tcV[2]/(c*V/H)^2" "\t(cV[0]-cH[0])/cH[0]/(c*V/H)^2"
  147.         "\n");
  148.     ffprintf(outfile,"notes");    /* put some text in notes column */
  149.     for(fNominal=0.0;;fNominal=MAX(fNominal*pow(2.0,1.0/2.0),fNominal+1.0/width)){
  150.         f=floor(fNominal*width+0.5)/width; /* integral periods for zero mean over line */
  151.         if(fNominal>0.5)f=0.5;
  152.         /* horizontal grating */
  153.         srcRect=dstRect=window->portRect;
  154.         dstRect.left=srcRect.right=1;
  155.         for(i=0;i<srcRect.bottom;i++){
  156.             row[0]=128.0+127.5*saw(TWO_PI*f*i);
  157.             SetWindowPixelsQuickly((WindowPtr)window,0,i,row,1);
  158.         }
  159.         /* CopyBits uses the inverse color table of the current GDevice. */
  160.         SetGDevice(device);
  161.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  162.             &srcRect,&dstRect,srcCopy,NULL);
  163.         SetGDevice(oldGDHandle);
  164.         cOut=MeasureContrast(device,&LR,tPeriod,cH,plotWindow);
  165.         ffprintf(outfile,"\t%10.5f" "\t%10.2f" "\t%12.6f",f,f*dstRect.right,cOut/c);
  166.         /* draw the MTF */
  167.         PlotXY(mtfWindow
  168.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  169.             ,&mtfStyle[0]);
  170.  
  171.         /* vertical grating */
  172.         srcRect=dstRect=window->portRect;
  173.         dstRect.bottom=srcRect.top=srcRect.bottom-1;
  174.         for(i=0;i<srcRect.right;i++)row[i]=128.0+127.5*saw(TWO_PI*f*i);
  175.         SetWindowPixelsQuickly((WindowPtr)window,0,srcRect.top,row,srcRect.right);
  176.         SetGDevice(device);
  177.         CopyBits((BitMap *)*window->portPixMap,(BitMap *)*window->portPixMap,
  178.             &srcRect,&dstRect,srcCopy,NULL);
  179.         SetGDevice(oldGDHandle);
  180.         cOut=MeasureContrast(device,&LR,tPeriod,cV,plotWindow);
  181.         ffprintf(outfile,"\t%20.6f" "\t%18.6f",cOut/c,cV[1]/cH[1]);
  182.         fprintf(outfile[0],"\n");
  183.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f" "\t%f"
  184.             ,c,cH[0],cH[1],cH[2],cV[0],cV[1],cV[2]);
  185.         a=(cV[0]-cH[0])/cH[0];    /* dc error, expressed as a contrast */
  186.         b=cV[1]/cH[1];            /* contrast gain of video amplifier */
  187.         fprintf(outfile[1],"\t%f" "\t%f" "\t%f" "\n"
  188.             ,a,cV[2]/(c*b*c*b),a/(c*b*c*b));
  189.         /* draw the MTF */
  190.         PlotXY(mtfWindow
  191.             ,log(f/(1./640.))/log(0.5/(1./640.)), log(cOut/c/0.3)/log(1.1/0.3)
  192.             ,&mtfStyle[1]);
  193.         
  194.         if(f==0.5)break;
  195.     }
  196.     
  197.     /* print notes */
  198.     IUDateString(seconds,longDate,(unsigned char *)string);
  199.     p2cstr((unsigned char *)string);
  200.     IUTimeString(seconds,FALSE,(unsigned char *)string2);
  201.     p2cstr((unsigned char *)string2);
  202.     ffprintf(outfile,"%s %s\n",string2,string);
  203.     ffprintf(outfile,"%.1f Hz\n",1.0/(tPeriod*0.015));
  204.     ffprintf(outfile,"%.2f cd/m^2\n",L);
  205.     ffprintf(outfile,"%.3f contrast\n",c);
  206.     ffprintf(outfile,"\n" "screen %d\n",LR.screen);
  207.     ffprintf(outfile,"\"%s\" monitor %s\n",LR.name,LR.id);
  208.     ffprintf(outfile,"on %s\n",LR.date);
  209.     ffprintf(outfile,"by %s\n",LR.notes);
  210.     ffprintf(outfile,"at %.2f %s\n",LR.LBackground,LR.units);
  211.  
  212.     GDDisposeWindow(window);
  213.     if(outfile[1] != NULL)fclose(outfile[1]);
  214.     printf("The sum of the times for GetVoltage() and LoadLuminances() should\n"
  215.         "be about one frame, 15 ms. If it's longer, e.g. 30 ms, you should reduce\n"
  216.         "the value of \"frames\" in MeasureContrast().\n");
  217.     DisposeWindow(plotWindow);
  218.     DisposeWindow(mtfWindow);
  219. }
  220.  
  221.  
  222. double MeasureContrast(GDHandle device,LuminanceRecord *LP
  223.     ,int tPeriod,double c[10],WindowPtr plotWindow)
  224. /*
  225. Measures the contrast of a drifting grating of unknown phase.
  226. The temporal period is tPeriod frames. The c[] array is
  227. filled with the amplitude spectrum, where c[i] is the contrast of the i-th harmonic,
  228. except that c[0], which would always be 1, instead is set to the dc level.
  229. */
  230. {
  231.     double v[256];
  232.     double p[10];
  233.     register int i,j;
  234.     ColorSpec doubleTable[512];    // was static, dgp 6/15/94
  235.     static double sinTable[256],cosTable[256];
  236.     static int tablePeriod=0;
  237.     register double a,b;
  238.     double frequency=2000.,gain=100.;
  239.     double frames=0.6;    /* How long the A/D should spend sampling the photometer */
  240.     long n;
  241.     static double vBlack=0.0;
  242.     static int blackSet=0;
  243.     char string[80];
  244.     WindowPtr oldPort;
  245.     int v0,ip;
  246.  
  247.     assert(StackSpace()>4000);
  248.     if(tPeriod>256){
  249.         printf("Warning. Reducing tPeriod to 256.\n");
  250.         tPeriod=256;
  251.     }
  252.     if(tablePeriod != tPeriod){
  253.         for(i=0;i<tPeriod;i++){
  254.             sinTable[i]=sin(i*TWO_PI/tPeriod);
  255.             cosTable[i]=cos(i*TWO_PI/tPeriod);
  256.         }
  257.         tablePeriod=tPeriod;
  258.     }
  259.     
  260.     n=(long)floor(0.5+frequency*0.015*frames);
  261.  
  262.     RemeasureContrast:
  263.     if(!blackSet){
  264.         doubleTable[0].rgb.red=doubleTable[0].rgb.green=doubleTable[0].rgb.blue=0;
  265.         for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=doubleTable[0];
  266.         LoadLuminances(device
  267.             ,(LuminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  268.         printf("Please block all light to set black. Hit cr when ready:");
  269.         gets(string);
  270.         vBlack=0.0;
  271.     }
  272.     else for(i=0;i<256;i++)doubleTable[i]=doubleTable[i+256]=LP->table[i];
  273.  
  274.     /* warm up for one period before collecting data */
  275.     SetPriority(7);
  276.     for(i=0;i<tPeriod;i++){
  277.         LoadLuminances(device
  278.             ,(LuminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  279.         GetVoltage(1,&gain,&frequency,n,NULL);
  280.         v[i]=0.0;
  281.     }
  282.     #if (THINK_C || THINK_CPLUS)
  283.         _profile=1;
  284.     #endif
  285.     for(i=0;i<tPeriod*4;i++){
  286.         LoadLuminances(device
  287.             ,(LuminanceRecord *)(doubleTable+(i%tPeriod)*(256/tPeriod)),0,255);
  288.         v[i%tPeriod]+=GetVoltage(1,&gain,&frequency,n,NULL);
  289.     }
  290.     #if (THINK_C || THINK_CPLUS)
  291.         _profile=0;
  292.     #endif
  293.     SetPriority(0);
  294.     a=0.0;
  295.     for(i=0;i<tPeriod;i++) a+=v[i];
  296.     c[0]=a/tPeriod-vBlack;
  297.     if(!blackSet){
  298.         vBlack=c[0];
  299.         blackSet=1;
  300.         printf("Black %g mV\n",vBlack*1000.0);
  301.         printf("Now please remove light block. Hit cr when ready:");
  302.         gets(string);
  303.         goto RemeasureContrast;
  304.     }
  305.     for(j=1;j<10;j++){
  306.         a=b=0.0;
  307.         for(i=0;i<tPeriod;i++){
  308.             a+=sinTable[i*j%tPeriod]*v[i];
  309.             b+=cosTable[i*j%tPeriod]*v[i];
  310.         }
  311.         c[j]=2.0*sqrt(a*a+b*b)/tPeriod/c[0];
  312.         p[j]=atan2(a,b);
  313.     }
  314.     /* Show one period of raw data, fundamental, raw minus fundamental, 2nd harmonic */
  315.     GetPort(&oldPort);
  316.     SetPort(plotWindow);
  317.     BringToFront(plotWindow);
  318.     EraseRect(&plotWindow->portRect);
  319.     v0=plotWindow->portRect.bottom/4;
  320.     SetOrigin(0,-2*v0);
  321.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]);
  322.     for(i=1;i<tPeriod;i++)LineTo(i,-v0*(v[i]-vBlack)/c[0]);
  323.     SetOrigin(0,-4*v0);
  324.     j=1;
  325.     ip=tPeriod*(1.0-p[j]/TWO_PI);
  326.     a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  327.     MoveTo(0,-v0*(v[0]-vBlack)/c[0]-a);
  328.     for(i=1;i<tPeriod;i++){
  329.         a=-v0*(0.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  330.         LineTo(i,-v0*(v[i]-vBlack)/c[0]-a);
  331.     }
  332.     ForeColor(blueColor);
  333.     for(j=1;j<3;j++){
  334.         SetOrigin(-tPeriod,-2*v0*j);
  335.         ip=tPeriod*(1.0-p[j]/TWO_PI);
  336.         i=0;
  337.         a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  338.         MoveTo(0,a);
  339.         for(i=1;i<tPeriod;i++){
  340.             a=-v0*(1.0+c[j]*cosTable[(i*j+ip)%tPeriod]);
  341.             LineTo(i,a);
  342.         }
  343.     }
  344.     ForeColor(blackColor);
  345.     SetOrigin(0,0);
  346.     SetPort(oldPort);
  347.     return c[1];
  348. }
  349.  
  350.  
  351. double saw(double x)
  352. /* returns sawtooth function with same phase, amplitude, and symmetry as sin() */
  353. {
  354.     x/=TWO_PI;
  355.     x-=0.5;
  356.     x-=floor(x);
  357.     return 2.0*x-1.0;
  358. }
  359.