home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / IndexColorModel.java < prev    next >
Text File  |  1996-05-03  |  10KB  |  298 lines

  1. /*
  2.  * @(#)IndexColorModel.java    1.13 96/02/22 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.13 22 Feb 1996
  39.  * @author     Jim Graham
  40.  */
  41. public class IndexColorModel extends ColorModel {
  42.     private int rgb[];
  43.     private int map_size;
  44.     private boolean opaque;
  45.  
  46.     private int transparent_index;
  47.  
  48.     /**
  49.      * Constructs an IndexColorModel from the given arrays of red,
  50.      * green, and blue components.  Pixels described by this color
  51.      * model will all have alpha components of 255 (fully opaque).
  52.      * All of the arrays specifying the color components must have
  53.      * at least the specified number of entries.
  54.      * @param bits    The number of bits each pixel occupies.
  55.      * @param size    The size of the color component arrays.
  56.      * @param r        The array of red color components.
  57.      * @param g        The array of green color components.
  58.      * @param b        The array of blue color components.
  59.      */
  60.     public IndexColorModel(int bits, int size,
  61.                byte r[], byte g[], byte b[]) {
  62.     super(bits);
  63.     setRGBs(size, r, g, b, null);
  64.     }
  65.  
  66.     /**
  67.      * Constructs an IndexColorModel from the given arrays of red,
  68.      * green, and blue components.  Pixels described by this color
  69.      * model will all have alpha components of 255 (fully opaque),
  70.      * except for the indicated transparent pixel.  All of the arrays
  71.      * specifying the color components must have at least the specified
  72.      * number of entries.
  73.      * @param bits    The number of bits each pixel occupies.
  74.      * @param size    The size of the color component arrays.
  75.      * @param r        The array of red color components.
  76.      * @param g        The array of green color components.
  77.      * @param b        The array of blue color components.
  78.      * @param trans    The index of the transparent pixel.
  79.      */
  80.     public IndexColorModel(int bits, int size,
  81.                byte r[], byte g[], byte b[], int trans) {
  82.     super(bits);
  83.     setRGBs(size, r, g, b, null);
  84.     setTransparentPixel(trans);
  85.     }
  86.  
  87.     /**
  88.      * Constructs an IndexColorModel from the given arrays of red,
  89.      * green, blue and alpha components.  All of the arrays specifying
  90.      * the color components must have at least the specified number
  91.      * of entries.
  92.      * @param bits    The number of bits each pixel occupies.
  93.      * @param size    The size of the color component arrays.
  94.      * @param r        The array of red color components.
  95.      * @param g        The array of green color components.
  96.      * @param b        The array of blue color components.
  97.      * @param a        The array of alpha value components.
  98.      */
  99.     public IndexColorModel(int bits, int size,
  100.                byte r[], byte g[], byte b[], byte a[]) {
  101.     super(bits);
  102.     if (size > 0 && a == null) {
  103.         throw new NullPointerException();
  104.     }
  105.     setRGBs(size, r, g, b, a);
  106.     }
  107.  
  108.     private void setRGBs(int size, byte r[], byte g[], byte b[], byte a[]) {
  109.     if ((pixel_bits > 8) || (size > (1 << pixel_bits))) {
  110.         throw new ArrayIndexOutOfBoundsException();
  111.     }
  112.     map_size = size;
  113.     rgb = new int[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.     if ((bits > 8) || (size > (1 << bits))) {
  167.         throw new ArrayIndexOutOfBoundsException();
  168.     }
  169.     map_size = size;
  170.     rgb = new int[256];
  171.     int j = start;
  172.     int alpha = 0xff;
  173.     opaque = true;
  174.     for (int i = 0; i < size; i++) {
  175.         rgb[i] = ((cmap[j++] & 0xff) << 16)
  176.         | ((cmap[j++] & 0xff) << 8)
  177.         | (cmap[j++] & 0xff);
  178.         if (hasalpha) {
  179.         alpha = cmap[j++];
  180.         if (alpha != 0xff) {
  181.             opaque = false;
  182.         }
  183.         }
  184.         rgb[i] |= (alpha << 24);
  185.     }
  186.     setTransparentPixel(trans);
  187.     }
  188.  
  189.     /**
  190.      * Returns the size of the color component arrays in this IndexColorModel.
  191.      */
  192.     final public int getMapSize() {
  193.     return map_size;
  194.     }
  195.  
  196.     /**
  197.      * Returns the index of the transparent pixel in this IndexColorModel
  198.      * or -1 if there is no transparent pixel.
  199.      */
  200.     final public int getTransparentPixel() {
  201.     return transparent_index;
  202.     }
  203.  
  204.     /**
  205.      * Copies the array of red 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 getReds(byte r[]) {
  210.     for (int i = 0; i < map_size; i++) {
  211.         r[i] = (byte) (rgb[i] >> 16);
  212.     }
  213.     }
  214.  
  215.     /**
  216.      * Copies the array of green color components into the given array.  Only
  217.      * the initial entries of the array as specified by getMapSize() are
  218.      *  written.
  219.      */
  220.     final public void getGreens(byte g[]) {
  221.     for (int i = 0; i < map_size; i++) {
  222.         g[i] = (byte) (rgb[i] >> 8);
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Copies the array of blue color components 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 getBlues(byte b[]) {
  232.     for (int i = 0; i < map_size; i++) {
  233.         b[i] = (byte) rgb[i];
  234.     }
  235.     }
  236.  
  237.     /**
  238.      * Copies the array of alpha transparency values into the given array.  Only
  239.      * the initial entries of the array as specified by getMapSize() will
  240.      * be written.
  241.      */
  242.     final public void getAlphas(byte a[]) {
  243.     for (int i = 0; i < map_size; i++) {
  244.         a[i] = (byte) (rgb[i] >> 24);
  245.     }
  246.     }
  247.  
  248.     private void setTransparentPixel(int trans) {
  249.     if (trans >= map_size || trans < 0) {
  250.         trans = -1;
  251.     } else {
  252.         rgb[trans] &= 0x00ffffff;
  253.         opaque = false;
  254.     }
  255.     transparent_index = trans;
  256.     }
  257.  
  258.     /**
  259.      * Returns the red color compoment for the specified pixel in the
  260.      * range 0-255.
  261.      */
  262.     final public int getRed(int pixel) {
  263.     return (rgb[pixel] >> 16) & 0xff;
  264.     }
  265.  
  266.     /**
  267.      * Returns the green color compoment for the specified pixel in the
  268.      * range 0-255.
  269.      */
  270.     final public int getGreen(int pixel) {
  271.     return (rgb[pixel] >> 8) & 0xff;
  272.     }
  273.  
  274.     /**
  275.      * Returns the blue color compoment for the specified pixel in the
  276.      * range 0-255.
  277.      */
  278.     final public int getBlue(int pixel) {
  279.     return rgb[pixel] & 0xff;
  280.     }
  281.  
  282.     /**
  283.      * Returns the alpha transparency value for the specified pixel in the
  284.      * range 0-255.
  285.      */
  286.     final public int getAlpha(int pixel) {
  287.     return (rgb[pixel] >> 24) & 0xff;
  288.     }
  289.  
  290.     /**
  291.      * Returns the color of the pixel in the default RGB color model.
  292.      * @see ColorModel#getRGBdefault
  293.      */
  294.     final public int getRGB(int pixel) {
  295.     return rgb[pixel];
  296.     }
  297. }
  298.