MagickWand(C API)
MagickWand API 是 C 编程语言与 ImageMagick 图像处理库之间推荐的接口。与 MagickCore C API 不同,MagickWand 仅使用少数几个不透明类型。可通过访问器设置或获取重要的 wand 属性。MagickWand 公开方法的说明可在 GitHub 上找到:
- Magick Wand 方法
- 设置或获取 Magick Wand 属性
- Magick Wand 图像方法
- Pixel Iterator 方法
- Pixel Wand 方法
- 图像矢量绘制
- 命令行接口
- Wand View 方法
- 已弃用的方法
- 错误与警告代码
编写好 MagickWand 程序后,按如下方式编译:
cc `MagickWand-config --cflags --cppflags` -O2 -o wand wand.c `MagickWand-config --ldflags --libs`
如果 ImageMagick 不在默认的系统路径中,请设置 PKG_CONFIG_PATH 环境变量:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
下面是一个使用 MagickWand API 的示例程序 wand.c,可助你入门。它读取一张图像,创建缩略图,并将结果写入磁盘。
#include <stdio.h>
#include <stdlib.h>
#include <MagickWand/MagickWand.h>
int main(int argc,char **argv)
{
#define ThrowWandException(wand) \
{ \
char \
*description; \
\
ExceptionType \
severity; \
\
description=MagickGetException(wand,&severity); \
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
description=(char *) MagickRelinquishMemory(description); \
exit(-1); \
}
MagickBooleanType
status;
MagickWand
*magick_wand;
if (argc != 3)
{
(void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
exit(0);
}
/*
Read an image.
*/
MagickWandGenesis();
magick_wand=NewMagickWand();
status=MagickReadImage(magick_wand,argv[1]);
if (status == MagickFalse)
ThrowWandException(magick_wand);
/*
Turn the images into a thumbnail sequence.
*/
MagickResetIterator(magick_wand);
while (MagickNextImage(magick_wand) != MagickFalse)
MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
/*
Write the image then destroy it.
*/
status=MagickWriteImages(magick_wand,argv[2],MagickTrue);
if (status == MagickFalse)
ThrowWandException(magick_wand);
magick_wand=DestroyMagickWand(magick_wand);
MagickWandTerminus();
return(0);
}
下面是另一个程序,展示了使用 MagickWand API 获取和设置图像像素的一种方法,contrast.c。它读取一张图像,应用 S 形非线性对比度控制,并将结果写入磁盘。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <MagickWand/MagickWand.h>
int main(int argc,char **argv)
{
#define SigmoidalContrast(x) \
(QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
#define ThrowWandException(wand) \
{ \
char \
*description; \
\
ExceptionType \
severity; \
\
description=MagickGetException(wand,&severity); \
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
description=(char *) MagickRelinquishMemory(description); \
exit(-1); \
}
long
y;
MagickBooleanType
status;
MagickWand
*contrast_wand,
*image_wand;
PixelInfo
pixel;
PixelIterator
*contrast_iterator,
*iterator;
PixelWand
**contrast_pixels,
**pixels;
register long
x;
unsigned long
width;
if (argc != 3)
{
(void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
exit(0);
}
/*
Read an image.
*/
MagickWandGenesis();
image_wand=NewMagickWand();
status=MagickReadImage(image_wand,argv[1]);
if (status == MagickFalse)
ThrowWandException(image_wand);
contrast_wand=CloneMagickWand(image_wand);
/*
Sigmoidal non-linearity contrast control.
*/
iterator=NewPixelIterator(image_wand);
contrast_iterator=NewPixelIterator(contrast_wand);
if ((iterator == (PixelIterator *) NULL) ||
(contrast_iterator == (PixelIterator *) NULL))
ThrowWandException(image_wand);
for (y=0; y < (long) MagickGetImageHeight(image_wand); y++)
{
pixels=PixelGetNextIteratorRow(iterator,&width);
contrast_pixels=PixelGetNextIteratorRow(contrast_iterator,&width);
if ((pixels == (PixelWand **) NULL) ||
(contrast_pixels == (PixelWand **) NULL))
break;
for (x=0; x < (long) width; x++)
{
PixelGetMagickColor(pixels[x],&pixel);
pixel.red=SigmoidalContrast(pixel.red);
pixel.green=SigmoidalContrast(pixel.green);
pixel.blue=SigmoidalContrast(pixel.blue);
pixel.index=SigmoidalContrast(pixel.index);
PixelSetPixelColor(contrast_pixels[x],&pixel);
}
(void) PixelSyncIterator(contrast_iterator);
}
if (y < (long) MagickGetImageHeight(image_wand))
ThrowWandException(image_wand);
contrast_iterator=DestroyPixelIterator(contrast_iterator);
iterator=DestroyPixelIterator(iterator);
image_wand=DestroyMagickWand(image_wand);
/*
Write the image then destroy it.
*/
status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
if (status == MagickFalse)
ThrowWandException(image_wand);
contrast_wand=DestroyMagickWand(contrast_wand);
MagickWandTerminus();
return(0);
}
现在,让我们利用 wand view 在双核或四核处理系统上并行运行该算法,执行同样的对比度增强。sigmoidal-contrast.c 模块读取一张图像,应用 S 形非线性对比度控制,并像前面的对比度增强程序一样将结果写入磁盘,但现在它以并行方式工作(假设 ImageMagick 是在启用 OpenMP 支持的情况下构建的)。
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <MagickWand/MagickWand.h>
static MagickBooleanType SigmoidalContrast(WandView *contrast_view,
const ssize_t y,const int thread_id,void *context)
{
#define SigmoidalContrast(x) \
(QuantumRange*(1.0/(1+exp(10.0*(0.5-QuantumScale*x)))-0.0066928509)*1.0092503)
PixelInfo
pixel;
PixelWand
**pixels;
RectangleInfo
extent;
register ssize_t
x;
extent=GetWandViewExtent(contrast_view);
pixels=GetWandViewPixels(contrast_view);
for (x=0; x < (ssize_t) extent.width; x++)
{
PixelGetMagickColor(pixels[x],&pixel);
pixel.red=SigmoidalContrast(pixel.red);
pixel.green=SigmoidalContrast(pixel.green);
pixel.blue=SigmoidalContrast(pixel.blue);
pixel.index=SigmoidalContrast(pixel.index);
PixelSetPixelColor(pixels[x],&pixel);
}
return(MagickTrue);
}
int main(int argc,char **argv)
{
#define ThrowWandException(wand) \
{ \
char \
*description; \
\
ExceptionType \
severity; \
\
description=MagickGetException(wand,&severity); \
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
description=(char *) MagickRelinquishMemory(description); \
exit(-1); \
}
MagickBooleanType
status;
MagickWand
*contrast_wand;
PixelInfo
pixel;
WandView
*contrast_view;
if (argc != 3)
{
(void) fprintf(stdout,"Usage: %s image sigmoidal-image\n",argv[0]);
exit(0);
}
/*
Read an image.
*/
MagickWandGenesis();
contrast_wand=NewMagickWand();
status=MagickReadImage(contrast_wand,argv[1]);
if (status == MagickFalse)
ThrowWandException(contrast_wand);
/*
Sigmoidal non-linearity contrast control.
*/
contrast_view=NewWandView(contrast_wand);
if (contrast_view == (WandView *) NULL)
ThrowWandException(contrast_wand);
status=UpdateWandViewIterator(contrast_view,SigmoidalContrast,(void *) NULL);
if (status == MagickFalse)
ThrowWandException(contrast_wand);
contrast_view=DestroyWandView(contrast_view);
/*
Write the image then destroy it.
*/
status=MagickWriteImages(contrast_wand,argv[2],MagickTrue);
if (status == MagickFalse)
ThrowWandException(contrast_wand);
contrast_wand=DestroyMagickWand(contrast_wand);
MagickWandTerminus();
return(0);
}
MagickWandTerminus() 是 ImageMagick 库中的一个函数,用于在关闭使用 ImageMagick 的应用程序时清理并释放资源。该函数应在关闭过程中于应用程序进程的主线程内调用。至关重要的是,只有在所有使用 ImageMagick 函数的线程都已终止之后,才能调用此函数。
ImageMagick 可能会通过 OpenMP(一种并行编程方法)在内部使用线程。因此,务必确保在调用 MagickWandTerminus() 之前,所有对 ImageMagick 的函数调用都已完成。这可以防止 OpenMP 工作线程访问被该终止函数销毁的资源所引发的问题。
如果使用了 OpenMP(从 5.0 版本开始),OpenMP 实现本身会使用自己的方法处理工作线程的启动与停止以及资源的分配与释放。这意味着在调用 MagickWandTerminus() 之后,某些 OpenMP 资源和工作线程可能仍处于已分配状态。为解决这一问题,可以调用函数 omp_pause_resource_all(omp_pause_hard)。该函数在 OpenMP 5.0 版本中引入,可确保 OpenMP 所分配的资源(如线程和线程专属内存)被释放。建议在 MagickWandTerminus() 执行完毕之后调用此函数。
用 C 编写的 MagickWand 示例 演示了如何使用 ImageMagick MagickWand API。每个示例都以 C 函数的形式呈现,并附带头文件,因此可以将其复制到文件中,然后包含进你自己的 C 项目里。