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

  1. /*
  2.  * @(#)DirectColorModel.java    1.14 96/11/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. import java.awt.AWTException;
  26.  
  27. /**
  28.  * A ColorModel class that specifies a translation from pixel values
  29.  * to alpha, red, green, and blue color components for pixels which
  30.  * have the color components embedded directly in the bits of the
  31.  * pixel itself.  This color model is similar to an X11 TrueColor
  32.  * visual.
  33.  
  34.  * <p>Many of the methods in this class are final. This is because the
  35.  * underlying native graphics code makes  assumptions about the layout
  36.  * and operation of this class  and those assumptions are reflected in
  37.  * the implementations of the methods here that are marked final.  You
  38.  * can subclass this class  for other reaons,  but you cannot override
  39.  * or modify the behaviour of those methods.
  40.  *
  41.  * @see ColorModel
  42.  *
  43.  * @version    1.14 11/23/96
  44.  * @author     Jim Graham
  45.  */
  46. public class DirectColorModel extends ColorModel {
  47.     private int red_mask;
  48.     private int green_mask;
  49.     private int blue_mask;
  50.     private int alpha_mask;
  51.     private int red_offset;
  52.     private int green_offset;
  53.     private int blue_offset;
  54.     private int alpha_offset;
  55.     private int red_scale;
  56.     private int green_scale;
  57.     private int blue_scale;
  58.     private int alpha_scale;
  59.  
  60.     /**
  61.      * Constructs a DirectColorModel from the given masks specifying
  62.      * which bits in the pixel contain the red, green and blue color
  63.      * components.  Pixels described by this color model will all
  64.      * have alpha components of 255 (fully opaque).  All of the bits
  65.      * in each mask must be contiguous and fit in the specified number
  66.      * of least significant bits of the integer.
  67.      */
  68.     public DirectColorModel(int bits,
  69.                 int rmask, int gmask, int bmask) {
  70.     this(bits, rmask, gmask, bmask, 0);
  71.     }
  72.  
  73.     /**
  74.      * Constructs a DirectColorModel from the given masks specifying
  75.      * which bits in the pixel contain the alhpa, red, green and blue
  76.      * color components.  All of the bits in each mask must be contiguous
  77.      * and fit in the specified number of least significant bits of the
  78.      * integer.
  79.      */
  80.     public DirectColorModel(int bits,
  81.                 int rmask, int gmask, int bmask, int amask) {
  82.     super(bits);
  83.     red_mask = rmask;
  84.     green_mask = gmask;
  85.     blue_mask = bmask;
  86.     alpha_mask = amask;
  87.     CalculateOffsets();
  88.     }
  89.  
  90.     /**
  91.      * Returns the mask indicating which bits in a pixel contain the red
  92.      * color component.
  93.      */
  94.     final public int getRedMask() {
  95.     return red_mask;
  96.     }
  97.  
  98.     /**
  99.      * Returns the mask indicating which bits in a pixel contain the green
  100.      * color component.
  101.      */
  102.     final public int getGreenMask() {
  103.     return green_mask;
  104.     }
  105.  
  106.     /**
  107.      * Returns the mask indicating which bits in a pixel contain the blue
  108.      * color component.
  109.      */
  110.     final public int getBlueMask() {
  111.     return blue_mask;
  112.     }
  113.  
  114.     /**
  115.      * Returns the mask indicating which bits in a pixel contain the alpha
  116.      * transparency component.
  117.      */
  118.     final public int getAlphaMask() {
  119.     return alpha_mask;
  120.     }
  121.  
  122.     private int accum_mask = 0;
  123.  
  124.     /*
  125.      * A utility function to decompose a single mask and verify that it
  126.      * fits in the specified pixel size, and that it does not overlap any
  127.      * other color component.  The values necessary to decompose and
  128.      * manipulate pixels are calculated as a side effect.
  129.      */
  130.     private void DecomposeMask(int mask, String componentName, int values[]) {
  131.     if ((mask & accum_mask) != 0) {
  132.         throw new IllegalArgumentException(componentName + " mask bits not unique");
  133.     }
  134.     int off = 0;
  135.     int count = 0;
  136.     if (mask != 0) {
  137.         while ((mask & 1) == 0) {
  138.         mask >>>= 1;
  139.         off++;
  140.         }
  141.         while ((mask & 1) == 1) {
  142.         mask >>>= 1;
  143.         count++;
  144.         }
  145.     }
  146.     if (mask != 0) {
  147.         throw new IllegalArgumentException(componentName + " mask bits not contiguous");
  148.     }
  149.     if (off + count > pixel_bits) {
  150.         throw new IllegalArgumentException(componentName + " mask overflows pixel");
  151.     }
  152.     int scale;
  153.     if (count < 8) {
  154.         scale = (1 << count) - 1;
  155.     } else {
  156.         scale = 0;
  157.         if (count > 8) {
  158.         off += (count - 8);
  159.         }
  160.     }
  161.     values[0] = off;
  162.     values[1] = scale;
  163.     }
  164.  
  165.     /*
  166.      * A utility function to verify all of the masks and to store
  167.      * the auxilliary values needed to manipulate the pixels.
  168.      */
  169.     private void CalculateOffsets() {
  170.     int values[] = new int[2];
  171.     DecomposeMask(red_mask, "red", values);
  172.     red_offset = values[0];
  173.     red_scale = values[1];
  174.     DecomposeMask(green_mask, "green", values);
  175.     green_offset = values[0];
  176.     green_scale = values[1];
  177.     DecomposeMask(blue_mask, "blue", values);
  178.     blue_offset = values[0];
  179.     blue_scale = values[1];
  180.     DecomposeMask(alpha_mask, "alpha", values);
  181.     alpha_offset = values[0];
  182.     alpha_scale = values[1];
  183.     }
  184.  
  185.     /**
  186.      * Returns the red color compoment for the specified pixel in the
  187.      * range 0-255.
  188.      */
  189.     final public int getRed(int pixel) {
  190.     int r = ((pixel & red_mask) >>> red_offset);
  191.     if (red_scale != 0) {
  192.         r = r * 255 / red_scale;
  193.     }
  194.     return r;
  195.     }
  196.  
  197.     /**
  198.      * Returns the green color compoment for the specified pixel in the
  199.      * range 0-255.
  200.      */
  201.     final public int getGreen(int pixel) {
  202.     int g = ((pixel & green_mask) >>> green_offset);
  203.     if (green_scale != 0) {
  204.         g = g * 255 / green_scale;
  205.     }
  206.     return g;
  207.     }
  208.  
  209.     /**
  210.      * Returns the blue color compoment for the specified pixel in the
  211.      * range 0-255.
  212.      */
  213.     final public int getBlue(int pixel) {
  214.     int b = ((pixel & blue_mask) >>> blue_offset);
  215.     if (blue_scale != 0) {
  216.         b = b * 255 / blue_scale;
  217.     }
  218.     return b;
  219.     }
  220.  
  221.     /**
  222.      * Return the alpha transparency value for the specified pixel in the
  223.      * range 0-255.
  224.      */
  225.     final public int getAlpha(int pixel) {
  226.     if (alpha_mask == 0) return 255;
  227.     int a = ((pixel & alpha_mask) >>> alpha_offset);
  228.     if (alpha_scale != 0) {
  229.         a = a * 255 / alpha_scale;
  230.     }
  231.     return a;
  232.     }
  233.  
  234.     /**
  235.      * Returns the color of the pixel in the default RGB color model.
  236.      * @see ColorModel#getRGBdefault
  237.      */
  238.     final public int getRGB(int pixel) {
  239.     return (getAlpha(pixel) << 24)
  240.         | (getRed(pixel) << 16)
  241.         | (getGreen(pixel) << 8)
  242.         | (getBlue(pixel) << 0);
  243.     }
  244. }
  245.