< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1997, 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.color.ColorSpace;
  29 import java.awt.color.ICC_ColorSpace;


  30 
  31 /**
  32  * A {@code ColorModel} class that works with pixel values that
  33  * represent color and alpha information as separate samples and that
  34  * store each sample in a separate data element.  This class can be
  35  * used with an arbitrary {@code ColorSpace}.  The number of
  36  * color samples in the pixel values must be same as the number of
  37  * color components in the {@code ColorSpace}. There may be a
  38  * single alpha sample.
  39  * <p>
  40  * For those methods that use
  41  * a primitive array pixel representation of type {@code transferType},
  42  * the array length is the same as the number of color and alpha samples.
  43  * Color samples are stored first in the array followed by the alpha
  44  * sample, if present.  The order of the color samples is specified
  45  * by the {@code ColorSpace}.  Typically, this order reflects the
  46  * name of the color space type. For example, for {@code TYPE_RGB},
  47  * index 0 corresponds to red, index 1 to green, and index 2 to blue.
  48  * <p>
  49  * The translation from pixel sample values to color/alpha components for


 183      * and {@code int} transfer types.
 184      */
 185     private boolean signed; // true for transfer types short, float, double
 186                             // false for byte, ushort, int
 187     private boolean is_sRGB_stdScale;
 188     private boolean is_LinearRGB_stdScale;
 189     private boolean is_LinearGray_stdScale;
 190     private boolean is_ICCGray_stdScale;
 191     private byte[] tosRGB8LUT;
 192     private byte[] fromsRGB8LUT8;
 193     private short[] fromsRGB8LUT16;
 194     private byte[] fromLinearGray16ToOtherGray8LUT;
 195     private short[] fromLinearGray16ToOtherGray16LUT;
 196     private boolean needScaleInit;
 197     private boolean noUnnorm;
 198     private boolean nonStdScale;
 199     private float[] min;
 200     private float[] diffMinMax;
 201     private float[] compOffset;
 202     private float[] compScale;

 203 
 204     /**
 205      * Constructs a {@code ComponentColorModel} from the specified
 206      * parameters. Color components will be in the specified
 207      * {@code ColorSpace}.  The supported transfer types are
 208      * {@code DataBuffer.TYPE_BYTE}, {@code DataBuffer.TYPE_USHORT},
 209      * {@code DataBuffer.TYPE_INT},
 210      * {@code DataBuffer.TYPE_SHORT}, {@code DataBuffer.TYPE_FLOAT},
 211      * and {@code DataBuffer.TYPE_DOUBLE}.
 212      * If not null, the {@code bits} array specifies the
 213      * number of significant bits per color and alpha component and its
 214      * length should be at least the number of components in the
 215      * {@code ColorSpace} if there is no alpha
 216      * information in the pixel values, or one more than this number if
 217      * there is alpha information.  When the {@code transferType} is
 218      * {@code DataBuffer.TYPE_SHORT}, {@code DataBuffer.TYPE_FLOAT},
 219      * or {@code DataBuffer.TYPE_DOUBLE} the {@code bits} array
 220      * argument is ignored.  {@code hasAlpha} indicates whether alpha
 221      * information is present.  If {@code hasAlpha} is true, then
 222      * the boolean {@code isAlphaPremultiplied}


2910      * alpha  channel.
2911      *
2912      * @return A {@code WritableRaster} containing the image's alpha channel.
2913      *
2914      */
2915     public WritableRaster getAlphaRaster(WritableRaster raster) {
2916         if (hasAlpha() == false) {
2917             return null;
2918         }
2919 
2920         int x = raster.getMinX();
2921         int y = raster.getMinY();
2922         int[] band = new int[1];
2923         band[0] = raster.getNumBands() - 1;
2924         return raster.createWritableChild(x, y, raster.getWidth(),
2925                                           raster.getHeight(), x, y,
2926                                           band);
2927     }
2928 
2929     /**
2930      * Compares this color model with another for equality.
2931      *
2932      * @param obj The object to compare with this color model.
2933      * @return {@code true} if the color model objects are equal,
2934      * {@code false} if they are not.


2935      */

2936     public boolean equals(Object obj) {
2937         if (!super.equals(obj)) {
2938             return false;
2939         }
2940 
2941         if (obj.getClass() !=  getClass()) {








2942             return false;
2943         }
2944 












2945         return true;
2946     }
2947 






















2948 }
   1 /*
   2  * Copyright (c) 1997, 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.color.ColorSpace;
  29 import java.awt.color.ICC_ColorSpace;
  30 import java.util.Arrays;
  31 import java.util.Objects;
  32 
  33 /**
  34  * A {@code ColorModel} class that works with pixel values that
  35  * represent color and alpha information as separate samples and that
  36  * store each sample in a separate data element.  This class can be
  37  * used with an arbitrary {@code ColorSpace}.  The number of
  38  * color samples in the pixel values must be same as the number of
  39  * color components in the {@code ColorSpace}. There may be a
  40  * single alpha sample.
  41  * <p>
  42  * For those methods that use
  43  * a primitive array pixel representation of type {@code transferType},
  44  * the array length is the same as the number of color and alpha samples.
  45  * Color samples are stored first in the array followed by the alpha
  46  * sample, if present.  The order of the color samples is specified
  47  * by the {@code ColorSpace}.  Typically, this order reflects the
  48  * name of the color space type. For example, for {@code TYPE_RGB},
  49  * index 0 corresponds to red, index 1 to green, and index 2 to blue.
  50  * <p>
  51  * The translation from pixel sample values to color/alpha components for


 185      * and {@code int} transfer types.
 186      */
 187     private boolean signed; // true for transfer types short, float, double
 188                             // false for byte, ushort, int
 189     private boolean is_sRGB_stdScale;
 190     private boolean is_LinearRGB_stdScale;
 191     private boolean is_LinearGray_stdScale;
 192     private boolean is_ICCGray_stdScale;
 193     private byte[] tosRGB8LUT;
 194     private byte[] fromsRGB8LUT8;
 195     private short[] fromsRGB8LUT16;
 196     private byte[] fromLinearGray16ToOtherGray8LUT;
 197     private short[] fromLinearGray16ToOtherGray16LUT;
 198     private boolean needScaleInit;
 199     private boolean noUnnorm;
 200     private boolean nonStdScale;
 201     private float[] min;
 202     private float[] diffMinMax;
 203     private float[] compOffset;
 204     private float[] compScale;
 205     private volatile int hashCode;
 206 
 207     /**
 208      * Constructs a {@code ComponentColorModel} from the specified
 209      * parameters. Color components will be in the specified
 210      * {@code ColorSpace}.  The supported transfer types are
 211      * {@code DataBuffer.TYPE_BYTE}, {@code DataBuffer.TYPE_USHORT},
 212      * {@code DataBuffer.TYPE_INT},
 213      * {@code DataBuffer.TYPE_SHORT}, {@code DataBuffer.TYPE_FLOAT},
 214      * and {@code DataBuffer.TYPE_DOUBLE}.
 215      * If not null, the {@code bits} array specifies the
 216      * number of significant bits per color and alpha component and its
 217      * length should be at least the number of components in the
 218      * {@code ColorSpace} if there is no alpha
 219      * information in the pixel values, or one more than this number if
 220      * there is alpha information.  When the {@code transferType} is
 221      * {@code DataBuffer.TYPE_SHORT}, {@code DataBuffer.TYPE_FLOAT},
 222      * or {@code DataBuffer.TYPE_DOUBLE} the {@code bits} array
 223      * argument is ignored.  {@code hasAlpha} indicates whether alpha
 224      * information is present.  If {@code hasAlpha} is true, then
 225      * the boolean {@code isAlphaPremultiplied}


2913      * alpha  channel.
2914      *
2915      * @return A {@code WritableRaster} containing the image's alpha channel.
2916      *
2917      */
2918     public WritableRaster getAlphaRaster(WritableRaster raster) {
2919         if (hasAlpha() == false) {
2920             return null;
2921         }
2922 
2923         int x = raster.getMinX();
2924         int y = raster.getMinY();
2925         int[] band = new int[1];
2926         band[0] = raster.getNumBands() - 1;
2927         return raster.createWritableChild(x, y, raster.getWidth(),
2928                                           raster.getHeight(), x, y,
2929                                           band);
2930     }
2931 
2932     /**
2933      * Tests if the specified {@code Object} is an instance
2934      * of {@code ComponentColorModel} and equals this
2935      * {@code ComponentColorModel}.
2936      * @param obj the {@code Object} to test for equality
2937      * @return {@code true} if the specified {@code Object}
2938      * is an instance of {@code ComponentColorModel} and equals this
2939      * {@code ComponentColorModel}; {@code false} otherwise.
2940      */
2941     @Override
2942     public boolean equals(Object obj) {
2943         if (!(obj instanceof ComponentColorModel)) {
2944             return false;
2945         }
2946 
2947         ComponentColorModel cm = (ComponentColorModel) obj;
2948         if (supportsAlpha != cm.hasAlpha() ||
2949             isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
2950             pixel_bits != cm.getPixelSize() ||
2951             transparency != cm.getTransparency() ||
2952             numComponents != cm.getNumComponents() ||
2953             (!(colorSpace.equals(cm.colorSpace))) ||
2954             transferType != cm.transferType)
2955         {
2956             return false;
2957         }
2958 
2959         int[] nb = cm.getComponentSize();
2960 
2961         if ((nBits != null) && (nb != null)) {
2962             for (int i = 0; i < numComponents; i++) {
2963                 if (nBits[i] != nb[i]) {
2964                     return false;
2965                 }
2966             }
2967         } else {
2968             return ((nBits == null) && (nb == null));
2969         }
2970 
2971         return true;
2972     }
2973 
2974     /**
2975      * Returns the hash code for this ComponentColorModel.
2976      *
2977      * @return    a hash code for this ComponentColorModel.
2978      */
2979     @Override
2980     public int hashCode() {
2981         int result = hashCode;
2982         if (result == 0) {
2983             result = 7;
2984             result = 89 * result + this.pixel_bits;
2985             result = 89 * result + Arrays.hashCode(this.nBits);
2986             result = 89 * result + this.transparency;
2987             result = 89 * result + (this.supportsAlpha ? 1 : 0);
2988             result = 89 * result + (this.isAlphaPremultiplied ? 1 : 0);
2989             result = 89 * result + this.numComponents;
2990             result = 89 * result + Objects.hashCode(this.colorSpace);
2991             result = 89 * result + this.transferType;
2992             hashCode = result;
2993         }
2994         return result;
2995     }
2996 }
< prev index next >