home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / OUTPUT / MASK_FIL.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  7.3 KB  |  286 lines

  1. package sub_arctic.output;
  2.  
  3. import sub_arctic.lib.sub_arctic_error;
  4.  
  5. import java.awt.Color;
  6. import java.awt.image.RGBImageFilter;
  7. import java.awt.Image;
  8. import java.lang.InternalError;
  9. import java.awt.image.PixelGrabber;
  10. import java.lang.InterruptedException;
  11. import java.awt.image.ImageObserver;
  12. import java.awt.Component;
  13. import java.awt.MediaTracker;
  14. import java.applet.Applet;
  15.  
  16. /**
  17.  * An RGBImageFilter for building images from an intensity mask and a color. 
  18.  *
  19.  * @see java.awt.image.RGBImageFilter
  20.  * @see sub_arctic.output.loaded_image#image_from_intensity_map
  21.  * @author Ian Smith
  22.  */
  23. public class mask_filter extends RGBImageFilter {
  24.  
  25.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  26.  
  27.   /**
  28.    * H of HSV representation of target. Range is [0,360)
  29.    * and -1.0 if H is undefined.
  30.    */
  31.   protected double target_h;
  32.  
  33.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  34.  
  35.   /**
  36.    * S of HSV representation of target. Range is [0,1)
  37.    */
  38.   protected double target_s;
  39.  
  40.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  41.  
  42.   /**
  43.    * V of HSV representation of target. Range is [0,1)
  44.    */
  45.   protected double target_v;
  46.  
  47.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  48.  
  49.   /**
  50.    * Convert an RGB value to RGB. This is section 13.3.4 of
  51.    * Foley & Van Dam.
  52.    */
  53.   protected void to_hsv(Color c) {
  54.     double r,g,b; // range [0,1]
  55.     int int_r=c.getRed(), int_g=c.getGreen(), int_b=c.getBlue();
  56.     double max, min, delta;
  57.  
  58.     r=((double)int_r/255.0);
  59.     g=((double)int_g/255.0);
  60.     b=((double)int_b/255.0);
  61.  
  62.     /* compute max */
  63.     max=r;
  64.     if (g>max) max=g;
  65.     if (b>max) max=b;
  66.  
  67.     /* compute min */
  68.     min=r;
  69.     if (g<min) min=g;
  70.     if (b<min) min=b;
  71.  
  72.     target_v=max; // got our v
  73.     
  74.     // work on saturation
  75.     if (max!=0.0) {
  76.       target_s=(max-min)/max;
  77.     } else {
  78.       target_s=0; // black's saturation is zero
  79.     }
  80.  
  81.     //work on hue
  82.     if (target_s==0.0) {
  83.       target_h=-1.0; // undefined hue, achromatic
  84.     } else {
  85.  
  86.       // its chromatic
  87.       delta=max-min;
  88.  
  89.       // is it between yellow and magenta
  90.       if (r==max) {
  91.     target_h=(g-b)/delta;
  92.       } else if (g==max) { // between cyan and yellow?
  93.     target_h=2.0+(b-r)/delta;
  94.       } else { // must be between magenta and cyan
  95.     target_h=4.0 + (r-g)/delta;
  96.       }
  97.  
  98.       /* convert hue to degrees */
  99.       target_h=target_h*60.0;
  100.       /* make sure its positive */
  101.       if (target_h<0) {
  102.     target_h+=360.0;
  103.       }
  104.     }
  105.   }
  106.  
  107.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  108.  
  109.   /**
  110.    * This is temporary holding place for the R value when we
  111.    * are doing a conversion from HSV to RGB.
  112.    */
  113.   protected int tmp_r;
  114.  
  115.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  116.  
  117.   /**
  118.    * This is temporary holding place for the G value when we
  119.    * are doing a conversion from HSV to RGB.
  120.    */
  121.   protected int tmp_g;
  122.  
  123.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  124.  
  125.   /**
  126.    * This is temporary holding place for the B value when we
  127.    * are doing a conversion from HSV to RGB.
  128.    */
  129.   protected int tmp_b;
  130.  
  131.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  132.  
  133.   /**
  134.    * Convert our HSV representation into a color with a 
  135.    * different v value.
  136.    *  
  137.    * @param double v the new v value
  138.    */
  139.   protected void new_color(double v) {
  140.     double h=target_h, s=target_s,i,f,p,q,t;
  141.     int which;
  142.  
  143.     /* is it achromatic ? */
  144.     if (s==0) {
  145.       if (h!=-1.0) {
  146.     throw new sub_arctic_error("Possible problem in HSV " +
  147.                    "conversion routine");
  148.       }
  149.  
  150.       /* its a greyscale, just make it from the value */
  151.       tmp_r=(int)(Math.round(v * 255.0));
  152.       tmp_g=tmp_r;
  153.       tmp_b=tmp_r;
  154.  
  155.     } else {
  156.  
  157.       /* it has a hue */
  158.       /* validity check on 360 */
  159.       if (h==360.0) h=0.0;
  160.       h/=60.0;
  161.       i=Math.floor(h);
  162.       f=h-i;
  163.       p=v*(1-s);
  164.       q=v*(1-(s*f));
  165.       t=v*(1-(s*(1-f)));
  166.       which=(int)i;
  167.  
  168.       switch (which) {
  169.       case 0:
  170.     tmp_r=(int)Math.round(v*255.0);
  171.     tmp_g=(int)Math.round(t*255.0);
  172.     tmp_b=(int)Math.round(p*255.0);
  173.     break;
  174.       case 1:
  175.     tmp_r=(int)Math.round(q*255.0);
  176.     tmp_g=(int)Math.round(v*255.0);
  177.     tmp_b=(int)Math.round(p*255.0);
  178.     break;
  179.       case 2:
  180.     tmp_r=(int)Math.round(p*255.0);
  181.     tmp_g=(int)Math.round(v*255.0);
  182.     tmp_b=(int)Math.round(t*255.0);
  183.     break;
  184.       case 3:
  185.     tmp_r=(int)Math.round(p*255.0);
  186.     tmp_g=(int)Math.round(q*255.0);
  187.     tmp_b=(int)Math.round(v*255.0);
  188.     break;
  189.       case 4:
  190.     tmp_r=(int)Math.round(t*255.0);
  191.     tmp_g=(int)Math.round(p*255.0);
  192.     tmp_b=(int)Math.round(v*255.0);
  193.     break;
  194.       case 5:
  195.     tmp_r=(int)Math.round(v*255.0);
  196.     tmp_g=(int)Math.round(p*255.0);
  197.     tmp_b=(int)Math.round(q*255.0);
  198.     break;
  199.       default:
  200.     System.out.println("Error in HSV->RGB conversion");
  201.     tmp_r=0;
  202.     tmp_g=0;
  203.     tmp_b=0;
  204.       }
  205.       /* done with it*/
  206.     }
  207.   }
  208.  
  209.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  210.  
  211.   /*
  212.    * Construct a new mask filter, given some color. This will
  213.    * filter the image passed through it as an intensity map
  214.    * of the color supplied here. Pixels in the intensity map
  215.    * which are lower than transp_intensity are made transparent.
  216.    * 
  217.    * @param Color c                the target color.
  218.    * @param int   transp_intensity this intensity or lower will be turned to 
  219.    *                               transparent.
  220.    */
  221.   public mask_filter(Color c,int transp_intensity) {
  222.     to_hsv(c);
  223.     //  The  filter's  operation  does  not  depend  on  the
  224.     //  pixel's  location,  so  IndexColorModels  can  be
  225.     //  filtered  directly.
  226.     canFilterIndexColorModel  =  true;
  227.     transp_color=transp_intensity;
  228.   }
  229.  
  230.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  231.  
  232.   /**
  233.    * Store the intensity we are using for transparent.
  234.    */
  235.   protected int transp_color;
  236.  
  237.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  238.  
  239.   /**
  240.    * This function gets called for each pixel.
  241.    * @param int x the x coord of the pixel (not used)
  242.    * @param int y the y coord of the pixel (not used)
  243.    * @param int rgb the rgb value (plus the alpha channel)
  244.    * @return int the new value
  245.    */
  246.   public int filterRGB(int x, int y, int rgb) {
  247.     /* we assume that all of r g & b are equal */
  248.     int intensity=rgb & 0xff, alpha = rgb & 0xff000000;
  249.     double scale= ((double)intensity/255.0);
  250.  
  251.     if (intensity<transp_color) {
  252.       ///* transparent */
  253.       alpha=0;
  254.       tmp_r=0;
  255.       tmp_g=0;
  256.       tmp_b=0;
  257.     } else {
  258.       /* do an RGB conversion scaling the V by scale*/
  259.       new_color(target_v*scale);
  260.     }
  261.     return ( alpha  | 
  262.          (tmp_b) |
  263.          (tmp_g << 8) |
  264.          (tmp_r << 16));
  265.   }
  266.  
  267.  /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  268.  
  269. }
  270. /*=========================== COPYRIGHT NOTICE ===========================
  271.  
  272. This file is part of the subArctic user interface toolkit.
  273.  
  274. Copyright (c) 1996 Scott Hudson and Ian Smith
  275. All rights reserved.
  276.  
  277. The subArctic system is freely available for most uses under the terms
  278. and conditions described in 
  279.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  280. and appearing in full in the lib/interactor.java source file.
  281.  
  282. The current release and additional information about this software can be 
  283. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  284.  
  285. ========================================================================*/
  286.