建议使用MagickWand API作为C编程语言与ImageMagick图像处理库之间的接口。与MagickCore C API不同,MagickWand仅使用少量不透明类型。可以使用访问器来设置或获取重要的魔杖属性。此处提供了MagickWand公共方法的描述。
- Magick Wand 方法
- 设置或获取Magick Wand属性
- Magick Wand图像方法
- 像素迭代器方法
- 像素Wand方法
- 图像矢量绘制
- 命令行接口
- Wand视图方法
- 已弃用方法
- 错误和警告代码
编写完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);
}
现在让我们利用双核或四核处理系统,通过并行运行算法利用魔杖视图来执行相同的对比度增强。 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项目中。