Method: Magick::Image#get_pixels

Defined in:
ext/RMagick/rmimage.c

#get_pixels(x_arg, y_arg, cols_arg, rows_arg) ⇒ Object

Call AcquireImagePixels.

Ruby usage:

- @verbatim Image#get_pixels(x, y, columns. rows) @endverbatim

Notes:

- This is the complement of store_pixels. Notice that the return value is
  an array object even when only one pixel is returned. store_pixels calls
  GetImagePixels, then SyncImage

rectangle defined by the geometry parameters.

Parameters:

  • self

    this object

  • x_arg

    x position of start of region

  • y_arg

    y position of start of region

  • cols_arg

    width of region

  • rows_arg

    height of region

Returns:

  • An array of Magick::Pixel objects corresponding to the pixels in the

See Also:

  • Image_store_pixels

7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
# File 'ext/RMagick/rmimage.c', line 7054

VALUE
Image_get_pixels(VALUE self, VALUE x_arg, VALUE y_arg, VALUE cols_arg, VALUE rows_arg)
{
    Image *image;
    ExceptionInfo *exception;
    long x, y;
    unsigned long columns, rows;
    long size, n;
    VALUE pixel_ary;
    const PixelPacket *pixels;

    image = rm_check_destroyed(self);
    x       = NUM2LONG(x_arg);
    y       = NUM2LONG(y_arg);
    columns = NUM2ULONG(cols_arg);
    rows    = NUM2ULONG(rows_arg);

    if ((x+columns) > image->columns || (y+rows) > image->rows)
    {
        rb_raise(rb_eRangeError, "geometry (%lux%lu%+ld%+ld) exceeds image bounds"
                 , columns, rows, x, y);
    }

    // Cast AcquireImagePixels to get rid of the const qualifier. We're not going
    // to change the pixels but I don't want to make "pixels" const.
    exception = AcquireExceptionInfo();
    pixels = GetVirtualPixels(image, x, y, columns, rows, exception);
    CHECK_EXCEPTION()

    (void) DestroyExceptionInfo(exception);

    // If the function failed, return a 0-length array.
    if (!pixels)
    {
        return rb_ary_new();
    }

    // Allocate an array big enough to contain the PixelPackets.
    size = (long)(columns * rows);
    pixel_ary = rb_ary_new2(size);

    // Convert the PixelPackets to Magick::Pixel objects
    for (n = 0; n < size; n++)
    {
        rb_ary_store(pixel_ary, n, Pixel_from_PixelPacket(&pixels[n]));
    }

    return pixel_ary;
}