通过Script-Fu将数组传递给GIMP模块

时间:2017-03-29 16:44:01

标签: scheme lisp gimp script-fu

最小工作示例

我试图使用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 <jane@doe.name>",
        "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

我在Debian 9盒子上这样做。 当我尝试使用Script-Fu界面运行插件时:

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

Calling the plugin via Script-Fu

使用以下内容创建名为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时,插件按预期工作:

Hit-or-Miss via GUI #1 Hit-or-Miss via GUI #1

但是,当我使用以下Script-Fu脚本时:

(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)

插件不会产生预期的结果:

Hit-or-Miss via Script-Fu #1 Hit-or-Miss via Script-Fu #1

我修改了插件,看看实际差异是什么:

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

正如所料。运行Script-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];
                                }

运行Script-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 

对我而言,这表示该数​​组的传递格式与插件所期望的格式不同。如何通过Script-Fu将数组传递到GIMP插件中,以使其保持不变?

1 个答案:

答案 0 :(得分:1)

在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)或元素大小(7),解释了2次测试中可用数组内容的不同数量。)