app, libgimp, pdb: gimp_gradient_get_custom_samples() also returns an array of colors.

This commit is contained in:
Jehan 2024-11-03 13:20:36 +01:00
parent 6327d1b3ef
commit 5ed3bc33e0
4 changed files with 121 additions and 153 deletions

View file

@ -215,8 +215,7 @@ gradient_get_custom_samples_invoker (GimpProcedure *procedure,
gsize num_samples;
const gdouble *positions;
gboolean reverse;
gsize num_color_samples = 0;
gdouble *color_samples = NULL;
GeglColor **color_samples = NULL;
gradient = g_value_get_object (gimp_value_array_index (args, 0));
positions = gimp_value_get_double_array (gimp_value_array_index (args, 1), &num_samples);
@ -227,29 +226,20 @@ gradient_get_custom_samples_invoker (GimpProcedure *procedure,
if (gradient)
{
GimpGradientSegment *seg = NULL;
gdouble *sample;
GeglColor **sample;
num_color_samples = num_samples * 4;
sample = color_samples = g_new0 (gdouble, num_color_samples);
sample = color_samples = g_new0 (GeglColor *, num_samples + 1);
while (num_samples--)
{
GeglColor *color = NULL;
seg = gimp_gradient_get_color_at (gradient, context,
seg, *positions,
reverse,
GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
&color);
sample);
if (color)
gegl_color_get_pixel (color, babl_format ("R'G'B'A double"), sample);
sample += 4;
sample++;
positions++;
g_clear_object (&color);
}
}
else
@ -260,7 +250,7 @@ gradient_get_custom_samples_invoker (GimpProcedure *procedure,
error ? *error : NULL);
if (success)
gimp_value_take_double_array (gimp_value_array_index (return_vals, 1), color_samples, num_color_samples);
g_value_take_boxed (gimp_value_array_index (return_vals, 1), color_samples);
return return_vals;
}
@ -1384,7 +1374,7 @@ register_gradient_procs (GimpPDB *pdb)
"gimp-gradient-get-custom-samples");
gimp_procedure_set_static_help (procedure,
"Sample the gradient in custom positions.",
"Samples the color of the gradient at positions from a list. The left endpoint of the gradient corresponds to position 0.0, and the right endpoint corresponds to 1.0. Returns a list of floating-point values, four for each sample (RGBA.)",
"Samples the color of the gradient at positions from a list. The left endpoint of the gradient corresponds to position 0.0, and the right endpoint corresponds to 1.0. Returns a list of colors, one for each sample.",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Federico Mena Quintero",
@ -1410,9 +1400,10 @@ register_gradient_procs (GimpPDB *pdb)
FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_double_array ("color-samples",
g_param_spec_boxed ("color-samples",
"color samples",
"Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }",
"Color samples",
GIMP_TYPE_COLOR_ARRAY,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);

View file

@ -199,33 +199,30 @@ gimp_gradient_get_uniform_samples (GimpGradient *gradient,
* @num_samples: The number of samples to take.
* @positions: (array length=num_samples) (element-type gdouble): The list of positions to sample along the gradient.
* @reverse: Use the reverse gradient.
* @num_color_samples: (out): Length of the color_samples array (4 * num_samples).
* @color_samples: (out) (array length=num_color_samples) (element-type gdouble) (transfer full): Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }.
*
* Sample the gradient in custom positions.
*
* Samples the color of the gradient at positions from a list. The left
* endpoint of the gradient corresponds to position 0.0, and the right
* endpoint corresponds to 1.0. Returns a list of floating-point
* values, four for each sample (RGBA.)
* endpoint corresponds to 1.0. Returns a list of colors, one for each
* sample.
*
* Returns: TRUE on success.
* Returns: (array zero-terminated=1) (transfer full): Color samples.
* The returned value must be freed with gimp_color_array_free().
*
* Since: 2.2
**/
gboolean
GeglColor **
gimp_gradient_get_custom_samples (GimpGradient *gradient,
gsize num_samples,
const gdouble *positions,
gboolean reverse,
gsize *num_color_samples,
gdouble **color_samples)
gboolean reverse)
{
GimpValueArray *args;
GimpValueArray *return_vals;
gboolean success = TRUE;
GeglColor **color_samples = NULL;
g_return_val_if_fail (num_samples >= 1, FALSE);
g_return_val_if_fail (num_samples >= 1, NULL);
args = gimp_value_array_new_from_types (NULL,
GIMP_TYPE_GRADIENT, gradient,
@ -239,19 +236,12 @@ gimp_gradient_get_custom_samples (GimpGradient *gradient,
args);
gimp_value_array_unref (args);
*num_color_samples = 0;
*color_samples = NULL;
success = GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS;
if (success)
{
*color_samples = GIMP_VALUES_DUP_DOUBLE_ARRAY (return_vals, 1, num_color_samples);
}
if (GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS)
color_samples = gimp_color_array_copy (g_value_get_boxed (gimp_value_array_index (return_vals, 1)));
gimp_value_array_unref (return_vals);
return success;
return color_samples;
}
/**

View file

@ -38,12 +38,10 @@ gint gimp_gradient_get_number_of_segments (GimpGradient
GeglColor** gimp_gradient_get_uniform_samples (GimpGradient *gradient,
gint num_samples,
gboolean reverse);
gboolean gimp_gradient_get_custom_samples (GimpGradient *gradient,
GeglColor** gimp_gradient_get_custom_samples (GimpGradient *gradient,
gsize num_samples,
const gdouble *positions,
gboolean reverse,
gsize *num_color_samples,
gdouble **color_samples);
gboolean reverse);
GeglColor* gimp_gradient_segment_get_left_color (GimpGradient *gradient,
gint segment);
gboolean gimp_gradient_segment_set_left_color (GimpGradient *gradient,

View file

@ -196,7 +196,7 @@ sub gradient_get_custom_samples {
Samples the color of the gradient at positions from a list.
The left endpoint of the gradient corresponds
to position 0.0, and the right endpoint corresponds to 1.0.
Returns a list of floating-point values, four for each sample (RGBA.)
Returns a list of colors, one for each sample.
HELP
&federico_pdb_misc('1997', '2.2');
@ -212,10 +212,8 @@ HELP
);
@outargs = (
{ name => 'color_samples', type => 'doublearray', void_ret => 1,
desc => 'Color samples: { R1, G1, B1, A1, ..., Rn, Gn, Bn, An }',
array => { desc => 'Length of the color_samples array (4 *
num_samples)' } }
{ name => 'color_samples', type => 'colorarray',
desc => 'Color samples' }
);
%invoke = (
@ -224,29 +222,20 @@ HELP
if (gradient)
{
GimpGradientSegment *seg = NULL;
gdouble *sample;
GeglColor **sample;
num_color_samples = num_samples * 4;
sample = color_samples = g_new0 (gdouble, num_color_samples);
sample = color_samples = g_new0 (GeglColor *, num_samples + 1);
while (num_samples--)
{
GeglColor *color = NULL;
seg = gimp_gradient_get_color_at (gradient, context,
seg, *positions,
reverse,
GIMP_GRADIENT_BLEND_RGB_PERCEPTUAL,
&color);
sample);
if (color)
gegl_color_get_pixel (color, babl_format ("R'G'B'A double"), sample);
sample += 4;
sample++;
positions++;
g_clear_object (&color);
}
}
else