2017-11-05 03:48:30 +00:00
|
|
|
/* gdktexture.c
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
|
|
|
* Copyright 2016 Benjamin Otte
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-02-21 05:13:57 +00:00
|
|
|
* GdkTexture:
|
|
|
|
*
|
|
|
|
* `GdkTexture` is the basic element used to refer to pixel data.
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* It is primarily meant for pixel data that will not change over
|
2017-09-25 01:53:54 +00:00
|
|
|
* multiple frames, and will be used for a long time.
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* There are various ways to create `GdkTexture` objects from a
|
2022-01-06 16:36:12 +00:00
|
|
|
* [class@GdkPixbuf.Pixbuf], or a Cairo surface, or other pixel data.
|
2020-05-12 17:43:56 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* The ownership of the pixel data is transferred to the `GdkTexture`
|
2021-11-28 07:03:07 +00:00
|
|
|
* instance; you can only make a copy of it, via [method@Gdk.Texture.download].
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* `GdkTexture` is an immutable object: That means you cannot change
|
2016-11-07 16:59:38 +00:00
|
|
|
* anything about it other than increasing the reference count via
|
2022-01-06 16:42:17 +00:00
|
|
|
* [method@GObject.Object.ref], and consequently, it is a thread-safe object.
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#include "gdktextureprivate.h"
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2021-09-17 01:42:27 +00:00
|
|
|
#include "gdkintl.h"
|
2018-03-06 02:41:13 +00:00
|
|
|
#include "gdkmemorytextureprivate.h"
|
2018-02-16 07:41:48 +00:00
|
|
|
#include "gdkpaintable.h"
|
2018-03-18 19:52:46 +00:00
|
|
|
#include "gdksnapshot.h"
|
2018-02-16 07:41:48 +00:00
|
|
|
|
2018-03-18 19:52:46 +00:00
|
|
|
#include <graphene.h>
|
2021-09-11 20:23:53 +00:00
|
|
|
#include "loaders/gdkpngprivate.h"
|
2021-09-11 20:30:37 +00:00
|
|
|
#include "loaders/gdktiffprivate.h"
|
2021-09-11 21:02:41 +00:00
|
|
|
#include "loaders/gdkjpegprivate.h"
|
2018-03-18 19:52:46 +00:00
|
|
|
|
2021-09-14 00:51:42 +00:00
|
|
|
G_DEFINE_QUARK (gdk-texture-error-quark, gdk_texture_error)
|
|
|
|
|
2018-03-18 19:52:46 +00:00
|
|
|
/* HACK: So we don't need to include any (not-yet-created) GSK or GTK headers */
|
|
|
|
void
|
|
|
|
gtk_snapshot_append_texture (GdkSnapshot *snapshot,
|
|
|
|
GdkTexture *texture,
|
2018-04-24 01:17:23 +00:00
|
|
|
const graphene_rect_t *bounds);
|
2018-01-17 05:32:26 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_WIDTH,
|
|
|
|
PROP_HEIGHT,
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
N_PROPS
|
|
|
|
};
|
2016-11-16 04:37:20 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static GParamSpec *properties[N_PROPS];
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2018-02-16 07:41:48 +00:00
|
|
|
static void
|
|
|
|
gdk_texture_paintable_snapshot (GdkPaintable *paintable,
|
|
|
|
GdkSnapshot *snapshot,
|
|
|
|
double width,
|
|
|
|
double height)
|
|
|
|
{
|
|
|
|
GdkTexture *self = GDK_TEXTURE (paintable);
|
|
|
|
|
|
|
|
gtk_snapshot_append_texture (snapshot,
|
|
|
|
self,
|
2018-04-24 01:17:23 +00:00
|
|
|
&GRAPHENE_RECT_INIT (0, 0, width, height));
|
2018-02-16 07:41:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GdkPaintableFlags
|
|
|
|
gdk_texture_paintable_get_flags (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
return GDK_PAINTABLE_STATIC_SIZE
|
|
|
|
| GDK_PAINTABLE_STATIC_CONTENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gdk_texture_paintable_get_intrinsic_width (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GdkTexture *self = GDK_TEXTURE (paintable);
|
|
|
|
|
|
|
|
return self->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gdk_texture_paintable_get_intrinsic_height (GdkPaintable *paintable)
|
|
|
|
{
|
|
|
|
GdkTexture *self = GDK_TEXTURE (paintable);
|
|
|
|
|
|
|
|
return self->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_texture_paintable_init (GdkPaintableInterface *iface)
|
|
|
|
{
|
|
|
|
iface->snapshot = gdk_texture_paintable_snapshot;
|
|
|
|
iface->get_flags = gdk_texture_paintable_get_flags;
|
|
|
|
iface->get_intrinsic_width = gdk_texture_paintable_get_intrinsic_width;
|
|
|
|
iface->get_intrinsic_height = gdk_texture_paintable_get_intrinsic_height;
|
|
|
|
}
|
|
|
|
|
2021-09-17 02:05:40 +00:00
|
|
|
static GVariant *
|
|
|
|
gdk_texture_icon_serialize (GIcon *icon)
|
|
|
|
{
|
|
|
|
GVariant *result;
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = gdk_texture_save_to_png_bytes (GDK_TEXTURE (icon));
|
|
|
|
result = g_variant_new_from_bytes (G_VARIANT_TYPE_BYTESTRING, bytes, TRUE);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
return g_variant_new ("(sv)", "bytes", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_texture_icon_init (GIconIface *iface)
|
|
|
|
{
|
|
|
|
iface->hash = (guint (*) (GIcon *)) g_direct_hash;
|
|
|
|
iface->equal = (gboolean (*) (GIcon *, GIcon *)) g_direct_equal;
|
|
|
|
iface->serialize = gdk_texture_icon_serialize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GInputStream *
|
|
|
|
gdk_texture_loadable_icon_load (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
char **type,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GInputStream *stream;
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = gdk_texture_save_to_png_bytes (GDK_TEXTURE (icon));
|
|
|
|
stream = g_memory_input_stream_new_from_bytes (bytes);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
if (type)
|
|
|
|
*type = NULL;
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_texture_loadable_icon_load_in_thread (GTask *task,
|
|
|
|
gpointer source_object,
|
|
|
|
gpointer task_data,
|
|
|
|
GCancellable *cancellable)
|
|
|
|
{
|
|
|
|
GInputStream *stream;
|
|
|
|
GBytes *bytes;
|
|
|
|
|
|
|
|
bytes = gdk_texture_save_to_png_bytes (source_object);
|
|
|
|
stream = g_memory_input_stream_new_from_bytes (bytes);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
g_task_return_pointer (task, stream, g_object_unref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_texture_loadable_icon_load_async (GLoadableIcon *icon,
|
|
|
|
int size,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
task = g_task_new (icon, cancellable, callback, user_data);
|
|
|
|
g_task_run_in_thread (task, gdk_texture_loadable_icon_load_in_thread);
|
|
|
|
g_object_unref (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GInputStream *
|
|
|
|
gdk_texture_loadable_icon_load_finish (GLoadableIcon *icon,
|
|
|
|
GAsyncResult *res,
|
|
|
|
char **type,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GInputStream *result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
|
|
|
|
|
|
|
|
result = g_task_propagate_pointer (G_TASK (res), error);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (type)
|
|
|
|
*type = NULL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_texture_loadable_icon_init (GLoadableIconIface *iface)
|
|
|
|
{
|
|
|
|
iface->load = gdk_texture_loadable_icon_load;
|
|
|
|
iface->load_async = gdk_texture_loadable_icon_load_async;
|
|
|
|
iface->load_finish = gdk_texture_loadable_icon_load_finish;
|
|
|
|
}
|
|
|
|
|
2018-02-16 07:41:48 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GdkTexture, gdk_texture, G_TYPE_OBJECT,
|
|
|
|
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
|
2021-09-17 02:05:40 +00:00
|
|
|
gdk_texture_paintable_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
|
|
|
|
gdk_texture_icon_init)
|
|
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON, gdk_texture_loadable_icon_init))
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#define GDK_TEXTURE_WARN_NOT_IMPLEMENTED_METHOD(obj,method) \
|
|
|
|
g_critical ("Texture of type '%s' does not implement GdkTexture::" # method, G_OBJECT_TYPE_NAME (obj))
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2021-09-10 00:40:21 +00:00
|
|
|
static void
|
2021-10-07 04:19:41 +00:00
|
|
|
gdk_texture_default_download (GdkTexture *texture,
|
|
|
|
GdkMemoryFormat format,
|
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
2021-09-10 00:40:21 +00:00
|
|
|
{
|
2021-10-07 04:19:41 +00:00
|
|
|
GDK_TEXTURE_WARN_NOT_IMPLEMENTED_METHOD (texture, download);
|
2021-09-10 00:40:21 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_set_property (GObject *gobject,
|
2017-01-01 12:40:13 +00:00
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (gobject);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDTH:
|
|
|
|
self->width = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
self->height = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_property (GObject *gobject,
|
2017-01-01 12:40:13 +00:00
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (gobject);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDTH:
|
|
|
|
g_value_set_int (value, self->width);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
g_value_set_int (value, self->height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_dispose (GObject *object)
|
2017-09-25 01:53:54 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (object);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_clear_render_data (self);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_OBJECT_CLASS (gdk_texture_parent_class)->dispose (object);
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_class_init (GdkTextureClass *klass)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
klass->download = gdk_texture_default_download;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gobject_class->set_property = gdk_texture_set_property;
|
|
|
|
gobject_class->get_property = gdk_texture_get_property;
|
|
|
|
gobject_class->dispose = gdk_texture_dispose;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-25 01:06:36 +00:00
|
|
|
* GdkTexture:width: (attributes org.gtk.Property.get=gdk_texture_get_width)
|
2017-01-01 12:40:13 +00:00
|
|
|
*
|
2020-10-29 17:16:49 +00:00
|
|
|
* The width of the texture, in pixels.
|
2017-01-01 12:40:13 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_WIDTH] =
|
|
|
|
g_param_spec_int ("width",
|
|
|
|
"Width",
|
|
|
|
"The width of the texture",
|
|
|
|
1,
|
|
|
|
G_MAXINT,
|
|
|
|
1,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
/**
|
2021-02-25 01:06:36 +00:00
|
|
|
* GdkTexture:height: (attributes org.gtk.Property.get=gdk_texture_get_height)
|
2017-01-01 12:40:13 +00:00
|
|
|
*
|
2020-10-29 17:16:49 +00:00
|
|
|
* The height of the texture, in pixels.
|
2017-01-01 12:40:13 +00:00
|
|
|
*/
|
|
|
|
properties[PROP_HEIGHT] =
|
|
|
|
g_param_spec_int ("height",
|
|
|
|
"Height",
|
|
|
|
"The height of the texture",
|
|
|
|
1,
|
|
|
|
G_MAXINT,
|
|
|
|
1,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, N_PROPS, properties);
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_init (GdkTexture *self)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 01:53:54 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_new_for_surface:
|
2017-09-25 01:53:54 +00:00
|
|
|
* @surface: a cairo image surface
|
|
|
|
*
|
|
|
|
* Creates a new texture object representing the surface.
|
2021-02-21 05:13:57 +00:00
|
|
|
*
|
2022-01-06 16:36:12 +00:00
|
|
|
* @surface must be an image surface with format `CAIRO_FORMAT_ARGB32`.
|
2017-09-25 01:53:54 +00:00
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Returns: (type GdkMemoryTexture): a new `GdkTexture`
|
2017-09-25 01:53:54 +00:00
|
|
|
*/
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_for_surface (cairo_surface_t *surface)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2018-03-06 02:41:13 +00:00
|
|
|
GdkTexture *texture;
|
|
|
|
GBytes *bytes;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE, NULL);
|
2018-02-02 13:58:39 +00:00
|
|
|
g_return_val_if_fail (cairo_image_surface_get_width (surface) > 0, NULL);
|
|
|
|
g_return_val_if_fail (cairo_image_surface_get_height (surface) > 0, NULL);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2018-03-06 02:41:13 +00:00
|
|
|
bytes = g_bytes_new_with_free_func (cairo_image_surface_get_data (surface),
|
|
|
|
cairo_image_surface_get_height (surface)
|
|
|
|
* cairo_image_surface_get_stride (surface),
|
|
|
|
(GDestroyNotify) cairo_surface_destroy,
|
|
|
|
cairo_surface_reference (surface));
|
|
|
|
|
|
|
|
texture = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
|
|
|
|
cairo_image_surface_get_height (surface),
|
2021-09-09 00:05:08 +00:00
|
|
|
GDK_MEMORY_DEFAULT,
|
2018-03-06 02:41:13 +00:00
|
|
|
bytes,
|
|
|
|
cairo_image_surface_get_stride (surface));
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2018-03-06 02:41:13 +00:00
|
|
|
g_bytes_unref (bytes);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2018-03-06 02:41:13 +00:00
|
|
|
return texture;
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-09-25 01:53:54 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_new_for_pixbuf:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @pixbuf: a `GdkPixbuf`
|
2017-09-25 01:53:54 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Creates a new texture object representing the `GdkPixbuf`.
|
2017-09-25 01:53:54 +00:00
|
|
|
*
|
2021-09-22 03:08:09 +00:00
|
|
|
* This function is threadsafe, so that you can e.g. use GTask
|
2022-01-06 16:36:12 +00:00
|
|
|
* and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
|
2021-09-22 03:08:09 +00:00
|
|
|
* while loading a big image.
|
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Returns: (type GdkMemoryTexture): a new `GdkTexture`
|
2017-09-25 01:53:54 +00:00
|
|
|
*/
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_for_pixbuf (GdkPixbuf *pixbuf)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2018-03-06 02:41:13 +00:00
|
|
|
GdkTexture *texture;
|
|
|
|
GBytes *bytes;
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
|
|
|
|
|
2018-03-06 02:41:13 +00:00
|
|
|
bytes = g_bytes_new_with_free_func (gdk_pixbuf_get_pixels (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf)
|
|
|
|
* gdk_pixbuf_get_rowstride (pixbuf),
|
|
|
|
g_object_unref,
|
|
|
|
g_object_ref (pixbuf));
|
|
|
|
texture = gdk_memory_texture_new (gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf),
|
|
|
|
gdk_pixbuf_get_has_alpha (pixbuf)
|
|
|
|
? GDK_MEMORY_GDK_PIXBUF_ALPHA
|
|
|
|
: GDK_MEMORY_GDK_PIXBUF_OPAQUE,
|
|
|
|
bytes,
|
|
|
|
gdk_pixbuf_get_rowstride (pixbuf));
|
|
|
|
|
|
|
|
g_bytes_unref (bytes);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2018-03-06 02:41:13 +00:00
|
|
|
return texture;
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:08:25 +00:00
|
|
|
/**
|
|
|
|
* gdk_texture_new_from_resource:
|
|
|
|
* @resource_path: the path of the resource file
|
|
|
|
*
|
|
|
|
* Creates a new texture by loading an image from a resource.
|
2021-02-21 05:13:57 +00:00
|
|
|
*
|
|
|
|
* The file format is detected automatically. The supported formats
|
|
|
|
* are PNG and JPEG, though more formats might be available.
|
2017-11-04 14:08:25 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-02-21 05:13:57 +00:00
|
|
|
* [ctor@Gdk.Texture.new_from_file] to load it.
|
2017-11-04 14:08:25 +00:00
|
|
|
*
|
2021-09-22 03:08:09 +00:00
|
|
|
* This function is threadsafe, so that you can e.g. use GTask
|
2022-01-06 16:36:12 +00:00
|
|
|
* and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
|
2021-09-22 03:08:09 +00:00
|
|
|
* while loading a big image.
|
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Return value: (type GdkMemoryTexture): A newly-created `GdkTexture`
|
2017-11-04 14:08:25 +00:00
|
|
|
*/
|
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_resource (const char *resource_path)
|
|
|
|
{
|
2021-09-14 12:52:35 +00:00
|
|
|
GBytes *bytes;
|
2017-11-04 14:08:25 +00:00
|
|
|
GdkTexture *texture;
|
2021-09-14 12:52:35 +00:00
|
|
|
GError *error = NULL;
|
2017-11-04 14:08:25 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (resource_path != NULL, NULL);
|
|
|
|
|
2021-09-14 12:52:35 +00:00
|
|
|
bytes = g_resources_lookup_data (resource_path, 0, &error);
|
|
|
|
if (bytes != NULL)
|
|
|
|
{
|
|
|
|
texture = gdk_texture_new_from_bytes (bytes, &error);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
texture = NULL;
|
2017-11-04 14:08:25 +00:00
|
|
|
|
2021-09-14 12:52:35 +00:00
|
|
|
if (texture == NULL)
|
2022-01-01 17:23:22 +00:00
|
|
|
g_error ("Resource path %s is not a valid image: %s", resource_path, error->message);
|
2017-11-04 14:08:25 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 03:48:30 +00:00
|
|
|
* gdk_texture_new_from_file:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @file: `GFile` to load
|
2017-11-04 14:08:25 +00:00
|
|
|
* @error: Return location for an error
|
|
|
|
*
|
2020-10-29 17:16:49 +00:00
|
|
|
* Creates a new texture by loading an image from a file.
|
2021-02-21 05:13:57 +00:00
|
|
|
*
|
|
|
|
* The file format is detected automatically. The supported formats
|
|
|
|
* are PNG and JPEG, though more formats might be available.
|
2020-10-29 17:16:49 +00:00
|
|
|
*
|
|
|
|
* If %NULL is returned, then @error will be set.
|
2017-11-04 14:08:25 +00:00
|
|
|
*
|
2021-09-22 03:08:09 +00:00
|
|
|
* This function is threadsafe, so that you can e.g. use GTask
|
2022-01-06 16:36:12 +00:00
|
|
|
* and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
|
2021-09-22 03:08:09 +00:00
|
|
|
* while loading a big image.
|
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Return value: (type GdkMemoryTexture): A newly-created `GdkTexture`
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-04 14:08:25 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_file (GFile *file,
|
|
|
|
GError **error)
|
|
|
|
{
|
2021-09-13 17:30:12 +00:00
|
|
|
GBytes *bytes;
|
2017-11-04 14:08:25 +00:00
|
|
|
GdkTexture *texture;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2021-09-13 17:30:12 +00:00
|
|
|
bytes = g_file_load_bytes (file, NULL, NULL, error);
|
|
|
|
if (bytes == NULL)
|
2017-11-04 14:08:25 +00:00
|
|
|
return NULL;
|
|
|
|
|
2021-09-13 17:30:12 +00:00
|
|
|
texture = gdk_texture_new_from_bytes (bytes, error);
|
|
|
|
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2021-09-14 15:40:09 +00:00
|
|
|
gboolean
|
|
|
|
gdk_texture_can_load (GBytes *bytes)
|
|
|
|
{
|
|
|
|
return gdk_is_png (bytes) ||
|
|
|
|
gdk_is_jpeg (bytes) ||
|
|
|
|
gdk_is_tiff (bytes);
|
|
|
|
}
|
2021-09-13 17:30:12 +00:00
|
|
|
|
2021-09-17 01:42:27 +00:00
|
|
|
static GdkTexture *
|
|
|
|
gdk_texture_new_from_bytes_internal (GBytes *bytes,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
if (gdk_is_png (bytes))
|
|
|
|
{
|
|
|
|
return gdk_load_png (bytes, error);
|
|
|
|
}
|
|
|
|
else if (gdk_is_jpeg (bytes))
|
|
|
|
{
|
|
|
|
return gdk_load_jpeg (bytes, error);
|
|
|
|
}
|
|
|
|
else if (gdk_is_tiff (bytes))
|
|
|
|
{
|
|
|
|
return gdk_load_tiff (bytes, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT,
|
|
|
|
_("Unknown image format."));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkTexture *
|
|
|
|
gdk_texture_new_from_bytes_pixbuf (GBytes *bytes,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GInputStream *stream;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
GdkTexture *texture;
|
|
|
|
|
|
|
|
stream = g_memory_input_stream_new_from_bytes (bytes);
|
|
|
|
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
|
|
|
|
g_object_unref (stream);
|
|
|
|
if (pixbuf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
texture = gdk_texture_new_for_pixbuf (pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-13 17:30:12 +00:00
|
|
|
/**
|
|
|
|
* gdk_texture_new_from_bytes:
|
|
|
|
* @bytes: a `GBytes` containing the data to load
|
|
|
|
* @error: Return location for an error
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-09-22 03:08:09 +00:00
|
|
|
* This function is threadsafe, so that you can e.g. use GTask
|
2022-01-06 16:36:12 +00:00
|
|
|
* and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
|
2021-09-22 03:08:09 +00:00
|
|
|
* while loading a big image.
|
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Return value: (type GdkMemoryTexture): A newly-created `GdkTexture`
|
2021-09-13 17:30:12 +00:00
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_bytes (GBytes *bytes,
|
|
|
|
GError **error)
|
|
|
|
{
|
2021-09-17 01:42:27 +00:00
|
|
|
GdkTexture *texture;
|
|
|
|
GError *internal_error = NULL;
|
|
|
|
|
2021-09-13 17:30:12 +00:00
|
|
|
g_return_val_if_fail (bytes != NULL, NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2021-09-17 01:42:27 +00:00
|
|
|
texture = gdk_texture_new_from_bytes_internal (bytes, &internal_error);
|
|
|
|
if (texture)
|
|
|
|
return texture;
|
|
|
|
|
|
|
|
if (!g_error_matches (internal_error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT) &&
|
|
|
|
!g_error_matches (internal_error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT))
|
2021-09-11 20:30:37 +00:00
|
|
|
{
|
2021-09-17 01:42:27 +00:00
|
|
|
g_propagate_error (error, internal_error);
|
|
|
|
return NULL;
|
2021-09-11 20:30:37 +00:00
|
|
|
}
|
2017-11-04 14:08:25 +00:00
|
|
|
|
2021-09-17 01:42:27 +00:00
|
|
|
g_clear_error (&internal_error);
|
2017-11-04 14:08:25 +00:00
|
|
|
|
2021-09-17 01:42:27 +00:00
|
|
|
return gdk_texture_new_from_bytes_pixbuf (bytes, error);
|
2017-11-04 14:08:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 20:08:33 +00:00
|
|
|
/**
|
|
|
|
* gdk_texture_new_from_filename:
|
|
|
|
* @path: (type filename): the filename to load
|
|
|
|
* @error: Return location for an error
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-09-22 03:08:09 +00:00
|
|
|
* This function is threadsafe, so that you can e.g. use GTask
|
2022-01-06 16:36:12 +00:00
|
|
|
* and [method@Gio.Task.run_in_thread] to avoid blocking the main thread
|
2021-09-22 03:08:09 +00:00
|
|
|
* while loading a big image.
|
|
|
|
*
|
2022-01-11 13:46:22 +00:00
|
|
|
* Return value: (type GdkMemoryTexture): A newly-created `GdkTexture`
|
2021-10-21 13:56:15 +00:00
|
|
|
*
|
|
|
|
* Since: 4.6
|
2021-09-15 20:08:33 +00:00
|
|
|
*/
|
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_filename (const char *path,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GdkTexture *texture;
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
g_return_val_if_fail (path, NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
|
|
file = g_file_new_for_path (path);
|
|
|
|
|
|
|
|
texture = gdk_texture_new_from_file (file, error);
|
|
|
|
|
|
|
|
g_object_unref (file);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:38 +00:00
|
|
|
/**
|
2021-02-25 01:06:36 +00:00
|
|
|
* gdk_texture_get_width: (attributes org.gtk.Method.get_property=width)
|
2021-02-21 05:13:57 +00:00
|
|
|
* @texture: a `GdkTexture`
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2020-10-29 17:16:49 +00:00
|
|
|
* Returns the width of @texture, in pixels.
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Returns: the width of the `GdkTexture`
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_width (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), 0);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
|
|
|
return texture->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-25 01:06:36 +00:00
|
|
|
* gdk_texture_get_height: (attributes org.gtk.Method.get_property=height)
|
2021-02-21 05:13:57 +00:00
|
|
|
* @texture: a `GdkTexture`
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2020-10-29 17:16:49 +00:00
|
|
|
* Returns the height of the @texture, in pixels.
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Returns: the height of the `GdkTexture`
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_height (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), 0);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
|
|
|
return texture->height;
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
void
|
|
|
|
gdk_texture_do_download (GdkTexture *texture,
|
|
|
|
GdkMemoryFormat format,
|
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
|
|
|
GDK_TEXTURE_GET_CLASS (texture)->download (texture, format, data,stride);
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
cairo_surface_t *
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_download_surface (GdkTexture *texture)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2020-09-24 13:40:17 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_status_t surface_status;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
|
|
|
texture->width, texture->height);
|
|
|
|
|
|
|
|
surface_status = cairo_surface_status (surface);
|
|
|
|
if (surface_status != CAIRO_STATUS_SUCCESS)
|
|
|
|
g_warning ("%s: surface error: %s", __FUNCTION__,
|
|
|
|
cairo_status_to_string (surface_status));
|
|
|
|
|
|
|
|
gdk_texture_download (texture,
|
|
|
|
cairo_image_surface_get_data (surface),
|
|
|
|
cairo_image_surface_get_stride (surface));
|
|
|
|
cairo_surface_mark_dirty (surface);
|
|
|
|
|
|
|
|
return surface;
|
2016-12-21 21:11:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:31:15 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_download:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @texture: a `GdkTexture`
|
2018-01-02 17:55:55 +00:00
|
|
|
* @data: (array): pointer to enough memory to be filled with the
|
2021-05-18 21:05:26 +00:00
|
|
|
* downloaded data of @texture
|
2016-12-21 21:11:52 +00:00
|
|
|
* @stride: rowstride in bytes
|
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Downloads the @texture into local memory.
|
|
|
|
*
|
|
|
|
* This may be an expensive operation, as the actual texture data
|
|
|
|
* may reside on a GPU or on a remote display server.
|
2016-12-21 21:11:52 +00:00
|
|
|
*
|
|
|
|
* The data format of the downloaded data is equivalent to
|
|
|
|
* %CAIRO_FORMAT_ARGB32, so every downloaded pixel requires
|
|
|
|
* 4 bytes of memory.
|
|
|
|
*
|
|
|
|
* Downloading a texture into a Cairo image surface:
|
2021-02-21 05:13:57 +00:00
|
|
|
* ```c
|
2016-12-21 21:11:52 +00:00
|
|
|
* surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_get_width (texture),
|
|
|
|
* gdk_texture_get_height (texture));
|
|
|
|
* gdk_texture_download (texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
* cairo_image_surface_get_data (surface),
|
|
|
|
* cairo_image_surface_get_stride (surface));
|
|
|
|
* cairo_surface_mark_dirty (surface);
|
2021-02-21 05:13:57 +00:00
|
|
|
* ```
|
2018-03-19 19:31:15 +00:00
|
|
|
*/
|
2016-12-21 21:11:52 +00:00
|
|
|
void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_download (GdkTexture *texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_if_fail (GDK_IS_TEXTURE (texture));
|
2016-12-21 21:11:52 +00:00
|
|
|
g_return_if_fail (data != NULL);
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_if_fail (stride >= gdk_texture_get_width (texture) * 4);
|
2016-12-21 21:11:52 +00:00
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
gdk_texture_do_download (texture,
|
|
|
|
GDK_MEMORY_DEFAULT,
|
|
|
|
data,
|
|
|
|
stride);
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 00:41:30 +00:00
|
|
|
GdkMemoryFormat
|
|
|
|
gdk_texture_get_format (GdkTexture *self)
|
|
|
|
{
|
|
|
|
return self->format;
|
|
|
|
}
|
|
|
|
|
2016-11-16 04:37:20 +00:00
|
|
|
gboolean
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_set_render_data (GdkTexture *self,
|
2016-11-16 04:37:20 +00:00
|
|
|
gpointer key,
|
|
|
|
gpointer data,
|
|
|
|
GDestroyNotify notify)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
|
|
|
|
if (self->render_key != NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
self->render_key = key;
|
|
|
|
self->render_data = data;
|
|
|
|
self->render_notify = notify;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_clear_render_data (GdkTexture *self)
|
2016-11-16 04:37:20 +00:00
|
|
|
{
|
|
|
|
if (self->render_notify)
|
|
|
|
self->render_notify (self->render_data);
|
|
|
|
|
|
|
|
self->render_key = NULL;
|
|
|
|
self->render_data = NULL;
|
|
|
|
self->render_notify = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_render_data (GdkTexture *self,
|
2016-11-16 04:37:20 +00:00
|
|
|
gpointer key)
|
|
|
|
{
|
|
|
|
if (self->render_key != key)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return self->render_data;
|
|
|
|
}
|
2018-04-12 11:50:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_texture_save_to_png:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @texture: a `GdkTexture`
|
2021-06-12 15:12:06 +00:00
|
|
|
* @filename: (type filename): the filename to store to
|
2018-04-12 11:50:33 +00:00
|
|
|
*
|
|
|
|
* 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
|
2022-01-06 16:36:12 +00:00
|
|
|
* want to store to a [iface@Gio.File] or other location, you might want to
|
2021-09-13 18:29:31 +00:00
|
|
|
* use [method@Gdk.Texture.save_to_png_bytes] or look into the
|
|
|
|
* gdk-pixbuf library.
|
2018-04-12 11:50:33 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if saving succeeded, %FALSE on failure.
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2018-04-12 11:50:33 +00:00
|
|
|
gboolean
|
|
|
|
gdk_texture_save_to_png (GdkTexture *texture,
|
|
|
|
const char *filename)
|
|
|
|
{
|
2021-09-12 13:24:59 +00:00
|
|
|
GBytes *bytes;
|
2018-04-12 11:50:33 +00:00
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
|
|
|
|
2021-09-12 13:24:59 +00:00
|
|
|
bytes = gdk_save_png (texture);
|
|
|
|
result = g_file_set_contents (filename,
|
|
|
|
g_bytes_get_data (bytes, NULL),
|
|
|
|
g_bytes_get_size (bytes),
|
|
|
|
NULL);
|
|
|
|
g_bytes_unref (bytes);
|
2018-04-12 11:50:33 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-13 18:29:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_texture_save_to_png_bytes:
|
|
|
|
* @texture: a `GdkTexture`
|
|
|
|
*
|
|
|
|
* Store the given @texture in memory as a PNG file.
|
2021-10-17 19:54:59 +00:00
|
|
|
*
|
2021-09-13 18:29:31 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2021-09-14 00:36:56 +00:00
|
|
|
* If you are dealing with high dynamic range float data, you
|
|
|
|
* might also want to consider [method@Gdk.Texture.save_to_tiff_bytes]
|
|
|
|
* instead.
|
|
|
|
*
|
2021-09-13 18:29:31 +00:00
|
|
|
* Returns: a newly allocated `GBytes` containing PNG data
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
GBytes *
|
|
|
|
gdk_texture_save_to_png_bytes (GdkTexture *texture)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
|
|
|
|
|
|
|
|
return gdk_save_png (texture);
|
|
|
|
}
|
2021-09-07 13:20:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_texture_save_to_tiff:
|
|
|
|
* @texture: a `GdkTexture`
|
|
|
|
* @filename: (type filename): the filename to store to
|
|
|
|
*
|
|
|
|
* Store the given @texture to the @filename as a TIFF file.
|
|
|
|
*
|
|
|
|
* GTK will attempt to store data without loss.
|
|
|
|
* Returns: %TRUE if saving succeeded, %FALSE on failure.
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gdk_texture_save_to_tiff (GdkTexture *texture,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
|
|
|
|
|
|
|
bytes = gdk_save_tiff (texture);
|
|
|
|
result = g_file_set_contents (filename,
|
|
|
|
g_bytes_get_data (bytes, NULL),
|
|
|
|
g_bytes_get_size (bytes),
|
|
|
|
NULL);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-09-14 00:36:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_texture_save_to_tiff_bytes:
|
|
|
|
* @texture: a `GdkTexture`
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Since: 4.6
|
|
|
|
*/
|
|
|
|
GBytes *
|
|
|
|
gdk_texture_save_to_tiff_bytes (GdkTexture *texture)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
|
|
|
|
|
|
|
|
return gdk_save_tiff (texture);
|
|
|
|
}
|