Method: MagickWand::Wand#crop

Defined in:
ext/magickwand/wand.c

#crop(*args) ⇒ Object

wand.crop(width, height=width, :x=>x, :y=y, :gravity=gravity, :repage=boolean) x and y default to 0 gravity defaults to NorthWestGravity repage defaults to true



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'ext/magickwand/wand.c', line 913

static VALUE wand_crop(int argc, VALUE *argv, VALUE obj)
{
    Wand *wand;
    Image *image;
    volatile VALUE width, height, options;
    VALUE v;
    unsigned long columns, rows;
    long x, y;
    GravityType gravity;
    GravityType keep_gravity;
    int repage;
    RectangleInfo rect;
    char geometry[200];
    ExceptionInfo exception;

    rb_check_frozen(obj);

    (void) rb_scan_args(argc, argv, "12", &width, &height, &options);
    columns = NUM2ULONG(width);
    if (height != Qnil)
    {
        rows = NUM2ULONG(height);
    }
    else
    {
        rows = columns;
    }
    (void) mwr_get_option(options, "gravity", &v);
    gravity = mwr_string_to_gravitytype(v, NorthWestGravity, RaiseUndefinedOption);
    x = mwr_get_option(options, "x", &v) ? FIX2LONG(v) : 0L;
    y = mwr_get_option(options, "y", &v) ? FIX2LONG(v) : 0L;
    repage = mwr_get_option(options, "repage", &v) ? RTEST(v) : 1;

    (void) sprintf(geometry, "%lux%lu%+ld%+ld", columns, rows, x, y);

    Data_Get_Struct(obj, Wand, wand);
    GetExceptionInfo(&exception);

    MagickResetIterator(wand->magickwand);

    // Crop each image individually. If they're different sizes then the
    // crop rectangle could be different.
    while (MagickNextImage(wand->magickwand) != MagickFalse)
    {
        image = GetImageFromMagickWand(wand->magickwand);
        mwr_check_magickwand_error(wand->magickwand);
        keep_gravity = image->gravity;
        image->gravity = gravity;
        memset(&rect, 0, sizeof(rect));
        (void) ParseGravityGeometry(image, geometry, &rect, &exception);
        MagickCropImage(wand->magickwand, rect.width, rect.height, rect.x, rect.y);
        image->gravity = keep_gravity;
        mwr_check_magickwand_error(wand->magickwand);
        if (repage)
        {
            MagickResetImagePage(wand->magickwand, "0x0+0+0");
            mwr_check_magickwand_error(wand->magickwand);
        }
    }

    DestroyExceptionInfo(&exception);

    return obj;
}