Page 1 of 1

Example of iOS Usage

Posted: 2014-04-16T12:00:59-07:00
by ImageGandalf
Hi,

I've just added the iOS ImageMagick by Claudio to my project and was wondering if there are any examples for using MagickWand in iOS? I'm just trying to do simple stuff like:

convert pug.png -ordered-dither h4X4a pug2.png

and am not really too versed in C. Is it possible to take a UIImage and add effects to it using ImageMagick iOS?

Thanks.

Re: Example of iOS Usage

Posted: 2014-04-16T14:04:14-07:00
by ImageGandalf
Done a little searching around and so far able to come up with:

Code: Select all

     MagickWandGenesis();
    MagickWand *wand = NewMagickWand();
    NSData *data = UIImagePNGRepresentation(self.originalImage);
    MagickReadImageBlob(wand, [data bytes], [data length]);
    
    int arg_count = 3;
    char *args[] = { "-ordered", "-dither", "h4x4a", NULL};
    
    ImageInfo *image_info = AcquireImageInfo();
    ExceptionInfo *exception = AcquireExceptionInfo();
    
    MagickBooleanType status = ConvertImageCommand(image_info, arg_count, args, NULL, exception);
    
    if (exception->severity != UndefinedException)
    {
        status = MagickTrue;
        CatchException(exception);
    }
    
    if (status == MagickFalse)
    {
        NSLog(@"FAIL");
    }
    
    size_t my_size;
    unsigned char * my_image = MagickGetImageBlob(wand, &my_size);
    NSData *outData = [[NSData alloc] initWithBytes:my_image length:my_size];
    free(my_image);
    
    self.imageView.image = [[UIImage alloc] initWithData:outData];
    
    image_info=DestroyImageInfo(image_info);
    exception=DestroyExceptionInfo(exception);
    DestroyMagickWand(wand);
    MagickWandTerminus();
Where originalImage is a UIImage. I'm applying this and it's doing nothing to the image. I wonder if my issue is with putting the args incorrectly, or if not extracting the resulting image correctly?

UPDATE: Edited my code to include the exception handling code in the example. Now am getting: UnrecognizedDitherMethod `h4x4a'.

Re: Example of iOS Usage

Posted: 2014-04-16T19:26:15-07:00
by snibgo
At the command-line, "-ordered-dither" is a single token, with no spaces. I don't know if this is your problem.

Re: Example of iOS Usage

Posted: 2014-04-16T20:35:28-07:00
by ImageGandalf
snibgo wrote:At the command-line, "-ordered-dither" is a single token, with no spaces. I don't know if this is your problem.
Thanks for taking a look. I've actually tried it the way you mentioned and when I do the status comes back as false. When I split them as above, the exception gets thrown, so not sure what that means.

I've also tried doing:

Code: Select all

char *args[] = {"-posterize", "4", NULL};
And that is coming back as false as well. I have a feeling that I'm doing the image reading incorrectly, i.e. not understanding if

Code: Select all

ImageInfo *image_info = AcquireImageInfo();
Is gathering what I did when I read the image in:

Code: Select all

MagickReadImageBlob(wand, [data bytes], [data length]);

Re: Example of iOS Usage

Posted: 2014-04-17T12:19:00-07:00
by ImageGandalf
Actually updated some of my code, I was failing to get the resulting image from the blob. Now if I substitute ConvertImageCommand with

Code: Select all

MagickBooleanType status = MagickPosterizeImage(wand,6,MagickFalse);
that actually works. However, still having trouble getting ConvertImageCommand to work. A guy on Stackoverflow recommended to use:

Code: Select all

MagickBooleanType status = MagickCommandGenesis(image_info, ConvertImageCommand, arg_count, args, NULL, exception);
But this gives the same result as ConvertImageCommand. Has anyone had success before executing either CovertImageCommand or MagickCommandGensis on iOS?

Re: Example of iOS Usage

Posted: 2014-04-17T12:43:09-07:00
by ImageGandalf
Well I found an example on Stackoverflow where a guy lists his command like this:

Code: Select all

    char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL };
Since I'm not grabbing my image from the bundle, I wonder, what would I put for the inputImage and outputImage parameters? I've tried NULL and it doesn't work.