ColorModelFactory changes for revisions 20599:20970

Change in LGPL header does not apply to Apache SIS. Formatting changes are reverted. Definition of variables for array lengths are reverted. Changes in synchronization with synchronized (colors) is replaced by another approach.

Command line:

svn diff --extensions "--unified --ignore-space-change --ignore-all-space --ignore-eol-style" -r20599:20970 https://svn.osgeo.org/geotools/trunk/modules/library/coverage/src/main/java/org/geotools/coverage/ColorModelFactory.java
Revision 20599Revision 20970
/*
 * Geotools 2 - OpenSource mapping toolkit
 * (C) 2003, Geotools Project Management Committee (PMC)
 * (C) 2001, Institut de Recherche pour le Développement
 *
 *    This library is free software; you can redistribute it and/or
/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2003-2006, Geotools Project Management Committee (PMC)
 * (C) 2001, Institut de Recherche pour le Développement
 *
 *    This library is free software; you can redistribute it and/or
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with this library; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
*
*    This package contains documentation from OpenGIS specifications.
*    OpenGIS consortium's work is fully acknowledged here.
*/
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*    Lesser General Public License for more details.
*
*    This package contains documentation from OpenGIS specifications.
*    OpenGIS consortium's work is fully acknowledged here.
*/
 * @return The requested color model, suitable for {@link RenderedImage} objects with values
 *         in the <code>{@link CategoryList#getRange}</code> range.
 */
public static synchronized ColorModel getColorModel(final Category[] categories,
                                                    final int type,
                                                    final int visibleBand,
                                                    final int numBands)
{
    ColorModelFactory key = new ColorModelFactory(categories, type, visibleBand, numBands);
    ColorModel model = (ColorModel) colors.get(key);
    if (model == null) {
 * @return The requested color model, suitable for {@link RenderedImage} objects with values
 *         in the <code>{@link CategoryList#getRange}</code> range.
 */
public static  ColorModel getColorModel(final Category[] categories,
                                                    final int type,
                                                    final int visibleBand,
                                                    final int numBands)
{
    synchronized (colors) {
    ColorModelFactory key = new ColorModelFactory(categories, type, visibleBand, numBands);
    ColorModel model = (ColorModel) colors.get(key);
    if (model == null) {
    }
    return model;
}

/**
 * Construct the color model.
 */
private ColorModel getColorModel() {
    if (type != DataBuffer.TYPE_BYTE &&
        type != DataBuffer.TYPE_USHORT)
    {
        // If the requested type is any type not supported by IndexColorModel,
        // fallback on a generic (but very slow!) color model.
        double min = 0;
        double max = 1;
        if (categories.length != 0) {
            min = categories[0].minimum;
            for (int i=categories.length; --i>=0;) {
                final double val = categories[i].maximum;
                if (!Double.isNaN(val)) {
                    max = val;
    }
    return model;
}
}

/**
 * Construct the color model.
 */
private ColorModel getColorModel() {
   final int length = categories.length;
   if (type != DataBuffer.TYPE_BYTE && type != DataBuffer.TYPE_USHORT) {
     // If the requested type is any type not supported by
     // IndexColorModel,
        // fallback on a generic (but very slow!) color model.
        double min = 0;
        double max = 1;

     if (length != 0) {
            min = categories[0].minimum;
      for (int i = length; --i >= 0;) {
                final double val = categories[i].maximum;
                if (!Double.isNaN(val)) {
                    max = val;
    // with something.
    return RasterFactory.createComponentColorModel(type, colors, false, false, transparency);
}
if (numBands==1 && categories.length==0) {
    // Construct a gray scale palette.
    final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    final int[] nBits = {DataBuffer.getDataTypeSize(type)};
     // with something.
     return RasterFactory.createComponentColorModel(type, colors, false, false, transparency);
 }
if (numBands == 1 && length == 0) {
     // Construct a gray scale palette.
     final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
     final int[] nBits = {DataBuffer.getDataTypeSize(type)};
 * Computes the number of entries required for the color palette.
 * We take the upper range value of the last category.
 */
final int mapSize = (int)Math.round(categories[categories.length-1].maximum)+1;
final int[]  ARGB = new int[mapSize];
/*
 * Interpolate the colors in the color palette. Colors that do not fall
 * in the range of a category will be set to a transparent color.
 */
for (int i=0; i<categories.length; i++) {
    final Category category = categories[i];
    ColorUtilities.expand(category.getColors(), ARGB,
                          (int)Math.round(category.minimum),
  * Computes the number of entries required for the color palette.
  * We take the upper range value of the last category.
  */
final int mapSize = (int) Math.round(categories[length - 1].maximum) + 1;
 final int[]  ARGB = new int[mapSize];
 /*
  * Interpolate the colors in the color palette. Colors that do not fall
  * in the range of a category will be set to a transparent color.
  */
for (int i = 0; i < length; i++) {
     final Category category = categories[i];
     ColorUtilities.expand(category.getColors(), ARGB,
                           (int)Math.round(category.minimum),
 */
public int hashCode() {
    int code = 962745549 + (numBands*37 + visibleBand)*37 + categories.length;
    for (int i=0; i<categories.length; i++) {
        code += categories[i].hashCode();
        // Better be independant of categories order.
    }
 */
public int hashCode() {
    int code = 962745549 + (numBands*37 + visibleBand)*37 + categories.length;
   final int length = categories.length;
   for (int i = 0; i < length; i++) {
        code += categories[i].hashCode();
        // Better be independant of categories order.
    }