Reduce pixbuf helpers

Concentrate pixbuf handling in gdkpixbufutils.c.
This commit is contained in:
Matthias Clasen 2023-05-16 21:53:55 -04:00
parent 4d66598f31
commit 847739aed7
5 changed files with 180 additions and 122 deletions

View File

@ -22,6 +22,8 @@
#include "gdk/gdktextureprivate.h"
/* {{{ Pixbuf helpers */
static GdkPixbuf *
load_from_stream (GdkPixbufLoader *loader,
GInputStream *stream,
@ -145,7 +147,7 @@ size_prepared_cb2 (GdkPixbufLoader *loader,
gdk_pixbuf_loader_set_size (loader, width, height);
}
GdkPixbuf *
static GdkPixbuf *
_gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
int width,
int height,
@ -172,15 +174,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
return pixbuf;
}
GdkPixbuf *
_gdk_pixbuf_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
return _gdk_pixbuf_new_from_stream_scaled (stream, 0, cancellable, error);
}
GdkPixbuf *
static GdkPixbuf *
_gdk_pixbuf_new_from_resource_at_scale (const char *resource_path,
int width,
int height,
@ -198,9 +192,11 @@ _gdk_pixbuf_new_from_resource_at_scale (const char *resource_path,
g_object_unref (stream);
return pixbuf;
}
/* }}} */
/* {{{ Symbolic processing */
static GdkPixbuf *
load_symbolic_svg (const char *escaped_file_data,
int width,
@ -306,7 +302,6 @@ gtk_make_symbolic_pixbuf_from_data (const char *file_data,
char *escaped_file_data;
/* Fetch size from the original icon */
{
GInputStream *stream = g_memory_input_stream_new_from_data (file_data, file_len, NULL);
GdkPixbuf *reference = gdk_pixbuf_new_from_stream (stream, NULL, error);
@ -318,7 +313,6 @@ gtk_make_symbolic_pixbuf_from_data (const char *file_data,
icon_width = gdk_pixbuf_get_width (reference);
icon_height = gdk_pixbuf_get_height (reference);
g_object_unref (reference);
}
escaped_file_data = g_base64_encode ((guchar *) file_data, file_len);
icon_width_str = g_strdup_printf ("%d", icon_width);
@ -389,8 +383,8 @@ out:
return pixbuf;
}
GdkPixbuf *
gtk_make_symbolic_pixbuf_from_resource (const char *path,
static GdkPixbuf *
make_symbolic_pixbuf_from_resource (const char *path,
int width,
int height,
double scale,
@ -414,8 +408,8 @@ gtk_make_symbolic_pixbuf_from_resource (const char *path,
return pixbuf;
}
GdkPixbuf *
gtk_make_symbolic_pixbuf_from_path (const char *path,
static GdkPixbuf *
make_symbolic_pixbuf_from_path (const char *path,
int width,
int height,
double scale,
@ -436,7 +430,7 @@ gtk_make_symbolic_pixbuf_from_path (const char *path,
}
static GdkPixbuf *
gtk_make_symbolic_pixbuf_from_file (GFile *file,
make_symbolic_pixbuf_from_file (GFile *file,
int width,
int height,
double scale,
@ -456,14 +450,73 @@ gtk_make_symbolic_pixbuf_from_file (GFile *file,
return pixbuf;
}
/* }}} */
/* {{{ Texture API */
GdkTexture *
gtk_load_symbolic_texture_from_resource (const char *path)
gdk_texture_new_from_stream_at_scale (GInputStream *stream,
int width,
int height,
gboolean aspect,
GCancellable *cancellable,
GError **error)
{
return gdk_texture_new_from_resource (path);
GdkPixbuf *pixbuf;
GdkTexture *texture = NULL;
pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream, width, height, aspect, cancellable, error);
if (pixbuf)
{
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
}
return texture;
}
GdkTexture *
gtk_make_symbolic_texture_from_resource (const char *path,
gdk_texture_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error)
{
GdkPixbuf *pixbuf;
GdkTexture *texture = NULL;
pixbuf = _gdk_pixbuf_new_from_stream_scaled (stream, 0, cancellable, error);
if (pixbuf)
{
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
}
return texture;
}
GdkTexture *
gdk_texture_new_from_resource_at_scale (const char *path,
int width,
int height,
gboolean preserve_aspect,
GError **error)
{
GdkPixbuf *pixbuf;
GdkTexture *texture = NULL;
pixbuf = _gdk_pixbuf_new_from_resource_at_scale (path, width, height, preserve_aspect, error);
if (pixbuf)
{
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
}
return texture;
}
/* }}} */
/* {{{ Symbolic texture API */
GdkTexture *
gdk_texture_new_from_path_symbolic (const char *path,
int width,
int height,
double scale,
@ -472,7 +525,33 @@ gtk_make_symbolic_texture_from_resource (const char *path,
GdkPixbuf *pixbuf;
GdkTexture *texture = NULL;
pixbuf = gtk_make_symbolic_pixbuf_from_resource (path, width, height, scale, error);
pixbuf = make_symbolic_pixbuf_from_path (path, width, height, scale, error);
if (pixbuf)
{
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
}
return texture;
}
GdkTexture *
gtk_load_symbolic_texture_from_resource (const char *path)
{
return gdk_texture_new_from_resource (path);
}
GdkTexture *
gdk_texture_new_from_resource_symbolic (const char *path,
int width,
int height,
double scale,
GError **error)
{
GdkPixbuf *pixbuf;
GdkTexture *texture = NULL;
pixbuf = make_symbolic_pixbuf_from_resource (path, width, height, scale, error);
if (pixbuf)
{
texture = gdk_texture_new_for_pixbuf (pixbuf);
@ -493,7 +572,7 @@ gtk_load_symbolic_texture_from_file (GFile *file)
if (stream == NULL)
return NULL;
pixbuf = _gdk_pixbuf_new_from_stream (stream, NULL, NULL);
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
g_object_unref (stream);
if (pixbuf == NULL)
return NULL;
@ -505,7 +584,7 @@ gtk_load_symbolic_texture_from_file (GFile *file)
}
GdkTexture *
gtk_make_symbolic_texture_from_file (GFile *file,
gdk_texture_new_from_file_symbolic (GFile *file,
int width,
int height,
double scale,
@ -514,13 +593,16 @@ gtk_make_symbolic_texture_from_file (GFile *file,
GdkPixbuf *pixbuf;
GdkTexture *texture;
pixbuf = gtk_make_symbolic_pixbuf_from_file (file, width, height, scale, error);
pixbuf = make_symbolic_pixbuf_from_file (file, width, height, scale, error);
texture = gdk_texture_new_for_pixbuf (pixbuf);
g_object_unref (pixbuf);
return texture;
}
/* }}} */
/* {{{ Scaled paintable API */
typedef struct {
int scale_factor;
} LoaderData;
@ -651,3 +733,7 @@ gdk_paintable_new_from_file_scaled (GFile *file,
return paintable;
}
/* }}} */
/* vim:set foldmethod=marker expandtab: */

View File

@ -21,20 +21,6 @@
G_BEGIN_DECLS
GdkPixbuf *_gdk_pixbuf_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error);
GdkPixbuf *_gdk_pixbuf_new_from_stream_at_scale (GInputStream *stream,
int width,
int height,
gboolean aspect,
GCancellable *cancellable,
GError **error);
GdkPixbuf *_gdk_pixbuf_new_from_resource_at_scale (const char *resource_path,
int width,
int height,
gboolean preserve_aspect,
GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_data (const char *data,
gsize len,
int width,
@ -42,28 +28,41 @@ GdkPixbuf *gtk_make_symbolic_pixbuf_from_data (const char *data,
double scale,
const char *debug_output_to,
GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_path (const char *path,
GdkTexture *gdk_texture_new_from_stream (GInputStream *stream,
GCancellable *cancellable,
GError **error);
GdkTexture *gdk_texture_new_from_stream_at_scale (GInputStream *stream,
int width,
int height,
gboolean aspect,
GCancellable *cancellable,
GError **error);
GdkTexture *gdk_texture_new_from_resource_at_scale (const char *path,
int width,
int height,
gboolean aspect,
GError **error);
GdkTexture *gdk_texture_new_from_path_symbolic (const char *path,
int width,
int height,
double scale,
GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_resource (const char *path,
GdkTexture *gdk_texture_new_from_file_symbolic (GFile *file,
int width,
int height,
double scale,
GError **error);
GdkTexture *gdk_texture_new_from_resource_symbolic (const char *path,
int width,
int height,
double scale,
GError **error);
GdkTexture *gtk_load_symbolic_texture_from_file (GFile *file);
GdkTexture *gtk_make_symbolic_texture_from_file (GFile *file,
int width,
int height,
double scale,
GError **error);
GdkTexture *gtk_load_symbolic_texture_from_resource (const char *data);
GdkTexture *gtk_make_symbolic_texture_from_resource (const char *path,
int width,
int height,
double scale,
GError **error);
GdkPaintable *gdk_paintable_new_from_path_scaled (const char *path,
int scale_factor);
GdkPaintable *gdk_paintable_new_from_resource_scaled (const char *path,
@ -72,4 +71,3 @@ GdkPaintable *gdk_paintable_new_from_file_scaled (GFile *file,
int scale_factor);
G_END_DECLS

View File

@ -110,7 +110,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor *recolor,
if (g_str_has_suffix (uri, ".symbolic.png"))
recolor->texture = gtk_load_symbolic_texture_from_resource (resource_path);
else
recolor->texture = gtk_make_symbolic_texture_from_resource (resource_path, 0, 0, 1.0, NULL);
recolor->texture = gdk_texture_new_from_resource_symbolic (resource_path, 0, 0, 1.0, NULL);
g_free (resource_path);
}
@ -119,7 +119,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor *recolor,
if (g_str_has_suffix (uri, ".symbolic.png"))
recolor->texture = gtk_load_symbolic_texture_from_file (recolor->file);
else
recolor->texture = gtk_make_symbolic_texture_from_file (recolor->file, 0, 0, 1.0, NULL);
recolor->texture = gdk_texture_new_from_file_symbolic (recolor->file, 0, 0, 1.0, NULL);
}
g_free (uri);

View File

@ -3747,22 +3747,15 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
{
if (icon->is_svg)
{
GdkPixbuf *source_pixbuf;
if (gtk_icon_paintable_is_symbolic (icon))
source_pixbuf = gtk_make_symbolic_pixbuf_from_resource (icon->filename,
icon->texture = gdk_texture_new_from_resource_symbolic (icon->filename,
pixel_size, pixel_size,
icon->desired_scale,
&load_error);
else
source_pixbuf = _gdk_pixbuf_new_from_resource_at_scale (icon->filename,
icon->texture = gdk_texture_new_from_resource_at_scale (icon->filename,
pixel_size, pixel_size,
TRUE, &load_error);
if (source_pixbuf)
{
icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
g_object_unref (source_pixbuf);
}
}
else
icon->texture = gdk_texture_new_from_resource (icon->filename);
@ -3771,10 +3764,8 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
{
if (icon->is_svg)
{
GdkPixbuf *source_pixbuf;
if (gtk_icon_paintable_is_symbolic (icon))
source_pixbuf = gtk_make_symbolic_pixbuf_from_path (icon->filename,
icon->texture = gdk_texture_new_from_path_symbolic (icon->filename,
pixel_size, pixel_size,
icon->desired_scale,
&load_error);
@ -3783,22 +3774,16 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
GFile *file = g_file_new_for_path (icon->filename);
GInputStream *stream = G_INPUT_STREAM (g_file_read (file, NULL, &load_error));
g_object_unref (file);
if (stream)
{
source_pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream,
icon->texture = gdk_texture_new_from_stream_at_scale (stream,
pixel_size, pixel_size,
TRUE, NULL,
&load_error);
g_object_unref (stream);
}
else
source_pixbuf = NULL;
}
if (source_pixbuf)
{
icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
g_object_unref (source_pixbuf);
g_object_unref (file);
}
}
else
@ -3809,35 +3794,24 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
else
{
GInputStream *stream;
GdkPixbuf *source_pixbuf;
g_assert (icon->loadable);
stream = g_loadable_icon_load (icon->loadable,
pixel_size,
NULL, NULL,
&load_error);
stream = g_loadable_icon_load (icon->loadable, pixel_size, NULL, NULL, &load_error);
if (stream)
{
/* SVG icons are a special case - we just immediately scale them
* to the desired size
*/
if (icon->is_svg)
{
source_pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream,
icon->texture = gdk_texture_new_from_stream_at_scale (stream,
pixel_size, pixel_size,
TRUE, NULL,
&load_error);
}
else
source_pixbuf = _gdk_pixbuf_new_from_stream (stream,
NULL, &load_error);
icon->texture = gdk_texture_new_from_stream (stream, NULL, &load_error);
g_object_unref (stream);
if (source_pixbuf)
{
icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
g_object_unref (source_pixbuf);
}
}
}