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
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
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
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} equals this
1445 * {@code ColorModel}.
1446 * In order to protect the symmetry property of
1447 * {@code (a.equals(b) == b.equals(a))},
1448 * the target object must be the exact same class as this
1449 * object to evaluate as {equals}.
1450 * @param obj the {@code Object} to test for equality
1451 * @return {@code true} if the specified {@code Object}
1452 * equals this {@code ColorModel}; {@code false} otherwise.
1453 */
1454 @Override
1455 public boolean equals(Object obj) {
1456 if (this == obj) {
1457 return true;
1458 }
1459 if ((obj == null) || (obj.getClass() != getClass())) {
1460 return false;
1461 }
1462
1463 ColorModel cm = (ColorModel) obj;
1464 if (supportsAlpha != cm.hasAlpha() ||
1465 isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
1466 pixel_bits != cm.getPixelSize() ||
1467 transparency != cm.getTransparency() ||
1468 numComponents != cm.getNumComponents())
1469 {
1470 return false;
1471 }
1472
1473 int[] nb = cm.getComponentSize();
1474
1475 if ((nBits != null) && (nb != null)) {
1476 for (int i = 0; i < numComponents; i++) {
1477 if (nBits[i] != nb[i]) {
1478 return false;
1479 }
1480 }
1481 } else {
1482 return ((nBits == null) && (nb == null));
1483 }
1484
1485 return true;
1486 }
1487
1488 /**
1489 * Returns the hash code for this ColorModel.
1490 *
1491 * @return a hash code for this ColorModel.
1492 */
1493 @Override
1494 public int hashCode() {
1495 int hash = 7;
1496 hash = 89 * hash + this.pixel_bits;
1497 hash = 89 * hash + Arrays.hashCode(this.nBits);
1498 hash = 89 * hash + this.transparency;
1499 hash = 89 * hash + (this.supportsAlpha ? 1 : 0);
1500 hash = 89 * hash + (this.isAlphaPremultiplied ? 1 : 0);
1501 hash = 89 * hash + this.numComponents;
1502 return hash;
1503 }
1504
1505 /**
1506 * Returns the {@code ColorSpace} associated with this
1507 * {@code ColorModel}.
1508 * @return the {@code ColorSpace} of this
1509 * {@code ColorModel}.
1510 */
1511 public final ColorSpace getColorSpace() {
1512 return colorSpace;
1513 }
1514
1515 /**
1516 * Forces the raster data to match the state specified in the
1517 * {@code isAlphaPremultiplied} variable, assuming the data is
1518 * currently correctly described by this {@code ColorModel}. It
1519 * may multiply or divide the color raster data by alpha, or do
1520 * nothing if the data is in the correct state. If the data needs to
1521 * be coerced, this method will also return an instance of this
1522 * {@code ColorModel} with the {@code isAlphaPremultiplied}
|