app, libgimp, pdb, plug-ins: change gimp_palette_set_colormap() to take a C array.

Similar to previous commit.
This commit is contained in:
Jehan 2024-09-22 17:11:03 +02:00
parent c60512514c
commit 9ad05ee6fa
7 changed files with 100 additions and 55 deletions

View File

@ -493,12 +493,12 @@ palette_get_bytes_invoker (GimpProcedure *procedure,
}
static GimpValueArray *
palette_set_colormap_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
palette_set_bytes_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpPalette *palette;
@ -1006,11 +1006,11 @@ register_palette_procs (GimpPDB *pdb)
g_object_unref (procedure);
/*
* gimp-palette-set-colormap
* gimp-palette-set-bytes
*/
procedure = gimp_procedure_new (palette_set_colormap_invoker);
procedure = gimp_procedure_new (palette_set_bytes_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-palette-set-colormap");
"gimp-palette-set-bytes");
gimp_procedure_set_static_help (procedure,
"Sets the entries in the image's colormap.",
"This procedure sets the entries in the specified palette in one go. The number of entries depens on the size of @colormap and the bytes-per-pixel size of @format.\n"

View File

@ -46,7 +46,7 @@ static void gimp_palette_init (GimpPalette *palette)
* @palette: The palette.
* @format: The desired color format.
* @num_colors: (out) (nullable): The number of colors in the palette.
* @num_bytes: (out) (nullable): The number of colors in the palette.
* @num_bytes: (out) (nullable): The byte-size of the returned value.
*
* This procedure returns a palette's colormap as an array of bytes with
* all colors converted to a given Babl @format.
@ -92,3 +92,43 @@ gimp_palette_get_colormap (GimpPalette *palette,
return colormap;
}
/**
* gimp_palette_set_colormap:
* @palette: The palette.
* @format: The desired color format.
* @colormap (array length=num_bytes): The new colormap values.
* @num_bytes: The byte-size of @colormap.
*
* This procedure sets the entries in the specified palette in one go,
* though they must all be in the same @format.
*
* The number of entries depens on the @num_bytes size of @colormap and
* the bytes-per-pixel size of @format.
* The procedure will fail if @num_bytes is not an exact multiple of the
* number of bytes per pixel of @format.
*
* Returns: %TRUE on success.
*
* Since: 3.0
**/
gboolean
gimp_palette_set_colormap (GimpPalette *palette,
const Babl *format,
guint8 *colormap,
gsize num_bytes)
{
GBytes *bytes;
gboolean success;
g_return_val_if_fail (GIMP_IS_PALETTE (palette), FALSE);
g_return_val_if_fail (format != NULL, FALSE);
g_return_val_if_fail (colormap != NULL, FALSE);
g_return_val_if_fail (num_bytes % babl_format_get_bytes_per_pixel (format) == 0, FALSE);
bytes = g_bytes_new_static (colormap, num_bytes);
success = _gimp_palette_set_bytes (palette, format, bytes);
g_bytes_unref (bytes);
return success;
}

View File

@ -37,10 +37,16 @@ G_BEGIN_DECLS
#define GIMP_TYPE_PALETTE (gimp_palette_get_type ())
G_DECLARE_FINAL_TYPE (GimpPalette, gimp_palette, GIMP, PALETTE, GimpResource)
guint8 * gimp_palette_get_colormap (GimpPalette *palette,
const Babl *format,
gint *num_colors,
gsize *num_bytes);
guint8 * gimp_palette_get_colormap (GimpPalette *palette,
const Babl *format,
gint *num_colors,
gsize *num_bytes);
gboolean gimp_palette_set_colormap (GimpPalette *palette,
const Babl *format,
guint8 *colormap,
gsize num_bytes);
G_END_DECLS

View File

@ -580,7 +580,7 @@ _gimp_palette_get_bytes (GimpPalette *palette,
}
/**
* gimp_palette_set_colormap:
* _gimp_palette_set_bytes:
* @palette: The palette.
* @format: The desired color format.
* @colormap: The new colormap values.
@ -598,9 +598,9 @@ _gimp_palette_get_bytes (GimpPalette *palette,
* Since: 3.0
**/
gboolean
gimp_palette_set_colormap (GimpPalette *palette,
const Babl *format,
GBytes *colormap)
_gimp_palette_set_bytes (GimpPalette *palette,
const Babl *format,
GBytes *colormap)
{
GimpValueArray *args;
GimpValueArray *return_vals;
@ -613,7 +613,7 @@ gimp_palette_set_colormap (GimpPalette *palette,
G_TYPE_NONE);
return_vals = _gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-palette-set-colormap",
"gimp-palette-set-bytes",
args);
gimp_value_array_unref (args);

View File

@ -32,36 +32,36 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
GimpPalette* gimp_palette_new (const gchar *name);
GimpPalette* gimp_palette_get_by_name (const gchar *name);
gint gimp_palette_get_color_count (GimpPalette *palette);
GeglColor** gimp_palette_get_colors (GimpPalette *palette);
gint gimp_palette_get_columns (GimpPalette *palette);
gboolean gimp_palette_set_columns (GimpPalette *palette,
gint columns);
gboolean gimp_palette_add_entry (GimpPalette *palette,
const gchar *entry_name,
GeglColor *color,
gint *entry_num);
gboolean gimp_palette_delete_entry (GimpPalette *palette,
gint entry_num);
GeglColor* gimp_palette_entry_get_color (GimpPalette *palette,
gint entry_num);
gboolean gimp_palette_entry_set_color (GimpPalette *palette,
gint entry_num,
GeglColor *color);
gboolean gimp_palette_entry_get_name (GimpPalette *palette,
gint entry_num,
gchar **entry_name);
gboolean gimp_palette_entry_set_name (GimpPalette *palette,
gint entry_num,
const gchar *entry_name);
G_GNUC_INTERNAL GBytes* _gimp_palette_get_bytes (GimpPalette *palette,
const Babl *format,
gint *num_colors);
gboolean gimp_palette_set_colormap (GimpPalette *palette,
const Babl *format,
GBytes *colormap);
GimpPalette* gimp_palette_new (const gchar *name);
GimpPalette* gimp_palette_get_by_name (const gchar *name);
gint gimp_palette_get_color_count (GimpPalette *palette);
GeglColor** gimp_palette_get_colors (GimpPalette *palette);
gint gimp_palette_get_columns (GimpPalette *palette);
gboolean gimp_palette_set_columns (GimpPalette *palette,
gint columns);
gboolean gimp_palette_add_entry (GimpPalette *palette,
const gchar *entry_name,
GeglColor *color,
gint *entry_num);
gboolean gimp_palette_delete_entry (GimpPalette *palette,
gint entry_num);
GeglColor* gimp_palette_entry_get_color (GimpPalette *palette,
gint entry_num);
gboolean gimp_palette_entry_set_color (GimpPalette *palette,
gint entry_num,
GeglColor *color);
gboolean gimp_palette_entry_get_name (GimpPalette *palette,
gint entry_num,
gchar **entry_name);
gboolean gimp_palette_entry_set_name (GimpPalette *palette,
gint entry_num,
const gchar *entry_name);
G_GNUC_INTERNAL GBytes* _gimp_palette_get_bytes (GimpPalette *palette,
const Babl *format,
gint *num_colors);
G_GNUC_INTERNAL gboolean _gimp_palette_set_bytes (GimpPalette *palette,
const Babl *format,
GBytes *colormap);
G_END_DECLS

View File

@ -477,7 +477,7 @@ CODE
);
}
sub palette_set_colormap {
sub palette_set_bytes {
$blurb = "Sets the entries in the image's colormap.";
$help = <<'HELP';
@ -490,6 +490,8 @@ HELP
&jehan_pdb_misc('2024', '3.0');
$lib_private = 1;
@inargs = (
{ name => 'palette', type => 'palette',
desc => 'The palette' },
@ -542,7 +544,7 @@ CODE
palette_add_entry palette_delete_entry
palette_entry_get_color palette_entry_set_color
palette_entry_get_name palette_entry_set_name
palette_get_bytes palette_set_colormap);
palette_get_bytes palette_set_bytes);
%exports = (app => [@procs], lib => [@procs]);

View File

@ -1208,15 +1208,12 @@ set_bw_color_table (GimpImage *image)
{
static guchar BWColorMap[2*3] = { 255, 255, 255, 0, 0, 0 };
GimpPalette *palette = gimp_image_get_palette (image);
GBytes *bytes;
#ifdef XWD_COL_DEBUG
g_printf ("Set GIMP b/w-colortable:\n");
#endif
bytes = g_bytes_new_static (BWColorMap, 2 * 3);
gimp_palette_set_colormap (palette, babl_format ("R'G'B' u8"), bytes);
g_bytes_unref (bytes);
gimp_palette_set_colormap (palette, babl_format ("R'G'B' u8"), BWColorMap, 2 * 3);
}