1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include "core-types.h"
#include "gimpimage.h"
#include "gimpimage-colorhash.h"
#include "gimpimage-colormap.h"
#define MAXDIFF 195076
#define HASH_TABLE_SIZE 1021
typedef struct _ColorHash ColorHash;
struct _ColorHash
{
gint pixel; /* R << 16 | G << 8 | B */
gint index; /* colormap index */
GimpImage *image;
};
static ColorHash color_hash_table[HASH_TABLE_SIZE];
static gint color_hash_misses;
static gint color_hash_hits;
void
gimp_image_color_hash_init (void)
{
gint i;
/* initialize the color hash table--invalidate all entries */
for (i = 0; i < HASH_TABLE_SIZE; i++)
{
color_hash_table[i].pixel = 0;
color_hash_table[i].index = 0;
color_hash_table[i].image = NULL;
}
color_hash_misses = 0;
color_hash_hits = 0;
}
void
gimp_image_color_hash_exit (void)
{
#if 0
/* print out the hash table statistics */
g_print ("RGB->indexed hash table lookups: %d\n",
color_hash_hits + color_hash_misses);
g_print ("RGB->indexed hash table hits: %d\n", color_hash_hits);
g_print ("RGB->indexed hash table misses: %d\n", color_hash_misses);
g_print ("RGB->indexed hash table hit rate: %f\n",
100.0 * color_hash_hits / (color_hash_hits + color_hash_misses));
#endif
}
void
gimp_image_color_hash_invalidate (GimpImage *image,
gint index)
{
gint i;
g_return_if_fail (GIMP_IS_IMAGE (image));
if (index == -1) /* invalidate all entries */
{
for (i = 0; i < HASH_TABLE_SIZE; i++)
if (color_hash_table[i].image == image)
{
color_hash_table[i].pixel = 0;
color_hash_table[i].index = 0;
color_hash_table[i].image = NULL;
}
}
else
{
for (i = 0; i < HASH_TABLE_SIZE; i++)
if (color_hash_table[i].image == image &&
color_hash_table[i].index == index)
{
color_hash_table[i].pixel = 0;
color_hash_table[i].index = 0;
color_hash_table[i].image = NULL;
}
}
}
gint
gimp_image_color_hash_rgb_to_indexed (const GimpImage *image,
gint r,
gint g,
gint b)
{
const guchar *cmap;
gint num_cols;
guint pixel;
gint hash_index;
gint cmap_index;
cmap = gimp_image_get_colormap (image);
num_cols = gimp_image_get_colormap_size (image);
pixel = (r << 16) | (g << 8) | b;
hash_index = pixel % HASH_TABLE_SIZE;
if (color_hash_table[hash_index].image == image &&
color_hash_table[hash_index].pixel == pixel)
{
/* Hash table lookup hit */
cmap_index = color_hash_table[hash_index].index;
color_hash_hits++;
}
else
{
/* Hash table lookup miss */
const guchar *col;
gint diff, sum, max;
gint i;
max = MAXDIFF;
cmap_index = 0;
col = cmap;
for (i = 0; i < num_cols; i++)
{
diff = r - *col++;
sum = diff * diff;
diff = g - *col++;
sum += diff * diff;
diff = b - *col++;
sum += diff * diff;
if (sum < max)
{
cmap_index = i;
max = sum;
}
}
/* update the hash table */
color_hash_table[hash_index].pixel = pixel;
color_hash_table[hash_index].index = cmap_index;
color_hash_table[hash_index].image = (GimpImage *) image;
color_hash_misses++;
}
return cmap_index;
}
|