home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / misc / imagefx_sdk / sas / examples / hooks / sample1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  4.1 KB  |  171 lines

  1. /*
  2.  * Sample1
  3.  *
  4.  * A quick example of how to build a simple hook that doesn't
  5.  * have an interface.  (See GedSample for interface tips.)
  6.  *
  7.  * This sample just adds 1 to the value of every pixel in the
  8.  * currently selected image (either the main buffer or the
  9.  * brush, depending on the state of the Region cycler).
  10.  *
  11.  * Written by Thomas Krehbiel, 25-Jan-94
  12.  *
  13.  */
  14.  
  15. #include <scan/hooks.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 = "Sample1";
  23.  
  24. /* A version string so the Version command works on us. */
  25. char *HookVersion = "\0$VER: Sample1 2.0.1 (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_Sample1";
  47. int   HookTextCount = 0;
  48. char **HookStrings[] = { NULL };
  49.  
  50.  
  51. /*******************************************************************
  52.  *
  53.  *  Hook entry point is here.
  54.  *
  55.  *******************************************************************/
  56.  
  57. void hook_main (int argc, char **argv)
  58. {
  59.    struct Buffer *buf;
  60.    int i, j;
  61.    int le, te, w, h;
  62.    UBYTE *red, *grn, *blu;
  63.  
  64.    /*
  65.     * Get current buffer.
  66.     */
  67.    if (ScanBase->sb_AreaTool == AT_BRUSH)
  68.       buf = ScanBase->sb_Brush;
  69.    else
  70.       buf = ScanBase->MainBuffer;
  71.  
  72.    /*
  73.     * If no buffer, complain and exit.
  74.     */
  75.    if (!buf) {
  76.       SetError(ERR_NoBuffer);
  77.       Error();
  78.       return;
  79.    }
  80.  
  81.    /*
  82.     * Get area to be affected.  If the user has defined a region,
  83.     * then the Buffer->Mask structure defines that region.  We
  84.     * limit our processing to that region.
  85.     */
  86.    if (buf->Mask) {
  87.       le = buf->Mask->OffsetX;
  88.       te = buf->Mask->OffsetY;
  89.       w  = buf->Mask->Width;
  90.       h  = buf->Mask->Height;
  91.    }
  92.    else {
  93.       le = 0;
  94.       te = 0;
  95.       w  = buf->Width;
  96.       h  = buf->Height;
  97.    }
  98.  
  99.    /*
  100.     * Save the region we are about to fiddle with into an undo buffer.
  101.     */
  102.    SaveUndo(buf, le, te, w, h);
  103.  
  104.    /*
  105.     * Start a cancel-able status bar.
  106.     */
  107.    BeginBar("Thingy", h, TRUE);
  108.  
  109.    /*
  110.     * Loop over each scanline.
  111.     */
  112.    for (j = 0; j < h; j++) {
  113.  
  114.       /*
  115.        * Iterate the status bar.
  116.        */
  117.       if (Bar(j)) {
  118.          /* Status bar was cancelled if Bar() returns non-zero */
  119.          RestoreUndo(FALSE);  /* restore portion that was already
  120.                                  modified from the undo buffer */
  121.          break;
  122.       }
  123.  
  124.       /*
  125.        * Get a scanline of data from the buffer.  Using this
  126.        * function automatically supports vmem.
  127.        */
  128.       if (!GetBufLine(buf, &red, &grn, &blu, j + te)) {
  129.          /* Failure means a vmem disk fault of some kind */
  130.          break;
  131.       }
  132.  
  133.       for (i = 0; i < w; i++) {
  134.  
  135.          /*
  136.           * The CheckMask() function finds out whether the
  137.           * given X,Y location is within the mask given.
  138.           * Note that the coordinates passed to CheckMask()
  139.           * are relative to the upper-left corner of the mask,
  140.           * NOT the image.
  141.           */
  142.          if (CheckMask(buf->Mask, i, j)) {
  143.             red[le+i] = MIN(255, red[le+i]+1);
  144.             grn[le+i] = MIN(255, grn[le+i]+1);
  145.             blu[le+i] = MIN(255, blu[le+i]+1);
  146.          }
  147.  
  148.       }
  149.  
  150.       /*
  151.        * After processing a scanline, we MUST use PutBufLine()
  152.        * to put it back into the buffer.  If not, then the
  153.        * changes will not take effect for a vmem buffer.
  154.        * PutBufLine() restores the last line read with GetBufLine().
  155.        */
  156.       PutBufLine(buf);
  157.  
  158.    }
  159.  
  160.    /*
  161.     * Get rid of the status bar.  (Don't forget this or the
  162.     * interface will be locked out.)
  163.     */
  164.    EndBar(NULL);
  165.  
  166.    /*
  167.     * Redraw the image to show what we did.
  168.     */
  169.    RedrawFull();
  170. }
  171.