diff --git a/app/pdb/palette-cmds.c b/app/pdb/palette-cmds.c index b80632c630..d11a59a889 100644 --- a/app/pdb/palette-cmds.c +++ b/app/pdb/palette-cmds.c @@ -454,12 +454,12 @@ palette_entry_set_name_invoker (GimpProcedure *procedure, } static GimpValueArray * -palette_get_colormap_invoker (GimpProcedure *procedure, - Gimp *gimp, - GimpContext *context, - GimpProgress *progress, - const GimpValueArray *args, - GError **error) +palette_get_bytes_invoker (GimpProcedure *procedure, + Gimp *gimp, + GimpContext *context, + GimpProgress *progress, + const GimpValueArray *args, + GError **error) { gboolean success = TRUE; GimpValueArray *return_vals; @@ -958,14 +958,14 @@ register_palette_procs (GimpPDB *pdb) g_object_unref (procedure); /* - * gimp-palette-get-colormap + * gimp-palette-get-bytes */ - procedure = gimp_procedure_new (palette_get_colormap_invoker); + procedure = gimp_procedure_new (palette_get_bytes_invoker); gimp_object_set_static_name (GIMP_OBJECT (procedure), - "gimp-palette-get-colormap"); + "gimp-palette-get-bytes"); gimp_procedure_set_static_help (procedure, "Returns the palette's colormap", - "This procedure returns an the image's colormap as a bytes array with all colors converted to a given Babl @format.\n" + "This procedure returns a palette's colormap as a bytes array with all colors converted to a given Babl @format.\n" "The byte-size of the returned colormap depends on the number of colors and on the bytes-per-pixel size of @format. E.g. that the following equality is ensured:\n" "\n" "```C\n" diff --git a/libgimp/gimppalette.c b/libgimp/gimppalette.c index 759401fc46..07a1802a68 100644 --- a/libgimp/gimppalette.c +++ b/libgimp/gimppalette.c @@ -40,3 +40,55 @@ static void gimp_palette_class_init (GimpPaletteClass *klass) static void gimp_palette_init (GimpPalette *palette) { } + +/** + * gimp_palette_get_colormap: + * @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. + * + * This procedure returns a palette's colormap as an array of bytes with + * all colors converted to a given Babl @format. + * + * The byte-size of the returned colormap depends on the number of + * colors and on the bytes-per-pixel size of @format. E.g. that the + * following equality is ensured: + * + * ```C + * num_bytes == num_colors * babl_format_get_bytes_per_pixel (format) + * ``` + * + * Therefore @num_colors and @num_bytes are kinda redundant since both + * indicate the size of the return value in a different way. You may + * both set them to %NULL but not at the same time. + * + * Returns: (transfer full) (array length=num_bytes): The palette's colormap. + * + * Since: 3.0 + **/ +guint8 * +gimp_palette_get_colormap (GimpPalette *palette, + const Babl *format, + gint *num_colors, + gsize *num_bytes) +{ + GBytes *bytes; + guint8 *colormap; + gint n_colors; + gsize n_bytes; + + g_return_val_if_fail (GIMP_IS_PALETTE (palette), NULL); + g_return_val_if_fail (format != NULL, NULL); + g_return_val_if_fail (num_colors != NULL || num_bytes != NULL, NULL); + + bytes = _gimp_palette_get_bytes (palette, format, &n_colors); + colormap = g_bytes_unref_to_data (bytes, &n_bytes); + + if (num_colors) + *num_colors = n_colors; + if (num_bytes) + *num_bytes = n_bytes; + + return colormap; +} diff --git a/libgimp/gimppalette.h b/libgimp/gimppalette.h index 5941e6b114..52a8e18e45 100644 --- a/libgimp/gimppalette.h +++ b/libgimp/gimppalette.h @@ -37,6 +37,10 @@ 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); G_END_DECLS diff --git a/libgimp/gimppalette_pdb.c b/libgimp/gimppalette_pdb.c index 34a7b6bb2f..eb1c81cb67 100644 --- a/libgimp/gimppalette_pdb.c +++ b/libgimp/gimppalette_pdb.c @@ -527,14 +527,14 @@ gimp_palette_entry_set_name (GimpPalette *palette, } /** - * gimp_palette_get_colormap: + * _gimp_palette_get_bytes: * @palette: The palette. * @format: The desired color format. * @num_colors: (out): The number of colors in the palette. * * Returns the palette's colormap * - * This procedure returns an the image's colormap as a bytes array with + * This procedure returns a palette's colormap as a bytes array with * all colors converted to a given Babl @format. * The byte-size of the returned colormap depends on the number of * colors and on the bytes-per-pixel size of @format. E.g. that the @@ -550,9 +550,9 @@ gimp_palette_entry_set_name (GimpPalette *palette, * Since: 3.0 **/ GBytes * -gimp_palette_get_colormap (GimpPalette *palette, - const Babl *format, - gint *num_colors) +_gimp_palette_get_bytes (GimpPalette *palette, + const Babl *format, + gint *num_colors) { GimpValueArray *args; GimpValueArray *return_vals; @@ -564,7 +564,7 @@ gimp_palette_get_colormap (GimpPalette *palette, G_TYPE_NONE); return_vals = _gimp_pdb_run_procedure_array (gimp_get_pdb (), - "gimp-palette-get-colormap", + "gimp-palette-get-bytes", args); gimp_value_array_unref (args); diff --git a/libgimp/gimppalette_pdb.h b/libgimp/gimppalette_pdb.h index 025af10b39..67383c789b 100644 --- a/libgimp/gimppalette_pdb.h +++ b/libgimp/gimppalette_pdb.h @@ -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); -GBytes* gimp_palette_get_colormap (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); +gboolean gimp_palette_set_colormap (GimpPalette *palette, + const Babl *format, + GBytes *colormap); G_END_DECLS diff --git a/libgimp/tests/test-palette.c b/libgimp/tests/test-palette.c index 08c06dce16..ac292b18e5 100644 --- a/libgimp/tests/test-palette.c +++ b/libgimp/tests/test-palette.c @@ -19,10 +19,10 @@ gimp_c_test_run (GimpProcedure *procedure, GimpPalette *palette2; GeglColor **colors; gint n_colors; - GBytes *colormap; - const guchar *u8_data; - const gfloat *float_data; + guchar *colormap; + gfloat *fcolormap; gint n_colormap_colors; + gsize n_bytes; const Babl *format; const Babl *format2; gint format_bpp; @@ -58,36 +58,34 @@ gimp_c_test_run (GimpProcedure *procedure, GIMP_TEST_END(rgb[0] == GIMP_TEST_COLOR_R_U8 && rgb[1] == GIMP_TEST_COLOR_G_U8 && rgb[2] == GIMP_TEST_COLOR_B_U8) GIMP_TEST_START("gimp_palette_get_colormap()") - colormap = gimp_palette_get_colormap (palette, format, &n_colormap_colors); + colormap = gimp_palette_get_colormap (palette, format, &n_colormap_colors, &n_bytes); format_bpp = babl_format_get_bytes_per_pixel (format); GIMP_TEST_END(colormap != NULL && n_colormap_colors == n_colors && - n_colormap_colors * format_bpp == g_bytes_get_size (colormap)) + n_colormap_colors * format_bpp == n_bytes) GIMP_TEST_START("Comparing fourth palette color's RGB components from colormap") - u8_data = g_bytes_get_data (colormap, NULL); - GIMP_TEST_END(u8_data[format_bpp * GIMP_TEST_COLOR_IDX] == GIMP_TEST_COLOR_R_U8 && - u8_data[format_bpp * GIMP_TEST_COLOR_IDX + 1] == GIMP_TEST_COLOR_G_U8 && - u8_data[format_bpp * GIMP_TEST_COLOR_IDX + 2]== GIMP_TEST_COLOR_B_U8) + GIMP_TEST_END(colormap[format_bpp * GIMP_TEST_COLOR_IDX] == GIMP_TEST_COLOR_R_U8 && + colormap[format_bpp * GIMP_TEST_COLOR_IDX + 1] == GIMP_TEST_COLOR_G_U8 && + colormap[format_bpp * GIMP_TEST_COLOR_IDX + 2]== GIMP_TEST_COLOR_B_U8) - g_bytes_unref (colormap); + g_free (colormap); GIMP_TEST_START("gimp_palette_get_colormap() in \"R'G'B'A float\" format") format2 = babl_format ("RGBA float"); format2_bpp = babl_format_get_bytes_per_pixel (format2); - colormap = gimp_palette_get_colormap (palette, format2, &n_colormap_colors); - GIMP_TEST_END(colormap != NULL && n_colormap_colors == n_colors && + fcolormap = (gfloat *) gimp_palette_get_colormap (palette, format2, &n_colormap_colors, &n_bytes); + GIMP_TEST_END(fcolormap != NULL && n_colormap_colors == n_colors && format2 != format && format2_bpp > format_bpp && - n_colormap_colors * format2_bpp == g_bytes_get_size (colormap)) + n_colormap_colors * format2_bpp == n_bytes) GIMP_TEST_START("Comparing fourth palette color's RGB components from colormap in float format") gegl_color_get_pixel (color, format2, rgba); - float_data = g_bytes_get_data (colormap, NULL); - GIMP_TEST_END(float_data[4 * GIMP_TEST_COLOR_IDX] == rgba[0] && - float_data[4 * GIMP_TEST_COLOR_IDX + 1] == rgba[1] && - float_data[4 * GIMP_TEST_COLOR_IDX + 2]== rgba[2]) + GIMP_TEST_END(fcolormap[4 * GIMP_TEST_COLOR_IDX] == rgba[0] && + fcolormap[4 * GIMP_TEST_COLOR_IDX + 1] == rgba[1] && + fcolormap[4 * GIMP_TEST_COLOR_IDX + 2]== rgba[2]) - g_bytes_unref (colormap); + g_free (fcolormap); /* Run the same tests through PDB. */ diff --git a/libgimp/tests/test-palette.py b/libgimp/tests/test-palette.py index 0dc6d11ac3..dfcce9dfb5 100644 --- a/libgimp/tests/test-palette.py +++ b/libgimp/tests/test-palette.py @@ -36,27 +36,25 @@ colormap, n_colormap_colors = pal.get_colormap (f) format_bpp = Babl.format_get_bytes_per_pixel (f); gimp_assert("gimp_palette_get_colormap()", colormap is not None and n_colormap_colors == n_colors and - n_colormap_colors * format_bpp == colormap.get_size()) + n_colormap_colors * format_bpp == len(colormap)) -u8_data = colormap.get_data () gimp_assert("Comparing fourth palette color's RGB components from colormap", - u8_data[format_bpp * GIMP_TEST_COLOR_IDX] == GIMP_TEST_COLOR_R_U8 and - u8_data[format_bpp * GIMP_TEST_COLOR_IDX + 1] == GIMP_TEST_COLOR_G_U8 and - u8_data[format_bpp * GIMP_TEST_COLOR_IDX + 2]== GIMP_TEST_COLOR_B_U8) + colormap[format_bpp * GIMP_TEST_COLOR_IDX] == GIMP_TEST_COLOR_R_U8 and + colormap[format_bpp * GIMP_TEST_COLOR_IDX + 1] == GIMP_TEST_COLOR_G_U8 and + colormap[format_bpp * GIMP_TEST_COLOR_IDX + 2]== GIMP_TEST_COLOR_B_U8) f2 = Babl.format('RGBA float') colormap, n_colormap_colors = pal.get_colormap (f2) f2_bpp = Babl.format_get_bytes_per_pixel (f2); gimp_assert('gimp_palette_get_colormap() in "RGBA float"', colormap is not None and n_colormap_colors == n_colors and - n_colormap_colors * f2_bpp == colormap.get_size()) + n_colormap_colors * f2_bpp == len(colormap)) -u8_data = colormap.get_data () start = f2_bpp * GIMP_TEST_COLOR_IDX -colormap_r = struct.unpack('f', u8_data[start:start + 4]) -colormap_g = struct.unpack('f', u8_data[start + 4:start + 8]) -colormap_b = struct.unpack('f', u8_data[start + 8:start + 12]) +colormap_r = struct.unpack('f', colormap[start:start + 4]) +colormap_g = struct.unpack('f', colormap[start + 4:start + 8]) +colormap_b = struct.unpack('f', colormap[start + 8:start + 12]) rgb_bytes = colors[GIMP_TEST_COLOR_IDX].get_bytes(f2) rgb = rgb_bytes.get_data() diff --git a/pdb/groups/palette.pdb b/pdb/groups/palette.pdb index a3b3908992..a962360143 100644 --- a/pdb/groups/palette.pdb +++ b/pdb/groups/palette.pdb @@ -430,11 +430,11 @@ CODE ); } -sub palette_get_colormap { +sub palette_get_bytes { $blurb = "Returns the palette's colormap"; $help = <<'HELP'; -This procedure returns an the image's colormap as a bytes array with all +This procedure returns a palette's colormap as a bytes array with all colors converted to a given Babl @format. The byte-size of the returned colormap depends on the number of colors @@ -447,6 +447,8 @@ HELP &jehan_pdb_misc('2024', '3.0'); + $lib_private = 1; + @inargs = ( { name => 'palette', type => 'palette', desc => 'The palette' }, @@ -458,7 +460,7 @@ HELP { name => 'colormap', type => 'bytes', desc => "The image's colormap." }, { name => 'num_colors', type => 'int32', - desc => 'The number of colors in the palette' } + desc => 'The number of colors in the palette', } ); %invoke = ( @@ -540,7 +542,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_colormap palette_set_colormap); + palette_get_bytes palette_set_colormap); %exports = (app => [@procs], lib => [@procs]); diff --git a/plug-ins/file-psd/psd-export.c b/plug-ins/file-psd/psd-export.c index 599bdbd9e8..ad7e818c7b 100644 --- a/plug-ins/file-psd/psd-export.c +++ b/plug-ins/file-psd/psd-export.c @@ -603,7 +603,6 @@ save_color_mode_data (GOutputStream *output, GimpImage *image, gboolean export_duotone) { - GBytes *bytes; guchar *cmap; guchar *cmap_modified; gint i; @@ -634,8 +633,7 @@ save_color_mode_data (GOutputStream *output, case GIMP_INDEXED: IFDBG(1) g_debug ("\tImage type: INDEXED"); - bytes = gimp_palette_get_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"), &nColors); - cmap = g_bytes_unref_to_data (bytes, NULL); + cmap = gimp_palette_get_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"), &nColors, NULL); IFDBG(1) g_debug ("\t\tLength of colormap returned by gimp_palette_get_colormap: %d", nColors); if (nColors == 0) diff --git a/plug-ins/file-tiff/file-tiff-export.c b/plug-ins/file-tiff/file-tiff-export.c index 408cb8a2b0..6f02518356 100644 --- a/plug-ins/file-tiff/file-tiff-export.c +++ b/plug-ins/file-tiff/file-tiff-export.c @@ -298,7 +298,6 @@ save_layer (TIFF *tif, guchar *src = NULL; guchar *data = NULL; GimpPalette *palette; - GBytes *cmap_bytes; guchar *cmap; gint num_colors; gint success; @@ -565,10 +564,9 @@ save_layer (TIFF *tif, case GIMP_INDEXED_IMAGE: case GIMP_INDEXEDA_IMAGE: - palette = gimp_image_get_palette (image); - format = gimp_drawable_get_format (GIMP_DRAWABLE (layer)); - cmap_bytes = gimp_palette_get_colormap (palette, format, &num_colors); - cmap = g_bytes_unref_to_data (cmap_bytes, NULL); + palette = gimp_image_get_palette (image); + format = gimp_drawable_get_format (GIMP_DRAWABLE (layer)); + cmap = gimp_palette_get_colormap (palette, babl_format_with_space ("R'G'B' u8", format), &num_colors, NULL); if (num_colors == 2 || num_colors == 1) { diff --git a/plug-ins/file-tiff/file-tiff.c b/plug-ins/file-tiff/file-tiff.c index 1cf9999335..30cc8d0d60 100644 --- a/plug-ins/file-tiff/file-tiff.c +++ b/plug-ins/file-tiff/file-tiff.c @@ -502,31 +502,33 @@ static gboolean image_is_monochrome (GimpImage *image) { GimpPalette *palette; - gint num_colors; gboolean monochrome = FALSE; g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE); palette = gimp_image_get_palette (image); - num_colors = gimp_palette_get_color_count (palette); - - if (palette && (num_colors == 2 || num_colors == 1)) + if (palette) { - GBytes *bytes; - const guchar *colors; - const guchar bw_map[] = { 0, 0, 0, 255, 255, 255 }; - const guchar wb_map[] = { 255, 255, 255, 0, 0, 0 }; + gint num_colors; - bytes = gimp_palette_get_colormap (palette, babl_format ("R'G'B' u8"), &num_colors); - colors = g_bytes_get_data (bytes, NULL); + num_colors = gimp_palette_get_color_count (palette); - if (memcmp (colors, bw_map, 3 * num_colors) == 0 || - memcmp (colors, wb_map, 3 * num_colors) == 0) + if (num_colors == 2 || num_colors == 1) { - monochrome = TRUE; - } + guchar *colors; + const guchar bw_map[] = { 0, 0, 0, 255, 255, 255 }; + const guchar wb_map[] = { 255, 255, 255, 0, 0, 0 }; - g_bytes_unref (bytes); + colors = gimp_palette_get_colormap (palette, babl_format ("R'G'B' u8"), &num_colors, NULL); + + if (memcmp (colors, bw_map, 3 * num_colors) == 0 || + memcmp (colors, wb_map, 3 * num_colors) == 0) + { + monochrome = TRUE; + } + + g_free (colors); + } } return monochrome;