removed the "last_visited" field from GimpGradient. Instead added the new

2005-02-27  Sven Neumann  <sven@gimp.org>

	* app/core/gimpgradient.[ch]: removed the "last_visited" field
	from GimpGradient. Instead added the new function
	gimp_gradient_get_color_at_segment() that allows the caller to do
	the same optimization.

	* app/actions/gradient-editor-commands.c
	* app/core/gimpdrawable-blend.c
	* app/core/gimppalette-import.c
	* app/paint/gimppaintoptions.c
	* app/widgets/gimpgradienteditor.c
	* app/widgets/gimpgradientselect.c
	* app/widgets/gimpviewrenderergradient.c: changed accordingly.

	* app/pdb/gradient_cmds.c
	* app/pdb/gradients_cmds.c: regenerated.
This commit is contained in:
Sven Neumann 2005-02-26 23:55:50 +00:00 committed by Sven Neumann
parent 5f4cec76da
commit 8de1e94bb7
14 changed files with 174 additions and 131 deletions

View File

@ -1,3 +1,21 @@
2005-02-27 Sven Neumann <sven@gimp.org>
* app/core/gimpgradient.[ch]: removed the "last_visited" field
from GimpGradient. Instead added the new function
gimp_gradient_get_color_at_segment() that allows the caller to do
the same optimization.
* app/actions/gradient-editor-commands.c
* app/core/gimpdrawable-blend.c
* app/core/gimppalette-import.c
* app/paint/gimppaintoptions.c
* app/widgets/gimpgradienteditor.c
* app/widgets/gimpgradientselect.c
* app/widgets/gimpviewrenderergradient.c: changed accordingly.
* app/pdb/gradient_cmds.c
* app/pdb/gradients_cmds.c: regenerated.
2005-02-26 Manish Singh <yosh@gimp.org>
* plug-ins/common/png.c: revert change to read images one row at a

View File

@ -775,8 +775,6 @@ gradient_editor_replace_selection (GimpGradientEditor *editor,
editor->control_sel_l = replace_seg;
editor->control_sel_r = replace_last;
gradient->last_visited = NULL; /* Force re-search */
}
static void

View File

@ -121,14 +121,14 @@ static void gradient_precalc_shapeburst (GimpImage *gimage,
gdouble dist,
GimpProgress *progress);
static void gradient_render_pixel (gdouble x,
gdouble y,
GimpRGB *color,
gpointer render_data);
static void gradient_put_pixel (gint x,
gint y,
GimpRGB *color,
gpointer put_pixel_data);
static void gradient_render_pixel (gdouble x,
gdouble y,
GimpRGB *color,
gpointer render_data);
static void gradient_put_pixel (gint x,
gint y,
GimpRGB *color,
gpointer put_pixel_data);
static void gradient_fill_region (GimpImage *gimage,
GimpDrawable *drawable,
@ -653,8 +653,8 @@ gradient_precalc_shapeburst (GimpImage *gimage,
static void
gradient_render_pixel (double x,
double y,
gradient_render_pixel (gdouble x,
gdouble y,
GimpRGB *color,
gpointer render_data)
{
@ -715,13 +715,13 @@ gradient_render_pixel (double x,
case GIMP_GRADIENT_SPIRAL_CLOCKWISE:
factor = gradient_calc_spiral_factor (rbd->dist,
rbd->vec, rbd->offset,
x - rbd->sx, y - rbd->sy,TRUE);
x - rbd->sx, y - rbd->sy, TRUE);
break;
case GIMP_GRADIENT_SPIRAL_ANTICLOCKWISE:
factor = gradient_calc_spiral_factor (rbd->dist,
rbd->vec, rbd->offset,
x - rbd->sx, y - rbd->sy,FALSE);
x - rbd->sx, y - rbd->sy, FALSE);
break;
default:
@ -761,7 +761,8 @@ gradient_render_pixel (double x,
if (rbd->blend_mode == GIMP_CUSTOM_MODE)
{
gimp_gradient_get_color_at (rbd->gradient, factor, rbd->reverse, color);
gimp_gradient_get_color_at (rbd->gradient, NULL,
factor, rbd->reverse, color);
}
else
{

View File

@ -60,11 +60,16 @@ static gboolean gimp_gradient_get_popup_size (GimpViewable *viewable,
static TempBuf * gimp_gradient_get_new_preview (GimpViewable *viewable,
gint width,
gint height);
static void gimp_gradient_dirty (GimpData *data);
static gchar * gimp_gradient_get_extension (GimpData *data);
static GimpData * gimp_gradient_duplicate (GimpData *data,
gboolean stingy_memory_use);
static GimpGradientSegment *
gimp_gradient_get_segment_at_internal (GimpGradient *gradient,
GimpGradientSegment *seg,
gdouble pos);
static inline gdouble gimp_gradient_calc_linear_factor (gdouble middle,
gdouble pos);
static inline gdouble gimp_gradient_calc_curved_factor (gdouble middle,
@ -127,7 +132,8 @@ gimp_gradient_calc_sphere_increasing_factor (gdouble middle,
{
pos = gimp_gradient_calc_linear_factor (middle, pos) - 1.0;
return sqrt (1.0 - pos * pos); /* Works for convex increasing and concave decreasing */
/* Works for convex increasing and concave decreasing */
return sqrt (1.0 - pos * pos);
}
static inline gdouble
@ -136,7 +142,8 @@ gimp_gradient_calc_sphere_decreasing_factor (gdouble middle,
{
pos = gimp_gradient_calc_linear_factor (middle, pos);
return 1.0 - sqrt(1.0 - pos * pos); /* Works for convex decreasing and concave increasing */
/* Works for convex decreasing and concave increasing */
return 1.0 - sqrt(1.0 - pos * pos);
}
@ -188,7 +195,6 @@ gimp_gradient_class_init (GimpGradientClass *klass)
viewable_class->get_popup_size = gimp_gradient_get_popup_size;
viewable_class->get_new_preview = gimp_gradient_get_new_preview;
data_class->dirty = gimp_gradient_dirty;
data_class->save = gimp_gradient_save;
data_class->get_extension = gimp_gradient_get_extension;
data_class->duplicate = gimp_gradient_duplicate;
@ -197,8 +203,7 @@ gimp_gradient_class_init (GimpGradientClass *klass)
static void
gimp_gradient_init (GimpGradient *gradient)
{
gradient->segments = NULL;
gradient->last_visited = NULL;
gradient->segments = NULL;
}
static void
@ -266,14 +271,15 @@ gimp_gradient_get_new_preview (GimpViewable *viewable,
gint width,
gint height)
{
GimpGradient *gradient = GIMP_GRADIENT (viewable);
TempBuf *temp_buf;
guchar *buf;
guchar *p;
guchar *row;
gint x, y;
gdouble dx, cur_x;
GimpRGB color;
GimpGradient *gradient = GIMP_GRADIENT (viewable);
GimpGradientSegment *seg = NULL;
TempBuf *temp_buf;
guchar *buf;
guchar *p;
guchar *row;
gint x, y;
gdouble dx, cur_x;
GimpRGB color;
dx = 1.0 / (width - 1);
cur_x = 0.0;
@ -283,7 +289,7 @@ gimp_gradient_get_new_preview (GimpViewable *viewable,
for (x = 0; x < width; x++)
{
gimp_gradient_get_color_at (gradient, cur_x, FALSE, &color);
seg = gimp_gradient_get_color_at (gradient, seg, cur_x, FALSE, &color);
*p++ = color.r * 255.0;
*p++ = color.g * 255.0;
@ -377,44 +383,46 @@ gimp_gradient_get_standard (void)
return standard_gradient;
}
static void
gimp_gradient_dirty (GimpData *data)
{
GimpGradient *gradient = GIMP_GRADIENT (data);
gradient->last_visited = NULL;
if (GIMP_DATA_CLASS (parent_class)->dirty)
GIMP_DATA_CLASS (parent_class)->dirty (data);
}
static gchar *
gimp_gradient_get_extension (GimpData *data)
{
return GIMP_GRADIENT_FILE_EXTENSION;
}
void
gimp_gradient_get_color_at (GimpGradient *gradient,
gdouble pos,
gboolean reverse,
GimpRGB *color)
/**
* gimp_gradient_get_color_at:
* @gradient: a gradient
* @seg: a segment to seed the search with (or %NULL)
* @pos: position in the gradient (between 0.0 and 1.0)
* @reverse:
* @color: returns the color
*
* If you are iterating over an gradient, you should pass the the
* return value from the last call for @seg.
*
* Return value: the gradient segment the color is from
**/
GimpGradientSegment *
gimp_gradient_get_color_at (GimpGradient *gradient,
GimpGradientSegment *seg,
gdouble pos,
gboolean reverse,
GimpRGB *color)
{
gdouble factor = 0.0;
GimpGradientSegment *seg;
gdouble seg_len;
gdouble middle;
GimpRGB rgb;
gdouble factor = 0.0;
gdouble seg_len;
gdouble middle;
GimpRGB rgb;
g_return_if_fail (GIMP_IS_GRADIENT (gradient));
g_return_if_fail (color != NULL);
g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
g_return_val_if_fail (color != NULL, NULL);
pos = CLAMP (pos, 0.0, 1.0);
if (reverse)
pos = 1.0 - pos;
seg = gimp_gradient_get_segment_at (gradient, pos);
seg = gimp_gradient_get_segment_at_internal (gradient, seg, pos);
seg_len = seg->right - seg->left;
@ -526,22 +534,19 @@ gimp_gradient_get_color_at (GimpGradient *gradient,
seg->left_color.a + (seg->right_color.a - seg->left_color.a) * factor;
*color = rgb;
return seg;
}
GimpGradientSegment *
gimp_gradient_get_segment_at (GimpGradient *gradient,
gdouble pos)
static GimpGradientSegment *
gimp_gradient_get_segment_at_internal (GimpGradient *gradient,
GimpGradientSegment *seg,
gdouble pos)
{
GimpGradientSegment *seg;
g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
/* handle FP imprecision at the edges of the gradient */
pos = CLAMP (pos, 0.0, 1.0);
if (gradient->last_visited)
seg = gradient->last_visited;
else
if (! seg)
seg = gradient->segments;
while (seg)
@ -550,7 +555,6 @@ gimp_gradient_get_segment_at (GimpGradient *gradient,
{
if (pos <= seg->right)
{
gradient->last_visited = seg; /* for speed */
return seg;
}
else
@ -565,12 +569,20 @@ gimp_gradient_get_segment_at (GimpGradient *gradient,
}
/* Oops: we should have found a segment, but we didn't */
g_warning ("%s: no matching segment for position %0.15f",
G_STRFUNC, pos);
g_warning ("%s: no matching segment for position %0.15f", G_STRFUNC, pos);
return NULL;
}
GimpGradientSegment *
gimp_gradient_get_segment_at (GimpGradient *gradient,
gdouble pos)
{
g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
return gimp_gradient_get_segment_at_internal (gradient, NULL, pos);
}
/* gradient segment functions */
GimpGradientSegment *
@ -683,7 +695,7 @@ gimp_gradient_segment_split_midpoint (GimpGradient *gradient,
gimp_data_freeze (GIMP_DATA (gradient));
/* Get color at original segment's midpoint */
gimp_gradient_get_color_at (gradient, lseg->middle, FALSE, &color);
gimp_gradient_get_color_at (gradient, lseg, lseg->middle, FALSE, &color);
/* Create a new segment and insert it in the list */
@ -765,8 +777,10 @@ gimp_gradient_segment_split_uniform (GimpGradient *gradient,
seg->right = lseg->left + (i + 1) * seg_len;
seg->middle = (seg->left + seg->right) / 2.0;
gimp_gradient_get_color_at (gradient, seg->left, FALSE, &seg->left_color);
gimp_gradient_get_color_at (gradient, seg->right, FALSE, &seg->right_color);
gimp_gradient_get_color_at (gradient, seg,
seg->left, FALSE, &seg->left_color);
gimp_gradient_get_color_at (gradient, seg,
seg->right, FALSE, &seg->right_color);
seg->type = lseg->type;
seg->color = lseg->color;
@ -802,15 +816,11 @@ gimp_gradient_segment_split_uniform (GimpGradient *gradient,
if (lseg->next)
lseg->next->prev = seg;
gradient->last_visited = NULL; /* Force re-search */
/* Done */
*newl = tmp;
*newr = seg;
/* Delete old segment */
gimp_gradient_segment_free (lseg);
gimp_data_thaw (GIMP_DATA (gradient));

View File

@ -57,9 +57,6 @@ struct _GimpGradient
GimpData parent_instance;
GimpGradientSegment *segments;
/*< private >*/
GimpGradientSegment *last_visited;
};
struct _GimpGradientClass
@ -68,18 +65,19 @@ struct _GimpGradientClass
};
GType gimp_gradient_get_type (void) G_GNUC_CONST;
GType gimp_gradient_get_type (void) G_GNUC_CONST;
GimpData * gimp_gradient_new (const gchar *name,
gboolean stingy_memory_use);
GimpData * gimp_gradient_get_standard (void);
GimpData * gimp_gradient_new (const gchar *name,
gboolean stingy_memory_use);
GimpData * gimp_gradient_get_standard (void);
void gimp_gradient_get_color_at (GimpGradient *gradient,
gdouble pos,
gboolean reverse,
GimpRGB *color);
GimpGradientSegment * gimp_gradient_get_segment_at (GimpGradient *grad,
gdouble pos);
GimpGradientSegment * gimp_gradient_get_color_at (GimpGradient *gradient,
GimpGradientSegment *seg,
gdouble pos,
gboolean reverse,
GimpRGB *color);
GimpGradientSegment * gimp_gradient_get_segment_at (GimpGradient *grad,
gdouble pos);
/* gradient segment functions */

View File

@ -62,10 +62,11 @@ gimp_palette_import_from_gradient (GimpGradient *gradient,
const gchar *palette_name,
gint n_colors)
{
GimpPalette *palette;
gdouble dx, cur_x;
GimpRGB color;
gint loop;
GimpPalette *palette;
GimpGradientSegment *seg = NULL;
gdouble dx, cur_x;
GimpRGB color;
gint i;
g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
g_return_val_if_fail (palette_name != NULL, NULL);
@ -75,9 +76,9 @@ gimp_palette_import_from_gradient (GimpGradient *gradient,
dx = 1.0 / (n_colors - 1);
for (loop = 0, cur_x = 0; loop < n_colors; loop++, cur_x += dx)
for (i = 0, cur_x = 0; i < n_colors; i++, cur_x += dx)
{
gimp_gradient_get_color_at (gradient, cur_x, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, cur_x, reverse, &color);
gimp_palette_add_entry (palette, -1, NULL, &color);
}

View File

@ -521,7 +521,7 @@ gimp_paint_options_get_gradient_color (GimpPaintOptions *paint_options,
if (pressure_options->color)
{
gimp_gradient_get_color_at (gradient, pressure,
gimp_gradient_get_color_at (gradient, NULL, pressure,
gradient_options->gradient_reverse,
color);
@ -565,7 +565,7 @@ gimp_paint_options_get_gradient_color (GimpPaintOptions *paint_options,
else
pos = pos - (gint) pos;
gimp_gradient_get_color_at (gradient, pos,
gimp_gradient_get_color_at (gradient, NULL, pos,
gradient_options->gradient_reverse,
color);

View File

@ -490,9 +490,10 @@ gradient_get_uniform_samples_invoker (Gimp *gimp,
if (gradient)
{
gdouble pos = 0.0;
gdouble delta = 1.0 / (num_samples - 1);
gdouble *sample;
GimpGradientSegment *seg = NULL;
gdouble pos = 0.0;
gdouble delta = 1.0 / (num_samples - 1);
gdouble *sample;
num_color_samples = num_samples * 4;
@ -502,7 +503,7 @@ gradient_get_uniform_samples_invoker (Gimp *gimp,
{
GimpRGB color;
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*sample++ = color.r;
*sample++ = color.g;
@ -612,7 +613,8 @@ gradient_get_custom_samples_invoker (Gimp *gimp,
if (gradient)
{
gdouble *sample;
GimpGradientSegment *seg = NULL;
gdouble *sample;
num_color_samples = num_samples * 4;
@ -622,7 +624,7 @@ gradient_get_custom_samples_invoker (Gimp *gimp,
{
GimpRGB color;
gimp_gradient_get_color_at (gradient, *pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, *pos, reverse, &color);
*sample++ = color.r;
*sample++ = color.g;

View File

@ -173,6 +173,8 @@ gradients_sample_uniform_invoker (Gimp *gimp,
if (success)
{
GimpGradientSegment *seg = NULL;
pos = 0.0;
delta = 1.0 / (i - 1);
@ -184,7 +186,7 @@ gradients_sample_uniform_invoker (Gimp *gimp,
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;
@ -278,6 +280,8 @@ gradients_sample_custom_invoker (Gimp *gimp,
if (success)
{
GimpGradientSegment *seg = NULL;
array_length = i * 4;
pv = color_samples = g_new (gdouble, array_length);
@ -286,7 +290,7 @@ gradients_sample_custom_invoker (Gimp *gimp,
while (i--)
{
gimp_gradient_get_color_at (gradient, *pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, *pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;
@ -397,10 +401,11 @@ gradients_get_gradient_data_invoker (Gimp *gimp,
if (gradient)
{
gdouble *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
GimpGradientSegment *seg = NULL;
gdouble *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
i = sample_size;
pos = 0.0;
@ -410,7 +415,7 @@ gradients_get_gradient_data_invoker (Gimp *gimp,
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;

View File

@ -797,7 +797,7 @@ view_set_hint (GimpGradientEditor *editor,
xpos = control_calc_g_pos (editor, x);
gimp_gradient_get_color_at (GIMP_GRADIENT (data_editor->data),
gimp_gradient_get_color_at (GIMP_GRADIENT (data_editor->data), NULL,
xpos, FALSE, &rgb);
gimp_rgb_to_hsv (&rgb, &hsv);
@ -838,7 +838,7 @@ view_set_foreground (GimpGradientEditor *editor,
xpos = control_calc_g_pos (editor, x);
gimp_gradient_get_color_at (gradient, xpos, FALSE, &color);
gimp_gradient_get_color_at (gradient, NULL, xpos, FALSE, &color);
gimp_context_set_foreground (user_context, &color);
@ -873,7 +873,7 @@ view_set_background (GimpGradientEditor *editor,
xpos = control_calc_g_pos (editor, x);
gimp_gradient_get_color_at (gradient, xpos, FALSE, &color);
gimp_gradient_get_color_at (gradient, NULL, xpos, FALSE, &color);
gimp_context_set_background (user_context, &color);

View File

@ -150,11 +150,12 @@ gimp_gradient_select_run_callback (GimpPdbDialog *dialog,
gboolean closing,
gint *n_return_vals)
{
GimpGradient *gradient = GIMP_GRADIENT (object);
gdouble *values, *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
GimpGradient *gradient = GIMP_GRADIENT (object);
GimpGradientSegment *seg = NULL;
gdouble *values, *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
i = GIMP_GRADIENT_SELECT (dialog)->sample_size;
pos = 0.0;
@ -165,7 +166,7 @@ gimp_gradient_select_run_callback (GimpPdbDialog *dialog,
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, FALSE, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, FALSE, &color);
*pv++ = color.r;
*pv++ = color.g;

View File

@ -132,6 +132,7 @@ gimp_view_renderer_gradient_render (GimpViewRenderer *renderer,
{
GimpViewRendererGradient *rendergrad;
GimpGradient *gradient;
GimpGradientSegment *seg = NULL;
guchar *even;
guchar *odd;
guchar *buf;
@ -168,7 +169,8 @@ gimp_view_renderer_gradient_render (GimpViewRenderer *renderer,
{
guchar r, g, b, a;
gimp_gradient_get_color_at (gradient, cur_x, rendergrad->reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg,
cur_x, rendergrad->reverse, &color);
cur_x += dx;
gimp_rgba_get_uchar (&color, &r, &g, &b, &a);

View File

@ -481,9 +481,10 @@ HELP
if (gradient)
{
gdouble pos = 0.0;
gdouble delta = 1.0 / (num_samples - 1);
gdouble *sample;
GimpGradientSegment *seg = NULL;
gdouble pos = 0.0;
gdouble delta = 1.0 / (num_samples - 1);
gdouble *sample;
num_color_samples = num_samples * 4;
@ -493,7 +494,7 @@ HELP
{
GimpRGB color;
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*sample++ = color.r;
*sample++ = color.g;
@ -545,7 +546,8 @@ HELP
if (gradient)
{
gdouble *sample;
GimpGradientSegment *seg = NULL;
gdouble *sample;
num_color_samples = num_samples * 4;
@ -555,7 +557,7 @@ HELP
{
GimpRGB color;
gimp_gradient_get_color_at (gradient, *pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, *pos, reverse, &color);
*sample++ = color.r;
*sample++ = color.g;

View File

@ -110,6 +110,8 @@ sub gradients_sample_uniform {
'GimpRGB color', 'gdouble *pv' ],
code => <<'CODE'
{
GimpGradientSegment *seg = NULL;
pos = 0.0;
delta = 1.0 / (i - 1);
@ -121,7 +123,7 @@ sub gradients_sample_uniform {
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;
@ -153,6 +155,8 @@ sub gradients_sample_custom {
vars => [ 'GimpGradient *gradient', 'GimpRGB color', 'gdouble *pv' ],
code => <<'CODE'
{
GimpGradientSegment *seg = NULL;
array_length = i * 4;
pv = color_samples = g_new (gdouble, array_length);
@ -161,7 +165,7 @@ sub gradients_sample_custom {
while (i--)
{
gimp_gradient_get_color_at (gradient, *pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, *pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;
@ -223,10 +227,11 @@ sub gradients_get_gradient_data {
if (gradient)
{
gdouble *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
GimpGradientSegment *seg = NULL;
gdouble *pv;
gdouble pos, delta;
GimpRGB color;
gint i;
i = sample_size;
pos = 0.0;
@ -236,7 +241,7 @@ sub gradients_get_gradient_data {
while (i--)
{
gimp_gradient_get_color_at (gradient, pos, reverse, &color);
seg = gimp_gradient_get_color_at (gradient, seg, pos, reverse, &color);
*pv++ = color.r;
*pv++ = color.g;