最小工作示例
我试图使用Scheme Script-Fu interface将一个整数数组传递给GIMP插件我有一个名为main.c的源代码文件,其中包含以下内容:

#include <libgimp/gimp.h>
#include <stdlib.h>
#include <stdio.h>

static void query (void);

static void run (
    const gchar *name,
    gint nparams,
    const GimpParam *param,
    gint *nreturn_vals,
    GimpParam **return_vals
);

const GimpPlugInInfo PLUG_IN_INFO = {
    NULL,  /* init_proc  */
    NULL,  /* quit_proc  */
    query, /* query_proc */
    run,   /* run_proc   */
};

MAIN ()

static void query (void)
{
    static GimpParamDef args[] = {
    {
      GIMP_PDB_INT32,
      "run-mode",
      "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }"
    },
    {
      GIMP_PDB_INT8ARRAY,
      "an array",
      ""
      "an array"
      }
    };

    gimp_install_procedure (
        "my-test-plugin",
        "My test plug-in",
        "Description",
        "Jane Doe <[email protected]>",
        "Copyright (C) Jane Doe\n"
        "http://foo.bar",
        "2017",
        g_strconcat("My test plug-in", "...", NULL),
        "RGB*, GRAY*",
        GIMP_PLUGIN,
        G_N_ELEMENTS (args),
        0,
        args,
        NULL
    );
}

static void run (
    const gchar *name,
    gint nparams,
    const GimpParam *param,
    gint *nreturn_vals,
    GimpParam **return_vals)
{
    static GimpParam value;
    value.type = GIMP_PDB_STATUS;
    value.data.d_status = GIMP_PDB_SUCCESS;
    *nreturn_vals = 1;
    *return_vals = &value;

    FILE* out = fopen("mylog.log", "w");
    fprintf(out, "Parameter #0:\t%d\nParameter #1:\t", param[0].data.d_int32);
    for (int i = 0; i < 16; ++i)
        fprintf(out, "%d ", param[1].data.d_int8array[i]);
    fprintf(out, "\n");
    fclose(out);
}

我编译并安装文件作为GIMP插件,如下所示:
sudo apt install libgimp2.0-dev
gcc `gimptool-2.0 --cflags --libs` main.c
gimptool-2.0 --install-bin ./a.out

我在Debian9上做这个。
当我尝试使用脚本Fu接口运行插件时:
(my-test-plugin 1 #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))

scheme - 通过Script-Fu将数组传递给GIMP模块-LMLPHP
将创建名为mylog.log的文件,其中包含以下内容:
Parameter #0:   1
Parameter #1:   1 0 0 0 0 0 0 0 64 201 246 126 174 85 0 0

鉴于以下为预期内容:
Parameter #0:   1
Parameter #1:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

真实世界的版本
我试图使用the gimp-plugin-morphop GIMP plugin使用Scheme Script-Fu interface当我使用GUI时,该插件按预期工作:
scheme - 通过Script-Fu将数组传递给GIMP模块-LMLPHP
scheme - 通过Script-Fu将数组传递给GIMP模块-LMLPHP
但是,当我使用以下脚本Fu Script时:
(plug-in-morphop RUN-NONINTERACTIVE 1 2 6 7
  #(-1 -1 -1 -1 -1 -1 -1
    -1  1 -1 -1 -1  1 -1
    -1 -1  0  0  0 -1 -1
    -1 -1  0  0  0 -1 -1
    -1 -1  0  0  0 -1 -1
    -1  1 -1 -1 -1  1 -1
    -1 -1 -1 -1 -1 -1 -1) 24 2)

插件未生成预期结果:
scheme - 通过Script-Fu将数组传递给GIMP模块-LMLPHP
scheme - 通过Script-Fu将数组传递给GIMP模块-LMLPHP
我修改了插件,看看实际的区别是什么:
diff --git a/src/morphop-algorithms.c b/src/morphop-algorithms.c
index b927ba3..9dff320 100644
--- a/src/morphop-algorithms.c
+++ b/src/morphop-algorithms.c
@@ -166,7 +166,16 @@ void start_operation(GimpDrawable *drawable, GimpPreview *preview, MorphOpSettin
                GimpPixelRgn src_rgn_c, dst_rgn_c;
                guchar* src_preview_c = NULL, *dst_preview_c = NULL;

+               FILE* log = fopen("morphop-operation.log", "w");
                int i, j;
+               for(i = 0; i < STRELEM_DEFAULT_SIZE; i++) {
+                       for(j = 0; j < STRELEM_DEFAULT_SIZE; j++) {
+                               fprintf(log, "%d ", settings.element.matrix[i][j]);
+                       }
+                       fprintf(log, "\n");
+               }
+               fclose(log);
+
                // white structuring element
                for(i = 0; i < STRELEM_DEFAULT_SIZE; i++) {
                        for(j = 0; j < STRELEM_DEFAULT_SIZE; j++) {

通过GUI使用模块后,morphop-operation.log文件包含以下文本:
-1 -1 -1 -1 -1 -1 -1
-1 1 -1 -1 -1 1 -1
-1 -1 0 0 0 -1 -1
-1 -1 0 0 0 -1 -1
-1 -1 0 0 0 -1 -1
-1 1 -1 -1 -1 1 -1
-1 -1 -1 -1 -1 -1 -1

如预期运行脚本Fu代码后,morphop-operation.log文件包含以下文本:
-1 0 0 86 0 103 114
-1 64 0 0 0 45 112
-1 41 64 0 0 105 104
-1 8 42 33 0 110 111
-1 95 8 0 112 45 112
-1 36 95 0 108 109 0
-1 86 36 0 117 111 32

其中,每行最右边的六个整数似乎是随机垃圾,每次调用时都会更改因为settings.element.matrix已经是接收数组的内部表示,所以我进一步修改了插件以直接显示接收数组:
diff --git a/src/morphop.c b/src/morphop.c
index 3895770..cb814b6 100644
--- a/src/morphop.c
+++ b/src/morphop.c
@@ -150,9 +150,16 @@ static void run (
                                }

                                msettings.operator = param[3].data.d_int32;
-
+
+                               FILE* log = fopen("morphop-init.log", "w");
                                int i;
                                for (i = 0; i < STRELEM_DEFAULT_SIZE * STRELEM_DEFAULT_SIZE; i++) {
+                                       fprintf(log, "%d ", param[5].data.d_int8array[i]);
+                               }
+                               fprintf(log, "\n");
+                               fclose(log);
+
+                               for (i = 0; i < STRELEM_DEFAULT_SIZE * STRELEM_DEFAULT_SIZE; i++) {
                                        msettings.element.matrix[i % STRELEM_DEFAULT_SIZE][(int)ceil((i + 1.0) / STRELEM_DEFAULT_SIZE) - 1] = param[5].data.d_int8array[i];
                                }

运行脚本Fu代码后,morphop-init.log文件包含以下文本:
255 255 255 255 255 255 255 0 64 169 248 158 193 85 0 0 64 170 248 158 193 85 0 0 33 0 0 0 0 0 0 0 112 108 117 103 45 105 110 45 109 111 114 112 104 111 112 0 32

第八个之后的值似乎是随机垃圾,尽管有些值在几次运行中保持不变:
255 255 255 255 255 255 255 0 64 89 10 89 79 86 0 0 64 90 10 89 79 86 0 0 33 0 0 0 0 0 0 0 112 108 117 103 45 105 110 45 109 111 114 112 104 111 112 0 32

255 255 255 255 255 255 255 0 64 217 110 172 167 85 0 0 64 218 110 172 167 85 0 0 33 0 0 0 0 0 0 0 112 108 117 103 45 105 110 45 109 111 114 112 104 111 112 0 32

对我来说,这意味着数组以插件所期望的不同格式传递如何通过脚本Fu将数组传递到GIMP插件中,使其保持完整?

最佳答案

在API中,它是necessary to pass the size of the array before the array

--- /tmp/plugin.orig    2017-04-02 16:45:34.902108610 +0200
+++ /tmp/plugin.c   2017-04-02 16:43:26.510105281 +0200
@@ -30,6 +30,11 @@
       "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }"
     },
     {
+      GIMP_PDB_INT32,
+      "array-size",
+      "The array bellow"
+    },
+    {
       GIMP_PDB_INT8ARRAY,
       "an array",
       ""
@@ -69,9 +74,9 @@
     *return_vals = &value;

     FILE* out = fopen("mylog.log", "w");
-    fprintf(out, "Parameter #0:\t%d\nParameter #1:\t", param[0].data.d_int32);
+    fprintf(out, "Parameter #0:\t%d\nParameter #2:\t", param[0].data.d_int32);
     for (int i = 0; i < 16; ++i)
-        fprintf(out, "%d ", param[1].data.d_int8array[i]);
+        fprintf(out, "%d ", param[2].data.d_int8array[i]);
     fprintf(out, "\n");
     fclose(out);
 }

然后:
 > (my-test-plugin 1 16 #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16))
 (#t)

结果:
$ cat mylog.log
Parameter #0:   1
Parameter #2:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

通过使大小大于长度,可以验证参数是否隐式链接:
> (my-test-plugin 1 17 #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))
Error: ( : 1) INT8 vector (argument 3) for function my-test-plugin has
size of 15 but expected size of 17

(前面的参数RUN-NONINTERACTIVE(1)或element size(7)解释了2个测试中可用数组内容的不同数量。)

10-05 18:08