home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / IndexColorModel.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  287 lines

  1. /*
  2.  * @(#)IndexColorModel.java    1.16 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17. /**
  18.  * A ColorModel class that specifies a translation from pixel values
  19.  * to alpha, red, green, and blue color components for pixels which
  20.  * represent indices into a fixed colormap.  An optional transparent
  21.  * pixel value can be supplied which indicates a completely transparent
  22.  * pixel, regardless of any alpha value recorded for that pixel value.
  23.  * This color model is similar to an X11 PseudoColor visual.
  24.  * <p>Many of the methods in this class are final.  The reason for
  25.  * this is that the underlying native graphics code makes assumptions
  26.  * about the layout and operation of this class and those assumptions
  27.  * are reflected in the implementations of the methods here that are
  28.  * marked final.  You can subclass this class for other reaons, but
  29.  * you cannot override or modify the behaviour of those methods.
  30.  *
  31.  * @see ColorModel
  32.  *
  33.  * @version    1.16 07/01/98
  34.  * @author     Jim Graham
  35.  */
  36. public class IndexColorModel extends ColorModel {
  37.     private int rgb[];
  38.     private int map_size;
  39.     private boolean opaque;
  40.  
  41.     private int transparent_index;
  42.  
  43.     /**
  44.      * Constructs an IndexColorModel from the given arrays of red,
  45.      * green, and blue components.  Pixels described by this color
  46.      * model will all have alpha components of 255 (fully opaque).
  47.      * All of the arrays specifying the color components must have
  48.      * at least the specified number of entries.
  49.      * @param bits    The number of bits each pixel occupies.
  50.      * @param size    The size of the color component arrays.
  51.      * @param r        The array of red color components.
  52.      * @param g        The array of green color components.
  53.      * @param b        The array of blue color components.
  54.      */
  55.     public IndexColorModel(int bits, int size,
  56.                byte r[], byte g[], byte b[]) {
  57.     super(bits);
  58.     setRGBs(size, r, g, b, null);
  59.     }
  60.  
  61.     /**
  62.      * Constructs an IndexColorModel from the given arrays of red,
  63.      * green, and blue components.  Pixels described by this color
  64.      * model will all have alpha components of 255 (fully opaque),
  65.      * except for the indicated transparent pixel.  All of the arrays
  66.      * specifying the color components must have at least the specified
  67.      * number of entries.
  68.      * @param bits    The number of bits each pixel occupies.
  69.      * @param size    The size of the color component arrays.
  70.      * @param r        The array of red color components.
  71.      * @param g        The array of green color components.
  72.      * @param b        The array of blue color components.
  73.      * @param trans    The index of the transparent pixel.
  74.      */
  75.     public IndexColorModel(int bits, int size,
  76.                byte r[], byte g[], byte b[], int trans) {
  77.     super(bits);
  78.     setRGBs(size, r, g, b, null);
  79.     setTransparentPixel(trans);
  80.     }
  81.  
  82.     /**
  83.      * Constructs an IndexColorModel from the given arrays of red,
  84.      * green, blue and alpha components.  All of the arrays specifying
  85.      * the color components must have at least the specified number
  86.      * of entries.
  87.      * @param bits    The number of bits each pixel occupies.
  88.      * @param size    The size of the color component arrays.
  89.      * @param r        The array of red color components.
  90.      * @param g        The array of green color components.
  91.      * @param b        The array of blue color components.
  92.      * @param a        The array of alpha value components.
  93.      */
  94.     public IndexColorModel(int bits, int size,
  95.                byte r[], byte g[], byte b[], byte a[]) {
  96.     super(bits);
  97.     if (size > 0 && a == null) {
  98.         throw new NullPointerException();
  99.     }
  100.     setRGBs(size, r, g, b, a);
  101.     }
  102.  
  103.     private void setRGBs(int size, byte r[], byte g[], byte b[], byte a[]) {
  104.     map_size = size;
  105.     rgb = new int[Math.max(size, 256)];
  106.     int alpha = 0xff;
  107.     opaque = true;
  108.     for (int i = 0; i < size; i++) {
  109.         if (a != null) {
  110.         alpha = (a[i] & 0xff);
  111.         if (alpha != 0xff) {
  112.             opaque = false;
  113.         }
  114.         }
  115.         rgb[i] = (alpha << 24)
  116.         | ((r[i] & 0xff) << 16)
  117.         | ((g[i] & 0xff) << 8)
  118.         | (b[i] & 0xff);
  119.     }
  120.     }
  121.  
  122.     /**
  123.      * Constructs an IndexColorModel from a single arrays of packed
  124.      * red, green, blue and optional alpha components.  The array
  125.      * must have enough values in it to fill all of the needed
  126.      * component arrays of the specified size.
  127.      * @param bits    The number of bits each pixel occupies.
  128.      * @param size    The size of the color component arrays.
  129.      * @param cmap    The array of color components.
  130.      * @param start    The starting offset of the first color component.
  131.      * @param hasalpha    Indicates whether alpha values are contained in
  132.      *            the cmap array.
  133.      */
  134.     public IndexColorModel(int bits, int size, byte cmap[], int start,
  135.                boolean hasalpha) {
  136.     this(bits, size, cmap, start, hasalpha, -1);
  137.     }
  138.  
  139.     /**
  140.      * Constructs an IndexColorModel from a single arrays of packed
  141.      * red, green, blue and optional alpha components.  The specified
  142.      * transparent index represents a pixel which will be considered
  143.      * entirely transparent regardless of any alpha value specified
  144.      * for it.  The array must have enough values in it to fill all
  145.      * of the needed component arrays of the specified size.
  146.      * @param bits    The number of bits each pixel occupies.
  147.      * @param size    The size of the color component arrays.
  148.      * @param cmap    The array of color components.
  149.      * @param start    The starting offset of the first color component.
  150.      * @param hasalpha    Indicates whether alpha values are contained in
  151.      *            the cmap array.
  152.      * @param trans    The index of the fully transparent pixel.
  153.      */
  154.     public IndexColorModel(int bits, int size, byte cmap[], int start,
  155.                boolean hasalpha, int trans) {
  156.     // REMIND: This assumes the ordering: RGB[A]
  157.     super(bits);
  158.     map_size = size;
  159.     rgb = new int[Math.max(size, 256)];
  160.     int j = start;
  161.     int alpha = 0xff;
  162.     opaque = true;
  163.     for (int i = 0; i < size; i++) {
  164.         rgb[i] = ((cmap[j++] & 0xff) << 16)
  165.         | ((cmap[j++] & 0xff) << 8)
  166.         | (cmap[j++] & 0xff);
  167.         if (hasalpha) {
  168.         alpha = cmap[j++];
  169.         if (alpha != 0xff) {
  170.             opaque = false;
  171.         }
  172.         }
  173.         rgb[i] |= (alpha << 24);
  174.     }
  175.     setTransparentPixel(trans);
  176.     }
  177.  
  178.     /**
  179.      * Returns the size of the color component arrays in this IndexColorModel.
  180.      */
  181.     final public int getMapSize() {
  182.     return map_size;
  183.     }
  184.  
  185.     /**
  186.      * Returns the index of the transparent pixel in this IndexColorModel
  187.      * or -1 if there is no transparent pixel.
  188.      */
  189.     final public int getTransparentPixel() {
  190.     return transparent_index;
  191.     }
  192.  
  193.     /**
  194.      * Copies the array of red color components into the given array.  Only
  195.      * the initial entries of the array as specified by getMapSize() are
  196.      * written.
  197.      */
  198.     final public void getReds(byte r[]) {
  199.     for (int i = 0; i < map_size; i++) {
  200.         r[i] = (byte) (rgb[i] >> 16);
  201.     }
  202.     }
  203.  
  204.     /**
  205.      * Copies the array of green color components into the given array.  Only
  206.      * the initial entries of the array as specified by getMapSize() are
  207.      *  written.
  208.      */
  209.     final public void getGreens(byte g[]) {
  210.     for (int i = 0; i < map_size; i++) {
  211.         g[i] = (byte) (rgb[i] >> 8);
  212.     }
  213.     }
  214.  
  215.     /**
  216.      * Copies the array of blue color components into the given array.  Only
  217.      * the initial entries of the array as specified by getMapSize() will
  218.      * be written.
  219.      */
  220.     final public void getBlues(byte b[]) {
  221.     for (int i = 0; i < map_size; i++) {
  222.         b[i] = (byte) rgb[i];
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Copies the array of alpha transparency values into the given array.  Only
  228.      * the initial entries of the array as specified by getMapSize() will
  229.      * be written.
  230.      */
  231.     final public void getAlphas(byte a[]) {
  232.     for (int i = 0; i < map_size; i++) {
  233.         a[i] = (byte) (rgb[i] >> 24);
  234.     }
  235.     }
  236.  
  237.     private void setTransparentPixel(int trans) {
  238.     if (trans >= map_size || trans < 0) {
  239.         trans = -1;
  240.     } else {
  241.         rgb[trans] &= 0x00ffffff;
  242.         opaque = false;
  243.     }
  244.     transparent_index = trans;
  245.     }
  246.  
  247.     /**
  248.      * Returns the red color compoment for the specified pixel in the
  249.      * range 0-255.
  250.      */
  251.     final public int getRed(int pixel) {
  252.     return (rgb[pixel] >> 16) & 0xff;
  253.     }
  254.  
  255.     /**
  256.      * Returns the green color compoment for the specified pixel in the
  257.      * range 0-255.
  258.      */
  259.     final public int getGreen(int pixel) {
  260.     return (rgb[pixel] >> 8) & 0xff;
  261.     }
  262.  
  263.     /**
  264.      * Returns the blue color compoment for the specified pixel in the
  265.      * range 0-255.
  266.      */
  267.     final public int getBlue(int pixel) {
  268.     return rgb[pixel] & 0xff;
  269.     }
  270.  
  271.     /**
  272.      * Returns the alpha transparency value for the specified pixel in the
  273.      * range 0-255.
  274.      */
  275.     final public int getAlpha(int pixel) {
  276.     return (rgb[pixel] >> 24) & 0xff;
  277.     }
  278.  
  279.     /**
  280.      * Returns the color of the pixel in the default RGB color model.
  281.      * @see ColorModel#getRGBdefault
  282.      */
  283.     final public int getRGB(int pixel) {
  284.     return rgb[pixel];
  285.     }
  286. }
  287.