< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2014, 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.math.BigInteger;


  31 
  32 /**
  33  * The {@code IndexColorModel} class is a {@code ColorModel}
  34  * class that works with pixel values consisting of a
  35  * single sample that is an index into a fixed colormap in the default
  36  * sRGB color space.  The colormap specifies red, green, blue, and
  37  * optional alpha components corresponding to each index.  All components
  38  * are represented in the colormap as 8-bit unsigned integral values.
  39  * Some constructors allow the caller to specify "holes" in the colormap
  40  * by indicating which colormap entries are valid and which represent
  41  * unusable colors via the bits set in a {@code BigInteger} object.
  42  * This color model is similar to an X11 PseudoColor visual.
  43  * <p>
  44  * Some constructors provide a means to specify an alpha component
  45  * for each pixel in the colormap, while others either provide no
  46  * such means or, in some cases, a flag to indicate whether the
  47  * colormap data contains alpha values.  If no alpha is supplied to
  48  * the constructor, an opaque alpha component (alpha = 1.0) is
  49  * assumed for each entry.
  50  * An optional transparent pixel value can be supplied that indicates a


 112  * <p>
 113  * Many of the methods in this class are final.  The reason for
 114  * this is that the underlying native graphics code makes assumptions
 115  * about the layout and operation of this class and those assumptions
 116  * are reflected in the implementations of the methods here that are
 117  * marked final.  You can subclass this class for other reasons, but
 118  * you cannot override or modify the behaviour of those methods.
 119  *
 120  * @see ColorModel
 121  * @see ColorSpace
 122  * @see DataBuffer
 123  *
 124  */
 125 public class IndexColorModel extends ColorModel {
 126     private int rgb[];
 127     private int map_size;
 128     private int pixel_mask;
 129     private int transparent_index = -1;
 130     private boolean allgrayopaque;
 131     private BigInteger validBits;

 132 
 133     private sun.awt.image.BufImgSurfaceData.ICMColorData colorData = null;
 134 
 135     private static int[] opaqueBits = {8, 8, 8};
 136     private static int[] alphaBits = {8, 8, 8, 8};
 137 
 138     private static native void initIDs();
 139     static {
 140         ColorModel.loadLibraries();
 141         initIDs();
 142     }
 143     /**
 144      * Constructs an {@code IndexColorModel} from the specified
 145      * arrays of red, green, and blue components.  Pixels described
 146      * by this color model all have alpha components of 255
 147      * unnormalized (1.0&nbsp;normalized), which means they
 148      * are fully opaque.  All of the arrays specifying the color
 149      * components must have at least the specified number of entries.
 150      * The {@code ColorSpace} is the default sRGB space.
 151      * Since there is no alpha information in any of the arguments


1515      */
1516     public void finalize() {
1517     }
1518 
1519     /**
1520      * Returns the {@code String} representation of the contents of
1521      * this {@code ColorModel} object.
1522      * @return a {@code String} representing the contents of this
1523      * {@code ColorModel} object.
1524      */
1525     public String toString() {
1526        return new String("IndexColorModel: #pixelBits = "+pixel_bits
1527                          + " numComponents = "+numComponents
1528                          + " color space = "+colorSpace
1529                          + " transparency = "+transparency
1530                          + " transIndex   = "+transparent_index
1531                          + " has alpha = "+supportsAlpha
1532                          + " isAlphaPre = "+isAlphaPremultiplied
1533                          );
1534     }

































































































1535 }
   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.math.BigInteger;
  31 import java.util.Arrays;
  32 import java.util.Objects;
  33 
  34 /**
  35  * The {@code IndexColorModel} class is a {@code ColorModel}
  36  * class that works with pixel values consisting of a
  37  * single sample that is an index into a fixed colormap in the default
  38  * sRGB color space.  The colormap specifies red, green, blue, and
  39  * optional alpha components corresponding to each index.  All components
  40  * are represented in the colormap as 8-bit unsigned integral values.
  41  * Some constructors allow the caller to specify "holes" in the colormap
  42  * by indicating which colormap entries are valid and which represent
  43  * unusable colors via the bits set in a {@code BigInteger} object.
  44  * This color model is similar to an X11 PseudoColor visual.
  45  * <p>
  46  * Some constructors provide a means to specify an alpha component
  47  * for each pixel in the colormap, while others either provide no
  48  * such means or, in some cases, a flag to indicate whether the
  49  * colormap data contains alpha values.  If no alpha is supplied to
  50  * the constructor, an opaque alpha component (alpha = 1.0) is
  51  * assumed for each entry.
  52  * An optional transparent pixel value can be supplied that indicates a


 114  * <p>
 115  * Many of the methods in this class are final.  The reason for
 116  * this is that the underlying native graphics code makes assumptions
 117  * about the layout and operation of this class and those assumptions
 118  * are reflected in the implementations of the methods here that are
 119  * marked final.  You can subclass this class for other reasons, but
 120  * you cannot override or modify the behaviour of those methods.
 121  *
 122  * @see ColorModel
 123  * @see ColorSpace
 124  * @see DataBuffer
 125  *
 126  */
 127 public class IndexColorModel extends ColorModel {
 128     private int rgb[];
 129     private int map_size;
 130     private int pixel_mask;
 131     private int transparent_index = -1;
 132     private boolean allgrayopaque;
 133     private BigInteger validBits;
 134     private volatile int hashCode;
 135 
 136     private sun.awt.image.BufImgSurfaceData.ICMColorData colorData = null;
 137 
 138     private static int[] opaqueBits = {8, 8, 8};
 139     private static int[] alphaBits = {8, 8, 8, 8};
 140 
 141     private static native void initIDs();
 142     static {
 143         ColorModel.loadLibraries();
 144         initIDs();
 145     }
 146     /**
 147      * Constructs an {@code IndexColorModel} from the specified
 148      * arrays of red, green, and blue components.  Pixels described
 149      * by this color model all have alpha components of 255
 150      * unnormalized (1.0&nbsp;normalized), which means they
 151      * are fully opaque.  All of the arrays specifying the color
 152      * components must have at least the specified number of entries.
 153      * The {@code ColorSpace} is the default sRGB space.
 154      * Since there is no alpha information in any of the arguments


1518      */
1519     public void finalize() {
1520     }
1521 
1522     /**
1523      * Returns the {@code String} representation of the contents of
1524      * this {@code ColorModel} object.
1525      * @return a {@code String} representing the contents of this
1526      * {@code ColorModel} object.
1527      */
1528     public String toString() {
1529        return new String("IndexColorModel: #pixelBits = "+pixel_bits
1530                          + " numComponents = "+numComponents
1531                          + " color space = "+colorSpace
1532                          + " transparency = "+transparency
1533                          + " transIndex   = "+transparent_index
1534                          + " has alpha = "+supportsAlpha
1535                          + " isAlphaPre = "+isAlphaPremultiplied
1536                          );
1537     }
1538 
1539     /**
1540      * Tests if the specified {@code Object} is an
1541      * instance of {@code IndexColorModel}
1542      * and if it equals this {@code IndexColorModel}
1543      * @param obj the {@code Object} to test for equality
1544      * @return {@code true} if the specified {@code Object}
1545      * equals this {@code IndexColorModel}; {@code false} otherwise.
1546      */
1547     @Override
1548     public boolean equals(Object obj) {
1549 
1550         if (!(obj instanceof IndexColorModel)) {
1551             return false;
1552         }
1553 
1554         IndexColorModel cm = (IndexColorModel) obj;
1555         if (supportsAlpha != cm.hasAlpha() ||
1556             isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
1557             pixel_bits != cm.getPixelSize() ||
1558             transparency != cm.getTransparency() ||
1559             numComponents != cm.getNumComponents() ||
1560             (!(colorSpace.equals(cm.colorSpace))) ||
1561             transferType != cm.transferType ||
1562             map_size != cm.map_size ||
1563             transparent_index != cm.transparent_index)
1564         {
1565             return false;
1566         }
1567 
1568         int[] nb = cm.getComponentSize();
1569 
1570         if ((nBits != null) && (nb != null)) {
1571             for (int i = 0; i < numComponents; i++) {
1572                 if (nBits[i] != nb[i]) {
1573                     return false;
1574                 }
1575             }
1576         } else {
1577             return ((nBits == null) && (nb == null));
1578         }
1579 
1580         // verify whether we have to check equality of all bits in validBits
1581         boolean testValidBits;
1582         if (validBits == cm.validBits) {
1583             testValidBits = false;
1584         } else if (validBits == null || cm.validBits == null) {
1585             return false;
1586         } else if (validBits.equals(cm.validBits)) {
1587             testValidBits = false;
1588         } else {
1589             testValidBits = true;
1590         }
1591 
1592         if (testValidBits) {
1593             for (int i = 0; i < map_size; i++) {
1594                 if (rgb[i] != cm.rgb[i] ||
1595                     validBits.testBit(i) != cm.validBits.testBit(i))
1596                 {
1597                     return false;
1598                 }
1599             }
1600         } else {
1601             for (int i = 0; i < map_size; i++) {
1602                 if (rgb[i] != cm.rgb[i]) {
1603                     return false;
1604                 }
1605             }
1606         }
1607         return true;
1608     }
1609 
1610     /**
1611      * Returns the hash code for IndexColorModel.
1612      *
1613      * @return    a hash code for IndexColorModel
1614      */
1615     @Override
1616     public int hashCode() {
1617         int result = hashCode;
1618         if (result == 0) {
1619             result = 7;
1620             result = 89 * result + this.pixel_bits;
1621             result = 89 * result + Arrays.hashCode(this.nBits);
1622             result = 89 * result + this.transparency;
1623             result = 89 * result + (this.supportsAlpha ? 1 : 0);
1624             result = 89 * result + (this.isAlphaPremultiplied ? 1 : 0);
1625             result = 89 * result + this.numComponents;
1626             result = 89 * result + Objects.hashCode(this.colorSpace);
1627             result = 89 * result + this.transferType;
1628             result = 89 * result + Arrays.hashCode(this.rgb);
1629             result = 89 * result + this.map_size;
1630             result = 89 * result + this.transparent_index;
1631             hashCode = result;
1632         }
1633         return result;
1634     }
1635 }
< prev index next >