Example of iOS Usage

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
ImageGandalf
Posts: 15
Joined: 2014-04-14T11:40:01-07:00
Authentication code: 6789

Example of iOS Usage

Post 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.
ImageGandalf
Posts: 15
Joined: 2014-04-14T11:40:01-07:00
Authentication code: 6789

Re: Example of iOS Usage

Post 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'.
Last edited by ImageGandalf on 2014-04-17T12:14:12-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Example of iOS Usage

Post by snibgo »

At the command-line, "-ordered-dither" is a single token, with no spaces. I don't know if this is your problem.
snibgo's IM pages: im.snibgo.com
ImageGandalf
Posts: 15
Joined: 2014-04-14T11:40:01-07:00
Authentication code: 6789

Re: Example of iOS Usage

Post 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]);
ImageGandalf
Posts: 15
Joined: 2014-04-14T11:40:01-07:00
Authentication code: 6789

Re: Example of iOS Usage

Post 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?
ImageGandalf
Posts: 15
Joined: 2014-04-14T11:40:01-07:00
Authentication code: 6789

Re: Example of iOS Usage

Post 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.
Post Reply