< prev index next >

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

Print this page




  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 
  31 /**
  32  * The {@code PackedColorModel} class is an abstract
  33  * {@link ColorModel} class that works with pixel values which represent
  34  * color and alpha information as separate samples and which pack all
  35  * samples for a single pixel into a single int, short, or byte quantity.
  36  * This class can be used with an arbitrary {@link ColorSpace}.  The number of
  37  * color samples in the pixel values must be the same as the number of color
  38  * components in the {@code ColorSpace}.  There can be a single alpha
  39  * sample.  The array length is always 1 for those methods that use a
  40  * primitive array pixel representation of type {@code transferType}.
  41  * The transfer types supported are DataBuffer.TYPE_BYTE,
  42  * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
  43  * Color and alpha samples are stored in the single element of the array
  44  * in bits indicated by bit masks.  Each bit mask must be contiguous and
  45  * masks must not overlap.  The same masks apply to the single int
  46  * pixel representation used by other methods.  The correspondence of
  47  * masks and color/alpha samples is as follows:
  48  * <ul>
  49  * <li> Masks are identified by indices running from 0 through


 368      * {@code WritableRaster}, but shares the data array.
 369      * @param raster a {@code WritableRaster} containing an image
 370      * @return a {@code WritableRaster} that represents the alpha
 371      *         channel of the image contained in {@code raster}.
 372      */
 373     public WritableRaster getAlphaRaster(WritableRaster raster) {
 374         if (hasAlpha() == false) {
 375             return null;
 376         }
 377 
 378         int x = raster.getMinX();
 379         int y = raster.getMinY();
 380         int[] band = new int[1];
 381         band[0] = raster.getNumBands() - 1;
 382         return raster.createWritableChild(x, y, raster.getWidth(),
 383                                           raster.getHeight(), x, y,
 384                                           band);
 385     }
 386 
 387     /**
 388      * Tests if the specified {@code Object} is an instance
 389      * of {@code PackedColorModel} and equals this
 390      * {@code PackedColorModel}.




 391      * @param obj the {@code Object} to test for equality
 392      * @return {@code true} if the specified {@code Object}
 393      * is an instance of {@code PackedColorModel} and equals this
 394      * {@code PackedColorModel}; {@code false} otherwise.
 395      */

 396     public boolean equals(Object obj) {
 397         if (!(obj instanceof PackedColorModel)) {
 398             return false;
 399         }
 400 
 401         if (!super.equals(obj)) {
 402             return false;
 403         }
 404 
 405         PackedColorModel cm = (PackedColorModel) obj;
 406         int numC = cm.getNumComponents();
 407         for(int i=0; i < numC; i++) {
 408             if (maskArray[i] != cm.getMask(i)) {
 409                 return false;
 410             }
 411         }
 412         return true;












 413     }
 414 
 415     private static final int[] createBitsArray(int[]colorMaskArray,
 416                                                int alphaMask) {
 417         int numColors = colorMaskArray.length;
 418         int numAlpha = (alphaMask == 0 ? 0 : 1);
 419         int[] arr = new int[numColors+numAlpha];
 420         for (int i=0; i < numColors; i++) {
 421             arr[i] = countBits(colorMaskArray[i]);
 422             if (arr[i] < 0) {
 423                 throw new IllegalArgumentException("Noncontiguous color mask ("
 424                                      + Integer.toHexString(colorMaskArray[i])+
 425                                      "at index "+i);
 426             }
 427         }
 428         if (alphaMask != 0) {
 429             arr[numColors] = countBits(alphaMask);
 430             if (arr[numColors] < 0) {
 431                 throw new IllegalArgumentException("Noncontiguous alpha mask ("
 432                                      + Integer.toHexString(alphaMask));




  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.util.Arrays;
  31 
  32 /**
  33  * The {@code PackedColorModel} class is an abstract
  34  * {@link ColorModel} class that works with pixel values which represent
  35  * color and alpha information as separate samples and which pack all
  36  * samples for a single pixel into a single int, short, or byte quantity.
  37  * This class can be used with an arbitrary {@link ColorSpace}.  The number of
  38  * color samples in the pixel values must be the same as the number of color
  39  * components in the {@code ColorSpace}.  There can be a single alpha
  40  * sample.  The array length is always 1 for those methods that use a
  41  * primitive array pixel representation of type {@code transferType}.
  42  * The transfer types supported are DataBuffer.TYPE_BYTE,
  43  * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
  44  * Color and alpha samples are stored in the single element of the array
  45  * in bits indicated by bit masks.  Each bit mask must be contiguous and
  46  * masks must not overlap.  The same masks apply to the single int
  47  * pixel representation used by other methods.  The correspondence of
  48  * masks and color/alpha samples is as follows:
  49  * <ul>
  50  * <li> Masks are identified by indices running from 0 through


 369      * {@code WritableRaster}, but shares the data array.
 370      * @param raster a {@code WritableRaster} containing an image
 371      * @return a {@code WritableRaster} that represents the alpha
 372      *         channel of the image contained in {@code raster}.
 373      */
 374     public WritableRaster getAlphaRaster(WritableRaster raster) {
 375         if (hasAlpha() == false) {
 376             return null;
 377         }
 378 
 379         int x = raster.getMinX();
 380         int y = raster.getMinY();
 381         int[] band = new int[1];
 382         band[0] = raster.getNumBands() - 1;
 383         return raster.createWritableChild(x, y, raster.getWidth(),
 384                                           raster.getHeight(), x, y,
 385                                           band);
 386     }
 387 
 388     /**
 389      * Tests if the specified {@code Object} equals this

 390      * {@code PackedColorModel}.
 391      * In order to protect the symmetry property of
 392      * {@code (a.equals(b) == b.equals(a))},
 393      * the target object must be the exact same class as this
 394      * object to evaluate as {equals}.
 395      * @param obj the {@code Object} to test for equality
 396      * @return {@code true} if the specified {@code Object}
 397      * equals this {@code PackedColorModel}; {@code false} otherwise.

 398      */
 399     @Override
 400     public boolean equals(Object obj) {
 401         /*
 402          * We verify the type of argument obj in super.equals() where we check
 403          * for equality of class name.
 404          */
 405         if (!super.equals(obj)) {
 406             return false;
 407         }
 408 
 409         PackedColorModel cm = (PackedColorModel) obj;
 410         int numC = cm.getNumComponents();
 411         for(int i=0; i < numC; i++) {
 412             if (maskArray[i] != cm.getMask(i)) {
 413                 return false;
 414             }
 415         }
 416         return true;
 417     }
 418 
 419     /**
 420      * Returns the hash code for this PackedColorModel.
 421      *
 422      * @return    a hash code for this PackedColorModel.
 423      */
 424     @Override
 425     public int hashCode() {
 426         int hash = super.hashCode();
 427         hash = 89 * hash + Arrays.hashCode(this.maskArray);
 428         return hash;
 429     }
 430 
 431     private static final int[] createBitsArray(int[]colorMaskArray,
 432                                                int alphaMask) {
 433         int numColors = colorMaskArray.length;
 434         int numAlpha = (alphaMask == 0 ? 0 : 1);
 435         int[] arr = new int[numColors+numAlpha];
 436         for (int i=0; i < numColors; i++) {
 437             arr[i] = countBits(colorMaskArray[i]);
 438             if (arr[i] < 0) {
 439                 throw new IllegalArgumentException("Noncontiguous color mask ("
 440                                      + Integer.toHexString(colorMaskArray[i])+
 441                                      "at index "+i);
 442             }
 443         }
 444         if (alphaMask != 0) {
 445             arr[numColors] = countBits(alphaMask);
 446             if (arr[numColors] < 0) {
 447                 throw new IllegalArgumentException("Noncontiguous alpha mask ("
 448                                      + Integer.toHexString(alphaMask));


< prev index next >