gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:33 24 4
gpt4 key购买 nike

最小工作示例

我正在尝试使用 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 插件中,使其保持完整?

最佳答案

在 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 个测试中可用数组内容的不同数量。)

关于scheme - 通过 Script-Fu 将数组传递给 GIMP 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43099467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com