Remove GimpUint8Array in favor of GBytes

GLib has a specific type for byte arrays: `GBytes` (and it's underlying
GType `G_TYPE_BYTES`).

By using this type, we can avoid having a `GimpUint8Array` which is a
bit cumbersome to use for both the C API, as well as bindings. By using
`GBytes`, we allow other languages to pass on byte arrays as they are
used to, while the bindings will make sure to do the right thing.

In the end, it makes the API a little bit simpler for everyone, and
reduces confusion for people who are used to working with byte arrays
in other C/GLib based code (and not having 2 different types to denote
the same thing).

Related: https://gitlab.gnome.org/GNOME/gimp/-/issues/5919
This commit is contained in:
Niels De Graef 2023-05-23 23:37:46 +02:00
parent 9e79bb78a1
commit 89c359ce47
98 changed files with 841 additions and 1681 deletions

View file

@ -120,10 +120,8 @@ EXPORTS
gimp_param_spec_object_array
gimp_param_spec_parasite
gimp_param_spec_rgb_array
gimp_param_spec_uint8_array
gimp_param_spec_unit
gimp_param_spec_value_array
gimp_param_uint8_array_get_type
gimp_param_unit_get_type
gimp_param_value_array_get_type
gimp_parasite_compare
@ -184,7 +182,6 @@ EXPORTS
gimp_type_get_translation_domain
gimp_type_set_translation_context
gimp_type_set_translation_domain
gimp_uint8_array_get_type
gimp_unit_format_string
gimp_unit_get_abbreviation
gimp_unit_get_deletion_flag
@ -223,12 +220,10 @@ EXPORTS
gimp_value_dup_int32_array
gimp_value_dup_object_array
gimp_value_dup_rgb_array
gimp_value_dup_uint8_array
gimp_value_get_float_array
gimp_value_get_int32_array
gimp_value_get_object_array
gimp_value_get_rgb_array
gimp_value_get_uint8_array
gimp_value_set_float_array
gimp_value_set_int32_array
gimp_value_set_object_array
@ -237,13 +232,10 @@ EXPORTS
gimp_value_set_static_int32_array
gimp_value_set_static_object_array
gimp_value_set_static_rgb_array
gimp_value_set_static_uint8_array
gimp_value_set_uint8_array
gimp_value_take_float_array
gimp_value_take_int32_array
gimp_value_take_object_array
gimp_value_take_rgb_array
gimp_value_take_uint8_array
gimp_vectors_stroke_type_get_type
gimp_wire_clear_error
gimp_wire_destroy

View file

@ -254,173 +254,6 @@ gimp_value_take_array (GValue *value,
}
/*
* GIMP_TYPE_UINT8_ARRAY
*/
typedef GimpArray GimpUint8Array;
G_DEFINE_BOXED_TYPE (GimpUint8Array, gimp_uint8_array, gimp_array_copy, gimp_array_free)
/*
* GIMP_TYPE_PARAM_UINT8_ARRAY
*/
static void gimp_param_uint8_array_class_init (GParamSpecClass *klass);
static void gimp_param_uint8_array_init (GParamSpec *pspec);
GType
gimp_param_uint8_array_get_type (void)
{
static GType type = 0;
if (! type)
{
const GTypeInfo info =
{
sizeof (GParamSpecClass),
NULL, NULL,
(GClassInitFunc) gimp_param_uint8_array_class_init,
NULL, NULL,
sizeof (GimpParamSpecUInt8Array),
0,
(GInstanceInitFunc) gimp_param_uint8_array_init
};
type = g_type_register_static (GIMP_TYPE_PARAM_ARRAY,
"GimpParamUInt8Array", &info, 0);
}
return type;
}
static void
gimp_param_uint8_array_class_init (GParamSpecClass *klass)
{
klass->value_type = GIMP_TYPE_UINT8_ARRAY;
}
static void
gimp_param_uint8_array_init (GParamSpec *pspec)
{
}
/**
* gimp_param_spec_uint8_array:
* @name: Canonical name of the property specified.
* @nick: Nick name of the property specified.
* @blurb: Description of the property specified.
* @flags: Flags for the property specified.
*
* Creates a new #GimpParamSpecUInt8Array specifying a
* #GIMP_TYPE_UINT8_ARRAY property.
*
* See g_param_spec_internal() for details on property names.
*
* Returns: (transfer full): The newly created #GimpParamSpecUInt8Array.
*
* Since: 3.0
**/
GParamSpec *
gimp_param_spec_uint8_array (const gchar *name,
const gchar *nick,
const gchar *blurb,
GParamFlags flags)
{
GimpParamSpecArray *array_spec;
array_spec = g_param_spec_internal (GIMP_TYPE_PARAM_UINT8_ARRAY,
name, nick, blurb, flags);
return G_PARAM_SPEC (array_spec);
}
/**
* gimp_value_get_uint8_array:
* @value: A valid value of type %GIMP_TYPE_UINT8_ARRAY
*
* Gets the contents of a %GIMP_TYPE_UINT8_ARRAY #GValue
*
* Returns: (transfer none) (array): The contents of @value
*/
const guint8 *
gimp_value_get_uint8_array (const GValue *value)
{
g_return_val_if_fail (GIMP_VALUE_HOLDS_UINT8_ARRAY (value), NULL);
return gimp_value_get_array (value);
}
/**
* gimp_value_dup_uint8_array:
* @value: A valid value of type %GIMP_TYPE_UINT8_ARRAY
*
* Gets the contents of a %GIMP_TYPE_UINT8_ARRAY #GValue
*
* Returns: (transfer full) (array): The contents of @value
*/
guint8 *
gimp_value_dup_uint8_array (const GValue *value)
{
g_return_val_if_fail (GIMP_VALUE_HOLDS_UINT8_ARRAY (value), NULL);
return gimp_value_dup_array (value);
}
/**
* gimp_value_set_uint8_array:
* @value: A valid value of type %GIMP_TYPE_UINT8_ARRAY
* @data: (array length=length): A #guint8 array
* @length: The number of elements in @data
*
* Sets the contents of @value to @data.
*/
void
gimp_value_set_uint8_array (GValue *value,
const guint8 *data,
gsize length)
{
g_return_if_fail (GIMP_VALUE_HOLDS_UINT8_ARRAY (value));
gimp_value_set_array (value, data, length);
}
/**
* gimp_value_set_static_uint8_array:
* @value: A valid value of type %GIMP_TYPE_UINT8_ARRAY
* @data: (array length=length): A #guint8 array
* @length: The number of elements in @data
*
* Sets the contents of @value to @data, without copying the data.
*/
void
gimp_value_set_static_uint8_array (GValue *value,
const guint8 *data,
gsize length)
{
g_return_if_fail (GIMP_VALUE_HOLDS_UINT8_ARRAY (value));
gimp_value_set_static_array (value, data, length);
}
/**
* gimp_value_take_uint8_array:
* @value: A valid value of type %GIMP_TYPE_UINT8_ARRAY
* @data: (transfer full) (array length=length): A #guint8 array
* @length: The number of elements in @data
*
* Sets the contents of @value to @data, and takes ownership of @data.
*/
void
gimp_value_take_uint8_array (GValue *value,
guint8 *data,
gsize length)
{
g_return_if_fail (GIMP_VALUE_HOLDS_UINT8_ARRAY (value));
gimp_value_take_array (value, data, length);
}
/*
* GIMP_TYPE_INT32_ARRAY
*/

View file

@ -128,51 +128,6 @@ GParamSpec * gimp_param_spec_array (const gchar *name,
GParamFlags flags);
/*
* GIMP_TYPE_UINT8_ARRAY
*/
#define GIMP_TYPE_UINT8_ARRAY (gimp_uint8_array_get_type ())
#define GIMP_VALUE_HOLDS_UINT8_ARRAY(value) (G_TYPE_CHECK_VALUE_TYPE ((value), GIMP_TYPE_UINT8_ARRAY))
GType gimp_uint8_array_get_type (void) G_GNUC_CONST;
/*
* GIMP_TYPE_PARAM_UINT8_ARRAY
*/
#define GIMP_TYPE_PARAM_UINT8_ARRAY (gimp_param_uint8_array_get_type ())
#define GIMP_PARAM_SPEC_UINT8_ARRAY(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_UINT8_ARRAY, GimpParamSpecUInt8Array))
#define GIMP_IS_PARAM_SPEC_UINT8_ARRAY(pspec) (G_TYPE_CHECK_INSTANCE_TYPE ((pspec), GIMP_TYPE_PARAM_UINT8_ARRAY))
typedef struct _GimpParamSpecUInt8Array GimpParamSpecUInt8Array;
struct _GimpParamSpecUInt8Array
{
GimpParamSpecArray parent_instance;
};
GType gimp_param_uint8_array_get_type (void) G_GNUC_CONST;
GParamSpec * gimp_param_spec_uint8_array (const gchar *name,
const gchar *nick,
const gchar *blurb,
GParamFlags flags);
const guint8 * gimp_value_get_uint8_array (const GValue *value);
guint8 * gimp_value_dup_uint8_array (const GValue *value);
void gimp_value_set_uint8_array (GValue *value,
const guint8 *data,
gsize length);
void gimp_value_set_static_uint8_array (GValue *value,
const guint8 *data,
gsize length);
void gimp_value_take_uint8_array (GValue *value,
guint8 *data,
gsize length);
/*
* GIMP_TYPE_INT32_ARRAY
*/

View file

@ -75,7 +75,7 @@ GParamSpec * gimp_param_spec_parasite (const gchar *name,
* @name: the parasite name, USE A UNIQUE PREFIX
* @flags: the parasite flags, like save in XCF etc.
* @size: the parasite size in bytes
* @data: the parasite data, the owner os the parasite is responsible
* @data: (array length=size): the parasite data, the owner os the parasite is responsible
* for tracking byte order and internal structure
**/
struct _GimpParasite

View file

@ -1638,6 +1638,26 @@ _gp_params_read (GIOChannel *channel,
}
break;
case GP_PARAM_TYPE_BYTES:
{
guint32 data_len;
guint8* data;
if (! _gimp_wire_read_int32 (channel, &data_len, 1, user_data))
goto cleanup;
data = g_new0 (guint8, data_len);
if (! _gimp_wire_read_int8 (channel, data, data_len, user_data))
{
g_free (data);
goto cleanup;
}
(*params)[i].data.d_bytes = g_bytes_new_take (data, data_len);
}
break;
case GP_PARAM_TYPE_STRV:
{
guint32 size;
@ -1803,6 +1823,22 @@ _gp_params_write (GIOChannel *channel,
return;
break;
case GP_PARAM_TYPE_BYTES:
{
const guint8 *bytes = NULL;
guint32 size = 0;
if (params[i].data.d_bytes)
{
bytes = g_bytes_get_data (params[i].data.d_bytes, (gsize *) &size);
}
if (! _gimp_wire_write_int32 (channel, &size, 1, user_data) ||
! _gimp_wire_write_int8 (channel, bytes, size, user_data))
return;
}
break;
case GP_PARAM_TYPE_STRV:
{
gint size;
@ -1901,6 +1937,10 @@ _gp_params_destroy (GPParam *params,
g_free (params[i].data.d_array.data);
break;
case GP_PARAM_TYPE_BYTES:
g_bytes_unref (params[i].data.d_bytes);
break;
case GP_PARAM_TYPE_STRV:
g_strfreev (params[i].data.d_strv);
break;

View file

@ -66,6 +66,7 @@ typedef enum
GP_PARAM_TYPE_FLOAT,
GP_PARAM_TYPE_STRING,
GP_PARAM_TYPE_STRV,
GP_PARAM_TYPE_BYTES,
GP_PARAM_TYPE_FILE,
GP_PARAM_TYPE_COLOR,
GP_PARAM_TYPE_PARASITE,
@ -250,6 +251,7 @@ struct _GPParam
gdouble d_float;
gchar *d_string;
gchar **d_strv;
GBytes *d_bytes;
GimpRGB d_color;
GimpParasite d_parasite;
GPParamArray d_array;