Class: GdkPixbuf::Pixbuf

Inherits:
Object
  • Object
show all
Extended by:
GLib::Deprecatable
Defined in:
lib/gdk_pixbuf2/pixbuf.rb,
lib/gdk_pixbuf2/deprecated.rb

Overview

A pixel buffer.

GdkPixbuf contains information about an image's pixel data, its color space, bits per sample, width and height, and the rowstride (the number of bytes between the start of one row and the start of the next).

Creating new GdkPixbuf

The most basic way to create a pixbuf is to wrap an existing pixel buffer with a [classGdkPixbuf.Pixbuf] instance. You can use the [ctor<b>GdkPixbuf</b>.Pixbuf.new_from_data] function to do this.

Every time you create a new GdkPixbuf instance for some data, you will need to specify the destroy notification function that will be called when the data buffer needs to be freed; this will happen when a GdkPixbuf is finalized by the reference counting functions. If you have a chunk of static data compiled into your application, you can pass in NULL as the destroy notification function so that the data will not be freed.

The [ctor<b>GdkPixbuf</b>.Pixbuf.new] constructor function can be used as a convenience to create a pixbuf with an empty buffer; this is equivalent to allocating a data buffer using malloc() and then wrapping it with gdk_pixbuf_new_from_data(). The gdk_pixbuf_new() function will compute an optimal rowstride so that rendering can be performed with an efficient algorithm.

As a special case, you can use the [ctor<b>GdkPixbuf</b>.Pixbuf.new_from_xpm_data] function to create a pixbuf from inline XPM image data.

You can also copy an existing pixbuf with the [methodPixbuf.copy] function. This is not the same as just acquiring a reference to the old pixbuf instance: the copy function will actually duplicate the pixel data in memory and create a new [classPixbuf] instance for it.

Reference counting

GdkPixbuf structures are reference counted. This means that an application can share a single pixbuf among many parts of the code. When a piece of the program needs to use a pixbuf, it should acquire a reference to it by calling g_object_ref(); when it no longer needs the pixbuf, it should release the reference it acquired by calling g_object_unref(). The resources associated with a GdkPixbuf will be freed when its reference count drops to zero. Newly-created GdkPixbuf instances start with a reference count of one.

Image Data

Image data in a pixbuf is stored in memory in an uncompressed, packed format. Rows in the image are stored top to bottom, and in each row pixels are stored from left to right.

There may be padding at the end of a row.

The "rowstride" value of a pixbuf, as returned by [method<b>GdkPixbuf</b>.Pixbuf.get_rowstride], indicates the number of bytes between rows.

NOTE: If you are copying raw pixbuf data with memcpy() note that the last row in the pixbuf may not be as wide as the full rowstride, but rather just as wide as the pixel data needs to be; that is: it is unsafe to do memcpy (dest, pixels, rowstride * height) to copy a whole pixbuf. Use [methodGdkPixbuf.Pixbuf.copy] instead, or compute the width in bytes of the last row as:

last_row = width * ((n_channels * bits_per_sample + 7) / 8);

The same rule applies when iterating over each row of a GdkPixbuf pixels array.

The following code illustrates a simple put_pixel() function for RGB pixbufs with 8 bits per channel with an alpha channel.

static void
put_pixel (GdkPixbuf *pixbuf,
           int x,
	   int y,
	   guchar red,
	   guchar green,
	   guchar blue,
	   guchar alpha)
{
  int n_channels = gdk_pixbuf_get_n_channels (pixbuf);

  // Ensure that the pixbuf is valid
  g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
  g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
  g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
  g_assert (n_channels == 4);

  int width = gdk_pixbuf_get_width (pixbuf);
  int height = gdk_pixbuf_get_height (pixbuf);

  // Ensure that the coordinates are in a valid range
  g_assert (x >= 0 && x < width);
  g_assert (y >= 0 && y < height);

  int rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  // The pixel buffer in the GdkPixbuf instance
  guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);

  // The pixel we wish to modify
  guchar *p = pixels + y * rowstride + x * n_channels;
  p[0] = red;
  p[1] = green;
  p[2] = blue;
  p[3] = alpha;
}

Loading images

The GdkPixBuf class provides a simple mechanism for loading an image from a file in synchronous and asynchronous fashion.

For GUI applications, it is recommended to use the asynchronous stream API to avoid blocking the control flow of the application.

Additionally, GdkPixbuf provides the [classGdkPixbuf.PixbufLoader`] API for progressive image loading.

Saving images

The GdkPixbuf class provides methods for saving image data in a number of file formats. The formatted data can be written to a file or to a memory buffer. GdkPixbuf can also call a user-defined callback on the data, which allows to e.g. write the image to a socket or store it in a database.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GdkPixbuf::Pixbuf

Creates a new pixbuf by parsing XPM data in memory.

This data is commonly the result of including an XPM file into a program's C source.

Parameters:

  • data (Array<String>)

    Pointer to inline XPM data.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 21

def initialize(*args)
  case args.size
  when 1
    case args[0]
    when Hash
      initialize_with_hash(args[0])
    when String
      message = "#{caller[0]}: #{self.class}.new(path) is deprecated. "
      message << "Use #{self.class}.new(:file => path) instead."
      warn message
      initialize_raw(args[0])
    when Array
      message = "#{caller[0]}: #{self.class}.new(xpm) is deprecated. "
      message << "Use #{self.class}.new(:xpm => xpm) instead."
      warn message
      initialize_new_from_xpm_data(args[0])
    else
      raise ArgumentError, "must be options: #{args[0].inspect}"
    end
  when 2
    message = "#{caller[0]}: "
    message << "#{self.class}.new(data, copy_pixels) is deprecated. "
    message << "Use Gio::Resource instead."
    warn message
    initialize_from_inline(*args)
  when 3
    message = "#{caller[0]}: "
    message << "#{self.class}.new(path, width, height) is deprecated. "
    message << "Use #{self.class}.new(:file => path, :width => width, "
    message << ":height => height) instead."
    warn message
    initialize_new_from_file_at_size(*args)
  when 4
    message = "#{caller[0]}: "
    message << "#{self.class}.new(path, width, height, "
    message << "preserve_aspect_ratio) is deprecated. "
    message << "Use #{self.class}.new(:file => path, :width => width, "
    message << ":height => height, "
    message << ":preserve_aspect_ratio => preserve_aspect_ratio) instead."
    warn message
    initialize_new_from_file_at_scale(*args)
  when 5
    message = "#{caller[0]}: "
    message << "#{self.class}.new(colorspace, has_alpha, bits_per_sample, "
    message << "width, height) is deprecated. "
    message << "Use #{self.class}.new(:colorspace => colorspace, "
    message << ":has_alpha => has_alpha, "
    message << ":bits_per_sample => bits_per_sample, "
    message << ":width => width, "
    message << ":height => height) instead."
    warn message
    initialize_new(*args)
  when 7
    message = "#{caller[0]}: "
    message << "#{self.class}.new(data, colorspace, has_alpha, "
    message << "bits_per_sample, width, height) is deprecated. "
    message << "Use #{self.class}.new(:data => data, "
    message << ":colorspace => colorspace, "
    message << ":has_alpha => has_alpha, "
    message << ":bits_per_sample => bits_per_sample, "
    message << ":width => width, "
    message << ":height => height) instead."
    warn message
    initialize_new_from_data(*args)
  else
    super
  end
end

Class Method Details

.calculate_rowstride(colorspace, has_alpha, bits_per_sample, width, height) ⇒ Integer

Calculates the rowstride that an image created with those values would have.

This function is useful for front-ends and backends that want to check image values without needing to create a GdkPixbuf.

Parameters:

  • colorspace (GdkPixbuf::Colorspace)

    Color space for image

  • has_alpha (Boolean)

    Whether the image should have transparency information

  • bits_per_sample (Integer)

    Number of bits per color sample

  • width (Integer)

    Width of image in pixels, must be > 0

  • height (Integer)

    Height of image in pixels, must be > 0

Returns:

  • (Integer)

    the rowstride for the given values, or -1 in case of error.

.formatsGLib::SList<GdkPixbuf::PixbufFormat>

Obtains the available information about the image formats supported by GdkPixbuf.

Returns:

.get_file_info(filename, width, height) ⇒ GdkPixbuf::PixbufFormat

Parses an image file far enough to determine its format and size.

Parameters:

  • filename (GdkPixbuf::filename)

    The name of the file to identify.

  • width (Integer)

    Return location for the width of the image

  • height (Integer)

    Return location for the height of the image

Returns:

.get_file_info_async(filename, cancellable, callback, user_data) ⇒ nil

Asynchronously parses an image file far enough to determine its format and size.

For more details see gdk_pixbuf_get_file_info(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call gdk_pixbuf_get_file_info_finish() to get the result of the operation.

Parameters:

  • filename (GdkPixbuf::filename)

    The name of the file to identify

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • callback (Gio::AsyncReadyCallback)

    a GAsyncReadyCallback to call when the file info is available

  • user_data (GObject)

    the data to pass to the callback function

Returns:

  • (nil)

.get_file_info_finish(async_result, width, height) ⇒ GdkPixbuf::PixbufFormat

Finishes an asynchronous pixbuf parsing operation started with gdk_pixbuf_get_file_info_async().

Parameters:

  • async_result (Gio::AsyncResult)

    a GAsyncResult

  • width (Integer)

    Return location for the width of the image, or NULL

  • height (Integer)

    Return location for the height of the image, or NULL

Returns:

.init_modules(path) ⇒ Boolean

Initalizes the gdk-pixbuf loader modules referenced by the loaders.cache file present inside that directory.

This is to be used by applications that want to ship certain loaders in a different location from the system ones.

This is needed when the OS or runtime ships a minimal number of loaders so as to reduce the potential attack surface of carefully crafted image files, especially for uncommon file types. Applications that require broader image file types coverage, such as image viewers, would be expected to ship the gdk-pixbuf modules in a separate location, bundled with the application in a separate directory from the OS or runtime- provided modules.

Parameters:

  • path (String)

    Path to directory where the loaders.cache is installed

Returns:

  • (Boolean)

.new(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gdk_pixbuf2/deprecated.rb', line 26

def new(*args, &block)
  if args[0].is_a?(Pixbuf)
    message = "#{caller[0]}: #{self}.new(pixbuf, ...) is deprecated. "
    message << "Use pixbuf.subpixbuf(...)  instead."
    warn(message)
    args[0].subpixbuf(*args[1..-1])
  elsif args.size == 1 and args[0].is_a?(Hash)
    options = args[0]
    src_pixbuf = options[:src_pixbuf]
    if src_pixbuf
      message = "#{caller[0]}: "
      message << "#{self}.new(:src_pixbuf => pixbuf, ...) is deprecated. "
      message << "Use pixbuf.subpixbuf(...)  instead."
      warn(message)
      src_pixbuf.subpixbuf(options[:src_x],
                           options[:src_y],
                           options[:width],
                           options[:height])
    else
      super
    end
  else
    super
  end
end

.new_from_stream_async(stream, cancellable, callback, user_data) ⇒ nil

Creates a new pixbuf by asynchronously loading an image from an input stream.

For more details see gdk_pixbuf_new_from_stream(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.

Parameters:

  • stream (Gio::InputStream)

    a GInputStream from which to load the pixbuf

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • callback (Gio::AsyncReadyCallback)

    a GAsyncReadyCallback to call when the pixbuf is loaded

  • user_data (GObject)

    the data to pass to the callback function

Returns:

  • (nil)

.new_from_stream_at_scale_async(stream, width, height, preserve_aspect_ratio, cancellable, callback, user_data) ⇒ nil

Creates a new pixbuf by asynchronously loading an image from an input stream.

For more details see gdk_pixbuf_new_from_stream_at_scale(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call gdk_pixbuf_new_from_stream_finish() to get the result of the operation.

Parameters:

  • stream (Gio::InputStream)

    a GInputStream from which to load the pixbuf

  • width (Integer)

    the width the image should have or -1 to not constrain the width

  • height (Integer)

    the height the image should have or -1 to not constrain the height

  • preserve_aspect_ratio (Boolean)

    TRUE to preserve the image's aspect ratio

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • callback (Gio::AsyncReadyCallback)

    a GAsyncReadyCallback to call when the pixbuf is loaded

  • user_data (GObject)

    the data to pass to the callback function

Returns:

  • (nil)

.save_to_stream_finish(async_result) ⇒ Boolean

Finishes an asynchronous pixbuf save operation started with gdk_pixbuf_save_to_stream_async().

Parameters:

  • async_result (Gio::AsyncResult)

    a GAsyncResult

Returns:

  • (Boolean)

    TRUE if the pixbuf was saved successfully, FALSE if an error was set.

Instance Method Details

#add_alpha(substitute_color, r, g, b) ⇒ GdkPixbuf::Pixbuf

Takes an existing pixbuf and adds an alpha channel to it.

If the existing pixbuf already had an alpha channel, the channel values are copied from the original; otherwise, the alpha channel is initialized to 255 (full opacity).

If substitute_color is TRUE, then the color specified by the (r, g, b) arguments will be assigned zero opacity. That is, if you pass (255, 255, 255) for the substitute color, all white pixels will become fully transparent.

If substitute_color is FALSE, then the (r, g, b) arguments will be ignored.

Parameters:

  • substitute_color (Boolean)

    Whether to set a color to zero opacity.

  • r (Integer)

    Red value to substitute.

  • g (Integer)

    Green value to substitute.

  • b (Integer)

    Blue value to substitute.

Returns:

#apply_embedded_orientationGdkPixbuf::Pixbuf

Takes an existing pixbuf and checks for the presence of an associated "orientation" option.

The orientation option may be provided by the JPEG loader (which reads the exif orientation tag) or the TIFF loader (which reads the TIFF orientation tag, and compensates it for the partial transforms performed by libtiff).

If an orientation option/tag is present, the appropriate transform will be performed so that the pixbuf is oriented correctly.

Returns:

#bits_per_sampleInteger

The number of bits per sample.

Currently only 8 bit per sample are supported.

Returns:

  • (Integer)

    bits-per-sample

#bits_per_sample=(bits_per_sample) ⇒ Integer

The number of bits per sample.

Currently only 8 bit per sample are supported.

Parameters:

  • bits_per_sample (Integer)

Returns:

  • (Integer)

    bits-per-sample

  • (Integer)

    bits-per-sample

#byte_lengthInteger

Returns the length of the pixel data, in bytes.

Returns:

  • (Integer)

    The length of the pixel data.

#colorspaceGdkPixbuf::Colorspace

The color space of the pixbuf.

Currently, only GDK_COLORSPACE_RGB is supported.

Returns:

#colorspace=(colorspace) ⇒ GdkPixbuf::Colorspace

The color space of the pixbuf.

Currently, only GDK_COLORSPACE_RGB is supported.

Parameters:

Returns:

#composite(options) ⇒ nil

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y.

This gives an image in the coordinates of the destination pixbuf. The rectangle (dest_x, dest_y, dest_width, dest_height) is then alpha blended onto the corresponding rectangle of the original destination image.

When the destination rectangle contains parts not in the source image, the data at the edges of the source image is replicated to infinity.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    the Gdk::Pixbuf into which to render the results

  • dest_x (Integer)

    the left coordinate for region to render

  • dest_y (Integer)

    the top coordinate for region to render

  • dest_width (Integer)

    the width of the region to render

  • dest_height (Integer)

    the height of the region to render

  • offset_x (Float)

    the offset in the X direction (currently rounded to an integer)

  • offset_y (Float)

    the offset in the Y direction (currently rounded to an integer)

  • scale_x (Float)

    the scale factor in the X direction

  • scale_y (Float)

    the scale factor in the Y direction

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

  • overall_alpha (Integer)

    overall alpha for source image (0..255)

Returns:

  • (nil)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 305

def composite(options)
  destination = options[:destination] || options[:dest]
  dest_x = options[:destination_x] || options[:dest_x] || 0
  dest_y = options[:destination_y] || options[:dest_y] || 0
  dest_width = options[:destination_width] || options[:dest_width]
  dest_height = options[:destination_height] || options[:dest_height]

  destination ||= Pixbuf.new(:colorspace => colorspace,
                             :has_alpha => has_alpha?,
                             :bits_per_sample => bits_per_sample,
                             :width => dest_x + dest_width,
                             :height => dest_y + dest_height)
  destination.composite!(self, options)
  destination
end

#composite!(source, options) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 321

def composite!(source, options)
  dest_x = options[:destination_x] || options[:dest_x] || 0
  dest_y = options[:destination_y] || options[:dest_y] || 0
  dest_width = options[:destination_width] || options[:dest_width]
  dest_height = options[:destination_height] || options[:dest_height]
  offset_x = options[:offset_x] || 0.0
  offset_y = options[:offset_y] || 0.0
  scale_x = options[:scale_x] || (dest_width / source.width.to_f)
  scale_y = options[:scale_y] || (dest_height / source.height.to_f)
  interpolation_type = options[:interpolation_type] ||
    options[:interp_type] ||
    :bilinear
  overall_alpha = options[:overall_alpha] || 255
  check_x = options[:check_x] || 0
  check_y = options[:check_y] || 0
  check_size = options[:check_size]
  color1 = options[:color1] || 0x999999
  color2 = options[:color2] || 0xdddddd

  if check_size
    source.composite_color(self,
                           dest_x,
                           dest_y,
                           dest_width,
                           dest_height,
                           offset_x,
                           offset_y,
                           scale_x,
                           scale_y,
                           interpolation_type,
                           overall_alpha,
                           check_x,
                           check_y,
                           check_size,
                           color1,
                           color2)
  else
    source.composite_raw(self,
                         dest_x,
                         dest_y,
                         dest_width,
                         dest_height,
                         offset_x,
                         offset_y,
                         scale_x,
                         scale_y,
                         interpolation_type,
                         overall_alpha)
  end
end

#composite_color(dest, dest_x, dest_y, dest_width, dest_height, offset_x, offset_y, scale_x, scale_y, interp_type, overall_alpha, check_x, check_y, check_size, color1, color2) ⇒ nil

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y, then alpha blends the rectangle (dest_x ,dest_y, dest_width, dest_height) of the resulting image with a checkboard of the colors color1 and color2 and renders it onto the destination image.

If the source image has no alpha channel, and overall_alpha is 255, a fast path is used which omits the alpha blending and just performs the scaling.

See gdk_pixbuf_composite_color_simple() for a simpler variant of this function suitable for many tasks.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    the Gdk::Pixbuf into which to render the results

  • dest_x (Integer)

    the left coordinate for region to render

  • dest_y (Integer)

    the top coordinate for region to render

  • dest_width (Integer)

    the width of the region to render

  • dest_height (Integer)

    the height of the region to render

  • offset_x (Float)

    the offset in the X direction (currently rounded to an integer)

  • offset_y (Float)

    the offset in the Y direction (currently rounded to an integer)

  • scale_x (Float)

    the scale factor in the X direction

  • scale_y (Float)

    the scale factor in the Y direction

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

  • overall_alpha (Integer)

    overall alpha for source image (0..255)

  • check_x (Integer)

    the X offset for the checkboard (origin of checkboard is at -check_x, -check_y)

  • check_y (Integer)

    the Y offset for the checkboard

  • check_size (Integer)

    the size of checks in the checkboard (must be a power of two)

  • color1 (Integer)

    the color of check at upper left

  • color2 (Integer)

    the color of the other check

Returns:

  • (nil)

#composite_color_simple(dest_width, dest_height, interp_type, overall_alpha, check_size, color1, color2) ⇒ GdkPixbuf::Pixbuf

Creates a new pixbuf by scaling src to dest_width x dest_height and alpha blending the result with a checkboard of colors color1 and color2.

Parameters:

  • dest_width (Integer)

    the width of destination image

  • dest_height (Integer)

    the height of destination image

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

  • overall_alpha (Integer)

    overall alpha for source image (0..255)

  • check_size (Integer)

    the size of checks in the checkboard (must be a power of two)

  • color1 (Integer)

    the color of check at upper left

  • color2 (Integer)

    the color of the other check

Returns:

#composite_rawnil

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y.

This gives an image in the coordinates of the destination pixbuf. The rectangle (dest_x, dest_y, dest_width, dest_height) is then alpha blended onto the corresponding rectangle of the original destination image.

When the destination rectangle contains parts not in the source image, the data at the edges of the source image is replicated to infinity.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    the Gdk::Pixbuf into which to render the results

  • dest_x (Integer)

    the left coordinate for region to render

  • dest_y (Integer)

    the top coordinate for region to render

  • dest_width (Integer)

    the width of the region to render

  • dest_height (Integer)

    the height of the region to render

  • offset_x (Float)

    the offset in the X direction (currently rounded to an integer)

  • offset_y (Float)

    the offset in the Y direction (currently rounded to an integer)

  • scale_x (Float)

    the scale factor in the X direction

  • scale_y (Float)

    the scale factor in the Y direction

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

  • overall_alpha (Integer)

    overall alpha for source image (0..255)

Returns:

  • (nil)


# File 'lib/gdk_pixbuf2/pixbuf.rb', line 304

#copyGdkPixbuf::Pixbuf

Creates a new GdkPixbuf with a copy of the information in the specified pixbuf.

Note that this does not copy the options set on the original GdkPixbuf, use gdk_pixbuf_copy_options() for this.

Returns:

#copy_area(src_x, src_y, width, height, dest_pixbuf, dest_x, dest_y) ⇒ nil

Copies a rectangular area from src_pixbuf to dest_pixbuf.

Conversion of pixbuf formats is done automatically.

If the source rectangle overlaps the destination rectangle on the same pixbuf, it will be overwritten during the copy operation. Therefore, you can not use this function to scroll a pixbuf.

Parameters:

  • src_x (Integer)

    Source X coordinate within src_pixbuf.

  • src_y (Integer)

    Source Y coordinate within src_pixbuf.

  • width (Integer)

    Width of the area to copy.

  • height (Integer)

    Height of the area to copy.

  • dest_pixbuf (GdkPixbuf::Pixbuf)

    Destination pixbuf.

  • dest_x (Integer)

    X coordinate within dest_pixbuf.

  • dest_y (Integer)

    Y coordinate within dest_pixbuf.

Returns:

  • (nil)

#copy_options(dest_pixbuf) ⇒ Boolean

Copies the key/value pair options attached to a GdkPixbuf to another GdkPixbuf.

This is useful to keep original metadata after having manipulated a file. However be careful to remove metadata which you've already applied, such as the "orientation" option after rotating the image.

Parameters:

Returns:

  • (Boolean)

    TRUE on success.

#dupObject



176
177
178
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 176

def dup
  copy
end

#fill(pixel) ⇒ nil

Clears a pixbuf to the given RGBA value, converting the RGBA value into the pixbuf's pixel format.

The alpha component will be ignored if the pixbuf doesn't have an alpha channel.

Parameters:

  • pixel (Integer)

    RGBA pixel to used to clear (0xffffffff is opaque white, 0x00000000 transparent black)

Returns:

  • (nil)

#fill!(pixel) ⇒ Object



186
187
188
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 186

def fill!(pixel)
  fill(pixel)
end

#flip(horizontal) ⇒ GdkPixbuf::Pixbuf

Flips a pixbuf horizontally or vertically and returns the result in a new pixbuf.

Parameters:

  • horizontal (Boolean)

    TRUE to flip horizontally, FALSE to flip vertically

Returns:

#get_option(key) ⇒ String

Looks up key in the list of options that may have been attached to the pixbuf when it was loaded, or that may have been attached by another function using gdk_pixbuf_set_option().

For instance, the ANI loader provides "Title" and "Artist" options. The ICO, XBM, and XPM loaders provide "x_hot" and "y_hot" hot-spot options for cursor definitions. The PNG loader provides the tEXt ancillary chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders return an "orientation" option string that corresponds to the embedded TIFF/Exif orientation tag (if present). Since 2.32, the TIFF loader sets the "multipage" option string to "yes" when a multi-page TIFF is loaded. Since 2.32 the JPEG and PNG loaders set "x-dpi" and "y-dpi" if the file contains image density information in dots per inch. Since 2.36.6, the JPEG loader sets the "comment" option with the comment EXIF tag.

Parameters:

  • key (String)

    a nul-terminated string.

Returns:

  • (String)

    the value associated with key

#get_pixels_with_length(length) ⇒ Array<Integer>

Queries a pointer to the pixel data of a pixbuf.

This function will cause an implicit copy of the pixbuf data if the pixbuf was created from read-only data.

Please see the section on image data for information about how the pixel data is stored in memory. pixel data.

Parameters:

  • length (Integer)

    The length of the binary data.

Returns:

  • (Array<Integer>)

    A pointer to the pixbuf's

#has_alphaBoolean

Queries whether a pixbuf has an alpha channel (opacity information).

Returns:

  • (Boolean)

    TRUE if it has an alpha channel, FALSE otherwise.

#has_alpha=(has_alpha) ⇒ Boolean

Whether the pixbuf has an alpha channel.

Parameters:

  • has_alpha (Boolean)

Returns:

  • (Boolean)

    has-alpha

  • (Boolean)

    has-alpha

#has_alpha?Boolean

Whether the pixbuf has an alpha channel.

Returns:

  • (Boolean)

    has-alpha

#heightInteger

The number of rows of the pixbuf.

Returns:

  • (Integer)

    height

#height=(height) ⇒ Integer

The number of rows of the pixbuf.

Parameters:

  • height (Integer)

Returns:

  • (Integer)

    height

  • (Integer)

    height

#initialize_rawGdkPixbuf::Pixbuf

Creates a new pixbuf by parsing XPM data in memory.

This data is commonly the result of including an XPM file into a program's C source.

Parameters:

  • data (Array<String>)

    Pointer to inline XPM data.

Returns:



# File 'lib/gdk_pixbuf2/pixbuf.rb', line 19

#n_channelsInteger

The number of samples per pixel.

Currently, only 3 or 4 samples per pixel are supported.

Returns:

  • (Integer)

    n-channels

#n_channels=(n_channels) ⇒ Integer

The number of samples per pixel.

Currently, only 3 or 4 samples per pixel are supported.

Parameters:

  • n_channels (Integer)

Returns:

  • (Integer)

    n-channels

  • (Integer)

    n-channels

#new_subpixbuf(src_x, src_y, width, height) ⇒ GdkPixbuf::Pixbuf

Creates a new pixbuf which represents a sub-region of src_pixbuf.

The new pixbuf shares its pixels with the original pixbuf, so writing to one affects both. The new pixbuf holds a reference to src_pixbuf, so src_pixbuf will not be finalized until the new pixbuf is finalized.

Note that if src_pixbuf is read-only, this function will force it to be mutable.

Parameters:

  • src_x (Integer)

    X coord in src_pixbuf

  • src_y (Integer)

    Y coord in src_pixbuf

  • width (Integer)

    width of region in src_pixbuf

  • height (Integer)

    height of region in src_pixbuf

Returns:

#optionsGLib::HashTable<String>

Returns a GHashTable with a list of all the options that may have been attached to the pixbuf when it was loaded, or that may have been attached by another function using [methodGdkPixbuf.Pixbuf.set_option].

Returns:

  • (GLib::HashTable<String>)

    a GHash::Table of key/values pairs

#pixel_bytesGLib::Bytes

Returns pixel-bytes.

Returns:

  • (GLib::Bytes)

    pixel-bytes

#pixel_bytes=(pixel_bytes) ⇒ GLib::Bytes

Parameters:

  • pixel_bytes (GLib::Bytes)

Returns:

  • (GLib::Bytes)

    pixel-bytes

  • (GLib::Bytes)

    pixel-bytes

#pixels=(pixels) ⇒ GObject

A pointer to the pixel data of the pixbuf.

Parameters:

  • pixels (GObject)

Returns:

  • (GObject)

    pixels

  • (GObject)

    pixels

#read_pixel_bytesGLib::Bytes

Provides a #GBytes buffer containing the raw pixel data; the data must not be modified.

This function allows skipping the implicit copy that must be made if gdk_pixbuf_get_pixels() is called on a read-only pixbuf.

Returns:

  • (GLib::Bytes)

    A new reference to a read-only copy of the pixel data. Note that for mutable pixbufs, this function will incur a one-time copy of the pixel data for conversion into the returned #GBytes.

#read_pixelsInteger

Provides a read-only pointer to the raw pixel data.

This function allows skipping the implicit copy that must be made if gdk_pixbuf_get_pixels() is called on a read-only pixbuf.

Returns:

  • (Integer)

    a read-only pointer to the raw pixel data

#refGdkPixbuf::Pixbuf

Adds a reference to a pixbuf.

Returns:

#remove_option(key) ⇒ Boolean

Removes the key/value pair option attached to a GdkPixbuf.

Parameters:

  • key (String)

    a nul-terminated string representing the key to remove.

Returns:

  • (Boolean)

    TRUE if an option was removed, FALSE if not.

#rotate(angle) ⇒ Object



190
191
192
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 190

def rotate(angle)
  rotate_simple(angle)
end

#rotate_simple(angle) ⇒ GdkPixbuf::Pixbuf

Rotates a pixbuf by a multiple of 90 degrees, and returns the result in a new pixbuf.

If angle is 0, this function will return a copy of src.

Parameters:

Returns:

#rowstrideInteger Also known as: row_stride

The number of bytes between the start of a row and the start of the next row.

This number must (obviously) be at least as large as the width of the pixbuf.

Returns:

  • (Integer)

    rowstride

#rowstride=(rowstride) ⇒ Integer

The number of bytes between the start of a row and the start of the next row.

This number must (obviously) be at least as large as the width of the pixbuf.

Parameters:

  • rowstride (Integer)

Returns:

  • (Integer)

    rowstride

  • (Integer)

    rowstride

#saturate_and_pixelate(saturation, pixelate) ⇒ nil

Modifies saturation and optionally pixelates src, placing the result in dest.

The src and dest pixbufs must have the same image format, size, and rowstride.

The src and dest arguments may be the same pixbuf with no ill effects.

If saturation is 1.0 then saturation is not changed. If it's less than 1.0, saturation is reduced (the image turns toward grayscale); if greater than 1.0, saturation is increased (the image gets more vivid colors).

If pixelate is TRUE, then pixels are faded in a checkerboard pattern to create a pixelated image.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    place to write modified version of src

  • saturation (GdkPixbuf::gfloat)

    saturation factor

  • pixelate (Boolean)

    whether to pixelate

Returns:

  • (nil)


195
196
197
198
199
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 195

def saturate_and_pixelate(saturation, pixelate)
  dest = dup
  saturate_and_pixelate_raw(dest, saturation, pixelate)
  dest
end

#saturate_and_pixelate_rawnil

Modifies saturation and optionally pixelates src, placing the result in dest.

The src and dest pixbufs must have the same image format, size, and rowstride.

The src and dest arguments may be the same pixbuf with no ill effects.

If saturation is 1.0 then saturation is not changed. If it's less than 1.0, saturation is reduced (the image turns toward grayscale); if greater than 1.0, saturation is increased (the image gets more vivid colors).

If pixelate is TRUE, then pixels are faded in a checkerboard pattern to create a pixelated image.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    place to write modified version of src

  • saturation (GdkPixbuf::gfloat)

    saturation factor

  • pixelate (Boolean)

    whether to pixelate

Returns:

  • (nil)


# File 'lib/gdk_pixbuf2/pixbuf.rb', line 194

#save(type, options = {}) ⇒ String, void #save(filename, type, options = {}) ⇒ void

Overloads:

  • #save(type, options = {}) ⇒ String, void

    Save as type format.

    Parameters:

    • type (String)

      The format to be saved. "jpeg", "png", "ico" and "bmp" are available by default.

    • options (Hash<Symbol, String>) (defaults to: {})

      The options for saving. Key-value pairs except :filename key are passed to save logic. Available keys are depended on format. For example, :quality => "100" is available in "jpeg" format.

    Options Hash (options):

    • :filename (String)

      The filename to be outputted.

    Returns:

    • (String, void)

      The saved data. If you specify :filename option, it returns nothing.

  • #save(filename, type, options = {}) ⇒ void
    Deprecated.

    since 3.1.1. Use save(type, :filename => filename) instead.

    This method returns an undefined value.

    Save to filename as type format.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 226

def save(*args)
  case args.size
  when 1
    arg1 = args[0]
    if arg1.respond_to?(:to_path)
      filename = arg1.to_path
    elsif arg1.is_a?(String) and arg1.include?(".")
      filename = args[0]
    else
      filename = nil
      type = arg1
    end
    if filename
      type = File.extname(filename).gsub(/\A\./, "").downcase
      case type
      when "jpg"
        type = "jpeg"
      end
    end
    options = {}
  when 2
    if args.last.is_a?(Hash)
      type, options = args
      if options.key?(:filename)
        options = options.dup
        filename = options.delete(:filename)
      else
        filename = nil
      end
    else
      filename, type = args
      options = {}
    end
  when 3
    filename, type, options = args
  else
    message = "wrong number of arguments (given #{args.size}, expected 1..3)"
    raise ArgumentError, message
  end

  keys = []
  values = []
  options.each do |key, value|
    key = key.to_s if key.is_a?(Symbol)
    keys << key
    values << value
  end
  if filename
    savev(filename, type, keys, values)
  else
    _, data = save_to_bufferv(type, keys, values)
    data.pack("C*")
  end
end

#save_to_buffer(buffer, buffer_size, type, error, array) ⇒ Boolean

Saves pixbuf to a new buffer in format type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".

This is a convenience function that uses gdk_pixbuf_save_to_callback() to do the real work.

Note that the buffer is not NUL-terminated and may contain embedded NUL characters.

If error is set, FALSE will be returned and buffer will be set to NULL. Possible errors include those in the GDK_PIXBUF_ERROR domain.

See gdk_pixbuf_save() for more details.

Parameters:

  • buffer (Array<Integer>)

    location to receive a pointer to the new buffer.

  • buffer_size (Integer)

    location to receive the size of the new buffer.

  • type (String)

    name of file format.

  • error (GLib::Error)

    return location for error, or NULL

  • array (Array)

    list of key-value save options

Returns:

  • (Boolean)

    whether an error was set

#save_to_bufferv(buffer, buffer_size, type, option_keys, option_values) ⇒ Boolean

Vector version of gdk_pixbuf_save_to_buffer().

Saves pixbuf to a new buffer in format type, which is currently "jpeg", "tiff", "png", "ico" or "bmp".

See [methodGdkPixbuf.Pixbuf.save_to_buffer] for more details.

Parameters:

  • buffer (Array<Integer>)

    location to receive a pointer to the new buffer.

  • buffer_size (Integer)

    location to receive the size of the new buffer.

  • type (String)

    name of file format.

  • option_keys (Array<String>)

    name of options to set

  • option_values (Array<String>)

    values for named options

Returns:

  • (Boolean)

    whether an error was set

#save_to_callback(save_func, user_data, type, error, array) ⇒ Boolean

Saves pixbuf in format type by feeding the produced data to a callback.

This function can be used when you want to store the image to something other than a file, such as an in-memory buffer or a socket.

If error is set, FALSE will be returned. Possible errors include those in the GDK_PIXBUF_ERROR domain and whatever the save function generates.

See [methodGdkPixbuf.Pixbuf.save] for more details.

Parameters:

  • save_func (GdkPixbuf::PixbufSaveFunc)

    a function that is called to save each block of data that the save routine generates.

  • user_data (GObject)

    user data to pass to the save function.

  • type (String)

    name of file format.

  • error (GLib::Error)

    return location for error, or NULL

  • array (Array)

    list of key-value save options

Returns:

  • (Boolean)

    whether an error was set

#save_to_callbackv(save_func, user_data, type, option_keys, option_values) ⇒ Boolean

Vector version of gdk_pixbuf_save_to_callback().

Saves pixbuf to a callback in format type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".

If error is set, FALSE will be returned.

See [methodGdkPixbuf.Pixbuf.save_to_callback] for more details.

Parameters:

  • save_func (GdkPixbuf::PixbufSaveFunc)

    a function that is called to save each block of data that the save routine generates.

  • user_data (GObject)

    user data to pass to the save function.

  • type (String)

    name of file format.

  • option_keys (Array<String>)

    name of options to set

  • option_values (Array<String>)

    values for named options

Returns:

  • (Boolean)

    whether an error was set

#save_to_stream(stream, type, cancellable, error, array) ⇒ Boolean

Saves pixbuf to an output stream.

Supported file formats are currently "jpeg", "tiff", "png", "ico" or "bmp". See gdk_pixbuf_save_to_buffer() for more details.

The cancellable can be used to abort the operation from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned. Other possible errors are in the GDK_PIXBUF_ERROR and G_IO_ERROR domains.

The stream is not closed at the end of this call.

Parameters:

  • stream (Gio::OutputStream)

    a GOutputStream to save the pixbuf to

  • type (String)

    name of file format

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • error (GLib::Error)

    return location for error, or NULL

  • array (Array)

    list of key-value save options

Returns:

  • (Boolean)

    TRUE if the pixbuf was saved successfully, FALSE if an error was set.

#save_to_stream_async(stream, type, cancellable, callback, user_data, array) ⇒ nil

Saves pixbuf to an output stream asynchronously.

For more details see gdk_pixbuf_save_to_stream(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread.

You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.

Parameters:

  • stream (Gio::OutputStream)

    a GOutputStream to which to save the pixbuf

  • type (String)

    name of file format

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • callback (Gio::AsyncReadyCallback)

    a GAsyncReadyCallback to call when the pixbuf is saved

  • user_data (GObject)

    the data to pass to the callback function

  • array (Array)

    list of key-value save options

Returns:

  • (nil)

#save_to_streamv(stream, type, option_keys, option_values, cancellable) ⇒ Boolean

Saves pixbuf to an output stream.

Supported file formats are currently "jpeg", "tiff", "png", "ico" or "bmp".

See [methodGdkPixbuf.Pixbuf.save_to_stream] for more details.

Parameters:

  • stream (Gio::OutputStream)

    a GOutputStream to save the pixbuf to

  • type (String)

    name of file format

  • option_keys (Array<String>)

    name of options to set

  • option_values (Array<String>)

    values for named options

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

Returns:

  • (Boolean)

    TRUE if the pixbuf was saved successfully, FALSE if an error was set.

#save_to_streamv_async(stream, type, option_keys, option_values, cancellable, callback, user_data) ⇒ nil

Saves pixbuf to an output stream asynchronously.

For more details see gdk_pixbuf_save_to_streamv(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread.

You can then call gdk_pixbuf_save_to_stream_finish() to get the result of the operation.

Parameters:

  • stream (Gio::OutputStream)

    a GOutputStream to which to save the pixbuf

  • type (String)

    name of file format

  • option_keys (Array<String>)

    name of options to set

  • option_values (Array<String>)

    values for named options

  • cancellable (Gio::Cancellable)

    optional GCancellable object, NULL to ignore

  • callback (Gio::AsyncReadyCallback)

    a GAsyncReadyCallback to call when the pixbuf is saved

  • user_data (GObject)

    the data to pass to the callback function

Returns:

  • (nil)

#savev(filename, type, option_keys, option_values) ⇒ Boolean

Vector version of gdk_pixbuf_save().

Saves pixbuf to a file in type, which is currently "jpeg", "png", "tiff", "ico" or "bmp".

If error is set, FALSE will be returned.

See [methodGdkPixbuf.Pixbuf.save] for more details.

Parameters:

  • filename (GdkPixbuf::filename)

    name of file to save.

  • type (String)

    name of file format.

  • option_keys (Array<String>)

    name of options to set

  • option_values (Array<String>)

    values for named options

Returns:

  • (Boolean)

    whether an error was set

#scale(*args) ⇒ Object

TODO: test TODO: Improve API by Hash



284
285
286
287
288
289
290
291
292
293
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 284

def scale(*args)
  case args.size
  when 2, 3
    width, height, interp_type = args
    interp_type ||= :bilinear
    scale_simple(width, height, interp_type)
  else
    scale_raw(*args)
  end
end

#scale!(source, *args) ⇒ Object

TODO: test TODO: Improve API by Hash



297
298
299
300
301
302
# File 'lib/gdk_pixbuf2/pixbuf.rb', line 297

def scale!(source, *args)
  if args.size == 8
    args << :bilinear
  end
  source.scale_raw(self, *args)
end

#scale_rawnil

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y, then renders the rectangle (dest_x, dest_y, dest_width, dest_height) of the resulting image onto the destination image replacing the previous contents.

Try to use gdk_pixbuf_scale_simple() first; this function is the industrial-strength power tool you can fall back to, if gdk_pixbuf_scale_simple() isn't powerful enough.

If the source rectangle overlaps the destination rectangle on the same pixbuf, it will be overwritten during the scaling which results in rendering artifacts.

Parameters:

  • dest (GdkPixbuf::Pixbuf)

    the Gdk::Pixbuf into which to render the results

  • dest_x (Integer)

    the left coordinate for region to render

  • dest_y (Integer)

    the top coordinate for region to render

  • dest_width (Integer)

    the width of the region to render

  • dest_height (Integer)

    the height of the region to render

  • offset_x (Float)

    the offset in the X direction (currently rounded to an integer)

  • offset_y (Float)

    the offset in the Y direction (currently rounded to an integer)

  • scale_x (Float)

    the scale factor in the X direction

  • scale_y (Float)

    the scale factor in the Y direction

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

Returns:

  • (nil)


# File 'lib/gdk_pixbuf2/pixbuf.rb', line 281

#scale_simple(dest_width, dest_height, interp_type) ⇒ GdkPixbuf::Pixbuf

Create a new pixbuf containing a copy of src scaled to dest_width x dest_height.

This function leaves src unaffected.

The interp_type should be GDK_INTERP_NEAREST if you want maximum speed (but when scaling down GDK_INTERP_NEAREST is usually unusably ugly). The default interp_type should be GDK_INTERP_BILINEAR which offers reasonable quality and speed.

You can scale a sub-portion of src by creating a sub-pixbuf pointing into src; see [methodGdkPixbuf.Pixbuf.new_subpixbuf].

If dest_width and dest_height are equal to the width and height of src, this function will return an unscaled copy of src.

For more complicated scaling/alpha blending see [methodGdkPixbuf.Pixbuf.scale] and [methodGdkPixbuf.Pixbuf.composite].

Parameters:

  • dest_width (Integer)

    the width of destination image

  • dest_height (Integer)

    the height of destination image

  • interp_type (GdkPixbuf::InterpType)

    the interpolation type for the transformation.

Returns:

#set_option(key, value) ⇒ Boolean

Attaches a key/value pair as an option to a GdkPixbuf.

If key already exists in the list of options attached to the pixbuf, the new value is ignored and FALSE is returned.

Parameters:

  • key (String)

    a nul-terminated string.

  • value (String)

    a nul-terminated string.

Returns:

  • (Boolean)

    TRUE on success

#unrefnil

Removes a reference from a pixbuf.

Returns:

  • (nil)

#widthInteger

The number of columns of the pixbuf.

Returns:

  • (Integer)

    width

#width=(width) ⇒ Integer

The number of columns of the pixbuf.

Parameters:

  • width (Integer)

Returns:

  • (Integer)

    width

  • (Integer)

    width