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.

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 
(256)" },
                { GIMP_PDB_INT32ARRAY,"n_pix_by_cmap_entries","Number of pixels 
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 ("";,

        gimp_install_procedure (ALIEN_PROCEDURE_NAME,
                                                        "Counts number of pixel 
for each colormap entries",
                                                        "No help yet",
                                                        "Laurent G. <[EMAIL 
                                                        "Laurent G. <[EMAIL 
                                                        N_("Indexed Colors 
(cc_in_args), G_N_ELEMENTS (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);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        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;
                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.

Gimp-developer mailing list

Reply via email to