简体   繁体   English

MagickCommandGenesis始终返回MagickFalse

[英]MagickCommandGenesis always returns MagickFalse

There is really not much relevant info, and official manual makes me cry. 确实没有太多相关信息,而且官方手册让我哭了。 I'm trying to run custom scripts via MagickCommandGenesis because I gave up in attempt to convert some scripts into clean c code with API usage. 我试图通过MagickCommandGenesis运行自定义脚本,因为我放弃了尝试将某些脚本转换为使用API​​的干净C代码。 However, this piece of code: 但是,这段代码:

    //char* cmdargs[] = { "convert", "-rotate", "-90" };
    char* cmdargs[] = { "-rotate", "-90" };
    int argcount = 2;

    ExceptionInfo* e = AcquireExceptionInfo();
    GetExceptionInfo(e);
    MagickBooleanType cmdres = MagickCommandGenesis(wand->image_info, ConvertImageCommand, argcount, cmdargs, NULL, e);

    if (e->severity != UndefinedException || cmdres == MagickFalse) {
        syslog(LOG_NOTICE, "mwb::error:%s", e->severity != UndefinedException ? "derp" : "undefined");
    }

always writes "undefined", ie MagickCommandGenesis returned false, but not throwed exception. 总是写“未定义”,即MagickCommandGenesis返回false,但未引发异常。

I can't use temporary files to read or write from, I want to do all operations in memory, therefore wand is read via MagickReadImageBlob. 我无法使用临时文件进行读取或写入,我想在内存中进行所有操作,因此通过MagickReadImageBlob读取了wand Blob is absolutely valid, I can do any API operations and finally draw image on screen, the only thing that not works is command run. Blob绝对有效,我可以执行任何API操作,最后在屏幕上绘制图像,唯一不起作用的是命令运行。

The only one relevant question I found is here but it not so useful. 我发现的唯一一个相关问题在这里,但没有那么有用。 Even more, OP used read and write in files and still got the error. 更何况,OP使用读取和写入文件的方式仍然出现错误。 There is answer marked as accepted, but it suggest to use MagickCommandGenesis , what I do already. 有标记为已接受的答案,但建议使用MagickCommandGenesis ,我已经做了。

Upd: UPD:

I figured out that image_info member of MagickWand actually has nothing (probably it just initialized with defaults and not used by MagickWand at all). 我发现image_info成员实际上没有任何内容(可能只是使用默认值初始化,而image_info根本没有使用过)。 So now I looking for proper way to instantiate ImageInfo structure from memory blob. 所以现在我在寻找从内存Blob实例化ImageInfo结构的正确方法。 That's what I tried: 那就是我尝试过的:

    char* cmdargs[] = { "-rotate", "-90" };
    int argcount = 2;

    ExceptionInfo* e = AcquireExceptionInfo();
    GetExceptionInfo(e);

    size_t len;
    u_char* blob = MagickGetImageBlob(wand, &len);
    ImageInfo* info = CloneImageInfo(NULL);
    SetImageInfoBlob(info, blob, len);

    MagickBooleanType cmdres = MagickCommandGenesis(info, ConvertImageCommand, argcount, cmdargs, NULL, e);

    MagickWand* target = NewMagickWand();
    MagickReadImageBlob(target, info->blob, info->length);
    // DestroyMagickWand(wand);
    wand = target;

    syslog(LOG_NOTICE, "mwb::blob is %zu", MagickGetImageWidth(target));

    if (e->severity != UndefinedException || cmdres == MagickFalse) {
        syslog(LOG_NOTICE, "mwb::error:%s", e->severity != UndefinedException ? "derp" : "undefined");
    }

Unfortunately, I still get "undefined" error, so MagickCommandGenesis still returns false, but don't throws exception. 不幸的是,我仍然收到“未定义”错误,因此MagickCommandGenesis仍然返回false,但不会引发异常。

Two items to consider. 要考虑的两个项目。

First: MagickCommandGenesis is expecting a full, not partial, command. 首先: MagickCommandGenesis期望完整而不是部分命令。 Example

 convert source.png -rotate -90 out.png

Second: ImageInfo pointer should be newly allocate memory for the command process to act against. 第二: ImageInfo指针应该是新分配的内存,以使命令进程可以对其进行操作。 Use AcquireImageInfo() , as giving wand->image_info will result in undefined behavior. 使用AcquireImageInfo() ,因为给wand->image_info将导致未定义的行为。

Ideally you should just use C-API methods exclusively, but you can mix existing MagickWand instances with MagickCommand by leveraging the memory program register protocol mpr: . 理想情况下,你应该只使用C-API方法完全,但你可以混合现有MagickWand与实例MagickCommand通过利用存储程序寄存器协议mpr:

MagickWand *wand = NewMagickWand();
MagickReadImage(wand, "rose:");
MagickNegateImage(wand, 1); // For example, negate image
// Hand-off image to memory program register
MagickWriteImage(wand, "mpr:my_label");
// Build full command
char* cmdargs[] = {"convert",
                   "mpr:my_label",
                   "-rotate",
                   "-90",
                   "output.png", // Or to another mpr:
                   NULL};
 int argcount = 5;
 // Allocate memory for MagickCommand
 ImageInfo * info = AcquireImageInfo();
 ExceptionInfo* e = AcquireExceptionInfo();
 // Execute command
 MagickCommandGenesis(info, ConvertImageCommand, argcount, cmdargs, NULL, e);
 wand = DestroyMagickWand(wand);
 /* Error handling omitted */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM