Class Texture

All Implemented Interfaces:
PointerInterface
Direct Known Subclasses:
GLTexture, MemoryTexture

public class Texture extends Object
`GdkTexture` is the basic element used to refer to pixel data.

It is primarily meant for pixel data that will not change over
multiple frames, and will be used for a long time.

There are various ways to create `GdkTexture` objects from a
[class@GdkPixbuf.Pixbuf], or a Cairo surface, or other pixel data.

The ownership of the pixel data is transferred to the `GdkTexture`
instance; you can only make a copy of it, via [method@Gdk.Texture.download].

`GdkTexture` is an immutable object: That means you cannot change
anything about it other than increasing the reference count via
[method@GObject.Object.ref], and consequently, it is a thread-safe object.

https://docs.gtk.org/gdk4/class.Texture.html

  • Constructor Details

  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • newForPixbufTexture

      public static Texture newForPixbufTexture(@Nonnull Pixbuf pixbuf)
      Creates a new texture object representing the `GdkPixbuf`.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      pixbuf - a `GdkPixbuf`
      Returns:
      a new `GdkTexture`
    • newFromBytesTexture

      public static Texture newFromBytesTexture(@Nonnull Bytes bytes) throws AllocationError
      Creates a new texture by loading an image from memory,

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      If %NULL is returned, then @error will be set.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      bytes - a `GBytes` containing the data to load
      Returns:
      A newly-created `GdkTexture`
      Throws:
      AllocationError
    • newFromFileTexture

      public static Texture newFromFileTexture(@Nonnull File file) throws AllocationError
      Creates a new texture by loading an image from a file.

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      If %NULL is returned, then @error will be set.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      file - `GFile` to load
      Returns:
      A newly-created `GdkTexture`
      Throws:
      AllocationError
    • newFromFilenameTexture

      public static Texture newFromFilenameTexture(@Nonnull Str path) throws AllocationError
      Creates a new texture by loading an image from a file.

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      If %NULL is returned, then @error will be set.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      path - the filename to load
      Returns:
      A newly-created `GdkTexture`
      Throws:
      AllocationError
    • newFromFilenameTexture

      public static Texture newFromFilenameTexture(String path) throws AllocationError
      Creates a new texture by loading an image from a file.

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      If %NULL is returned, then @error will be set.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      path - the filename to load
      Returns:
      A newly-created `GdkTexture`
      Throws:
      AllocationError
    • newFromResourceTexture

      public static Texture newFromResourceTexture(@Nonnull Str resource_path)
      Creates a new texture by loading an image from a resource.

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      It is a fatal error if @resource_path does not specify a valid
      image resource and the program will abort if that happens.
      If you are unsure about the validity of a resource, use
      [ctor@Gdk.Texture.new_from_file] to load it.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      resource_path - the path of the resource file
      Returns:
      A newly-created `GdkTexture`
    • newFromResourceTexture

      public static Texture newFromResourceTexture(String resource_path)
      Creates a new texture by loading an image from a resource.

      The file format is detected automatically. The supported formats
      are PNG and JPEG, though more formats might be available.

      It is a fatal error if @resource_path does not specify a valid
      image resource and the program will abort if that happens.
      If you are unsure about the validity of a resource, use
      [ctor@Gdk.Texture.new_from_file] to load it.

      This function is threadsafe, so that you can e.g. use GTask
      and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
      while loading a big image.
      Parameters:
      resource_path - the path of the resource file
      Returns:
      A newly-created `GdkTexture`
    • getHeight

      public int getHeight()
      Returns the height of the @texture, in pixels.
      Returns:
      the height of the `GdkTexture`
    • getWidth

      public int getWidth()
      Returns the width of @texture, in pixels.
      Returns:
      the width of the `GdkTexture`
    • saveToPng

      public boolean saveToPng(@Nonnull Str filename)
      Store the given @texture to the @filename as a PNG file.

      This is a utility function intended for debugging and testing.
      If you want more control over formats, proper error handling or
      want to store to a [iface@Gio.File] or other location, you might want to
      use [method@Gdk.Texture.save_to_png_bytes] or look into the
      gdk-pixbuf library.
      Parameters:
      filename - the filename to store to
      Returns:
      %TRUE if saving succeeded, %FALSE on failure.
    • saveToPng

      public boolean saveToPng(String filename)
      Store the given @texture to the @filename as a PNG file.

      This is a utility function intended for debugging and testing.
      If you want more control over formats, proper error handling or
      want to store to a [iface@Gio.File] or other location, you might want to
      use [method@Gdk.Texture.save_to_png_bytes] or look into the
      gdk-pixbuf library.
      Parameters:
      filename - the filename to store to
      Returns:
      %TRUE if saving succeeded, %FALSE on failure.
    • saveToPngBytes

      public Bytes saveToPngBytes()
      Store the given @texture in memory as a PNG file.

      Use [ctor@Gdk.Texture.new_from_bytes] to read it back.

      If you want to serialize a texture, this is a convenient and
      portable way to do that.

      If you need more control over the generated image, such as
      attaching metadata, you should look into an image handling
      library such as the gdk-pixbuf library.

      If you are dealing with high dynamic range float data, you
      might also want to consider [method@Gdk.Texture.save_to_tiff_bytes]
      instead.
      Returns:
      a newly allocated `GBytes` containing PNG data
    • saveToTiff

      public boolean saveToTiff(@Nonnull Str filename)
      Store the given @texture to the @filename as a TIFF file.

      GTK will attempt to store data without loss.
      Parameters:
      filename - the filename to store to
      Returns:
      %TRUE if saving succeeded, %FALSE on failure.
    • saveToTiff

      public boolean saveToTiff(String filename)
      Store the given @texture to the @filename as a TIFF file.

      GTK will attempt to store data without loss.
      Parameters:
      filename - the filename to store to
      Returns:
      %TRUE if saving succeeded, %FALSE on failure.
    • saveToTiffBytes

      public Bytes saveToTiffBytes()
      Store the given @texture in memory as a TIFF file.

      Use [ctor@Gdk.Texture.new_from_bytes] to read it back.

      This function is intended to store a representation of the
      texture's data that is as accurate as possible. This is
      particularly relevant when working with high dynamic range
      images and floating-point texture data.

      If that is not your concern and you are interested in a
      smaller size and a more portable format, you might want to
      use [method@Gdk.Texture.save_to_png_bytes].
      Returns:
      a newly allocated `GBytes` containing TIFF data
    • asPaintable

      public Paintable asPaintable()
      Implements interface Paintable. Call this to get access to interface functions.
      Returns:
      Paintable
    • asIcon

      public Icon asIcon()
      Implements interface Icon. Call this to get access to interface functions.
      Returns:
      Icon
    • asLoadableIcon

      public LoadableIcon asLoadableIcon()
      Implements interface LoadableIcon. Call this to get access to interface functions.
      Returns:
      LoadableIcon
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()