< 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
  55  * array of primitive types.  The Java(tm) Platform 1.0 and 1.1 APIs
  56  * represented pixels as single {@code byte} or single
  57  * {@code int} values.  For purposes of the {@code ColorModel}


1424      *          {@code pixel} is not large enough to hold a pixel
1425      *          value for this {@code ColorModel}.
1426      * @throws UnsupportedOperationException if the
1427      *          constructor of this {@code ColorModel} called the
1428      *          {@code super(bits)} constructor, but did not
1429      *          override this method.  See the constructor,
1430      *          {@link #ColorModel(int)}.
1431      * @throws UnsupportedOperationException if this method is unable
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 import java.util.Objects;
  39 
  40 /**
  41  * The {@code ColorModel} abstract class encapsulates the
  42  * methods for translating a pixel value to color components
  43  * (for example, red, green, and blue) and an alpha component.
  44  * In order to render an image to the screen, a printer, or another
  45  * image, pixel values must be converted to color and alpha components.
  46  * As arguments to or return values from methods of this class,
  47  * pixels are represented as 32-bit ints or as arrays of primitive types.
  48  * The number, order, and interpretation of color components for a
  49  * {@code ColorModel} is specified by its {@code ColorSpace}.
  50  * A {@code ColorModel} used with pixel data that does not include
  51  * alpha information treats all pixels as opaque, which is an alpha
  52  * value of 1.0.
  53  * <p>
  54  * This {@code ColorModel} class supports two representations of
  55  * pixel values.  A pixel value can be a single 32-bit int or an
  56  * array of primitive types.  The Java(tm) Platform 1.0 and 1.1 APIs
  57  * represented pixels as single {@code byte} or single
  58  * {@code int} values.  For purposes of the {@code ColorModel}


1425      *          {@code pixel} is not large enough to hold a pixel
1426      *          value for this {@code ColorModel}.
1427      * @throws UnsupportedOperationException if the
1428      *          constructor of this {@code ColorModel} called the
1429      *          {@code super(bits)} constructor, but did not
1430      *          override this method.  See the constructor,
1431      *          {@link #ColorModel(int)}.
1432      * @throws UnsupportedOperationException if this method is unable
1433      *          to determine the number of bits per component
1434      * @since 1.4
1435      */
1436     public float[] getNormalizedComponents(Object pixel,
1437                                            float[] normComponents,
1438                                            int normOffset) {
1439         int components[] = getComponents(pixel, null, 0);
1440         return getNormalizedComponents(components, 0,
1441                                        normComponents, normOffset);
1442     }
1443 
1444     /**
1445      * Tests if the specified {@code Object} equals this

1446      * {@code ColorModel}.
1447      * In order to protect the symmetry property of
1448      * {@code (a.equals(b) == b.equals(a))},
1449      * the target object must be the exact same class as this
1450      * object to evaluate as {equals}.
1451      * @param obj the {@code Object} to test for equality
1452      * @return {@code true} if the specified {@code Object}
1453      * equals this {@code ColorModel}; {@code false} otherwise.

1454      */
1455     @Override
1456     public boolean equals(Object obj) {
1457         if (this == obj) {
1458             return true;
1459         }
1460         if ((obj == null) || (obj.getClass() !=  getClass())) {
1461             return false;
1462         }

1463 
1464         ColorModel cm = (ColorModel) obj;


1465         if (supportsAlpha != cm.hasAlpha() ||
1466             isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
1467             pixel_bits != cm.getPixelSize() ||
1468             transparency != cm.getTransparency() ||
1469             numComponents != cm.getNumComponents() ||
1470             colorSpace != cm.colorSpace ||
1471             transferType != cm.transferType)
1472         {
1473             return false;
1474         }
1475 
1476         int[] nb = cm.getComponentSize();
1477 
1478         if ((nBits != null) && (nb != null)) {
1479             for (int i = 0; i < numComponents; i++) {
1480                 if (nBits[i] != nb[i]) {
1481                     return false;
1482                 }
1483             }
1484         } else {
1485             return ((nBits == null) && (nb == null));
1486         }
1487 
1488         return true;
1489     }
1490 
1491     /**
1492      * Returns the hash code for this ColorModel.
1493      *
1494      * @return    a hash code for this ColorModel.
1495      */
1496     @Override
1497     public int hashCode() {
1498         int hash = 7;
1499         hash = 89 * hash + this.pixel_bits;
1500         hash = 89 * hash + Arrays.hashCode(this.nBits);
1501         hash = 89 * hash + this.transparency;
1502         hash = 89 * hash + (this.supportsAlpha ? 1 : 0);
1503         hash = 89 * hash + (this.isAlphaPremultiplied ? 1 : 0);
1504         hash = 89 * hash + this.numComponents;
1505         hash = 89 * hash + Objects.hashCode(this.colorSpace);
1506         hash = 89 * hash + this.transferType;
1507         return hash;






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


< prev index next >