Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

ios - Using the C API for ImageMagick (on iPhone?) to convert to monochrome?

I am using the code referenced in this post, but would like to switch to an ImageMagick C-API based solution, as I want to standardize on a single image manipulation library, and need IM for some other tasks.

I can find tons of examples of using the convert command line tool, but none on how to do the monochrome conversion in code.

Any sample code out there?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Monochrome with dither

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... Monochrome without dither

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

iOS Simulator


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...