Method: Magick::Image#distort

Defined in:
ext/RMagick/rmimage.c

#distort(*args) ⇒ Object

Call DistortImage.

Ruby usage:

- @verbatim Image#distort(type, points) { optional arguments } @endverbatim
- @verbatim Image#distort(type, points, bestfit) { optional arguments } @endverbatim

Notes:

- Default bestfit is false
- Points is an Array of Numeric values
- Optional arguments are:
  - self.define "distort:viewport", WxH+X+Y
  - self.define "distort:scale", N
  - self.verbose true

Parameters:

  • argc

    number of input arguments

  • argv

    array of input arguments

  • self

    this object

Returns:

  • a new image

[View source]

5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
# File 'ext/RMagick/rmimage.c', line 5092

VALUE
Image_distort(int argc, VALUE *argv, VALUE self)
{
    Image *image, *new_image;
    volatile VALUE pts;
    unsigned long n, npoints;
    DistortImageMethod distortion_method;
    double *points;
    MagickBooleanType bestfit = MagickFalse;
    ExceptionInfo exception;

    image = rm_check_destroyed(self);
    rm_get_optional_arguments(self);

    switch (argc)
    {
        case 3:
            bestfit = RTEST(argv[2]);
        case 2:
            // Ensure pts is an array
            pts = rb_Array(argv[1]);
            VALUE_TO_ENUM(argv[0], distortion_method, DistortImageMethod);
            break;
        default:
            rb_raise(rb_eArgError, "wrong number of arguments (expected 2 or 3, got %d)", argc);
            break;
    }

    npoints = RARRAY_LEN(pts);
    // Allocate points array from Ruby's memory. If an error occurs Ruby will
    // be able to clean it up.
    points = ALLOC_N(double, npoints);

    for (n = 0; n < npoints; n++)
    {
        points[n] = NUM2DBL(rb_ary_entry(pts, n));
    }

    GetExceptionInfo(&exception);
    new_image = DistortImage(image, distortion_method, npoints, points, bestfit, &exception);
    xfree(points);
    rm_check_exception(&exception, new_image, DestroyOnError);
    (void) DestroyExceptionInfo(&exception);
    rm_ensure_result(new_image);

    return rm_image_new(new_image);
}