CdkPixbuf.Pixbuf
Description: Reference for CdkPixbuf.Pixbuf

### Class

# CdkPixbufPixbuf

#### Description [src]

    final class CdkPixbuf.Pixbuf : GObject.Object
      implements Gio.Icon, Gio.LoadableIcon {
      /* No available fields */
    }

A pixel buffer.

`CdkPixbuf` 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 `CdkPixbuf`

The most basic way to create a pixbuf is to wrap an existing pixel buffer with a `CdkPixbuf` instance. You can use the `cdk_pixbuf_new_from_data()` function to do this.

Every time you create a new `CdkPixbuf` 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 `CdkPixbuf` 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 `cdk_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 `cdk_pixbuf_new_from_data()`. The `cdk_pixbuf_new()` function will compute an optimal rowstride so that rendering can be performed with an efficient algorithm.

You can also copy an existing pixbuf with the `cdk_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 `CdkPixbuf` instance for it.

## Reference counting

`CdkPixbuf` 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 `CdkPixbuf` will be freed when its reference count drops to zero. Newly-created `CdkPixbuf` 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 `cdk_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 `cdk_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 `CdkPixbuf` 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 (CdkPixbuf *pixbuf,
               int x,
           int y,
           guchar red,
           guchar green,
           guchar blue,
           guchar alpha)
    {
      int n_channels = cdk_pixbuf_get_n_channels (pixbuf);

      // Ensure that the pixbuf is valid
      g_assert (cdk_pixbuf_get_colorspace (pixbuf) == CDK_COLORSPACE_RGB);
      g_assert (cdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
      g_assert (cdk_pixbuf_get_has_alpha (pixbuf));
      g_assert (n_channels == 4);

      int width = cdk_pixbuf_get_width (pixbuf);
      int height = cdk_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 = cdk_pixbuf_get_rowstride (pixbuf);

      // The pixel buffer in the CdkPixbuf instance
      guchar *pixels = cdk_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 `CdkPixBuf` 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, `CdkPixbuf` provides the `CdkPixbufLoader` API for progressive image loading.

## Saving images

The `CdkPixbuf` 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. `CdkPixbuf` 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.

#### Ancestors

- GObject

#### Implements

- GIcon
- GLoadableIcon

#### Constructors

###### cdk_pixbuf_new

Creates a new `CdkPixbuf` structure and allocates a buffer for it.

###### cdk_pixbuf_new_from_bytes

Creates a new `CdkPixbuf` out of in-memory readonly image data.

since: 2.32

###### cdk_pixbuf_new_from_data

Creates a new `CdkPixbuf` out of in-memory image data.

###### cdk_pixbuf_new_from_file

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

###### cdk_pixbuf_new_from_file_at_scale

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

since: 2.6

###### cdk_pixbuf_new_from_file_at_size

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

since: 2.4

###### cdk_pixbuf_new_from_inline

Creates a `CdkPixbuf` from a flat representation that is suitable for storing as inline data in a program.

deprecated: 2.32

###### cdk_pixbuf_new_from_resource

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

since: 2.26

###### cdk_pixbuf_new_from_resource_at_scale

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

since: 2.26

###### cdk_pixbuf_new_from_stream

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

since: 2.14

###### cdk_pixbuf_new_from_stream_at_scale

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

since: 2.14

###### cdk_pixbuf_new_from_stream_finish

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

since: 2.24

###### cdk_pixbuf_new_from_xpm_data

Creates a new pixbuf by parsing XPM data in memory.

deprecated: 2.44

#### Functions

###### cdk_pixbuf_calculate_rowstride

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

since: 2.36.8

###### cdk_pixbuf_get_file_info

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

since: 2.4

###### cdk_pixbuf_get_file_info_async

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

since: 2.32

###### cdk_pixbuf_get_file_info_finish

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

since: 2.32

###### cdk_pixbuf_get_formats

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

since: 2.2

###### cdk_pixbuf_init_modules

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

since: 2.40

###### cdk_pixbuf_new_from_stream_async

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

since: 2.24

###### cdk_pixbuf_new_from_stream_at_scale_async

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

since: 2.24

###### cdk_pixbuf_save_to_stream_finish

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

since: 2.24

#### Instance methods

###### cdk_pixbuf_add_alpha

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

###### cdk_pixbuf_apply_embedded_orientation

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

since: 2.12

###### cdk_pixbuf_composite

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

###### cdk_pixbuf_composite_color

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.

###### cdk_pixbuf_composite_color_simple

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`.

###### cdk_pixbuf_copy

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

###### cdk_pixbuf_copy_area

Copies a rectangular area from `src_pixbuf` to `dest_pixbuf`.

###### cdk_pixbuf_copy_options

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

since: 2.36

###### cdk_pixbuf_fill

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

###### cdk_pixbuf_flip

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

since: 2.6

###### cdk_pixbuf_get_bits_per_sample

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

###### cdk_pixbuf_get_byte_length

Returns the length of the pixel data, in bytes.

since: 2.26

###### cdk_pixbuf_get_colorspace

Queries the color space of a pixbuf.

###### cdk_pixbuf_get_has_alpha

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

###### cdk_pixbuf_get_height

Queries the height of a pixbuf.

###### cdk_pixbuf_get_n_channels

Queries the number of channels of a pixbuf.

###### cdk_pixbuf_get_option

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 cdk_pixbuf_set_option().

###### cdk_pixbuf_get_options

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 `cdk_pixbuf_set_option()`.

since: 2.32

###### cdk_pixbuf_get_pixels

Queries a pointer to the pixel data of a pixbuf.

###### cdk_pixbuf_get_pixels_with_length

Queries a pointer to the pixel data of a pixbuf.

since: 2.26

###### cdk_pixbuf_get_rowstride

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.

###### cdk_pixbuf_get_width

Queries the width of a pixbuf.

###### cdk_pixbuf_new_subpixbuf

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

###### cdk_pixbuf_read_pixel_bytes

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

since: 2.32

###### cdk_pixbuf_read_pixels

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

since: 2.32

###### cdk_pixbuf_ref

Adds a reference to a pixbuf.

deprecated: 2.0

###### cdk_pixbuf_remove_option

Removes the key/value pair option attached to a `CdkPixbuf`.

since: 2.36

###### cdk_pixbuf_rotate_simple

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

since: 2.6

###### cdk_pixbuf_saturate_and_pixelate

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

###### cdk_pixbuf_save

Saves pixbuf to a file in format `type`. By default, “jpeg”, “png”, “ico” and “bmp” are possible file formats to save in, but more formats may be installed. The list of all writable formats can be determined in the following way:.

###### cdk_pixbuf_save_to_buffer

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

since: 2.4

###### cdk_pixbuf_save_to_bufferv

Vector version of `cdk_pixbuf_save_to_buffer()`.

since: 2.4

###### cdk_pixbuf_save_to_callback

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

since: 2.4

###### cdk_pixbuf_save_to_callbackv

Vector version of `cdk_pixbuf_save_to_callback()`.

since: 2.4

###### cdk_pixbuf_save_to_stream

Saves `pixbuf` to an output stream.

since: 2.14

###### cdk_pixbuf_save_to_stream_async

Saves `pixbuf` to an output stream asynchronously.

since: 2.24

###### cdk_pixbuf_save_to_streamv

Saves `pixbuf` to an output stream.

since: 2.36

###### cdk_pixbuf_save_to_streamv_async

Saves `pixbuf` to an output stream asynchronously.

since: 2.36

###### cdk_pixbuf_savev

Vector version of `cdk_pixbuf_save()`.

###### cdk_pixbuf_scale

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.

###### cdk_pixbuf_scale_simple

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

###### cdk_pixbuf_set_option

Attaches a key/value pair as an option to a `CdkPixbuf`.

since: 2.2

###### cdk_pixbuf_unref

Removes a reference from a pixbuf.

deprecated: 2.0

##### Methods inherited from GObject (43)

Please see GObject for a full list of methods.

##### Methods inherited from GIcon (4)

###### g_icon_equal

Checks if two icons are equal.

###### g_icon_hash

Gets a hash for an icon.

###### g_icon_serialize

Serializes a `GIcon` into a `GVariant`. An equivalent `GIcon` can be retrieved back by calling `g_icon_deserialize()` on the returned value. As serialization will avoid using raw icon data when possible, it only makes sense to transfer the `GVariant` between processes on the same machine, (as opposed to over the network), and within the same file system namespace.

###### g_icon_to_string

Generates a textual representation of `icon` that can be used for serialization such as when passing `icon` to a different process or saving it to persistent storage. Use `g_icon_new_for_string()` to get `icon` back from the returned string.

##### Methods inherited from GLoadableIcon (3)

###### g_loadable_icon_load

Loads a loadable icon. For the asynchronous version of this function, see g_loadable_icon_load_async().

###### g_loadable_icon_load_async

Loads an icon asynchronously. To finish this function, see g_loadable_icon_load_finish(). For the synchronous, blocking version of this function, see g_loadable_icon_load().

###### g_loadable_icon_load_finish

Finishes an asynchronous icon load started in g_loadable_icon_load_async().

#### Properties

###### CdkPixbuf.Pixbuf:bits-per-sample

The number of bits per sample.

###### CdkPixbuf.Pixbuf:colorspace

The color space of the pixbuf.

###### CdkPixbuf.Pixbuf:has-alpha

Whether the pixbuf has an alpha channel.

###### CdkPixbuf.Pixbuf:height

The number of rows of the pixbuf.

###### CdkPixbuf.Pixbuf:n-channels

The number of samples per pixel.

###### CdkPixbuf.Pixbuf:pixel-bytes

No description available.

###### CdkPixbuf.Pixbuf:pixels

A pointer to the pixel data of the pixbuf.

###### CdkPixbuf.Pixbuf:rowstride

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

###### CdkPixbuf.Pixbuf:width

The number of columns of the pixbuf.

#### Signals

##### Signals inherited from GObject (1)

###### GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.