RSS/Atom feed Twitter
Site is read-only, email is disabled

Problem returning ARRAYs in plugins

This discussion is connected to the gimp-developer-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

7 of 8 messages available
Toggle history

Please log in to manage your subscriptions.

Problem returning ARRAYs in plugins nserrano@iti.upv.es 14 Nov 10:34
  Problem returning ARRAYs in plugins Sven Neumann 15 Nov 09:25
   Problem returning ARRAYs in plugins Nicolás Serrano Martínez Santos 15 Nov 10:29
Problem returning ARRAYs in plugins Laurent G. 17 Nov 23:59
  Problem returning ARRAYs in plugins Kevin Cozens 18 Nov 07:21
   Problem returning ARRAYs in plugins Laurent G. 19 Nov 19:44
mailman.3.1195416004.23195.... 07 Oct 20:26
  Problem returning ARRAYs in plugins nserrano@iti.upv.es 19 Nov 09:27
nserrano@iti.upv.es
2007-11-14 10:34:53 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Hello, I'm developing a plugin for GIMP and I ended using "GIMP_PDB_INT8ARRAY" as a return value. My problem is that when you use an *ARRAY as return value the previous return value has to be the number of elements of the array. As you can see in the source code of gimp:

/* extracted form libgimpbase/gimpprotocol.c */

case GIMP_PDB_INT8ARRAY: (*params)[i-1].data.d_int32 = MAX (0, (*params)[i-1].data.d_int32); (*params)[i].data.d_int8array = g_new (guint8, (*params)[i-1].data.d_int32); if (! _gimp_wire_read_int8 (channel, (*params)[i].data.d_int8array, (*params)[i-1].data.d_int32, user_data)) {
g_free ((*params)[i].data.d_int8array); goto cleanup;
}
break;

I think that if a newbie developing plugins uses this value(an array type) he will miss that he has to add the size before of it. I have miss a few a days of development due to this problem. I would like that you add this information to the API or the tutorial of "Kevin Corner" so others developers don't go thought this problem.

Sorry if it's explained in some other place and i have just missed it. Sorry for my English too...

Nico

------------------------------------------------- This mail sent through IMP: http://horde.org/imp/

Sven Neumann
2007-11-15 09:25:14 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Hi,

On Wed, 2007-11-14 at 10:34 +0100, nserrano@iti.upv.es wrote:

I think that if a newbie developing plugins uses this value(an array type) he will miss that he has to add the size before of it. I have miss a few a days of development due to this problem. I would like that you add this information to the API

We can surely add it. I am just wondering where this information should go. Where exactly in the API reference manual would you have expected this information?

Sven

Nicolás Serrano Martínez Santos
2007-11-15 10:29:18 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

El jue, 15-11-2007 a las 09:25 +0100, Sven Neumann escribió:

Hi,

On Wed, 2007-11-14 at 10:34 +0100, nserrano@iti.upv.es wrote:

I think that if a newbie developing plugins uses this value(an array type) he will miss that he has to add the size before of it. I have miss a few a days of development due to this problem. I would like that you add this information to the API

We can surely add it. I am just wondering where this information should go. Where exactly in the API reference manual would you have expected this information?

Sven

Maybe a short remark at the array types on http://developer.gimp.org/api/2.0/libgimp/libgimp-gimp.html#GimpParamData or
http://developer.gimp.org/api/2.0/libgimpbase/libgimpbase-gimpbaseenums.html#GimpPDBArgType

I ended visiting these links when looking for the possible return types of a GIMP plugin.

Another option would be add this information on the "gimp_install_procedure" function. Like "WARNING you should add the size of the array as a new parameter before an array parameter".

Nico

Laurent G.
2007-11-17 23:59:29 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Hi,
I have been fighting the evening long, trying to give data through such an array.
I saw this post from this month on the archive and registered to ask the writer of the advice if he could send us (or just me) an example of a working returning of array.
I followed the "trick" of inserting an int32 before the array in args, but I keep getting my plugin to explode gimp on my calling to my simple procedure, at least while calling from python console. I can not figure out where I made the first mistake. So if I could compare my code with a working one, I could detect my error(s). Thanks in advance.

Laurent G.

Kevin Cozens
2007-11-18 07:21:59 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Laurent G. wrote:

I have been fighting the evening long, trying to give data through such an array.

[snip]

I followed the "trick" of inserting an int32 before the array in args, but I keep getting my plugin to explode gimp on my calling to my simple procedure, at least while calling from python console. I can not figure out where I made the first mistake. So if I could compare my code with a working one, I could detect my error(s).

It would be better if you could post your script somewhere so it can be looked at. It should not be possible for a script or plug-in to "explode GIMP". If your script is able to do that, having the script available for review will allow the problem to be fixed so that other scripts won't be able to break GIMP.

If you are passing an array to GIMP the argument right before you pass the array is used to tell GIMP the number of items in the array. It is assumed that the array contains at least that number of items in it. If you are telling GIMP that the array has 10 items in it (for example) but you have not put at least 10 items in to the array before passing it to GIMP you are going to run in to problems.

nserrano@iti.upv.es
2007-11-19 09:27:34 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Message: 1
Date: Sat, 17 Nov 2007 23:59:29 +0100 From: "Laurent G."
Subject: [Gimp-developer] Problem returning ARRAYs in plugins To: gimp-developer@lists.XCF.Berkeley.EDU Message-ID:
Content-Type: text/plain; charset=ISO-8859-1

Hi, I have been fighting the evening long, trying to give data through such an array.
I saw this post from this month on the archive and registered to ask the writer of the advice if he could send us (or just me) an example of a working returning of array.
I followed the "trick" of inserting an int32 before the array in args, but I keep getting my plugin to explode gimp on my calling to my simple procedure, at least while calling from python console. I can not figure out where I made the first mistake. So if I could compare my code with a working one, I could detect my error(s). Thanks in advance.

Laurent G.

Hi,

Here is some of the code that I use in order to return some int8 into an array.

gint w,h, ***fea_vector;

if (strcmp (name, PLUGIN_NAME) == 0){ vector = (guint8 **) malloc(sizeof(guint8 *)); if(get_some_pixels(image_ID,drawable,&w,&h,fea_vector)){/*put some vals*/ *nreturn_vals=5;/*in fea_vector*/ values[1].type= GIMP_PDB_INT32;
values[1].data.d_int32= h;
values[2].type= GIMP_PDB_INT32;
values[2].data.d_int32= (gint32)w; values[3].type= GIMP_PDB_INT32;
values[3].data.d_int32= (gint32) w * h; values[4].type= GIMP_PDB_INT8ARRAY; values[4].data.d_int8array = *fea_vector;

I don't use Python for writing plugins but I strongly recommend you doing some debug. You can get to the code of "_gimp_wire_write" in libgimp/gimpprotocol.c where the plugin surely crash while writing the values of the INT8ARRAY (at least, it was here in my case).

------------------------------------------------- This mail sent through IMP: http://horde.org/imp/

Laurent G.
2007-11-19 19:44:29 UTC (over 16 years ago)

Problem returning ARRAYs in plugins

Kevin Cozens a écrit :

Laurent G. wrote:

I have been fighting the evening long, trying to give data through such an array.

[snip]

I followed the "trick" of inserting an int32 before the array in args, but I keep getting my plugin to explode gimp on my calling to my simple procedure, at least while calling from python console. I can not figure out where I made the first mistake. So if I could compare my code with a working one, I could detect my error(s).

It would be better if you could post your script somewhere so it can be looked at. It should not be possible for a script or plug-in to "explode GIMP". If your script is able to do that, having the script available for review will allow the problem to be fixed so that other scripts won't be able to break GIMP.

If you are passing an array to GIMP the argument right before you pass the array is used to tell GIMP the number of items in the array. It is assumed that the array contains at least that number of items in it. If you are telling GIMP that the array has 10 items in it (for example) but you have not put at least 10 items in to the array before passing it to GIMP you are going to run in to problems.

Hi
I don't know why it blows, but
1) Monkey-trying, I got it to work
2) I got the minimum meaningful difference between success and failure :

I used the plugin template. So here we are:

static void
query (void)
{
gchar *help_path;
gchar *help_uri;

static GimpParamDef cc_in_args[] = {
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" }, { GIMP_PDB_IMAGE, "image", "Input image" }, { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }, };
static GimpParamDef cc_out_args[] = {
{ GIMP_PDB_STATUS, "status","Status" }, { GIMP_PDB_INT32, "nb_used_colors","Number of colors with at least one pixel" },
//{ GIMP_PDB_INT32, "next_array_size","Number of entries in next (256)" },
{ GIMP_PDB_INT32ARRAY,"n_pix_by_cmap_entries","Number of pixels by colormap entry"},
};

gimp_plugin_domain_register (PLUGIN_NAME, LOCALEDIR);

help_path = g_build_filename (DATADIR, "help", NULL); help_uri = g_filename_to_uri (help_path, NULL, NULL); g_free (help_path);

gimp_plugin_help_register ("http://gna.org/boundary_shrinker/help", help_uri);

gimp_install_procedure (ALIEN_PROCEDURE_NAME, "Counts number of pixel for each colormap entries", "No help yet",
"Laurent G. ",
"Laurent G. ",
"2007-2007",
N_("Indexed Colors Usage"),
"INDEXED",
GIMP_PLUGIN,
G_N_ELEMENTS (cc_in_args), G_N_ELEMENTS (cc_out_args), cc_in_args, cc_out_args);
}

static void run (const gchar *name, gint n_params,
const GimpParam *param,
gint *nreturn_vals, GimpParam **return_vals)
{
static GimpParam values[4];
GimpDrawable *drawable;
gint32 image_ID;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;

*return_vals = values;

/* Initialize i18n support */ bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); #endif
textdomain (GETTEXT_PACKAGE);

run_mode = param[0].data.d_int32; image_ID = param[1].data.d_int32;
drawable = gimp_drawable_get(param[2].data.d_drawable);

if (strcmp (name, ALIEN_PROCEDURE_NAME) == 0) {
static gint32 colors_usage[256];
//gint32 *colors_usage = g_new(gint32, 256); *nreturn_vals = 4;
values[1].type = GIMP_PDB_INT32;
values[2].type = GIMP_PDB_INT32;
values[3].type = GIMP_PDB_INT32ARRAY;

values[2].data.d_int32 = 256L; values[3].data.d_int32array = colors_usage; gint32 nb_used = 0;

count_pixels(image_ID, drawable, &nb_used, colors_usage);

values[1].data.d_int32 = nb_used; }
else
{
status = GIMP_PDB_CALLING_ERROR;
}

values[0].type = GIMP_PDB_STATUS; values[0].data.d_status = status;
printf("status:%d\n", values[0].data.d_status); }

/* python code to call it
image = gimp.image_list()[0]
drawable = pdb.gimp_image_get_active_drawable(image) nb_used_colors, next_array_size, n_pix_by_cmap_entries = pdb.n_pixels_by_cmap_entries(image, drawable) nb_used_colors, next_array_size, n_pix_by_cmap_entries */

To have it work, I had to give length of the array just before the array, but what I missed (on don't blame me ;-) ) is that one has to do this addition in the run part, but not in the query part. The key difference between success and blow is the c++-style commentary stating "next_array_size" in cc_out_args.

Hope this helps. Regards

Laurent G.