Pixbuf

open class Pixbuf(pointer: <Error class: unknown class><<Error class: unknown class>>)

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 class@GdkPixbuf.Pixbuf instance. You can use the `ctor@GdkPixbuf.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@GdkPixbuf.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@GdkPixbuf.Pixbuf.new_from_xpm_data` function to create a pixbuf from inline XPM image data.

You can also copy an existing pixbuf with the method@Pixbuf.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 class@Pixbuf 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@GdkPixbuf.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 method@GdkPixbuf.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 class@GdkPixbuf.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.

Skipped during bindings generation

  • parameter r: guint8

  • method get_pixels: gdk_pixbuf_get_pixels is shadowedBy get_pixels_with_length

  • parameter length: length: Out parameter is not supported

  • method read_pixels: Return type guint8 is unsupported

  • parameter buffer: buffer: Out parameter is not supported

  • method pixel-bytes: Property has no getter nor setter

  • method pixels: Property has no getter nor setter

  • parameter data: guint8

  • parameter data: guint8

  • parameter width: width: Out parameter is not supported

  • parameter width: width: Out parameter is not supported

Constructors

Link copied to clipboard
constructor(colorspace: Colorspace, hasAlpha: Boolean, bitsPerSample: Int, width: Int, height: Int)

Creates a new GdkPixbuf structure and allocates a buffer for it.

constructor(data: <Error class: unknown class>, colorspace: Colorspace, hasAlpha: Boolean, bitsPerSample: Int, width: Int, height: Int, rowstride: Int)

Creates a new #GdkPixbuf out of in-memory readonly image data.

constructor(filename: String)

Creates a new pixbuf by loading an image from a file.

constructor(filename: String, width: Int, height: Int, preserveAspectRatio: Boolean)

Creates a new pixbuf by loading an image from a file.

constructor(filename: String, width: Int, height: Int)

Creates a new pixbuf by loading an image from a file.

constructor(stream: <Error class: unknown class>, cancellable: <Error class: unknown class>? = null)

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

constructor(stream: <Error class: unknown class>, width: Int, height: Int, preserveAspectRatio: Boolean, cancellable: <Error class: unknown class>? = null)

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

constructor(asyncResult: <Error class: unknown class>)

Finishes an asynchronous pixbuf creation operation started with gdk_pixbuf_new_from_stream_async().

constructor(data: List<String>)

Creates a new pixbuf by parsing XPM data in memory.

constructor(pointer: <Error class: unknown class><<Error class: unknown class>>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open val bitsPerSample: Int

The number of bits per sample.

Link copied to clipboard

The color space of the pixbuf.

Link copied to clipboard
val gdkpixbufPixbufPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard
open val gioIconPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard
open val gioLoadableIconPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard
open val hasAlpha: Boolean

Whether the pixbuf has an alpha channel.

Link copied to clipboard
open val height: Int

The number of rows of the pixbuf.

Link copied to clipboard
open val nChannels: Int

The number of samples per pixel.

Link copied to clipboard
open val rowstride: Int

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

Link copied to clipboard
open val width: Int

The number of columns of the pixbuf.

Functions

Link copied to clipboard

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

Link copied to clipboard
open fun composite(dest: Pixbuf, destX: Int, destY: Int, destWidth: Int, destHeight: Int, offsetX: Double, offsetY: Double, scaleX: Double, scaleY: Double, interpType: InterpType, overallAlpha: Int)

Creates a transformation of the source image @src by scaling by

Link copied to clipboard
open fun compositeColor(dest: Pixbuf, destX: Int, destY: Int, destWidth: Int, destHeight: Int, offsetX: Double, offsetY: Double, scaleX: Double, scaleY: Double, interpType: InterpType, overallAlpha: Int, checkX: Int, checkY: Int, checkSize: Int, color1: <Error class: unknown class>, color2: <Error class: unknown class>)

Creates a transformation of the source image @src by scaling by

Link copied to clipboard
open fun compositeColorSimple(destWidth: Int, destHeight: Int, interpType: InterpType, overallAlpha: Int, checkSize: Int, color1: <Error class: unknown class>, color2: <Error class: unknown class>): 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.

Link copied to clipboard
open fun copy(): Pixbuf?

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

Link copied to clipboard
open fun copyArea(srcX: Int, srcY: Int, width: Int, height: Int, destPixbuf: Pixbuf, destX: Int, destY: Int)

Copies a rectangular area from src_pixbuf to dest_pixbuf.

Link copied to clipboard
open fun copyOptions(destPixbuf: Pixbuf): Boolean

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

Link copied to clipboard
open fun fill(pixel: <Error class: unknown class>)

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

Link copied to clipboard
open fun flip(horizontal: Boolean): Pixbuf?

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

Link copied to clipboard
open fun getBitsPerSample(): Int

Queries the number of bits per color sample in a pixbuf.

Link copied to clipboard
open fun getByteLength(): <Error class: unknown class>

Returns the length of the pixel data, in bytes.

Link copied to clipboard

Queries the color space of a pixbuf.

Link copied to clipboard
open fun getHasAlpha(): Boolean

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

Link copied to clipboard
open fun getHeight(): Int

Queries the height of a pixbuf.

Link copied to clipboard
open fun getNChannels(): Int

Queries the number of channels of a pixbuf.

Link copied to clipboard
open fun getOption(key: String): String?

Looks up @key in the list of options that may have been attached to the

Link copied to clipboard
open fun getOptions(): <Error class: unknown class>

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 method@GdkPixbuf.Pixbuf.set_option.

Link copied to clipboard
open fun getRowstride(): Int

Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row and the start of the next row.

Link copied to clipboard
open fun getWidth(): Int

Queries the width of a pixbuf.

Link copied to clipboard
open fun newSubpixbuf(srcX: Int, srcY: Int, width: Int, height: Int): Pixbuf

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

Link copied to clipboard
open fun readPixelBytes(): <Error class: unknown class>

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

Link copied to clipboard
open fun removeOption(key: String): Boolean

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

Link copied to clipboard

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

Link copied to clipboard
open fun saturateAndPixelate(dest: Pixbuf, saturation: Float, pixelate: Boolean)

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

Link copied to clipboard
open fun saveToCallbackv(saveFunc: PixbufSaveFunc, type: String, optionKeys: List<String>? = null, optionValues: List<String>? = null): <Error class: unknown class><Boolean>

Vector version of gdk_pixbuf_save_to_callback().

Link copied to clipboard
open fun saveToStreamv(stream: <Error class: unknown class>, type: String, optionKeys: List<String>? = null, optionValues: List<String>? = null, cancellable: <Error class: unknown class>? = null): <Error class: unknown class><Boolean>

Saves pixbuf to an output stream.

Link copied to clipboard
open fun saveToStreamvAsync(stream: <Error class: unknown class>, type: String, optionKeys: List<String>? = null, optionValues: List<String>? = null, cancellable: <Error class: unknown class>? = null, callback: <Error class: unknown class>)

Saves pixbuf to an output stream asynchronously.

Link copied to clipboard
open fun savev(filename: String, type: String, optionKeys: List<String>? = null, optionValues: List<String>? = null): <Error class: unknown class><Boolean>

Vector version of gdk_pixbuf_save().

Link copied to clipboard
open fun scale(dest: Pixbuf, destX: Int, destY: Int, destWidth: Int, destHeight: Int, offsetX: Double, offsetY: Double, scaleX: Double, scaleY: Double, interpType: InterpType)

Creates a transformation of the source image @src by scaling by

Link copied to clipboard
open fun scaleSimple(destWidth: Int, destHeight: Int, interpType: InterpType): Pixbuf?

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

Link copied to clipboard
open fun setOption(key: String, value: String): Boolean

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