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

View File

@ -21,20 +21,6 @@
G_BEGIN_DECLS 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, GdkPixbuf *gtk_make_symbolic_pixbuf_from_data (const char *data,
gsize len, gsize len,
int width, int width,
@ -42,28 +28,41 @@ GdkPixbuf *gtk_make_symbolic_pixbuf_from_data (const char *data,
double scale, double scale,
const char *debug_output_to, const char *debug_output_to,
GError **error); 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 width,
int height, int height,
double scale, double scale,
GError **error); GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_resource (const char *path, GdkTexture *gdk_texture_new_from_file_symbolic (GFile *file,
int width, int width,
int height, int height,
double scale, double scale,
GError **error); 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_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_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, GdkPaintable *gdk_paintable_new_from_path_scaled (const char *path,
int scale_factor); int scale_factor);
GdkPaintable *gdk_paintable_new_from_resource_scaled (const char *path, 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); int scale_factor);
G_END_DECLS 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")) if (g_str_has_suffix (uri, ".symbolic.png"))
recolor->texture = gtk_load_symbolic_texture_from_resource (resource_path); recolor->texture = gtk_load_symbolic_texture_from_resource (resource_path);
else 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); g_free (resource_path);
} }
@ -119,7 +119,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor *recolor,
if (g_str_has_suffix (uri, ".symbolic.png")) if (g_str_has_suffix (uri, ".symbolic.png"))
recolor->texture = gtk_load_symbolic_texture_from_file (recolor->file); recolor->texture = gtk_load_symbolic_texture_from_file (recolor->file);
else 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); g_free (uri);

View File

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

View File

@ -133,9 +133,9 @@ main (int argc, char **argv)
out = g_file_replace (dest, out = g_file_replace (dest,
NULL, FALSE, NULL, FALSE,
G_FILE_CREATE_REPLACE_DESTINATION, G_FILE_CREATE_REPLACE_DESTINATION,
NULL, &error); NULL, &error);
if (out == NULL) if (out == NULL)
{ {
g_printerr (_("Cant save file %s: %s\n"), pngpath, error->message); g_printerr (_("Cant save file %s: %s\n"), pngpath, error->message);