home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / image / indexc~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  9.6 KB  |  288 lines

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