简体   繁体   中英

ios - ImageMagick How to code to apply ShepardsDistortion on image

I am new to ImageMagick,and i want to develop an effect of ShepardsDistortion on source image. i gone through many posts and sites, but i didn't find way to implement "ShepardsDistortion" in iOS.

    MagickWand *mw = NewMagickWand();
    MagickSetFormat(mw, "png");

    UIImage *sourceImage=[_sourceImgView image];
    NSData *imgData=UIImagePNGRepresentation(sourceImage);
    MagickReadImageBlob(mw, [imgData bytes], [imgData length]);
    Image *image=GetImageFromMagickWand(mw);

    DistortImage(image, ShepardsDistortion, , ,);

I done upto this, but i dont know what to pass as arg in DitortImage(). So if anyone knows then help me.

EDIT:

-(void)distortImage{

    MagickWandGenesis();
    MagickWand * wand;
    MagickBooleanType status;

    wand = NewMagickWand();
    MagickSetFormat(wand, "png");
    status = MagickReadImage(wand,"chess.png");

    // Arguments for Shepards
    double points[8];
    points[0] = 250; // First X point (starting)
    points[1] = 250; // First Y point (starting)
    points[2] =  50; // First X point (ending)
    points[3] = 150; // First Y point (ending)
    points[4] = 500; // Second X point (starting)
    points[5] = 380; // Second Y point (starting)
    points[6] = 600; // Second X point (ending)
    points[7] = 460; // Second Y point (ending)


    MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
    NSString * tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.png"];
    MagickWriteImage(wand,[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
    UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
    _resultImgView.image=imgObj;

//    
//    unsigned char * cBlob;
//    size_t data_size;
//    cBlob = MagickGetImageBlob(wand, &data_size);
//    NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
//    UIImage *uiImage = [UIImage imageWithData:nsBlob];
//    _resultImgView.image=uiImage;

    MagickWriteImage(wand,"out.png");
    wand=DestroyMagickWand(wand);
    MagickWandTerminus();
}

This might help:

MagickWandGenesis();
magick_wand = NewMagickWand();
double points[24];
points[0] = 250; 
points[1] = 250; 
points[2] =  50; 
points[3] = 150; 
points[4] = 0; 
points[5] = 0; 
points[6] =  0; 
points[7] = 0; 
points[8] = self.frame.width; 
points[9] = 0; 
points[10] = self.frame.width; 
points[11] = 0; 
points[12] = self.frame.width; 
points[13] = self.frame.height; 
points[14] = self.frame.width; 
points[15] = self.frame.height; 
points[16] = self.frame.width; 
points[17] = self.frame.height; 
points[18] = self.frame.width; 
points[19] = self.frame.height;
points[20] = 0; 
points[21] = self.frame.height; 
points[22] = 0; 
points[23] = self.frame.height;  
NSData * dataObject = UIImagePNGRepresentation([UIImage imageNamed:@"Imagemagick-logo.png"]);//UIImageJPEGRepresentation([imageViewButton imageForState:UIControlStateNormal], 90);
MagickBooleanType status;
status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);
if (status == MagickFalse) {
    ThrowWandException(magick_wand);
}

// posterize the image, this filter uses a configuration file, that means that everything in IM should be working great
status =  MagickDistortImage(magick_wand,ShepardsDistortion,24,points,MagickFalse);

//status = MagickOrderedPosterizeImage(magick_wand, "h8x8o");
if (status == MagickFalse) {
    ThrowWandException(magick_wand);
}

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];

Arguments are passed to DistortImage as the start of a list of doubles, and size information about the list. Example:

size_t SizeOfPoints = 8;
double Points[SizeOfPoints];
DistortImage(image,
             ShepardsDistoration,
             SizeOfPoints,
             Points,
             MagickFalse,
             NULL
            );

In your example, you seem to be mixing MagickWand & MagickCore methods; which, seems unnecessary and confusing. I would keep this distortion simple, and only use MagickWand's MagickDistortImage method. Here's a example in

int main(int argc. const char **argv)
{
  MagickWandGenesis();
  MagickWand * wand;
  MagickBooleanType status;

  wand = NewMagickWand();
  status = MagickReadImage(wand,"logo:");

  // Arguments for Shepards
  double points[8];
  // 250x250 -> 50x150
  points[0] = 250; // First X point (starting)
  points[1] = 250; // First Y point (starting)
  points[2] =  50; // First X point (ending)
  points[3] = 150; // First Y point (ending)
  // 500x380 -> 600x460
  points[4] = 500; // Second X point (starting)
  points[5] = 380; // Second Y point (starting)
  points[6] = 600; // Second X point (ending)
  points[7] = 460; // Second Y point (ending)

  MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
  MagickWriteImage(wand,"out.png");

  wand=DestroyMagickWand(wand);
  MagickWandTerminus();

  return 0;
}

Resulting in a distorted translated image ( details )

ShepardsDistortion示例

Edit

For iOS, you can use NSTemporaryDirectory (like in this answer ), or create an image dynamically using NSData (like in this question ).

Example with temporary path:

NSString * tempFilePath = [NSTemporaryDirectory() 
                             stringByAppendingPathComponent:@"out.png"];
MagickWriteImage(self.wand,
    [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];

And an example with NSData + blob

unsigned char * cBlob;
size_t data_size;
cBlob = MagickGetImageBlob(wand, &data_size);
NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
UIImage * uiImage = [UIImage imageWithData:nsBlob];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM