Method: Magick::Image#extent

Defined in:
ext/RMagick/rmimage.cpp

#extent(width, height, x = 0, y = 0) ⇒ Magick::Image

If width or height is greater than the target image’s width or height, extends the width and height of the target image to the specified values. The new pixels are set to the background color. If width or height is less than the target image’s width or height, crops the target image.

Returns a new image.

Parameters:

  • width (Numeric)

    The width of the new image

  • height (Numeric)

    The height of the new image

  • x (Numeric) (defaults to: 0)

    The upper-left corner of the new image is positioned

  • y (Numeric) (defaults to: 0)

    The upper-left corner of the new image is positioned

Returns:



6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
# File 'ext/RMagick/rmimage.cpp', line 6705

VALUE
Image_extent(int argc, VALUE *argv, VALUE self)
{
    Image *image, *new_image;
    RectangleInfo geometry;
    long height, width;
    ExceptionInfo *exception;

    rm_check_destroyed(self);

    if (argc < 2 || argc > 4)
    {
        rb_raise(rb_eArgError, "wrong number of arguments (expected 2 to 4, got %d)", argc);
    }

    geometry.y = geometry.x = 0L;
    switch (argc)
    {
        case 4:
            geometry.y = NUM2LONG(argv[3]);
        case 3:
            geometry.x = NUM2LONG(argv[2]);
        default:
            geometry.height = height = NUM2LONG(argv[1]);
            geometry.width = width = NUM2LONG(argv[0]);
            break;
    }

    // Use the signed versions of these two values to test for < 0
    if (height <= 0L || width <= 0L)
    {
        if (geometry.x == 0 && geometry.y == 0)
        {
            rb_raise(rb_eArgError, "invalid extent geometry %ldx%ld", width, height);
        }
        else
        {
            rb_raise(rb_eArgError, "invalid extent geometry %ldx%ld+%" RMIdSIZE "+%" RMIdSIZE "",
                     width, height, geometry.x, geometry.y);
        }
    }


    TypedData_Get_Struct(self, Image, &rm_image_data_type, image);
    exception = AcquireExceptionInfo();

    GVL_STRUCT_TYPE(ExtentImage) args = { image, &geometry, exception };
    new_image = (Image *)CALL_FUNC_WITHOUT_GVL(GVL_FUNC(ExtentImage), &args);
    rm_check_exception(exception, new_image, DestroyOnError);
    DestroyExceptionInfo(exception);

    return rm_image_new(new_image);
}