home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / IndexColorModel.java < prev    next >
Text File  |  1997-05-20  |  10KB  |  295 lines

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