ios - Using the C API for ImageMagick (on iPhone?) to convert to monochrome? -
i using code referenced in this post, switch imagemagick c-api based solution, want standardize on single image manipulation library, , need im other tasks.
i can find tons of examples of using convert command line tool, none on how monochrome conversion in code.
any sample code out there?
you can achieve monochrome conversion, described here, magickquantizeimage function. i'm not quite familiar dithering images, example may 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 rather speckled image @ times.
adjusting depth, color-number, and/or disabling dithering may give results closer expect 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 effort needed port example code ios. nextstep/objective-c methods compatible magickwand library. following example uses temporary file store monochrome image, i'm sure there better way pass magick image-data directly 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 c-string magickreadimage(self.wand, [filepath cstringusingencoding:nsasciistringencoding] ); // monochrome image magickquantizeimage(self.wand,2,graycolorspace,1,magickfalse,magickfalse); // write temporary file magickwriteimage(self.wand, [tempfilepath cstringusingencoding:nsasciistringencoding] ); // load uiimage 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
Comments
Post a Comment