< prev index next >

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

Print this page


   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


 143  *
 144  * @see IndexColorModel
 145  * @see ComponentColorModel
 146  * @see PackedColorModel
 147  * @see DirectColorModel
 148  * @see java.awt.Image
 149  * @see BufferedImage
 150  * @see RenderedImage
 151  * @see java.awt.color.ColorSpace
 152  * @see SampleModel
 153  * @see Raster
 154  * @see DataBuffer
 155  */
 156 public abstract class ColorModel implements Transparency{
 157     private long pData;         // Placeholder for data for native functions
 158 
 159     /**
 160      * The total number of bits in the pixel.
 161      */
 162     protected int pixel_bits;
 163     int nBits[];
 164     int transparency = Transparency.TRANSLUCENT;
 165     boolean supportsAlpha = true;
 166     boolean isAlphaPremultiplied = false;
 167     int numComponents = -1;
 168     int numColorComponents = -1;
 169     ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 170     int colorSpaceType = ColorSpace.TYPE_RGB;
 171     int maxBits;
 172     boolean is_sRGB = true;
 173 
 174     /**
 175      * Data type of the array used to represent pixel values.
 176      */
 177     protected int transferType;
 178 
 179     /**
 180      * This is copied from java.awt.Toolkit since we need the library
 181      * loaded in java.awt.image also:
 182      *
 183      * WARNING: This is a temporary workaround for a problem in the


 628      * method throws an exception if the subclass uses a
 629      * {@code transferType} other than
 630      * {@code DataBuffer.TYPE_BYTE},
 631      * {@code DataBuffer.TYPE_USHORT}, or
 632      * {@code DataBuffer.TYPE_INT}.
 633      * @param inData an array of pixel values
 634      * @return the value of the red component of the specified pixel.
 635      * @throws ClassCastException if {@code inData}
 636      *  is not a primitive array of type {@code transferType}
 637      * @throws ArrayIndexOutOfBoundsException if
 638      *  {@code inData} is not large enough to hold a pixel value
 639      *  for this {@code ColorModel}
 640      * @throws UnsupportedOperationException if this
 641      *  {@code transferType} is not supported by this
 642      *  {@code ColorModel}
 643      */
 644     public int getRed(Object inData) {
 645         int pixel=0,length=0;
 646         switch (transferType) {
 647             case DataBuffer.TYPE_BYTE:
 648                byte bdata[] = (byte[])inData;
 649                pixel = bdata[0] & 0xff;
 650                length = bdata.length;
 651             break;
 652             case DataBuffer.TYPE_USHORT:
 653                short sdata[] = (short[])inData;
 654                pixel = sdata[0] & 0xffff;
 655                length = sdata.length;
 656             break;
 657             case DataBuffer.TYPE_INT:
 658                int idata[] = (int[])inData;
 659                pixel = idata[0];
 660                length = idata.length;
 661             break;
 662             default:
 663                throw new UnsupportedOperationException("This method has not been "+
 664                    "implemented for transferType " + transferType);
 665         }
 666         if (length == 1) {
 667             return getRed(pixel);
 668         }
 669         else {
 670             throw new UnsupportedOperationException
 671                 ("This method is not supported by this color model");
 672         }
 673     }
 674 
 675     /**
 676      * Returns the green color component for the specified pixel, scaled
 677      * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
 678      * color conversion is done if necessary.  The pixel value is


 695      * method throws an exception if the subclass uses a
 696      * {@code transferType} other than
 697      * {@code DataBuffer.TYPE_BYTE},
 698      * {@code DataBuffer.TYPE_USHORT}, or
 699      * {@code DataBuffer.TYPE_INT}.
 700      * @param inData an array of pixel values
 701      * @return the value of the green component of the specified pixel.
 702      * @throws ClassCastException if {@code inData}
 703      *  is not a primitive array of type {@code transferType}
 704      * @throws ArrayIndexOutOfBoundsException if
 705      *  {@code inData} is not large enough to hold a pixel value
 706      *  for this {@code ColorModel}
 707      * @throws UnsupportedOperationException if this
 708      *  {@code transferType} is not supported by this
 709      *  {@code ColorModel}
 710      */
 711     public int getGreen(Object inData) {
 712         int pixel=0,length=0;
 713         switch (transferType) {
 714             case DataBuffer.TYPE_BYTE:
 715                byte bdata[] = (byte[])inData;
 716                pixel = bdata[0] & 0xff;
 717                length = bdata.length;
 718             break;
 719             case DataBuffer.TYPE_USHORT:
 720                short sdata[] = (short[])inData;
 721                pixel = sdata[0] & 0xffff;
 722                length = sdata.length;
 723             break;
 724             case DataBuffer.TYPE_INT:
 725                int idata[] = (int[])inData;
 726                pixel = idata[0];
 727                length = idata.length;
 728             break;
 729             default:
 730                throw new UnsupportedOperationException("This method has not been "+
 731                    "implemented for transferType " + transferType);
 732         }
 733         if (length == 1) {
 734             return getGreen(pixel);
 735         }
 736         else {
 737             throw new UnsupportedOperationException
 738                 ("This method is not supported by this color model");
 739         }
 740     }
 741 
 742     /**
 743      * Returns the blue color component for the specified pixel, scaled
 744      * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
 745      * color conversion is done if necessary.  The pixel value is


 762      * method throws an exception if the subclass uses a
 763      * {@code transferType} other than
 764      * {@code DataBuffer.TYPE_BYTE},
 765      * {@code DataBuffer.TYPE_USHORT}, or
 766      * {@code DataBuffer.TYPE_INT}.
 767      * @param inData an array of pixel values
 768      * @return the value of the blue component of the specified pixel.
 769      * @throws ClassCastException if {@code inData}
 770      *  is not a primitive array of type {@code transferType}
 771      * @throws ArrayIndexOutOfBoundsException if
 772      *  {@code inData} is not large enough to hold a pixel value
 773      *  for this {@code ColorModel}
 774      * @throws UnsupportedOperationException if this
 775      *  {@code transferType} is not supported by this
 776      *  {@code ColorModel}
 777      */
 778     public int getBlue(Object inData) {
 779         int pixel=0,length=0;
 780         switch (transferType) {
 781             case DataBuffer.TYPE_BYTE:
 782                byte bdata[] = (byte[])inData;
 783                pixel = bdata[0] & 0xff;
 784                length = bdata.length;
 785             break;
 786             case DataBuffer.TYPE_USHORT:
 787                short sdata[] = (short[])inData;
 788                pixel = sdata[0] & 0xffff;
 789                length = sdata.length;
 790             break;
 791             case DataBuffer.TYPE_INT:
 792                int idata[] = (int[])inData;
 793                pixel = idata[0];
 794                length = idata.length;
 795             break;
 796             default:
 797                throw new UnsupportedOperationException("This method has not been "+
 798                    "implemented for transferType " + transferType);
 799         }
 800         if (length == 1) {
 801             return getBlue(pixel);
 802         }
 803         else {
 804             throw new UnsupportedOperationException
 805                 ("This method is not supported by this color model");
 806         }
 807     }
 808 
 809     /**
 810      * Returns the alpha component for the specified pixel, scaled
 811      * from 0 to 255.  The pixel value is specified by an array of data
 812      * elements of type transferType passed in as an object reference.


 825      * {@code transferType} other than
 826      * {@code DataBuffer.TYPE_BYTE},
 827      * {@code DataBuffer.TYPE_USHORT}, or
 828      * {@code DataBuffer.TYPE_INT}.
 829      * @param inData the specified pixel
 830      * @return the alpha component of the specified pixel, scaled from
 831      * 0 to 255.
 832      * @throws ClassCastException if {@code inData}
 833      *  is not a primitive array of type {@code transferType}
 834      * @throws ArrayIndexOutOfBoundsException if
 835      *  {@code inData} is not large enough to hold a pixel value
 836      *  for this {@code ColorModel}
 837      * @throws UnsupportedOperationException if this
 838      *  {@code tranferType} is not supported by this
 839      *  {@code ColorModel}
 840      */
 841     public int getAlpha(Object inData) {
 842         int pixel=0,length=0;
 843         switch (transferType) {
 844             case DataBuffer.TYPE_BYTE:
 845                byte bdata[] = (byte[])inData;
 846                pixel = bdata[0] & 0xff;
 847                length = bdata.length;
 848             break;
 849             case DataBuffer.TYPE_USHORT:
 850                short sdata[] = (short[])inData;
 851                pixel = sdata[0] & 0xffff;
 852                length = sdata.length;
 853             break;
 854             case DataBuffer.TYPE_INT:
 855                int idata[] = (int[])inData;
 856                pixel = idata[0];
 857                length = idata.length;
 858             break;
 859             default:
 860                throw new UnsupportedOperationException("This method has not been "+
 861                    "implemented for transferType " + transferType);
 862         }
 863         if (length == 1) {
 864             return getAlpha(pixel);
 865         }
 866         else {
 867             throw new UnsupportedOperationException
 868                 ("This method is not supported by this color model");
 869         }
 870     }
 871 
 872     /**
 873      * Returns the color/alpha components for the specified pixel in the
 874      * default RGB color model format.  A color conversion is done if
 875      * necessary.  The pixel value is specified by an array of data


1316      * normalized form to the unnormalized form and then calls
1317      * {@code getDataElement(int[], int)}.  Subclasses which may
1318      * have instances which do not support the unnormalized form must
1319      * override this method.
1320      * @param normComponents an array of normalized color and alpha
1321      * components
1322      * @param normOffset the index into {@code normComponents} at which to
1323      * begin retrieving the color and alpha components
1324      * @return an {@code int} pixel value in this
1325      * {@code ColorModel} corresponding to the specified components.
1326      * @throws IllegalArgumentException if
1327      *  pixel values for this {@code ColorModel} are not
1328      *  conveniently representable as a single {@code int}
1329      * @throws ArrayIndexOutOfBoundsException if
1330      *  the {@code normComponents} array is not large enough to
1331      *  hold all of the color and alpha components starting at
1332      *  {@code normOffset}
1333      * @since 1.4
1334      */
1335     public int getDataElement(float[] normComponents, int normOffset) {
1336         int components[] = getUnnormalizedComponents(normComponents,
1337                                                      normOffset, null, 0);
1338         return getDataElement(components, 0);
1339     }
1340 
1341     /**
1342      * Returns a data element array representation of a pixel in this
1343      * {@code ColorModel}, given an array of normalized color/alpha
1344      * components.  This array can then be passed to the
1345      * {@code setDataElements} method of a {@code WritableRaster}
1346      * object.  An {@code ArrayIndexOutOfBoundsException} is thrown
1347      * if the {@code normComponents} array is not large enough to hold
1348      * all the color and alpha components (starting at
1349      * {@code normOffset}).  If the {@code obj} variable is
1350      * {@code null}, a new array will be allocated.  If
1351      * {@code obj} is not {@code null}, it must be a primitive
1352      * array of type transferType; otherwise, a
1353      * {@code ClassCastException} is thrown.  An
1354      * {@code ArrayIndexOutOfBoundsException} is thrown if
1355      * {@code obj} is not large enough to hold a pixel value for this
1356      * {@code ColorModel}.


1364      * @param normComponents an array of normalized color and alpha
1365      * components
1366      * @param normOffset the index into {@code normComponents} at which to
1367      * begin retrieving color and alpha components
1368      * @param obj a primitive data array to hold the returned pixel
1369      * @return an {@code Object} which is a primitive data array
1370      * representation of a pixel
1371      * @throws ClassCastException if {@code obj}
1372      *  is not a primitive array of type {@code transferType}
1373      * @throws ArrayIndexOutOfBoundsException if
1374      *  {@code obj} is not large enough to hold a pixel value
1375      *  for this {@code ColorModel} or the {@code normComponents}
1376      *  array is not large enough to hold all of the color and alpha
1377      *  components starting at {@code normOffset}
1378      * @see WritableRaster#setDataElements
1379      * @see SampleModel#setDataElements
1380      * @since 1.4
1381      */
1382     public Object getDataElements(float[] normComponents, int normOffset,
1383                                   Object obj) {
1384         int components[] = getUnnormalizedComponents(normComponents,
1385                                                      normOffset, null, 0);
1386         return getDataElements(components, 0, obj);
1387     }
1388 
1389     /**
1390      * Returns an array of all of the color/alpha components in normalized
1391      * form, given a pixel in this {@code ColorModel}.  The pixel
1392      * value is specified by an array of data elements of type transferType
1393      * passed in as an object reference.  If pixel is not a primitive array
1394      * of type transferType, a {@code ClassCastException} is thrown.
1395      * An {@code ArrayIndexOutOfBoundsException} is thrown if
1396      * {@code pixel} is not large enough to hold a pixel value for this
1397      * {@code ColorModel}.
1398      * Normalized components are float values between a per component minimum
1399      * and maximum specified by the {@code ColorSpace} object for this
1400      * {@code ColorModel}.  If the
1401      * {@code normComponents} array is {@code null}, a new array
1402      * will be allocated.  The {@code normComponents} array
1403      * will be returned.  Color/alpha components are stored in the
1404      * {@code normComponents} array starting at


1425      * @throws ClassCastException if {@code pixel} is not a primitive
1426      *          array of type transferType
1427      * @throws ArrayIndexOutOfBoundsException if
1428      *          {@code normComponents} is not large enough to hold all
1429      *          color and alpha components starting at {@code normOffset}
1430      * @throws ArrayIndexOutOfBoundsException if
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      * This method simply delegates to the default implementation in {@code Object}
1452      * which is identical to an {@code ==} test since this class cannot enforce the
1453      * issues of a proper equality test among multiple independent subclass
1454      * branches.
1455      * Subclasses are encouraged to override this method and provide equality
1456      * testing for their own properties in addition to equality tests for the
1457      * following common base properties of {@code ColorModel}:
1458      * <ul>
1459      * <li>Support for alpha component.</li>
1460      * <li>Is alpha premultiplied.</li>
1461      * <li>Number of bits per pixel.</li>
1462      * <li>Type of transparency like Opaque, Bitmask or Translucent.</li>
1463      * <li>Number of components in a pixel.</li>
1464      * <li>{@code ColorSpace} type.</li>
1465      * <li>Type of the array used to represent pixel values.</li>


   1 /*
   2  * Copyright (c) 1995, 2018, 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


 143  *
 144  * @see IndexColorModel
 145  * @see ComponentColorModel
 146  * @see PackedColorModel
 147  * @see DirectColorModel
 148  * @see java.awt.Image
 149  * @see BufferedImage
 150  * @see RenderedImage
 151  * @see java.awt.color.ColorSpace
 152  * @see SampleModel
 153  * @see Raster
 154  * @see DataBuffer
 155  */
 156 public abstract class ColorModel implements Transparency{
 157     private long pData;         // Placeholder for data for native functions
 158 
 159     /**
 160      * The total number of bits in the pixel.
 161      */
 162     protected int pixel_bits;
 163     int[] nBits;
 164     int transparency = Transparency.TRANSLUCENT;
 165     boolean supportsAlpha = true;
 166     boolean isAlphaPremultiplied = false;
 167     int numComponents = -1;
 168     int numColorComponents = -1;
 169     ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 170     int colorSpaceType = ColorSpace.TYPE_RGB;
 171     int maxBits;
 172     boolean is_sRGB = true;
 173 
 174     /**
 175      * Data type of the array used to represent pixel values.
 176      */
 177     protected int transferType;
 178 
 179     /**
 180      * This is copied from java.awt.Toolkit since we need the library
 181      * loaded in java.awt.image also:
 182      *
 183      * WARNING: This is a temporary workaround for a problem in the


 628      * method throws an exception if the subclass uses a
 629      * {@code transferType} other than
 630      * {@code DataBuffer.TYPE_BYTE},
 631      * {@code DataBuffer.TYPE_USHORT}, or
 632      * {@code DataBuffer.TYPE_INT}.
 633      * @param inData an array of pixel values
 634      * @return the value of the red component of the specified pixel.
 635      * @throws ClassCastException if {@code inData}
 636      *  is not a primitive array of type {@code transferType}
 637      * @throws ArrayIndexOutOfBoundsException if
 638      *  {@code inData} is not large enough to hold a pixel value
 639      *  for this {@code ColorModel}
 640      * @throws UnsupportedOperationException if this
 641      *  {@code transferType} is not supported by this
 642      *  {@code ColorModel}
 643      */
 644     public int getRed(Object inData) {
 645         int pixel=0,length=0;
 646         switch (transferType) {
 647             case DataBuffer.TYPE_BYTE:
 648                byte[] bdata = (byte[])inData;
 649                pixel = bdata[0] & 0xff;
 650                length = bdata.length;
 651             break;
 652             case DataBuffer.TYPE_USHORT:
 653                short[] sdata = (short[])inData;
 654                pixel = sdata[0] & 0xffff;
 655                length = sdata.length;
 656             break;
 657             case DataBuffer.TYPE_INT:
 658                int[] idata = (int[])inData;
 659                pixel = idata[0];
 660                length = idata.length;
 661             break;
 662             default:
 663                throw new UnsupportedOperationException("This method has not been "+
 664                    "implemented for transferType " + transferType);
 665         }
 666         if (length == 1) {
 667             return getRed(pixel);
 668         }
 669         else {
 670             throw new UnsupportedOperationException
 671                 ("This method is not supported by this color model");
 672         }
 673     }
 674 
 675     /**
 676      * Returns the green color component for the specified pixel, scaled
 677      * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
 678      * color conversion is done if necessary.  The pixel value is


 695      * method throws an exception if the subclass uses a
 696      * {@code transferType} other than
 697      * {@code DataBuffer.TYPE_BYTE},
 698      * {@code DataBuffer.TYPE_USHORT}, or
 699      * {@code DataBuffer.TYPE_INT}.
 700      * @param inData an array of pixel values
 701      * @return the value of the green component of the specified pixel.
 702      * @throws ClassCastException if {@code inData}
 703      *  is not a primitive array of type {@code transferType}
 704      * @throws ArrayIndexOutOfBoundsException if
 705      *  {@code inData} is not large enough to hold a pixel value
 706      *  for this {@code ColorModel}
 707      * @throws UnsupportedOperationException if this
 708      *  {@code transferType} is not supported by this
 709      *  {@code ColorModel}
 710      */
 711     public int getGreen(Object inData) {
 712         int pixel=0,length=0;
 713         switch (transferType) {
 714             case DataBuffer.TYPE_BYTE:
 715                byte[] bdata = (byte[])inData;
 716                pixel = bdata[0] & 0xff;
 717                length = bdata.length;
 718             break;
 719             case DataBuffer.TYPE_USHORT:
 720                short[] sdata = (short[])inData;
 721                pixel = sdata[0] & 0xffff;
 722                length = sdata.length;
 723             break;
 724             case DataBuffer.TYPE_INT:
 725                int[] idata = (int[])inData;
 726                pixel = idata[0];
 727                length = idata.length;
 728             break;
 729             default:
 730                throw new UnsupportedOperationException("This method has not been "+
 731                    "implemented for transferType " + transferType);
 732         }
 733         if (length == 1) {
 734             return getGreen(pixel);
 735         }
 736         else {
 737             throw new UnsupportedOperationException
 738                 ("This method is not supported by this color model");
 739         }
 740     }
 741 
 742     /**
 743      * Returns the blue color component for the specified pixel, scaled
 744      * from 0 to 255 in the default RGB {@code ColorSpace}, sRGB.  A
 745      * color conversion is done if necessary.  The pixel value is


 762      * method throws an exception if the subclass uses a
 763      * {@code transferType} other than
 764      * {@code DataBuffer.TYPE_BYTE},
 765      * {@code DataBuffer.TYPE_USHORT}, or
 766      * {@code DataBuffer.TYPE_INT}.
 767      * @param inData an array of pixel values
 768      * @return the value of the blue component of the specified pixel.
 769      * @throws ClassCastException if {@code inData}
 770      *  is not a primitive array of type {@code transferType}
 771      * @throws ArrayIndexOutOfBoundsException if
 772      *  {@code inData} is not large enough to hold a pixel value
 773      *  for this {@code ColorModel}
 774      * @throws UnsupportedOperationException if this
 775      *  {@code transferType} is not supported by this
 776      *  {@code ColorModel}
 777      */
 778     public int getBlue(Object inData) {
 779         int pixel=0,length=0;
 780         switch (transferType) {
 781             case DataBuffer.TYPE_BYTE:
 782                byte[] bdata = (byte[])inData;
 783                pixel = bdata[0] & 0xff;
 784                length = bdata.length;
 785             break;
 786             case DataBuffer.TYPE_USHORT:
 787                short[] sdata = (short[])inData;
 788                pixel = sdata[0] & 0xffff;
 789                length = sdata.length;
 790             break;
 791             case DataBuffer.TYPE_INT:
 792                int[] idata = (int[])inData;
 793                pixel = idata[0];
 794                length = idata.length;
 795             break;
 796             default:
 797                throw new UnsupportedOperationException("This method has not been "+
 798                    "implemented for transferType " + transferType);
 799         }
 800         if (length == 1) {
 801             return getBlue(pixel);
 802         }
 803         else {
 804             throw new UnsupportedOperationException
 805                 ("This method is not supported by this color model");
 806         }
 807     }
 808 
 809     /**
 810      * Returns the alpha component for the specified pixel, scaled
 811      * from 0 to 255.  The pixel value is specified by an array of data
 812      * elements of type transferType passed in as an object reference.


 825      * {@code transferType} other than
 826      * {@code DataBuffer.TYPE_BYTE},
 827      * {@code DataBuffer.TYPE_USHORT}, or
 828      * {@code DataBuffer.TYPE_INT}.
 829      * @param inData the specified pixel
 830      * @return the alpha component of the specified pixel, scaled from
 831      * 0 to 255.
 832      * @throws ClassCastException if {@code inData}
 833      *  is not a primitive array of type {@code transferType}
 834      * @throws ArrayIndexOutOfBoundsException if
 835      *  {@code inData} is not large enough to hold a pixel value
 836      *  for this {@code ColorModel}
 837      * @throws UnsupportedOperationException if this
 838      *  {@code tranferType} is not supported by this
 839      *  {@code ColorModel}
 840      */
 841     public int getAlpha(Object inData) {
 842         int pixel=0,length=0;
 843         switch (transferType) {
 844             case DataBuffer.TYPE_BYTE:
 845                byte[] bdata = (byte[])inData;
 846                pixel = bdata[0] & 0xff;
 847                length = bdata.length;
 848             break;
 849             case DataBuffer.TYPE_USHORT:
 850                short[] sdata = (short[])inData;
 851                pixel = sdata[0] & 0xffff;
 852                length = sdata.length;
 853             break;
 854             case DataBuffer.TYPE_INT:
 855                int[] idata = (int[])inData;
 856                pixel = idata[0];
 857                length = idata.length;
 858             break;
 859             default:
 860                throw new UnsupportedOperationException("This method has not been "+
 861                    "implemented for transferType " + transferType);
 862         }
 863         if (length == 1) {
 864             return getAlpha(pixel);
 865         }
 866         else {
 867             throw new UnsupportedOperationException
 868                 ("This method is not supported by this color model");
 869         }
 870     }
 871 
 872     /**
 873      * Returns the color/alpha components for the specified pixel in the
 874      * default RGB color model format.  A color conversion is done if
 875      * necessary.  The pixel value is specified by an array of data


1316      * normalized form to the unnormalized form and then calls
1317      * {@code getDataElement(int[], int)}.  Subclasses which may
1318      * have instances which do not support the unnormalized form must
1319      * override this method.
1320      * @param normComponents an array of normalized color and alpha
1321      * components
1322      * @param normOffset the index into {@code normComponents} at which to
1323      * begin retrieving the color and alpha components
1324      * @return an {@code int} pixel value in this
1325      * {@code ColorModel} corresponding to the specified components.
1326      * @throws IllegalArgumentException if
1327      *  pixel values for this {@code ColorModel} are not
1328      *  conveniently representable as a single {@code int}
1329      * @throws ArrayIndexOutOfBoundsException if
1330      *  the {@code normComponents} array is not large enough to
1331      *  hold all of the color and alpha components starting at
1332      *  {@code normOffset}
1333      * @since 1.4
1334      */
1335     public int getDataElement(float[] normComponents, int normOffset) {
1336         int[] components = getUnnormalizedComponents(normComponents,
1337                                                      normOffset, null, 0);
1338         return getDataElement(components, 0);
1339     }
1340 
1341     /**
1342      * Returns a data element array representation of a pixel in this
1343      * {@code ColorModel}, given an array of normalized color/alpha
1344      * components.  This array can then be passed to the
1345      * {@code setDataElements} method of a {@code WritableRaster}
1346      * object.  An {@code ArrayIndexOutOfBoundsException} is thrown
1347      * if the {@code normComponents} array is not large enough to hold
1348      * all the color and alpha components (starting at
1349      * {@code normOffset}).  If the {@code obj} variable is
1350      * {@code null}, a new array will be allocated.  If
1351      * {@code obj} is not {@code null}, it must be a primitive
1352      * array of type transferType; otherwise, a
1353      * {@code ClassCastException} is thrown.  An
1354      * {@code ArrayIndexOutOfBoundsException} is thrown if
1355      * {@code obj} is not large enough to hold a pixel value for this
1356      * {@code ColorModel}.


1364      * @param normComponents an array of normalized color and alpha
1365      * components
1366      * @param normOffset the index into {@code normComponents} at which to
1367      * begin retrieving color and alpha components
1368      * @param obj a primitive data array to hold the returned pixel
1369      * @return an {@code Object} which is a primitive data array
1370      * representation of a pixel
1371      * @throws ClassCastException if {@code obj}
1372      *  is not a primitive array of type {@code transferType}
1373      * @throws ArrayIndexOutOfBoundsException if
1374      *  {@code obj} is not large enough to hold a pixel value
1375      *  for this {@code ColorModel} or the {@code normComponents}
1376      *  array is not large enough to hold all of the color and alpha
1377      *  components starting at {@code normOffset}
1378      * @see WritableRaster#setDataElements
1379      * @see SampleModel#setDataElements
1380      * @since 1.4
1381      */
1382     public Object getDataElements(float[] normComponents, int normOffset,
1383                                   Object obj) {
1384         int[] components = getUnnormalizedComponents(normComponents,
1385                                                      normOffset, null, 0);
1386         return getDataElements(components, 0, obj);
1387     }
1388 
1389     /**
1390      * Returns an array of all of the color/alpha components in normalized
1391      * form, given a pixel in this {@code ColorModel}.  The pixel
1392      * value is specified by an array of data elements of type transferType
1393      * passed in as an object reference.  If pixel is not a primitive array
1394      * of type transferType, a {@code ClassCastException} is thrown.
1395      * An {@code ArrayIndexOutOfBoundsException} is thrown if
1396      * {@code pixel} is not large enough to hold a pixel value for this
1397      * {@code ColorModel}.
1398      * Normalized components are float values between a per component minimum
1399      * and maximum specified by the {@code ColorSpace} object for this
1400      * {@code ColorModel}.  If the
1401      * {@code normComponents} array is {@code null}, a new array
1402      * will be allocated.  The {@code normComponents} array
1403      * will be returned.  Color/alpha components are stored in the
1404      * {@code normComponents} array starting at


1425      * @throws ClassCastException if {@code pixel} is not a primitive
1426      *          array of type transferType
1427      * @throws ArrayIndexOutOfBoundsException if
1428      *          {@code normComponents} is not large enough to hold all
1429      *          color and alpha components starting at {@code normOffset}
1430      * @throws ArrayIndexOutOfBoundsException if
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      * This method simply delegates to the default implementation in {@code Object}
1452      * which is identical to an {@code ==} test since this class cannot enforce the
1453      * issues of a proper equality test among multiple independent subclass
1454      * branches.
1455      * Subclasses are encouraged to override this method and provide equality
1456      * testing for their own properties in addition to equality tests for the
1457      * following common base properties of {@code ColorModel}:
1458      * <ul>
1459      * <li>Support for alpha component.</li>
1460      * <li>Is alpha premultiplied.</li>
1461      * <li>Number of bits per pixel.</li>
1462      * <li>Type of transparency like Opaque, Bitmask or Translucent.</li>
1463      * <li>Number of components in a pixel.</li>
1464      * <li>{@code ColorSpace} type.</li>
1465      * <li>Type of the array used to represent pixel values.</li>


< prev index next >