home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gfx / imagefx_sdk-2.0.lha / ImageFX_SDK / sas / examples / hooks / sample3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  4.9 KB  |  170 lines

  1. /*
  2.  * Sample3
  3.  *
  4.  * This sample demonstrates some of the newer features in ImageFX.
  5.  * It shows the recommended way to handle Arexx arguments, remember
  6.  * GUI settings,
  7.  *
  8.  * This hook does a simple blur.
  9.  *
  10.  * Written by Thomas Krehbiel, 15-Jan-95
  11.  *
  12.  */
  13.  
  14. #include <scan/hooks.h>
  15. #include <scan/procinfo.h>
  16. #include <clib/dos_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/graphics_protos.h>
  19.  
  20.  
  21. /* This information is required by the "start.o" startup code. */
  22. char *HookName = "Sample3";
  23.  
  24. /* A version string so the Version command works on us. */
  25. char *HookVersion = "\0$VER: Sample3 2.0.6 (15.2.95)";
  26.  
  27.  
  28. /*******************************************************************
  29.  *
  30.  *  Defines
  31.  *
  32.  *******************************************************************/
  33.  
  34. #define MIN(a,b)     ((a) < (b) ? (a) : (b))
  35.  
  36.  
  37. /*******************************************************************
  38.  *
  39.  *  Text Array and Indexes
  40.  *
  41.  *******************************************************************/
  42.  
  43. /* This information is required by the "start.o" startup code. */
  44. /* For this example, we're not doing any localization stuff. */
  45.  
  46. char *HookText = "Hook_Sample3";
  47. int   HookTextCount = 0;
  48. char **HookStrings[] = { NULL };
  49.  
  50.  
  51. /*******************************************************************
  52.  *
  53.  *  Support functions
  54.  *
  55.  *******************************************************************/
  56.  
  57. static void __inline do_blur (UBYTE *src, UBYTE *dst, LONG n, LONG bytes, LONG center)
  58. {
  59.    register LONG total;
  60.    register LONG bytes2 = bytes * 2;
  61.  
  62.    while (n--)
  63.    {
  64.  
  65.       total = *(src-1)        + *(src)                + *(src+1)       +
  66.               *(src+bytes-1)  + *(src+bytes) * center + *(src+bytes+1) +
  67.               *(src+bytes2-1) + *(src+bytes2)         + *(src+bytes2+1);
  68.  
  69.       *dst++ = total / (8 + center);
  70.       src++;
  71.  
  72.    }
  73. }
  74.  
  75. /*******************************************************************
  76.  *
  77.  *  Hook entry point is here.
  78.  *
  79.  *******************************************************************/
  80.  
  81. void hook_main (int argc, char **argv)
  82. {
  83.    struct Buffer *buf;
  84.    struct ProcessInfo *pi;
  85.    LONG argarray[8] = { 0 };
  86.    APTR as;
  87.    int amount;
  88.    int j;
  89.    BOOL success = FALSE;
  90.  
  91.    // ObtainCurrentBuffer() returns a pointer to the buffer we should
  92.    // work on.  This could be either the main buffer or the current
  93.    // brush, depending on the Region setting.
  94.  
  95.    if (!(buf = ObtainCurrentBuffer()))
  96.       hook_exit(20);
  97.  
  98.    if (argc > 1)
  99.    {
  100.       // arexx arguments were provided.  We use the ReadArgv() function
  101.       // found in scan.lib to parse the arguments.  ReadArgv() performs
  102.       // almost the same function as ReadArgs() in dos.library.  The
  103.       // format of the results in argarray[] are the same as ReadArgs().
  104.  
  105.       if (as = ReadArgv(argc, argv, "Amount/N", argarray))
  106.       {
  107.          amount = *((LONG *)argarray[0]);
  108.          FreeArgv(as);
  109.       }
  110.    }
  111.    else
  112.    {
  113.       // no arexx arguments provided.  Show our interface.  We use
  114.       // GetIfxVar() in scan.lib to find out what the last value the
  115.       // user entered into this requester was.
  116.  
  117.       amount = GetIfxVar("HookBlurAmount", 8);
  118.       amount = IntegerRequest("Enter blur amount:", 1, 16, amount);
  119.       if (amount < 1)
  120.       {
  121.          SetError(ERR_UserCancel);
  122.          hook_exit(20);
  123.       }
  124.  
  125.       // save this setting with SetIfxVar() for the next time we run
  126.       // this hook.
  127.       SetIfxVar("HookBlurAmount", amount);
  128.    }
  129.  
  130.    amount = 17 - amount;      // reverse logic of amount
  131.  
  132.    // This example uses the PrepareProcess()/FinishProcess() function
  133.    // calls to process the image.  The PFF_UNDO flag tells PrepareProcess()
  134.    // to save an undo buffer before proceeding.  Using these functions
  135.    // ensures that your hook will handle feathering, regions, include/exclude,
  136.    // channel masks, virtual memory, and any other miscellaneous settings
  137.    // properly.
  138.  
  139.    if (pi = PrepareProcess("Sample3", buf, 3, 3, PPF_UNDO))
  140.    {
  141.       for (j = 0; j < pi->Height; j++)
  142.       {
  143.          if (!(success = GetData(pi, j - 1, 3, 0))) break;
  144.          do_blur(pi->SR, pi->TR, pi->Width, pi->Bytes, amount);
  145.          if (buf->Depth > 1)
  146.          {
  147.             do_blur(pi->SG, pi->TG, pi->Width, pi->Bytes, amount);
  148.             do_blur(pi->SB, pi->TB, pi->Width, pi->Bytes, amount);
  149.          }
  150.          if (!(success = PutData(pi, j, 1, 0))) break;
  151.       }
  152.       FinishProcess(pi);
  153.  
  154.       // if successful, we should call the scan.library Learn() function
  155.       // to record the settings necessary to duplicate what was just
  156.       // done.  This is recorded into a learn file if this feature is
  157.       // enabled by the user.
  158.       if (success) Learn("Hook Sample3 %ld", 17 - amount);
  159.    }
  160.  
  161.    // if successful, redraw what was modified
  162.    if (success)
  163.       RedrawCurrent();
  164.  
  165.    // ImageFX checks the error value after the hook is finished.  If it
  166.    // is non-zero, ImageFX displays an error requester corresponding
  167.    // to the error value returned.  Otherwise, no requester is shown.
  168.    SetError(0);
  169. }
  170.