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


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


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