Page 1 of 1

Gray virtual pixels and Distort

Posted: 2012-10-12T01:40:08-07:00
by djalfirevic
Hello guys,

I'm new to ImageMagick library, and my client want some of its functions in iOS application.

He gave me an image processing code: convert IMG.JPG -virtual-pixel gray -distort Barrel "0.4 0.0 0.0 0.4" IMG_BAREL.PNG

Now, I have to replicate that on iPhone. Here and there, I've managed to do something, but not a whole thing. Can you help me?

My method is here:

Code: Select all

- (void)convertAndDistort:(UIImage *)sourceImage
{
    // Instantiate MagickWand-a
    MagickWandGenesis();
    magick_wand = NewMagickWand();
    
    // Get image as BLOB in MagickWand
    NSData *dataObject = UIImagePNGRepresentation(sourceImage);
    MagickBooleanType status;
    status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]); // U MagickWand 'ubacujemo' BLOB objekat
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }
    
    // Distort image
    const double controlPoints[4] = {0.4, 0.0, 0.0, 0.4};
    const size_t q = 4;
    status = MagickDistortImage(magick_wand, BarrelDistortion, q, controlPoints, MagickFalse);
    if (status == MagickFalse) {
        ThrowWandException(magick_wand);
    }
    
    // Take BLOB from MagickWand
    size_t length;
    unsigned char *imageBytes = MagickGetImageBlob(magick_wand, &length);
    NSData *data = [[NSData alloc] initWithBytes:imageBytes length:length];
    free(imageBytes);
    
    // Destroy MagickWand
    magick_wand = DestroyMagickWand(magick_wand);
    MagickWandTerminus();
    
    // Set image on button
    UIImage *image = [[UIImage alloc] initWithData:data];
    [data release];
    [imageViewButton setImage:image forState:UIControlStateNormal];
    [image release];
}
As you can see, I've managed to do some distort, as barrel with some control points, and that is this part of code:
-distort Barrel "0.4 0.0 0.0 0.4"

But how can I do the first part?
convert IMG.JPG -virtual-pixel gray

How to set everything to gray, except that 'barel piece' of image?

Thanks!

Re: Gray virtual pixels and Distort

Posted: 2013-02-26T10:30:22-07:00
by alhad
Dear friend,
Please add the following line above the MagickDistortImage
MagickSetImageVirtualPixelMethod(magick_wand, BlackVirtualPixelMethod);

Please change the following line
status = MagickDistortImage(magick_wand, BarrelDistortion, q, controlPoints, MagickFalse);
to
MagickDistortImage(magick_wand, BarrelDistortion, q, (const double *)&controlPoints, MagickTrue);


So now your code should look like this.
MagickSetImageVirtualPixelMethod(magick_wand, BlackVirtualPixelMethod);
MagickDistortImage(magick_wand, BarrelDistortion, q, (const double *)&controlPoints, MagickTrue);


Hope this helps.

All the best.