1 /*
2 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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}
345 bits.length);
346 }
347
348 // 4186669
349 if (transparency < Transparency.OPAQUE ||
350 transparency > Transparency.TRANSLUCENT)
351 {
352 throw new IllegalArgumentException("Unknown transparency: "+
353 transparency);
354 }
355
356 if (supportsAlpha == false) {
357 this.isAlphaPremultiplied = false;
358 this.transparency = Transparency.OPAQUE;
359 }
360 else {
361 this.isAlphaPremultiplied = isAlphaPremultiplied;
362 this.transparency = transparency;
363 }
364
365 nBits = bits.clone();
366 this.pixel_bits = pixel_bits;
367 if (pixel_bits <= 0) {
368 throw new IllegalArgumentException("Number of pixel bits must "+
369 "be > 0");
370 }
371 // Check for bits < 0
372 maxBits = 0;
373 for (int i=0; i < bits.length; i++) {
374 // bug 4304697
375 if (bits[i] < 0) {
376 throw new
377 IllegalArgumentException("Number of bits must be >= 0");
378 }
379 if (maxBits < bits[i]) {
380 maxBits = bits[i];
381 }
382 }
383
384 // Make sure that we don't have all 0-bit components
385 if (maxBits == 0) {
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}
|
1 /*
2 * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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.Collections;
35 import java.util.Map;
36 import java.util.WeakHashMap;
37 import java.util.Arrays;
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}
345 bits.length);
346 }
347
348 // 4186669
349 if (transparency < Transparency.OPAQUE ||
350 transparency > Transparency.TRANSLUCENT)
351 {
352 throw new IllegalArgumentException("Unknown transparency: "+
353 transparency);
354 }
355
356 if (supportsAlpha == false) {
357 this.isAlphaPremultiplied = false;
358 this.transparency = Transparency.OPAQUE;
359 }
360 else {
361 this.isAlphaPremultiplied = isAlphaPremultiplied;
362 this.transparency = transparency;
363 }
364
365 /*
366 * We need significant bits value only for the length
367 * of number of components, so we truncate remaining part.
368 * It also helps in hashCode calculation since bits[] can contain
369 * different values after the length of number of components between
370 * two ColorModels.
371 */
372 nBits = Arrays.copyOf(bits, numComponents);
373 this.pixel_bits = pixel_bits;
374 if (pixel_bits <= 0) {
375 throw new IllegalArgumentException("Number of pixel bits must "+
376 "be > 0");
377 }
378 // Check for bits < 0
379 maxBits = 0;
380 for (int i=0; i < bits.length; i++) {
381 // bug 4304697
382 if (bits[i] < 0) {
383 throw new
384 IllegalArgumentException("Number of bits must be >= 0");
385 }
386 if (maxBits < bits[i]) {
387 maxBits = bits[i];
388 }
389 }
390
391 // Make sure that we don't have all 0-bit components
392 if (maxBits == 0) {
1431 * {@code pixel} is not large enough to hold a pixel
1432 * value for this {@code ColorModel}.
1433 * @throws UnsupportedOperationException if the
1434 * constructor of this {@code ColorModel} called the
1435 * {@code super(bits)} constructor, but did not
1436 * override this method. See the constructor,
1437 * {@link #ColorModel(int)}.
1438 * @throws UnsupportedOperationException if this method is unable
1439 * to determine the number of bits per component
1440 * @since 1.4
1441 */
1442 public float[] getNormalizedComponents(Object pixel,
1443 float[] normComponents,
1444 int normOffset) {
1445 int components[] = getComponents(pixel, null, 0);
1446 return getNormalizedComponents(components, 0,
1447 normComponents, normOffset);
1448 }
1449
1450 /**
1451 * Tests if the specified {@code Object} equals this {@code ColorModel}.
1452 * @param obj the {@code Object} to test for equality
1453 * @return {@code true} if the specified {@code Object}
1454 * equals this {@code ColorModel}; {@code false} otherwise.
1455 */
1456 public boolean equals(Object obj) {
1457 return (obj == this);
1458 }
1459
1460 /**
1461 * Returns the {@code ColorSpace} associated with this
1462 * {@code ColorModel}.
1463 * @return the {@code ColorSpace} of this
1464 * {@code ColorModel}.
1465 */
1466 public final ColorSpace getColorSpace() {
1467 return colorSpace;
1468 }
1469
1470 /**
1471 * Forces the raster data to match the state specified in the
1472 * {@code isAlphaPremultiplied} variable, assuming the data is
1473 * currently correctly described by this {@code ColorModel}. It
1474 * may multiply or divide the color raster data by alpha, or do
1475 * nothing if the data is in the correct state. If the data needs to
1476 * be coerced, this method will also return an instance of this
1477 * {@code ColorModel} with the {@code isAlphaPremultiplied}
|