Gray virtual pixels and Distort

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
djalfirevic
Posts: 1
Joined: 2012-10-12T01:29:41-07:00
Authentication code: 67789

Gray virtual pixels and Distort

Post 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!
alhad
Posts: 1
Joined: 2013-02-26T10:17:51-07:00
Authentication code: 6789

Re: Gray virtual pixels and Distort

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