打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Using the C API for ImageMagick(on iPhone?) to convert to monochrome?

You can achieve monochrome conversion, as described here, with MagickQuantizeImage function. I'm not quite familiar with dithering images, but an example may look like the following.

#include <wand/MagickWand.h>int main(int argc, char **argv) {  const size_t number_colors = 2;  const size_t treedepth     = 1;  MagickWandGenesis();  MagickWand *wand = NULL;  wand = NewMagickWand();  MagickReadImage(wand,"source.png");  MagickQuantizeImage(                     wand,            // MagickWand                     number_colors,   // Target number colors                     GRAYColorspace,  // Colorspace                     treedepth,       // Optimal depth                     MagickTrue,      // Dither                     MagickFalse      // Quantization error                 );  MagickWriteImage(wand,"out.png");  if(wand)wand = DestroyMagickWand(wand);  MagickWandTerminus();  return 0;}

This can give you a rather speckled image at times.

Adjusting depth, color-number, and/or disabling dithering may give you results closer to what you would expect from the examples provided.

MagickQuantizeImage(                   wand,            // MagickWand                   number_colors,   // Target number colors                   GRAYColorspace,  // Colorspace                   treedepth,       // Optimal depth                   MagickFalse,     // No-dither                   MagickFalse      // Quantization error               );

Like such...

For iOS

Not much effort is needed to port the example code to iOS. NextStep/Objective-c methods are compatible with MagickWand library. The following example uses a temporary file to store the monochrome image, but I'm sure there is a better way to pass Magick image-data directly to UImage object.

// MyViewController.h#import <UIKit/UIKit.h>#import <wand/MagickWand.h>@interface MyViewController : UIViewController@property (retain, nonatomic) IBOutlet UIImageView *imageView;@property (retain, nonatomic) MagickWand *wand;@end// MyViewController.m#import "MyViewController.h"@implementation MyViewController- (void)viewDidLoad{    [super viewDidLoad];    MagickWandGenesis();    self.wand = NewMagickWand();    [self drawMonochromeImage:@"logo:"];}-(void)drawMonochromeImage:(NSString *)filePath{    // Create temporary file    NSString *tempFilePath = [NSTemporaryDirectory()                                  stringByAppendingPathComponent:@"logo.jpg"];    // Read given image with C-string    MagickReadImage(self.wand,        [filePath cStringUsingEncoding:NSASCIIStringEncoding]    );    // Monochrome image    MagickQuantizeImage(self.wand,2,GRAYColorspace,1,MagickFalse,MagickFalse);    // Write to temporary file    MagickWriteImage(self.wand,        [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]    );    // Load UIImage from temporary file    UIImage *imgObj = [UIImage imageWithContentsOfFile:tempFilePath];    // Display on device    [self.imageView setImage:imgObj];    [self.imageView setContentMode:UIViewContentModeScaleAspectFit];}-(void)viewDidUnload{    // Clean-up    if (self.wand)        self.wand = DestroyMagickWand(self.wand);    MagickWandTerminus();}@end

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PHP图像处理类库MagickWand用法实例分析 PHP
iPhone开发之启动画面及动画
iPhone开发学习手记–Hello World(分析篇) | 雨雪霏霏 - iPhone...
MJRefresh刷新第三方库
由pushViewController说起可能出线的各种死法
iOS开发
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服