Page 1 of 1

how to use ConvertImageCommand in iOS

Posted: 2014-06-09T08:33:30-07:00
by skuenstler
Hi,
I try to use "ConvertImageCommand()" for my iOS project (see example below).

The code below works - but not quite as I would expect :)

Somehow the only conversion that is successful is if I take the Bundle-Image (input_image / output_image). Or in other words, the conversion writes the output_image.
But isn't there a way to make ConvertImageCommand convert the magick_wand ????. But I always end up with "FAIL" !

The workarround below writes the converted output_image into the magick_wand. But again, isn't there a more direct way ???

Reading through examples in this forum, there are quite many posts asking about very similar issues (i.e. exceptions/error using ConvertImageCommand). But no example really gave me an answer to that problem. Can you please help me ?

Thanks for any support on this !
Sincerely,
skuenstler

Code: Select all

- (IBAction)buttonConv04:(id)sender {
    
    // MagickWandGenesis();

    int arg_count = 4;
    char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
    char *args[] = {"convert", input_image, "-negate", output_image, NULL};

    MagickCoreGenesis(*args, MagickTrue);
    magick_wand = NewMagickWand();
    NSData * dataObject = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:input_image]], 1.0f);
    MagickBooleanType status;
    status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);

    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }

    int args_count;
    for(args_count = 0; args[args_count] != (char *)NULL; args_count++);
    
    ImageInfo *image_info = AcquireImageInfo();
    ExceptionInfo *exception = AcquireExceptionInfo();
    
    status = ConvertImageCommand(image_info, arg_count, args, NULL, exception);
    
    if (exception->severity != UndefinedException)
    {
        status = MagickTrue;
        CatchException(exception);
    }
    
    if (status == MagickFalse)
    {
        NSLog(@"FAIL");
    }
    
    NSData * dataObject2 = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:output_image]], 1.0f);
    status = MagickReadImageBlob(magick_wand, [dataObject2 bytes], [dataObject2 length]);
    
    size_t my_size;
    unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size);
    NSData * data = [[NSData alloc] initWithBytes:my_image length:my_size];
    free(my_image);
    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();
    UIImage * image = [[UIImage alloc] initWithData:data];
    
    free(input_image);
    free(output_image);
    
    image_info=DestroyImageInfo(image_info);
    exception=DestroyExceptionInfo(exception);
    
    MagickCoreTerminus();
    
    [iKK_imageView setImage:image];
}