我正在尝试了解 ImageMagick 和 C 的工作原理,因此我正在尝试将图像加载到磁盘上,液体调整其大小并将其保存回磁盘,但我似乎无法使其工作。该程序不一定会卡住。 (控制台窗口中的插入符闪烁?)
但它也没有任何进一步的进展。这是我第一次使用 C 编程,我只是在关注 MagickCore 文档中的内容。我没有看到问题?
这是我的代码:
int main(void)
{
Image
*image;
ImageInfo
*imageinfo;
ExceptionInfo
*ei;
char *path = "./";
MagickCoreGenesis(path, MagickTrue);
ei = AcquireExceptionInfo();
// Load the image
imageinfo = AcquireImageInfo();
(void)CopyMagickString(imageinfo->filename, "image.png", MaxTextExtent);
//(imageinfo->filename, _countof("image.png"), "image.png");
image = ReadImage(imageinfo, ei);
if (ei->severity != UndefinedException)
CatchException(ei);
if (image == (Image *)NULL)
exit(1);
size_t
c, r;
PointInfo pi;
pi = image->resolution;
c = pi.x / 100 * 50;
r = pi.y / 100 * 50;
image = LiquidRescaleImage(image, c, r, 0, 0, ei);
strcpy_s(imageinfo->filename, _countof("img.png"), "img.png");
WriteImage(imageinfo, image, ei);
return 0;
}
最佳答案
我认为您使用 ImageMagick 来学习 C 很棒,但我强烈建议您使用 MagickWand
库而不是 MagickCore
。 MagickCore
是非常底层的东西,您将花费大部分时间分配/取消分配内存、复制缓冲区和管理堆/堆栈。 MagickWand
是处理繁忙工作的 C-API,可让您专注于手头的任务。这是使用 MagickWand
后的代码。
#include <MagickWand/MagickWand.h>
int main(int argc, const char * argv[]) {
MagickWandGenesis();
MagickBooleanType ok;
MagickWand * wand;
wand = NewMagickWand();
// Error handle of wand is null.
ok = MagickReadImage(wand, "image.png");
// Error handle if not `ok'.
size_t
columns,
rows;
columns = MagickGetImageWidth(wand) / 100 * 50;
rows = MagickGetImageHeight(wand) / 100 * 50;
ok = MagickLiquidRescaleImage(wand, columns, rows, 0, 0);
// Error handle if not `ok'.
MagickWriteImage(wand, "img.png");
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
更干净。
现在是您提供的 MagickCore
代码。我相信需要进行一些调试才能确定问题。尝试以下...
Image
*image,
*newImage;
// ... Skip ahead ...
newImage = LiquidRescaleImage(image, c, r, 0, 0, ei);
if (newImage == (Image *)NULL) {
// Error handle
} else {
// Only replace `image' if LiquidRescaleImage was successful.
ReplaceImageInList(&newImage, image);
}
同时调试 c
和 r
的值。我推测 pi.x
和 pi.y
是零值。
But it doesn't progress any further either.
imageinfo->filename
中可能缺少 NULL 终止符。其他要用调试器检查的东西。
关于c - MagickCore - 卡在 ReadImage 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279807/