home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Snippets / GammaFade / GammaPaslib.p next >
Encoding:
Text File  |  1995-08-06  |  11.7 KB  |  435 lines  |  [TEXT/MWPS]

  1. Unit GammaPaslib;
  2.  
  3. {--------------------------------------------------------------------------------------------------------------- }
  4. { File "gamma.p" - Source for Altering the Gamma Tables of GDevices from Gamma.c                                 }
  5. {   Last updated 6/29/95, MJS                                                                                     }
  6. {--------------------------------------------------------------------------------------------------------------- }
  7. {    7-13-95    ported to pascal  by Matthew Xavier Mora mxmora@mxmdesigns.com                                         }
  8. {     7-18-95     fixed all the porting bugs and got it to work in think pascal                                     }
  9. {----------------------------------------------------------------------------------------------------------------}
  10. {     7-18-95 ported to CW (68k and PPC) by Bill Catambay (pretty easy), cleaned the code a bit (no more labels),     }
  11. {           brought back Matthew's delay fade routines (in main program).                                             }
  12. {----------------------------------------------------------------------------------------------------------------}
  13.  
  14.  
  15.  
  16. {---------------------------------------------------------------------------------------------------------------}
  17. {    This is the Source Code for the Gamma Utils Library file. Use this to build                                    }
  18. {        new functionality into the library or make an A4-based library.                                         }
  19. {    See the header file "gamma.h" for much more information. -- MJS                                                }
  20. {---------------------------------------------------------------------------------------------------------------}
  21. Interface
  22.  
  23. Uses
  24.     Traps, Video, ToolUtils, Devices;
  25.  
  26. Const
  27.     kGammaUtilsSig = 'GAMA';
  28.     kGetDeviceListTrapNum = $AA29;
  29.  
  30. Type
  31.     globalGammasPtr = ^globalGammas;
  32.     globalGammasHdl = ^globalGammasPtr;
  33.     globalGammas = record
  34.         size, dataOffset: Integer;
  35.         saved, hacked: GammaTblHandle;
  36.         theGDevice: GDHandle;
  37.         next: globalGammasHdl;
  38.         end;
  39.     gammaData = packed array[0..100000] of Byte;  {used to set the gamma}
  40.     gammaDataPtr = ^gammaData;
  41.  
  42. Var
  43.     gammaUtilsInstalled: OSType;
  44.     gammaTables: globalGammasHdl;
  45.     gammaFaded: boolean;
  46.  
  47. { Function Prototypes}
  48.  
  49. {    These routines help you determine whether you can use the Gamma Table Utils}
  50. {        on the current machine. The first checks all attached monitors, and the }
  51. {        second just checks the indicated monitor. Each returns TRUE if you can }
  52. {        use the functions, or FALSE if you can't. • Note: Before calling any other}
  53. {        Gamma Table function below, use this function to see if you are allowed.}
  54.  
  55. { * ****************************************************************************** *}
  56. Function IsGammaAvailable: Boolean;
  57. Function IsOneGammaAvailable (theGDevice: GDHandle): Boolean;
  58.  
  59.  
  60. {    These routines must bracket any calls to the Gamma Table functions, perhaps}
  61. {        at the head and tail of your main(). The first sets up the data structures}
  62. {        necessary to save and restore the state of your monitors. The second}
  63. {        disposes of all the internal data structures, but does not reset the}
  64. {        monitors to their original states. Both return the error code if some}
  65. {        part failed. }
  66.  
  67. { * ****************************************************************************** *}
  68. Function SetupGammaTools: OSErr;
  69. Function DisposeGammaTools: OSErr;
  70.  
  71. {    Use the first function to Fade each of your monitors to some percentage of their}
  72. {        initial brightness (100 = bright, 0 = dim). Repeatedly call this to ramp your}
  73. {        monitors up or down. The second function performs the same function, but only}
  74. {        for the specified monitor. Both return any applicable error codes.}
  75. {    Be sure to set up the necessary save-state data structures before you start by}
  76. {        calling the compatibility and initialization functions. }
  77.  
  78. { * ****************************************************************************** *}
  79. Function DoGammaFade (percent: Integer): OSErr;
  80. Function DoOneGammaFade (theGDevice: GDHandle;
  81.                                percent: Integer): OSErr;
  82.  
  83.  
  84. {    These routines are low-level interfaces to the device drivers for the monitors.}
  85. {        Use them at your own risk.}
  86. Function GetDevGammaTable (theGDevice: GDHandle;
  87.                               Var theTable: GammaTblPtr): OSErr;
  88. Function SetDevGammaTable (theGDevice: GDHandle;
  89.                                Var theTable: GammaTblPtr): OSErr;
  90.  
  91.  
  92. Procedure DelayFadeToBlack (delayTicks: longint);
  93. Procedure FadeToBlack (speed: integer);
  94. Procedure FadeFromBlack (speed: integer);
  95. Procedure DelayFadeFromBlack (delayTicks: longint);
  96.  
  97.  
  98. Implementation
  99.  
  100. Function IsGammaAvailable: Boolean;
  101.  
  102. Var
  103.    theGDevice: GDHandle;
  104.  
  105.     Begin
  106.     IsGammaAvailable := false;
  107.     If (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) = 
  108.         NGetTrapAddress(_Unimplemented, ToolTrap)) Then
  109.           exit(IsGammaAvailable);
  110.     theGDevice := GetDeviceList;
  111.     While (theGDevice <> Nil) Do
  112.         Begin
  113.         If (TestDeviceAttribute(theGDevice, screenDevice) And 
  114.             TestDeviceAttribute(theGDevice, noDriver)) Then
  115.             exit(IsGammaAvailable);
  116.         If (theGDevice^^.gdType = fixedType) Then
  117.             exit(IsGammaAvailable);
  118.         theGDevice := GetNextDevice(theGDevice);
  119.         End;
  120.     IsGammaAvailable := true; {If we made it this far then its true}
  121.     End;
  122.  
  123.  
  124. Function IsOneGammaAvailable (theGDevice: GDHandle): Boolean;
  125.  
  126.     Begin
  127.     IsOneGammaAvailable := false;
  128.     If (NGetTrapAddress(kGetDeviceListTrapNum, ToolTrap) = 
  129.         NGetTrapAddress(_Unimplemented, ToolTrap)) Then
  130.           exit(IsOneGammaAvailable);
  131.     If (TestDeviceAttribute(theGDevice, screenDevice) And 
  132.         TestDeviceAttribute(theGDevice, noDriver)) Then
  133.           exit(IsOneGammaAvailable);
  134.     If (theGDevice^^.gdType = fixedType) Then
  135.           exit(IsOneGammaAvailable);
  136.     IsOneGammaAvailable := true;
  137.     End;
  138.  
  139.  
  140. Function SetupGammaTools: OSErr;
  141.  
  142. Var
  143.    errorCold: Integer;
  144.    tempHdl: globalGammasHdl;
  145.    masterGTable: GammaTblPtr;
  146.    theGDevice: GDHandle;
  147.  
  148.     Begin
  149.     If (gammaUtilsInstalled = kGammaUtilsSig) Then
  150.         Begin
  151.         SetupGammaTools := -1;
  152.         exit(SetupGammaTools);
  153.         End;
  154.     gammaTables := Nil;
  155.     gammaUtilsInstalled := kGammaUtilsSig;
  156.     gammaFaded := FALSE;
  157.     theGDevice := GetDeviceList;
  158.     While (theGDevice <> Nil) Do
  159.         Begin
  160.         errorCold := GetDevGammaTable(theGDevice, masterGTable);
  161.         If (errorCold <> 0) Then
  162.             Begin
  163.             SetupGammaTools := errorCold;
  164.             exit(SetupGammaTools);
  165.             End;
  166.         tempHdl := globalGammasHdl(NewHandle(sizeof(globalGammas)));
  167.         If (tempHdl = Nil) Then
  168.             Begin
  169.             SetupGammaTools := MemError;
  170.             exit(SetupGammaTools);
  171.             End;
  172.         With masterGTable^ Do
  173.             Begin
  174.             tempHdl^^.size := sizeof(GammaTbl) + gFormulaSize + (gChanCnt * gDataCnt * gDataWidth Div 8);
  175.             tempHdl^^.dataOffset := gFormulaSize;
  176.             tempHdl^^.theGDevice := theGDevice;
  177.             End;
  178.         tempHdl^^.saved := GammaTblHandle(NewHandle(tempHdl^^.size));
  179.         If (tempHdl^^.saved = Nil) Then
  180.             Begin
  181.             SetupGammaTools := MemError;
  182.             exit(SetupGammaTools);
  183.             End;
  184.         tempHdl^^.hacked := GammaTblHandle(NewHandle(tempHdl^^.size));
  185.         If (tempHdl^^.hacked = Nil) Then
  186.             Begin
  187.             SetupGammaTools := MemError;
  188.             exit(SetupGammaTools);
  189.             End;
  190.         BlockMove(Ptr(masterGTable), Ptr(tempHdl^^.saved^), tempHdl^^.size);
  191.         tempHdl^^.next := gammaTables;
  192.         gammaTables := tempHdl;
  193.         theGDevice := GetNextDevice(theGDevice)
  194.         End;
  195.     SetupGammaTools := 0;
  196.     End;
  197.  
  198. Function DoGammaFade (percent: Integer): OSErr;
  199.  
  200. Var
  201.    errorCold: Integer;
  202.    thesize, i, theNum: LongInt;
  203.    tempHdl: globalGammasHdl;
  204.    gdp: gammaDataPtr;
  205.    tempLong: Longint;
  206.  
  207.     Begin
  208.     If (gammaUtilsInstalled <> kGammaUtilsSig) Then
  209.         Begin
  210.         DoGammaFade := -1;
  211.         exit(DoGammaFade);
  212.         End;
  213.     tempHdl := gammaTables;
  214.     While (tempHdl <> Nil) Do
  215.         Begin
  216.         With tempHdl^^ Do
  217.             Begin
  218.             BlockMove(Ptr(saved^), Ptr(hacked^), size);
  219.             tempLong := ord(@hacked^^.gFormulaData) + dataOffset;
  220.             gdp := gammaDataPtr(ord(@hacked^^.gFormulaData) + dataOffset);
  221.             thesize := hacked^^.gChanCnt * hacked^^.gDataCnt;
  222.             End;
  223.         For i := 0 To thesize - 1 Do
  224.             Begin
  225.             theNum := gdp^[i];
  226.             theNum := (theNum * percent) Div 100;
  227.             gdp^[i] := theNum;
  228.             End;
  229.         errorCold := SetDevGammaTable(tempHdl^^.theGDevice, tempHdl^^.hacked^);
  230.         If (errorCold <> 0) Then
  231.             Begin
  232.             DoGammaFade := errorCold;
  233.             exit(DoGammaFade);
  234.             End;
  235.         tempHdl := tempHdl^^.next;
  236.         End;
  237.     DoGammaFade := 0;
  238.     End;
  239.  
  240. Function DoOneGammaFade (theGDevice: GDHandle;
  241.                            percent: Integer): OSErr;
  242.  
  243. Var
  244.    errorCold: Integer;
  245.    thesize, i, theNum: LongInt;
  246.    tempHdl: globalGammasHdl;
  247.    gdp: gammaDataPtr;
  248.  
  249.     Begin
  250.     If (gammaUtilsInstalled <> kGammaUtilsSig) Then
  251.         DoOneGammaFade := -1;
  252.     tempHdl := gammaTables;
  253.     While ((tempHdl <> Nil) And (theGDevice <> tempHdl^^.theGDevice)) Do
  254.         tempHdl := tempHdl^^.next;
  255.     With tempHdl^^ Do
  256.         Begin
  257.         BlockMove(Ptr(saved^), Ptr(hacked^), size);
  258.         gdp := gammaDataPtr(ord(@hacked^^.gFormulaData) + dataOffset);
  259.         thesize := hacked^^.gChanCnt * hacked^^.gDataCnt;
  260.         End;
  261.     For i := 0 To thesize - 1 Do
  262.         Begin
  263.         theNum := gdp^[i];
  264.         theNum := (theNum * percent) Div 100;
  265.         gdp^[i] := theNum;
  266.         End;
  267.     errorCold := SetDevGammaTable(tempHdl^^.theGDevice, tempHdl^^.hacked^);
  268.     DoOneGammaFade := errorCold;
  269.     End;
  270.  
  271. Function DisposeGammaTools: OSErr;
  272.  
  273. Var
  274.    tempHdl, nextHdl: globalGammasHdl;
  275.  
  276.     Begin
  277.     If (gammaUtilsInstalled <> kGammaUtilsSig) Then
  278.         Begin
  279.         DisposeGammaTools := -1;
  280.         exit(DisposeGammaTools);
  281.         End;
  282.     tempHdl := gammaTables;
  283.     While (tempHdl <> Nil) Do
  284.         Begin
  285.         HLock(Handle(tempHdl));
  286.         With tempHdl^^ Do
  287.             Begin
  288.             nextHdl := next;
  289.             DisposeHandle(Handle(saved));
  290.             DisposeHandle(Handle(hacked));
  291.             HUnLock(Handle(tempHdl));
  292.             DisposeHandle(Handle(tempHdl));
  293.             tempHdl := nextHdl;
  294.             End;
  295.         End;
  296.     gammaUtilsInstalled := '    ';
  297.     DisposeGammaTools := 0;
  298.     End;
  299.  
  300. Function GetDevGammaTable (theGDevice: GDHandle;
  301.                        Var theTable: GammaTblPtr): OSErr;
  302.  
  303. Var
  304.    errorCold: Integer;
  305.    myCPB: ParmBlkPtr;
  306.  
  307.     Begin
  308.     theTable := Nil;
  309.     If Not IsOneGammaAvailable(theGDevice) Then
  310.         Begin
  311.         GetDevGammaTable := -1;
  312.         exit(GetDevGammaTable);
  313.         End;
  314.     myCPB := ParmBlkPtr(NewPtrClear(sizeof(ParamBlockRec)));
  315.     If (myCPB = Nil) Then
  316.         Begin
  317.         GetDevGammaTable := MemError;
  318.         exit(GetDevGammaTable);
  319.         End;
  320.     myCPB^.csCode := cscGetGamma;
  321.     myCPB^.ioCRefNum := theGDevice^^.gdRefNum;
  322.     myCPB^.csParam[0] := HiWord(longint(@theTable));
  323.     myCPB^.csParam[1] := LoWord(longint(@theTable));
  324.     errorCold := PBStatus(myCPB, false);
  325.     DisposePtr(Ptr(myCPB));
  326.     GetDevGammaTable := errorCold;
  327.     End;
  328.  
  329. Function SetDevGammaTable (theGDevice: GDHandle;
  330.                        Var theTable: GammaTblPtr): OSErr;
  331.  
  332. Var
  333.    myCPB: ParmBlkPtr;
  334.    errorCold: Integer;
  335.    cTab: CTabHandle;
  336.    saveGDevice: GDHandle;
  337.  
  338.     Begin
  339.     If Not IsOneGammaAvailable(theGDevice) Then
  340.         Begin
  341.         SetDevGammaTable := -1;
  342.         exit(SetDevGammaTable);
  343.         End;
  344.     myCPB := ParmBlkPtr(NewPtrClear(sizeof(ParamBlockRec)));
  345.     If (myCPB = Nil) Then
  346.         Begin
  347.         SetDevGammaTable := MemError;
  348.         exit(SetDevGammaTable);
  349.         End;
  350.     myCPB^.csCode := cscSetGamma;
  351.     myCPB^.ioCRefNum := theGDevice^^.gdRefNum;
  352.     myCPB^.csParam[0] := HiWord(longint(@theTable));
  353.     myCPB^.csParam[1] := LoWord(longint(@theTable));
  354.     errorCold := PBControl(myCPB, false);
  355.     If (errorCold = 0) Then
  356.         Begin
  357.         saveGDevice := GetGDevice;
  358.         SetGDevice(theGDevice);
  359.         cTab := theGDevice^^.gdPMap^^.pmTable;
  360.         SetEntries(0, cTab^^.ctSize, cTab^^.ctTable);
  361.         SetGDevice(saveGDevice);
  362.         End;
  363.     DisposePtr(Ptr(myCPB));
  364.     SetDevGammaTable := errorCold;
  365.     End;
  366.  
  367. Procedure DelayFadeToBlack (delayTicks: longint);
  368.  
  369. Var
  370.     i: integer;
  371.     oe: integer;
  372.     finalTicks: longint;
  373.  
  374.     begin
  375.     i := 100;
  376.     while i > 0 do
  377.         begin
  378.         oe := DoGammaFade(i);
  379.         i := i - 1;
  380.         Delay(delayTicks, finalTicks);
  381.            end;
  382.     gammaFaded := TRUE;
  383.     end;
  384.  
  385. Procedure FadeToBlack (speed: integer);
  386.  
  387. Var
  388.     i: integer;
  389.     oe: integer;
  390.  
  391.     begin
  392.     i := 100;
  393.     while (i >= 0) do
  394.         begin
  395.         oe := DoGammaFade(i);
  396.         i := i - speed;
  397.         end;
  398.     gammaFaded := TRUE;
  399.     end;
  400.  
  401. Procedure FadeFromBlack (speed: integer);
  402.  
  403. Var
  404.     i: integer;
  405.     oe: integer;
  406.  
  407.     begin
  408.     i := 0;
  409.     while (i <= 100) do
  410.         begin
  411.         oe := DoGammaFade(i);
  412.         i := i + speed;
  413.         end;
  414.     gammaFaded := FALSE;
  415.     end;
  416.  
  417. Procedure DelayFadeFromBlack (delayTicks: longint);
  418.  
  419. Var
  420.     i: integer;
  421.     oe: integer;
  422.     finalTicks: longint;
  423.     
  424.     begin
  425.     i := 0;
  426.     while (i <= 100) do
  427.         begin
  428.         oe := DoGammaFade(i);
  429.         i := i + 1;
  430.         Delay(delayTicks, finalTicks);
  431.         end;
  432.     gammaFaded := FALSE;
  433.     end;
  434.  
  435. End.