Pixbuf

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.ObjectG.ref_]; when it no longer needs the pixbuf, it should release the reference it acquired by calling [gobject.object.ObjectG.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 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 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.

class Pixbuf : ObjectG , Icon , LoadableIcon {}

Constructors

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

Members

Functions

addAlpha
gdkpixbuf.pixbuf.Pixbuf addAlpha(bool substituteColor, ubyte r, ubyte g, ubyte b)

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

applyEmbeddedOrientation
gdkpixbuf.pixbuf.Pixbuf applyEmbeddedOrientation()

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

composite
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 scale_x and scale_y then translating by offset_x and offset_y.

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

compositeColorSimple
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 dest_width x dest_height and alpha blending the result with a checkboard of colors color1 and color2.

copy
gdkpixbuf.pixbuf.Pixbuf copy()

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

copyArea
void copyArea(int srcX, int srcY, int width, int height, gdkpixbuf.pixbuf.Pixbuf destPixbuf, int destX, int destY)

Copies a rectangular area from src_pixbuf to dest_pixbuf.

copyOptions
bool copyOptions(gdkpixbuf.pixbuf.Pixbuf destPixbuf)

Copies the key/value pair options attached to a gdkpixbuf.pixbuf.Pixbuf to another gdkpixbuf.pixbuf.Pixbuf.

fill
void fill(uint pixel)

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

flip
gdkpixbuf.pixbuf.Pixbuf flip(bool horizontal)

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

getBitsPerSample
int getBitsPerSample()

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

getByteLength
size_t getByteLength()

Returns the length of the pixel data, in bytes.

getColorspace
gdkpixbuf.types.Colorspace getColorspace()

Queries the color space of a pixbuf.

getHasAlpha
bool getHasAlpha()

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

getHeight
int getHeight()

Queries the height of a pixbuf.

getNChannels
int getNChannels()

Queries the number of channels of a pixbuf.

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

getOptions
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 gdkpixbuf.pixbuf.Pixbuf.setOption.

getPixels
ubyte[] getPixels()

Queries a pointer to the pixel data of a pixbuf.

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

getWidth
int getWidth()

Queries the width of a pixbuf.

newSubpixbuf
gdkpixbuf.pixbuf.Pixbuf newSubpixbuf(int srcX, int srcY, int width, int height)

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

readPixelBytes
glib.bytes.Bytes readPixelBytes()

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

readPixels
const(ubyte)* readPixels()

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

removeOption
bool removeOption(string key)

Removes the key/value pair option attached to a gdkpixbuf.pixbuf.Pixbuf.

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

saturateAndPixelate
void saturateAndPixelate(gdkpixbuf.pixbuf.Pixbuf dest, float saturation, bool pixelate)

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

saveToBufferv
bool saveToBufferv(ubyte[] buffer, string type, string[] optionKeys, string[] optionValues)

Vector version of [gdkpixbuf.pixbuf.Pixbuf.saveToBuffer].

saveToCallbackv
bool saveToCallbackv(gdkpixbuf.types.PixbufSaveFunc saveFunc, string type, string[] optionKeys, string[] optionValues)

Vector version of [gdkpixbuf.pixbuf.Pixbuf.saveToCallback].

saveToStreamv
bool saveToStreamv(gio.output_stream.OutputStream stream, string type, string[] optionKeys, string[] optionValues, gio.cancellable.Cancellable cancellable)

Saves pixbuf to an output stream.

saveToStreamvAsync
void saveToStreamvAsync(gio.output_stream.OutputStream stream, string type, string[] optionKeys, string[] optionValues, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Saves pixbuf to an output stream asynchronously.

savev
bool savev(string filename, string type, string[] optionKeys, string[] optionValues)

Vector version of [gdkpixbuf.pixbuf.Pixbuf.save].

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

scaleSimple
gdkpixbuf.pixbuf.Pixbuf scaleSimple(int destWidth, int destHeight, gdkpixbuf.types.InterpType interpType)

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

setOption
bool setOption(string key, string value)

Attaches a key/value pair as an option to a gdkpixbuf.pixbuf.Pixbuf.

Static functions

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

getFileInfo
gdkpixbuf.pixbuf_format.PixbufFormat getFileInfo(string filename, int width, int height)

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

getFileInfoAsync
void getFileInfoAsync(string filename, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

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

getFileInfoFinish
gdkpixbuf.pixbuf_format.PixbufFormat getFileInfoFinish(gio.async_result.AsyncResult asyncResult, int width, int height)

Finishes an asynchronous pixbuf parsing operation started with gdkpixbuf.pixbuf.Pixbuf.getFileInfoAsync.

getFormats
gdkpixbuf.pixbuf_format.PixbufFormat[] getFormats()

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

initModules
bool initModules(string path)

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

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

newFromFile
gdkpixbuf.pixbuf.Pixbuf newFromFile(string filename)

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

newFromFileAtScale
gdkpixbuf.pixbuf.Pixbuf newFromFileAtScale(string filename, int width, int height, bool preserveAspectRatio)

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

newFromFileAtSize
gdkpixbuf.pixbuf.Pixbuf newFromFileAtSize(string filename, int width, int height)

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

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

newFromResource
gdkpixbuf.pixbuf.Pixbuf newFromResource(string resourcePath)

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

newFromResourceAtScale
gdkpixbuf.pixbuf.Pixbuf newFromResourceAtScale(string resourcePath, int width, int height, bool preserveAspectRatio)

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

newFromStream
gdkpixbuf.pixbuf.Pixbuf newFromStream(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable)

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

newFromStreamAsync
void newFromStreamAsync(gio.input_stream.InputStream stream, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

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

newFromStreamAtScale
gdkpixbuf.pixbuf.Pixbuf newFromStreamAtScale(gio.input_stream.InputStream stream, int width, int height, bool preserveAspectRatio, gio.cancellable.Cancellable cancellable)

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

newFromStreamAtScaleAsync
void newFromStreamAtScaleAsync(gio.input_stream.InputStream stream, int width, int height, bool preserveAspectRatio, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

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

newFromStreamFinish
gdkpixbuf.pixbuf.Pixbuf newFromStreamFinish(gio.async_result.AsyncResult asyncResult)

Finishes an asynchronous pixbuf creation operation started with gdkpixbuf.pixbuf.Pixbuf.newFromStreamAsync.

newFromXpmData
gdkpixbuf.pixbuf.Pixbuf newFromXpmData(string[] data)

Creates a new pixbuf by parsing XPM data in memory.

saveToStreamFinish
bool saveToStreamFinish(gio.async_result.AsyncResult asyncResult)

Finishes an asynchronous pixbuf save operation started with gdkpixbuf.pixbuf.Pixbuf.saveToStreamAsync.

Mixed In Members

From mixin IconT!()

equal
bool equal(gio.icon.Icon icon2)

Checks if two icons are equal.

hash
uint hash()

Gets a hash for an icon.

serialize
glib.variant.VariantG serialize()

Serializes a #GIcon into a #GVariant. An equivalent #GIcon can be retrieved back by calling gio.icon.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.

toString_
string toString_()

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 gio.icon.Icon.newForString to get icon back from the returned string.

From mixin LoadableIconT!()

load
gio.input_stream.InputStream load(int size, string type, gio.cancellable.Cancellable cancellable)

Loads a loadable icon. For the asynchronous version of this function, see gio.loadable_icon.LoadableIcon.loadAsync.

loadAsync
void loadAsync(int size, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Loads an icon asynchronously. To finish this function, see gio.loadable_icon.LoadableIcon.loadFinish. For the synchronous, blocking version of this function, see gio.loadable_icon.LoadableIcon.load.

loadFinish
gio.input_stream.InputStream loadFinish(gio.async_result.AsyncResult res, string type)

Finishes an asynchronous icon load started in gio.loadable_icon.LoadableIcon.loadAsync.

Inherited Members

From ObjectG

setGObject
void setGObject(void* cObj, Flag!"Take" take)

Set the GObject of a D ObjectG wrapper.

cPtr
void* cPtr(Flag!"Dup" dup)

Get a pointer to the underlying C object.

ref_
void* ref_(void* gObj)

Calls g_object_ref() on a GObject.

unref
unref(void* gObj)

Calls g_object_unref() on a GObject.

getType
GType getType()

Get the GType of an object.

gType
GType gType [@property getter]

GObject GType property.

self
ObjectG self()

Convenience method to return this cast to a type. For use in D with statements.

getDObject
T getDObject(void* cptr, Flag!"Take" take)

Template to get the D object from a C GObject and cast it to the given D object type.

connectSignalClosure
ulong connectSignalClosure(string signalDetail, DClosure closure, Flag!"After" after)

Connect a D closure to an object signal.

setProperty
void setProperty(string propertyName, T val)

Template for setting a GObject property.

getProperty
T getProperty(string propertyName)

Template for getting a GObject property.

compatControl
size_t compatControl(size_t what, void* data)
bindProperty
gobject.binding.Binding bindProperty(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags)

Creates a binding between source_property on source and target_property on target.

bindPropertyFull
gobject.binding.Binding bindPropertyFull(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags, gobject.closure.Closure transformTo, gobject.closure.Closure transformFrom)

Creates a binding between source_property on source and target_property on target, allowing you to set the transformation functions to be used by the binding.

forceFloating
void forceFloating()

This function is intended for #GObject implementations to re-enforce a floating[floating-ref] object reference. Doing this is seldom required: all #GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling gobject.object.ObjectG.refSink.

freezeNotify
void freezeNotify()

Increases the freeze count on object. If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one #GObject::notify signal is emitted for each property modified while the object is frozen.

getData
void* getData(string key)

Gets a named field from the objects table of associations (see gobject.object.ObjectG.setData).

getProperty
void getProperty(string propertyName, gobject.value.Value value)

Gets a property of an object.

getQdata
void* getQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata.

getv
void getv(string[] names, gobject.value.Value[] values)

Gets n_properties properties for an object. Obtained properties will be set to values. All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.

isFloating
bool isFloating()

Checks whether object has a floating[floating-ref] reference.

notify
void notify(string propertyName)

Emits a "notify" signal for the property property_name on object.

notifyByPspec
void notifyByPspec(gobject.param_spec.ParamSpec pspec)

Emits a "notify" signal for the property specified by pspec on object.

refSink
gobject.object.ObjectG refSink()

Increase the reference count of object, and possibly remove the floating[floating-ref] reference, if object has a floating reference.

runDispose
void runDispose()

Releases all references to other objects. This can be used to break reference cycles.

setData
void setData(string key, void* data)

Each object carries around a table of associations from strings to pointers. This function lets you set an association.

setProperty
void setProperty(string propertyName, gobject.value.Value value)

Sets a property on an object.

stealData
void* stealData(string key)

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.

stealQdata
void* stealQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example:

thawNotify
void thawNotify()

Reverts the effect of a previous call to gobject.object.ObjectG.freezeNotify. The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted.

watchClosure
void watchClosure(gobject.closure.Closure closure)

This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling gobject.closure.Closure.invalidate on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, gobject.object.ObjectG.ref_ and gobject.object.ObjectG.unref are added as marshal guards to the closure, to ensure that an extra reference count is held on object during invocation of the closure. Usually, this function will be called on closures that use this object as closure data.

connectNotify
ulong connectNotify(string detail, T callback, Flag!"After" after)

Connect to Notify signal.

From Icon

deserialize
gio.icon.Icon deserialize(glib.variant.VariantG value)

Deserializes a #GIcon previously serialized using gio.icon.Icon.serialize.

newForString
gio.icon.Icon newForString(string str)

Generate a #GIcon instance from str. This function can fail if str is not valid - see gio.icon.Icon.toString_ for discussion.

equal
bool equal(gio.icon.Icon icon2)

Checks if two icons are equal.

hash
uint hash()

Gets a hash for an icon.

serialize
glib.variant.VariantG serialize()

Serializes a #GIcon into a #GVariant. An equivalent #GIcon can be retrieved back by calling gio.icon.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.

toString_
string toString_()

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 gio.icon.Icon.newForString to get icon back from the returned string.

From LoadableIcon

load
gio.input_stream.InputStream load(int size, string type, gio.cancellable.Cancellable cancellable)

Loads a loadable icon. For the asynchronous version of this function, see gio.loadable_icon.LoadableIcon.loadAsync.

loadAsync
void loadAsync(int size, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Loads an icon asynchronously. To finish this function, see gio.loadable_icon.LoadableIcon.loadFinish. For the synchronous, blocking version of this function, see gio.loadable_icon.LoadableIcon.load.

loadFinish
gio.input_stream.InputStream loadFinish(gio.async_result.AsyncResult res, string type)

Finishes an asynchronous icon load started in gio.loadable_icon.LoadableIcon.loadAsync.