home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / touchup / part05 / ffill.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-14  |  2.7 KB  |  120 lines

  1.  
  2. /**************************************************************************
  3.    Touchup a bitmap graphics editor for the Sun Workstation running SunView
  4.    Copyright (c) 1988 by Raymond Kreisel
  5.    1/22/88 @ Suny Stony Brook
  6.  
  7.    This program may be redistributed without fee as long as this copyright
  8.    notice is intact.
  9.  
  10. ==> PLEASE send comments and bug reports to one of the following addresses:
  11.  
  12.        Ray Kreisel
  13.        CS Dept., SUNY at Stony Brook, Stony Brook NY 11794
  14.  
  15.        UUCP: {allegra, philabs, pyramid, research}!sbcs!rayk   
  16.        ARPA-Internet: rayk@sbcs.sunysb.edu            
  17.        CSnet: rayk@suny-sb
  18.        (If nobody is home at any of the above addresses try:
  19.         S72QKRE@TOWSONVX.BITNET                    )
  20.  
  21.  "If I get home before daylight, I just might get some sleep tonight...."
  22.  
  23. **************************************************************************/
  24. /**************************************************************************
  25.     file: ffill.c
  26.     purpose: this file has the functions that do flood fill
  27.  
  28.     modifications:
  29.         date:    Tue Mar 22 22:04:58 EST 1988
  30.         author:    rayk
  31.         changes:add comments
  32. **************************************************************************/
  33.  
  34. #include "header.h"
  35.  
  36.  
  37. /*
  38.  * let's go into flood fill mode because we got a button click on flood fill
  39.  */
  40. fill_mode(item, event)
  41. Panel_item      item;
  42. Event           *event;
  43. {
  44. int x,y;
  45.  
  46.  
  47.   if (select_pt_x != -1)
  48.       {
  49.          print_msg("Hold down the RIGHT mouse button to cancel flood fill.");
  50.        x = select_pt_x;
  51.          y = select_pt_y;
  52.      clean_point();
  53.      save_screen();
  54.      pw_put(pw,x,y,cur_color^1);
  55.      if (ffill(x,y,cur_color))
  56.         ERROR("Flood fill cancelled !!");
  57.      else
  58.         hide_msg();
  59.       }
  60.   else
  61.       {
  62.     ERROR("Select point first, then select flood fill");
  63.     set_select_mode();
  64.       }
  65. }
  66.  
  67.  
  68. #define FILL_CANCEL 1
  69.  
  70. /*
  71.  * this is a simple recursive flood fill that will work
  72.  * on mono bitmaps
  73.  * call it will the start x,y coordinates and the color
  74.  * to fill with, which is also the bounary color
  75.  */
  76. ffill(x,y,n)
  77. int x,y,n;
  78. {
  79. int flag;
  80. register i,j,k,bx;
  81.  
  82.         if (window_get(canvas, WIN_EVENT_STATE, MS_RIGHT))
  83.             return(FILL_CANCEL);
  84.  
  85.     if (x<0 || x>=image_wid || y<0 || y>=image_hgt || pw_get(pw,x,y)==n)
  86.         return(0);
  87.     pw_put(pw,x,y,n);
  88.         bx = x-1;
  89.     for (i=x-1;i>=0;i--) {
  90.         if (pw_get(pw,i,y)!=n)
  91.             bx = i;
  92.         else 
  93.             {
  94.             pw_vector(pw,bx,y,x,y, PIX_SRC,n);
  95.             break;
  96.             }
  97.         }
  98.         bx = x+1;
  99.     for (j=x+1;j<image_wid;j++)
  100.         if (pw_get(pw,j,y)!=n)
  101.             bx = j;
  102.         else 
  103.              {
  104.             pw_vector(pw,bx,y,x,y, PIX_SRC,n);
  105.             break;
  106.              }
  107.  
  108.         flag = 0;
  109.     for (k=i+1;k<j;k++)
  110.     {
  111.         if (y>0 && pw_get(pw,k,y-1)!=n)
  112.             flag = ffill(k,y-1,n);
  113.         if (y<image_hgt-1 && pw_get(pw,k,y+1)!=n)
  114.             flag = ffill(k,y+1,n);
  115.                 if (flag) break;
  116.     }
  117. return(flag);
  118. }
  119.  
  120.