gdkpixbuf.pixbuf
Module for [Pixbuf] class
Types 3
A pixel buffer.
[gdkpixbuf.pixbuf.Pixbuf] 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.pixbuf.Pixbuf]
The most basic way to create a pixbuf is to wrap an existing pixel buffer with a [gdkpixbuf.pixbuf.Pixbuf] instance. You can use the [gdkpixbuf.pixbuf.Pixbuf.newFromData] function to do this.
Every time you create a new [gdkpixbuf.pixbuf.Pixbuf] 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.pixbuf.Pixbuf] 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 [gdkpixbuf.pixbuf.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 [gdkpixbuf.pixbuf.Pixbuf.newFromData]. The [gdkpixbuf.pixbuf.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 [gdkpixbuf.pixbuf.Pixbuf.newFromXpmData] function to create a pixbuf from inline XPM image data.
You can also copy an existing pixbuf with the [gdkpixbuf.pixbuf.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.pixbuf.Pixbuf] 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 [gobject.object.ObjectWrap.ref_]; when it no longer needs the pixbuf, it should release the reference it acquired by calling [gobject.object.ObjectWrap.unref]. The resources associated with a [gdkpixbuf.pixbuf.Pixbuf] will be freed when its reference count drops to zero. Newly-created [gdkpixbuf.pixbuf.Pixbuf] 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 [gdkpixbuf.pixbuf.Pixbuf.getRowstride], indicates the number of bytes between rows.
NOTE: If you are copying raw pixbuf data withmemcpy() 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 [gdkpixbuf.pixbuf.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.pixbuf.Pixbuf] 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.pixbuf.Pixbuf] provides the [gdkpixbuf.pixbuf_loader.PixbufLoader] API for progressive image loading.
Saving images
The [gdkpixbuf.pixbuf.Pixbuf] 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.pixbuf.Pixbuf] 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.
PixbufGidBuilder builder()Get builder for [gdkpixbuf.pixbuf.Pixbuf] Returns: New builder objectgdkpixbuf.types.Colorspace colorspace() @propertyGet `colorspace` property. Returns: The color space of the pixbuf.glib.bytes.Bytes pixelBytes() @propertyint rowstride() @propertyGet `rowstride` property. Returns: The number of bytes between the start of a row and the start of the next row.gdkpixbuf.pixbuf.Pixbuf newFromBytes(glib.bytes.Bytes data, gdkpixbuf.types.Colorspace colorspace, bool hasAlpha, int bitsPerSample, int width, int height, int rowstride)Creates a new #GdkPixbuf out of in-memory readonly image data.gdkpixbuf.pixbuf.Pixbuf newFromFile(string filename)Creates a new pixbuf by loading an image from a file.gdkpixbuf.pixbuf.Pixbuf newFromFileAtScale(string filename, int width, int height, bool preserveAspectRatio)Creates a new pixbuf by loading an image from a file.gdkpixbuf.pixbuf.Pixbuf newFromFileAtSize(string filename, int width, int height)Creates a new pixbuf by loading an image from a file.gdkpixbuf.pixbuf.Pixbuf newFromInline(ubyte[] data, bool copyPixels)Creates a [gdkpixbuf.pixbuf.Pixbuf] from a flat representation that is suitable for storing as inline data in a program.gdkpixbuf.pixbuf.Pixbuf newFromResource(string resourcePath)Creates a new pixbuf by loading an image from an resource.gdkpixbuf.pixbuf.Pixbuf newFromResourceAtScale(string resourcePath, int width, int height, bool preserveAspectRatio)Creates a new pixbuf by loading an image from an resource.gdkpixbuf.pixbuf.Pixbuf newFromStream(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable = null)Creates a new pixbuf by loading an image from an input stream.gdkpixbuf.pixbuf.Pixbuf newFromStreamAtScale(gio.input_stream.InputStream stream, int width, int height, bool preserveAspectRatio, gio.cancellable.Cancellable cancellable = null)Creates a new pixbuf by loading an image from an input stream.gdkpixbuf.pixbuf.Pixbuf newFromStreamFinish(gio.async_result.AsyncResult asyncResult)Finishes an asynchronous pixbuf creation operation started with [gdkpixbuf.pixbuf.Pixbuf.newFromStreamAsync].gdkpixbuf.pixbuf.Pixbuf newFromXpmData(string[] data)Creates a new pixbuf by parsing XPM data in memory.int calculateRowstride(gdkpixbuf.types.Colorspace colorspace, bool hasAlpha, int bitsPerSample, int width, int height)Calculates the rowstride that an image created with those values would have.gdkpixbuf.pixbuf_format.PixbufFormat getFileInfo(string filename, out int width, out int height)Parses an image file far enough to determine its format and size.void getFileInfoAsync(string filename, gio.cancellable.Cancellable cancellable = null, gio.types.AsyncReadyCallback callback = null)Asynchronously parses an image file far enough to determine its format and size.gdkpixbuf.pixbuf_format.PixbufFormat getFileInfoFinish(gio.async_result.AsyncResult asyncResult, out int width, out int height)Finishes an asynchronous pixbuf parsing operation started with [gdkpixbuf.pixbuf.Pixbuf.getFileInfoAsync].gdkpixbuf.pixbuf_format.PixbufFormat[] getFormats()Obtains the available information about the image formats supported by GdkPixbuf. Returns: A list of support image formats.bool initModules(string path)Initalizes the gdk-pixbuf loader modules referenced by the `loaders.cache` file present inside that directory.void newFromStreamAsync(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable = null, gio.types.AsyncReadyCallback callback = null)Creates a new pixbuf by asynchronously loading an image from an input stream.void newFromStreamAtScaleAsync(gio.input_stream.InputStream stream, int width, int height, bool preserveAspectRatio, gio.cancellable.Cancellable cancellable = null, gio.types.AsyncReadyCallback callback = null)Creates a new pixbuf by asynchronously loading an image from an input stream.bool saveToStreamFinish(gio.async_result.AsyncResult asyncResult)Finishes an asynchronous pixbuf save operation started with [gdkpixbuf.pixbuf.Pixbuf.saveToStreamAsync].gdkpixbuf.pixbuf.Pixbuf addAlpha(bool substituteColor, ubyte r, ubyte g, ubyte b)Takes an existing pixbuf and adds an alpha channel to it.gdkpixbuf.pixbuf.Pixbuf applyEmbeddedOrientation()Takes an existing pixbuf and checks for the presence of an associated "orientation" option.void composite(gdkpixbuf.pixbuf.Pixbuf dest, int destX, int destY, int destWidth, int destHeight, double offsetX, double offsetY, double scaleX, double scaleY, gdkpixbuf.types.InterpType interpType, int overallAlpha)Creates a transformation of the source image src by scaling by scalex and scaley then translating by offsetx and offsety.void compositeColor(gdkpixbuf.pixbuf.Pixbuf dest, int destX, int destY, int destWidth, int destHeight, double offsetX, double offsetY, double scaleX, double scaleY, gdkpixbuf.types.InterpType interpType, int overallAlpha, int checkX, int checkY, int checkSize, uint color1, uint color2)Creates a transformation of the source image src by scaling by scalex and scaley then translating by offsetx and offsety, then alpha blends the rectangle (destx ,desty, destwidth, destheight) of th...gdkpixbuf.pixbuf.Pixbuf compositeColorSimple(int destWidth, int destHeight, gdkpixbuf.types.InterpType interpType, int overallAlpha, int checkSize, uint color1, uint color2)Creates a new pixbuf by scaling `src` to `destwidth` x `destheight` and alpha blending the result with a checkboard of colors `color1` and `color2`.gdkpixbuf.pixbuf.Pixbuf copy()Creates a new [gdkpixbuf.pixbuf.Pixbuf] with a copy of the information in the specified `pixbuf`.void copyArea(int srcX, int srcY, int width, int height, gdkpixbuf.pixbuf.Pixbuf destPixbuf, int destX, int destY)Copies a rectangular area from `srcpixbuf` to `destpixbuf`.bool copyOptions(gdkpixbuf.pixbuf.Pixbuf destPixbuf)Copies the key/value pair options attached to a [gdkpixbuf.pixbuf.Pixbuf] to another [gdkpixbuf.pixbuf.Pixbuf].void fill(uint pixel)Clears a pixbuf to the given RGBA value, converting the RGBA value into the pixbuf's pixel format.gdkpixbuf.pixbuf.Pixbuf flip(bool horizontal)Flips a pixbuf horizontally or vertically and returns the result in a new pixbuf.int getBitsPerSample()Queries the number of bits per color sample in a pixbuf. Returns: Number of bits per color sample.size_t getByteLength()Returns the length of the pixel data, in bytes. Returns: The length of the pixel data.gdkpixbuf.types.Colorspace getColorspace()Queries the color space of a pixbuf. Returns: Color space.bool getHasAlpha()Queries whether a pixbuf has an alpha channel (opacity information). Returns: `TRUE` if it has an alpha channel, `FALSE` otherwise.int getHeight()Queries the height of a pixbuf. Returns: Height in pixels.int getNChannels()Queries the number of channels of a pixbuf. Returns: Number of channels.string getOption(string key)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 [gdkpixbuf.pixbuf.Pixbuf.setOption].string[string] getOptions()Returns a [glib.hash_table.HashTable] 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 [gdkpix...ubyte[] getPixels()Queries a pointer to the pixel data of a pixbuf.int getRowstride()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. Returns: Distance between row starts.int getWidth()Queries the width of a pixbuf. Returns: Width in pixels.gdkpixbuf.pixbuf.Pixbuf newSubpixbuf(int srcX, int srcY, int width, int height)Creates a new pixbuf which represents a sub-region of `src_pixbuf`.glib.bytes.Bytes readPixelBytes()Provides a #GBytes buffer containing the raw pixel data; the data must not be modified.const(ubyte) * readPixels()Provides a read-only pointer to the raw pixel data.bool removeOption(string key)Removes the key/value pair option attached to a [gdkpixbuf.pixbuf.Pixbuf].gdkpixbuf.pixbuf.Pixbuf rotateSimple(gdkpixbuf.types.PixbufRotation angle)Rotates a pixbuf by a multiple of 90 degrees, and returns the result in a new pixbuf.void saturateAndPixelate(gdkpixbuf.pixbuf.Pixbuf dest, float saturation, bool pixelate)Modifies saturation and optionally pixelates `src`, placing the result in `dest`.bool saveToBufferv(out ubyte[] buffer, string type, string[] optionKeys = null, string[] optionValues = null)Vector version of `[gdkpixbuf.pixbuf.Pixbuf.saveToBuffer]`.bool saveToCallbackv(gdkpixbuf.types.PixbufSaveFunc saveFunc, string type, string[] optionKeys = null, string[] optionValues = null)Vector version of `[gdkpixbuf.pixbuf.Pixbuf.saveToCallback]`.bool saveToStreamv(gio.output_stream.OutputStream stream, string type, string[] optionKeys = null, string[] optionValues = null, gio.cancellable.Cancellable cancellable = null)Saves `pixbuf` to an output stream.void saveToStreamvAsync(gio.output_stream.OutputStream stream, string type, string[] optionKeys = null, string[] optionValues = null, gio.cancellable.Cancellable cancellable = null, gio.types.AsyncReadyCallback callback = null)Saves `pixbuf` to an output stream asynchronously.bool savev(string filename, string type, string[] optionKeys = null, string[] optionValues = null)Vector version of `[gdkpixbuf.pixbuf.Pixbuf.save]`.void scale(gdkpixbuf.pixbuf.Pixbuf dest, int destX, int destY, int destWidth, int destHeight, double offsetX, double offsetY, double scaleX, double scaleY, gdkpixbuf.types.InterpType interpType)Creates a transformation of the source image src by scaling by scalex and scaley then translating by offsetx and offsety, then renders the rectangle (destx, desty, destwidth, destheight) of the res...gdkpixbuf.pixbuf.Pixbuf scaleSimple(int destWidth, int destHeight, gdkpixbuf.types.InterpType interpType)Create a new pixbuf containing a copy of `src` scaled to `destwidth` x `destheight`.bool setOption(string key, string value)Attaches a key/value pair as an option to a [gdkpixbuf.pixbuf.Pixbuf].this(gdkpixbuf.types.Colorspace colorspace, bool hasAlpha, int bitsPerSample, int width, int height)Creates a new [gdkpixbuf.pixbuf.Pixbuf] structure and allocates a buffer for it.T bitsPerSample(int propval)Set `bitsPerSample` property. Params: propval = The number of bits per sample.T colorspace(gdkpixbuf.types.Colorspace propval)Set `colorspace` property. Params: propval = The color space of the pixbuf.T hasAlpha(bool propval)Set `hasAlpha` property. Params: propval = Whether the pixbuf has an alpha channel. Returns: Builder instance for fluent chainingT height(int propval)Set `height` property. Params: propval = The number of rows of the pixbuf. Returns: Builder instance for fluent chainingT nChannels(int propval)Set `nChannels` property. Params: propval = The number of samples per pixel.T pixelBytes(glib.bytes.Bytes propval)T pixels(void * propval)Set `pixels` property. Params: propval = A pointer to the pixel data of the pixbuf. Returns: Builder instance for fluent chainingT rowstride(int propval)Set `rowstride` property. Params: propval = The number of bytes between the start of a row and the start of the next row.T width(int propval)Set `width` property. Params: propval = The number of columns of the pixbuf. Returns: Builder instance for fluent chaining