home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / DirectColorModel.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  6.8 KB  |  242 lines

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