src/share/classes/java/awt/image/IndexColorModel.java

Print this page


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


 892                 break;
 893             }
 894             if (rgb == lookupcache[i+1]) {
 895                 return installpixel(pixel, ~pix);
 896             }
 897         }
 898 
 899         if (allgrayopaque) {
 900             // IndexColorModel objects are all tagged as
 901             // non-premultiplied so ignore the alpha value
 902             // of the incoming color, convert the
 903             // non-premultiplied color components to a
 904             // grayscale value and search for the closest
 905             // gray value in the palette.  Since all colors
 906             // in the palette are gray, we only need compare
 907             // to one of the color components for a match
 908             // using a simple linear distance formula.
 909 
 910             int minDist = 256;
 911             int d;
 912             int gray = (int) (red*77 + green*150 + blue*29 + 128)/256;
 913 
 914             for (int i = 0; i < map_size; i++) {
 915                 if (this.rgb[i] == 0x0) {
 916                     // For allgrayopaque colormaps, entries are 0
 917                     // iff they are an invalid color and should be
 918                     // ignored during color searches.
 919                     continue;
 920                 }
 921                 d = (this.rgb[i] & 0xff) - gray;
 922                 if (d < 0) d = -d;
 923                 if (d < minDist) {
 924                     pix = i;
 925                     if (d == 0) {
 926                         break;
 927                     }
 928                     minDist = d;
 929                 }
 930             }
 931         } else if (transparency == OPAQUE) {
 932             // IndexColorModel objects are all tagged as


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


 892                 break;
 893             }
 894             if (rgb == lookupcache[i+1]) {
 895                 return installpixel(pixel, ~pix);
 896             }
 897         }
 898 
 899         if (allgrayopaque) {
 900             // IndexColorModel objects are all tagged as
 901             // non-premultiplied so ignore the alpha value
 902             // of the incoming color, convert the
 903             // non-premultiplied color components to a
 904             // grayscale value and search for the closest
 905             // gray value in the palette.  Since all colors
 906             // in the palette are gray, we only need compare
 907             // to one of the color components for a match
 908             // using a simple linear distance formula.
 909 
 910             int minDist = 256;
 911             int d;
 912             int gray = (red*77 + green*150 + blue*29 + 128)/256;
 913 
 914             for (int i = 0; i < map_size; i++) {
 915                 if (this.rgb[i] == 0x0) {
 916                     // For allgrayopaque colormaps, entries are 0
 917                     // iff they are an invalid color and should be
 918                     // ignored during color searches.
 919                     continue;
 920                 }
 921                 d = (this.rgb[i] & 0xff) - gray;
 922                 if (d < 0) d = -d;
 923                 if (d < minDist) {
 924                     pix = i;
 925                     if (d == 0) {
 926                         break;
 927                     }
 928                     minDist = d;
 929                 }
 930             }
 931         } else if (transparency == OPAQUE) {
 932             // IndexColorModel objects are all tagged as