ConvertImageCommand() and Objective-C.

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
fulvio
Posts: 8
Joined: 2011-09-20T17:59:18-07:00
Authentication code: 8675308

ConvertImageCommand() and Objective-C.

Post by fulvio »

I have the following code, which is simply meant to resize an image using:

Code: Select all

ConvertImageCommand()
However, with the lack of documentation in Objective-C I'm not quite sure whether I'm implemented it correctly at all.

This is what I have so far (it is simply the IM_Test.zip Xcode project) from here:

http://www.cloudgoessocial.net/im_iphone/IM_Test.zip
http://www.cloudgoessocial.net/2009/07/ ... one-xcode/

Here is my code (which sits in IM_TestViewController.m):

Code: Select all

- (void)convertImage {
    MagickWandGenesis();
    magick_wand = NewMagickWand();
    //UIImageJPEGRepresentation([imageViewButton imageForState:UIControlStateNormal], 90);
    NSData * dataObject = UIImagePNGRepresentation([UIImage imageNamed:@"iphone.png"]);
    MagickBooleanType status;
    status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }

    // Resize image.
    ImageInfo *imageInfo = AcquireImageInfo();
    ExceptionInfo *exceptionInfo = AcquireExceptionInfo();

    // Get image from bundle.
    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 };

    // ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *);
    status = ConvertImageCommand(imageInfo, 5, argv, NULL, exceptionInfo);

    free(input_image);
    free(output_image);

    if (status == MagickFalse) {
        ThrowWandException(magick_wand); // Always throws an exception here...
    }

    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];
    [data release];

    [imageViewButton setImage:image forState:UIControlStateNormal];
    [image release];
}
I'm not sure whether I'm using ImageInfo and ExceptionInfo properly considering I'm using MagickWand (do I even need this?).

Also the parameters I'm passing to ConvertImageCommand(), I'm not sure whether they are correct.

Any help would be greatly appreciated and thanks in advance.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: ConvertImageCommand() and Objective-C.

Post by el_supremo »

ConvertImageCommand is a MagickCore function and there doesn't appear to be an interface to it in MagickWand (would be nice if there is one).
If all you are ever going to do in this function is a fixed resize, you could do it all with MagickWand functions and avoid MagickCore and ConvertImageCommand altogether.
I can't test this code so you'll probably have to play with it a bit.
I have assumed that input_image and output_image are filenames. If not, this won't work at all.

Pete

Code: Select all

- (void)convertImage {
	MagickWandGenesis();
	magick_wand = NewMagickWand();
	
	// Get image from bundle.
	char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
	char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]);
	
	if(MagickReadImage(magick_wand,input_image) == MagickFalse) {
		//>>> Report an error reading the input file
	}
	//>>> If "LanczosFilter" isn't known, use 22 instead. That's not
	//>>> the best way around it but it'll do for now.
	MagickResizeImage(magick_wand,100,100,LanczosFilter,1);
	// If this function only needs to display the result and the output file isn't actually required
	// you can omit this next statement
	MagickWriteImage(magick_wand,output_image);
	
	// Just to be sure that the blob is the correct format
	MagickSetImageFormat(magick_wand,"png");
	
	free(input_image);
	free(output_image);
	
	size_t my_size;
	unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size);
	NSData * data = [[NSData alloc] initWithBytes:my_image length:my_size];
	
	//>>> !!!! I'm not sure free(my_image) will work. "my_image" is allocated by MagickWand
	//>>> and usually you have to use its function to release the memory (but they
	//>>> may do the same thing)
	//>>> free(my_image);
	RelinquishMagickMemory(my_image);
	magick_wand = DestroyMagickWand(magick_wand);
	MagickWandTerminus();
	
	UIImage * image = [[UIImage alloc] initWithData:data];
	[data release];
	
	[imageViewButton setImage:image forState:UIControlStateNormal];
	[image release];
}
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
jgalyan
Posts: 3
Joined: 2011-12-03T23:50:09-07:00
Authentication code: 8675308

Re: ConvertImageCommand() and Objective-C.

Post by jgalyan »

Check out MagickCommandGenesis().
Post Reply