< prev index next >

src/java.desktop/share/classes/java/awt/image/ColorModel.java

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.image;
  27 
  28 import java.awt.Transparency;
  29 import java.awt.color.ColorSpace;
  30 import java.awt.color.ICC_ColorSpace;
  31 import sun.java2d.cmm.CMSManager;
  32 import sun.java2d.cmm.ColorTransform;
  33 import sun.java2d.cmm.PCMM;
  34 import java.awt.Toolkit;
  35 import java.util.Collections;
  36 import java.util.Map;
  37 import java.util.WeakHashMap;
  38 
  39 /**
  40  * The {@code ColorModel} abstract class encapsulates the
  41  * methods for translating a pixel value to color components
  42  * (for example, red, green, and blue) and an alpha component.
  43  * In order to render an image to the screen, a printer, or another
  44  * image, pixel values must be converted to color and alpha components.
  45  * As arguments to or return values from methods of this class,
  46  * pixels are represented as 32-bit ints or as arrays of primitive types.
  47  * The number, order, and interpretation of color components for a
  48  * {@code ColorModel} is specified by its {@code ColorSpace}.
  49  * A {@code ColorModel} used with pixel data that does not include
  50  * alpha information treats all pixels as opaque, which is an alpha
  51  * value of 1.0.
  52  * <p>
  53  * This {@code ColorModel} class supports two representations of
  54  * pixel values.  A pixel value can be a single 32-bit int or an


1432      *          to determine the number of bits per component
1433      * @since 1.4
1434      */
1435     public float[] getNormalizedComponents(Object pixel,
1436                                            float[] normComponents,
1437                                            int normOffset) {
1438         int components[] = getComponents(pixel, null, 0);
1439         return getNormalizedComponents(components, 0,
1440                                        normComponents, normOffset);
1441     }
1442 
1443     /**
1444      * Tests if the specified {@code Object} is an instance of
1445      * {@code ColorModel} and if it equals this
1446      * {@code ColorModel}.
1447      * @param obj the {@code Object} to test for equality
1448      * @return {@code true} if the specified {@code Object}
1449      * is an instance of {@code ColorModel} and equals this
1450      * {@code ColorModel}; {@code false} otherwise.
1451      */

1452     public boolean equals(Object obj) {
1453         if (!(obj instanceof ColorModel)) {
1454             return false;
1455         }
1456         ColorModel cm = (ColorModel) obj;
1457 
1458         if (this == cm) {
1459             return true;
1460         }
1461         if (supportsAlpha != cm.hasAlpha() ||
1462             isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
1463             pixel_bits != cm.getPixelSize() ||
1464             transparency != cm.getTransparency() ||
1465             numComponents != cm.getNumComponents())
1466         {
1467             return false;
1468         }
1469 
1470         int[] nb = cm.getComponentSize();
1471 
1472         if ((nBits != null) && (nb != null)) {
1473             for (int i = 0; i < numComponents; i++) {
1474                 if (nBits[i] != nb[i]) {
1475                     return false;
1476                 }
1477             }
1478         } else {
1479             return ((nBits == null) && (nb == null));
1480         }
1481 
1482         return true;
1483     }
1484 
1485     /**
1486      * Returns the hash code for this ColorModel.
1487      *
1488      * @return    a hash code for this ColorModel.
1489      */

1490     public int hashCode() {
1491 
1492         int result = 0;
1493 
1494         result = (supportsAlpha ? 2 : 3) +
1495                  (isAlphaPremultiplied ? 4 : 5) +
1496                  pixel_bits * 6 +
1497                  transparency * 7 +
1498                  numComponents * 8;
1499 
1500         if (nBits != null) {
1501             for (int i = 0; i < numComponents; i++) {
1502                 result = result + nBits[i] * (i + 9);
1503             }
1504         }
1505 
1506         return result;
1507     }
1508 
1509     /**
1510      * Returns the {@code ColorSpace} associated with this
1511      * {@code ColorModel}.
1512      * @return the {@code ColorSpace} of this
1513      * {@code ColorModel}.
1514      */
1515     public final ColorSpace getColorSpace() {
1516         return colorSpace;
1517     }
1518 
1519     /**
1520      * Forces the raster data to match the state specified in the
1521      * {@code isAlphaPremultiplied} variable, assuming the data is
1522      * currently correctly described by this {@code ColorModel}.  It
1523      * may multiply or divide the color raster data by alpha, or do
1524      * nothing if the data is in the correct state.  If the data needs to
1525      * be coerced, this method will also return an instance of this
1526      * {@code ColorModel} with the {@code isAlphaPremultiplied}




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt.image;
  27 
  28 import java.awt.Transparency;
  29 import java.awt.color.ColorSpace;
  30 import java.awt.color.ICC_ColorSpace;
  31 import sun.java2d.cmm.CMSManager;
  32 import sun.java2d.cmm.ColorTransform;
  33 import sun.java2d.cmm.PCMM;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.Map;
  37 import java.util.WeakHashMap;
  38 
  39 /**
  40  * The {@code ColorModel} abstract class encapsulates the
  41  * methods for translating a pixel value to color components
  42  * (for example, red, green, and blue) and an alpha component.
  43  * In order to render an image to the screen, a printer, or another
  44  * image, pixel values must be converted to color and alpha components.
  45  * As arguments to or return values from methods of this class,
  46  * pixels are represented as 32-bit ints or as arrays of primitive types.
  47  * The number, order, and interpretation of color components for a
  48  * {@code ColorModel} is specified by its {@code ColorSpace}.
  49  * A {@code ColorModel} used with pixel data that does not include
  50  * alpha information treats all pixels as opaque, which is an alpha
  51  * value of 1.0.
  52  * <p>
  53  * This {@code ColorModel} class supports two representations of
  54  * pixel values.  A pixel value can be a single 32-bit int or an


1432      *          to determine the number of bits per component
1433      * @since 1.4
1434      */
1435     public float[] getNormalizedComponents(Object pixel,
1436                                            float[] normComponents,
1437                                            int normOffset) {
1438         int components[] = getComponents(pixel, null, 0);
1439         return getNormalizedComponents(components, 0,
1440                                        normComponents, normOffset);
1441     }
1442 
1443     /**
1444      * Tests if the specified {@code Object} is an instance of
1445      * {@code ColorModel} and if it equals this
1446      * {@code ColorModel}.
1447      * @param obj the {@code Object} to test for equality
1448      * @return {@code true} if the specified {@code Object}
1449      * is an instance of {@code ColorModel} and equals this
1450      * {@code ColorModel}; {@code false} otherwise.
1451      */
1452     @Override
1453     public boolean equals(Object obj) {
1454         if (!(obj instanceof ColorModel)) {
1455             return false;
1456         }
1457         ColorModel cm = (ColorModel) obj;
1458 
1459         if (this == cm) {
1460             return true;
1461         }
1462         if (supportsAlpha != cm.hasAlpha() ||
1463             isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
1464             pixel_bits != cm.getPixelSize() ||
1465             transparency != cm.getTransparency() ||
1466             numComponents != cm.getNumComponents())
1467         {
1468             return false;
1469         }
1470 
1471         int[] nb = cm.getComponentSize();
1472 
1473         if ((nBits != null) && (nb != null)) {
1474             for (int i = 0; i < numComponents; i++) {
1475                 if (nBits[i] != nb[i]) {
1476                     return false;
1477                 }
1478             }
1479         } else {
1480             return ((nBits == null) && (nb == null));
1481         }
1482 
1483         return true;
1484     }
1485 
1486     /**
1487      * Returns the hash code for this ColorModel.
1488      *
1489      * @return    a hash code for this ColorModel.
1490      */
1491     @Override
1492     public int hashCode() {
1493         int hash = 7;
1494         hash = 89 * hash + this.pixel_bits;
1495         hash = 89 * hash + Arrays.hashCode(this.nBits);
1496         hash = 89 * hash + this.transparency;
1497         hash = 89 * hash + (this.supportsAlpha ? 1 : 0);
1498         hash = 89 * hash + (this.isAlphaPremultiplied ? 1 : 0);
1499         hash = 89 * hash + this.numComponents;
1500         return hash;








1501     }
1502 
1503     /**
1504      * Returns the {@code ColorSpace} associated with this
1505      * {@code ColorModel}.
1506      * @return the {@code ColorSpace} of this
1507      * {@code ColorModel}.
1508      */
1509     public final ColorSpace getColorSpace() {
1510         return colorSpace;
1511     }
1512 
1513     /**
1514      * Forces the raster data to match the state specified in the
1515      * {@code isAlphaPremultiplied} variable, assuming the data is
1516      * currently correctly described by this {@code ColorModel}.  It
1517      * may multiply or divide the color raster data by alpha, or do
1518      * nothing if the data is in the correct state.  If the data needs to
1519      * be coerced, this method will also return an instance of this
1520      * {@code ColorModel} with the {@code isAlphaPremultiplied}


< prev index next >