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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimppickable.c
* Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
*
* 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/>.
*/
/* This file contains an interface for pixel objects that their color at
* a given position can be picked. Also included is a utility for
* sampling an average area (which uses the implemented picking
* functions).
*/
#include "config.h"
#include <cairo.h>
#include <gegl.h>
#include "libgimpcolor/gimpcolor.h"
#include "core-types.h"
#include "gimpobject.h"
#include "gimpimage.h"
#include "gimppickable.h"
GType
gimp_pickable_interface_get_type (void)
{
static GType pickable_iface_type = 0;
if (! pickable_iface_type)
{
const GTypeInfo pickable_iface_info =
{
sizeof (GimpPickableInterface),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
};
pickable_iface_type = g_type_register_static (G_TYPE_INTERFACE,
"GimpPickableInterface",
&pickable_iface_info,
0);
g_type_interface_add_prerequisite (pickable_iface_type, GIMP_TYPE_OBJECT);
}
return pickable_iface_type;
}
void
gimp_pickable_flush (GimpPickable *pickable)
{
GimpPickableInterface *pickable_iface;
g_return_if_fail (GIMP_IS_PICKABLE (pickable));
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->flush)
pickable_iface->flush (pickable);
}
GimpImage *
gimp_pickable_get_image (GimpPickable *pickable)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), NULL);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_image)
return pickable_iface->get_image (pickable);
return NULL;
}
GimpImageType
gimp_pickable_get_image_type (GimpPickable *pickable)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), -1);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_image_type)
return pickable_iface->get_image_type (pickable);
return -1;
}
gint
gimp_pickable_get_bytes (GimpPickable *pickable)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), 0);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_bytes)
return pickable_iface->get_bytes (pickable);
return 0;
}
TileManager *
gimp_pickable_get_tiles (GimpPickable *pickable)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), NULL);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_tiles)
return pickable_iface->get_tiles (pickable);
return NULL;
}
gboolean
gimp_pickable_get_pixel_at (GimpPickable *pickable,
gint x,
gint y,
guchar *pixel)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), FALSE);
g_return_val_if_fail (pixel != NULL, FALSE);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_pixel_at)
return pickable_iface->get_pixel_at (pickable, x, y, pixel);
return FALSE;
}
gboolean
gimp_pickable_get_color_at (GimpPickable *pickable,
gint x,
gint y,
GimpRGB *color)
{
guchar pixel[4];
guchar col[4];
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), FALSE);
g_return_val_if_fail (color != NULL, FALSE);
if (! gimp_pickable_get_pixel_at (pickable, x, y, pixel))
return FALSE;
gimp_image_get_color (gimp_pickable_get_image (pickable),
gimp_pickable_get_image_type (pickable),
pixel, col);
gimp_rgba_set_uchar (color, col[0], col[1], col[2], col[3]);
return TRUE;
}
gint
gimp_pickable_get_opacity_at (GimpPickable *pickable,
gint x,
gint y)
{
GimpPickableInterface *pickable_iface;
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), 0);
pickable_iface = GIMP_PICKABLE_GET_INTERFACE (pickable);
if (pickable_iface->get_opacity_at)
return pickable_iface->get_opacity_at (pickable, x, y);
return 0;
}
gboolean
gimp_pickable_pick_color (GimpPickable *pickable,
gint x,
gint y,
gboolean sample_average,
gdouble average_radius,
GimpRGB *color,
gint *color_index)
{
GimpImage *image;
GimpImageType type;
guchar pixel[4];
guchar col[4];
g_return_val_if_fail (GIMP_IS_PICKABLE (pickable), FALSE);
if (! gimp_pickable_get_pixel_at (pickable, x, y, pixel))
return FALSE;
image = gimp_pickable_get_image (pickable);
type = gimp_pickable_get_image_type (pickable);
if (sample_average)
{
gint count = 0;
gint color_avg[4] = { 0, 0, 0, 0 };
gint radius = (gint) average_radius;
gint i, j;
for (i = x - radius; i <= x + radius; i++)
for (j = y - radius; j <= y + radius; j++)
if (gimp_pickable_get_pixel_at (pickable, i, j, pixel))
{
count++;
gimp_image_get_color (image, type, pixel, col);
color_avg[RED] += col[RED];
color_avg[GREEN] += col[GREEN];
color_avg[BLUE] += col[BLUE];
color_avg[ALPHA] += col[ALPHA];
}
col[RED] = (guchar) ((color_avg[RED] + count / 2) / count);
col[GREEN] = (guchar) ((color_avg[GREEN] + count / 2) / count);
col[BLUE] = (guchar) ((color_avg[BLUE] + count / 2) / count);
col[ALPHA] = (guchar) ((color_avg[ALPHA] + count / 2) / count);
}
else
{
gimp_image_get_color (image, type, pixel, col);
}
gimp_rgba_set_uchar (color,
col[RED],
col[GREEN],
col[BLUE],
col[ALPHA]);
if (color_index)
{
if (GIMP_IMAGE_TYPE_IS_INDEXED (type) && ! sample_average)
*color_index = pixel[0];
else
*color_index = -1;
}
return TRUE;
}
|