icontheme: scale missing sizes like Mac OS X

SVGs are rendered at twice the size and bitmaps taken from one at least
twice as big, then scaled down with Lanczos instead of bilinear.
This commit is contained in:
Daemonratte 2026-10-17 15:02:53 +02:00
commit eb7eee49be

View file

@ -26,6 +26,7 @@
#endif
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <glib.h>
#include <glib/gstdio.h>
@ -2180,17 +2181,19 @@ theme_lookup_icon (IconTheme *theme,
gboolean use_builtin)
{
GList *dirs, *l;
IconThemeDir *dir, *min_dir;
IconThemeDir *dir, *min_dir, *double_dir;
char *file;
int min_difference, difference;
BuiltinIcon *closest_builtin = NULL;
gboolean smaller, has_larger, match;
gboolean smaller, has_larger, match, scalable_match;
IconSuffix suffix;
min_difference = G_MAXINT;
min_dir = NULL;
double_dir = NULL;
has_larger = FALSE;
match = FALSE;
scalable_match = FALSE;
/* Builtin icons are logically part of the default theme and
* are searched before other subdirectories of the default theme.
@ -2232,6 +2235,7 @@ theme_lookup_icon (IconTheme *theme,
if (!match)
{
min_dir = dir;
scalable_match = TRUE;
break;
}
}
@ -2252,6 +2256,11 @@ theme_lookup_icon (IconTheme *theme,
}
}
/* the smallest bitmap of twice the size or more */
if (dir->type != ICON_THEME_DIR_SCALABLE && dir->size >= 2 * size &&
(double_dir == NULL || dir->size < double_dir->size))
double_dir = dir;
if (!match)
{
if (!has_larger)
@ -2283,6 +2292,12 @@ theme_lookup_icon (IconTheme *theme,
}
}
/* A size there is no picture of is made, as Mac OS X does, from one of
* twice the size or more where there is one, made smaller: sharper than
* from the next larger one, or enlarged from a smaller one */
if (!match && !scalable_match && double_dir != NULL)
min_dir = double_dir;
if (min_dir)
{
GtkIconInfo *icon_info = icon_info_new ();
@ -2981,7 +2996,168 @@ apply_emblems (GtkIconInfo *info)
info->emblems_applied = TRUE;
}
/* Scales an icon to @dest_width x @dest_height.
/* The weights of a Lanczos-3 filter that makes @src samples into @dst
* (fewer) ones: for each output sample, the first input sample and
* three times the reduction worth of weights */
typedef struct
{
gint first;
gint n;
gfloat *w;
} ReduceTaps;
static gfloat
lanczos3 (gdouble t)
{
if (t < 0)
t = -t;
if (t < 1e-6)
return 1.f;
if (t >= 3.0)
return 0.f;
return (gfloat) (3.0 * sin (G_PI * t) * sin (G_PI * t / 3.0) / (G_PI * G_PI * t * t));
}
static ReduceTaps *
reduce_taps (gint src,
gint dst)
{
ReduceTaps *taps = g_new (ReduceTaps, dst);
gdouble scale = (gdouble) dst / src;
gdouble support = 3.0 / scale;
gint x, i;
for (x = 0; x < dst; x++)
{
gdouble center = (x + 0.5) / scale;
gint first = (gint) floor (center - support);
gint last = (gint) ceil (center + support);
gfloat sum = 0.f;
taps[x].first = first;
taps[x].n = last - first;
taps[x].w = g_new (gfloat, taps[x].n);
for (i = 0; i < taps[x].n; i++)
{
taps[x].w[i] = lanczos3 ((first + i + 0.5 - center) * scale);
sum += taps[x].w[i];
}
for (i = 0; i < taps[x].n; i++)
taps[x].w[i] /= sum;
}
return taps;
}
static void
reduce_taps_free (ReduceTaps *taps,
gint n)
{
gint i;
for (i = 0; i < n; i++)
g_free (taps[i].w);
g_free (taps);
}
/* Makes an icon smaller with a Lanczos filter, as Mac OS X makes a
* picture of twice the size smaller: sharper than the bilinear filter
* of gdk-pixbuf, which averages. In premultiplied colour, so the
* colours of transparent pixels do not bleed into the edges. */
static GdkPixbuf *
icon_reduce (GdkPixbuf *source,
gint dest_width,
gint dest_height)
{
gint sw = gdk_pixbuf_get_width (source);
gint sh = gdk_pixbuf_get_height (source);
gint n_channels = gdk_pixbuf_get_n_channels (source);
gboolean alpha = gdk_pixbuf_get_has_alpha (source);
gint srs = gdk_pixbuf_get_rowstride (source);
const guchar *spix = gdk_pixbuf_get_pixels (source);
ReduceTaps *htaps, *vtaps;
GdkPixbuf *dest;
gfloat *in, *tmp;
gint x, y, c, i;
if (dest_width >= sw || dest_height >= sh ||
gdk_pixbuf_get_bits_per_sample (source) != 8 || (n_channels != 3 && n_channels != 4))
return gdk_pixbuf_scale_simple (source, dest_width, dest_height, GDK_INTERP_BILINEAR);
dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, dest_width, dest_height);
if (dest == NULL)
return NULL;
in = g_new (gfloat, (gsize) sw * sh * 4);
for (y = 0; y < sh; y++)
for (x = 0; x < sw; x++)
{
const guchar *p = spix + y * srs + x * n_channels;
gfloat a = alpha ? p[3] / 255.f : 1.f;
gfloat *q = in + ((gsize) y * sw + x) * 4;
q[0] = p[0] * a;
q[1] = p[1] * a;
q[2] = p[2] * a;
q[3] = a;
}
/* across, then down */
htaps = reduce_taps (sw, dest_width);
tmp = g_new0 (gfloat, (gsize) dest_width * sh * 4);
for (y = 0; y < sh; y++)
for (x = 0; x < dest_width; x++)
{
gfloat *q = tmp + ((gsize) y * dest_width + x) * 4;
for (i = 0; i < htaps[x].n; i++)
{
gint sx = CLAMP (htaps[x].first + i, 0, sw - 1);
const gfloat *s = in + ((gsize) y * sw + sx) * 4;
for (c = 0; c < 4; c++)
q[c] += htaps[x].w[i] * s[c];
}
}
reduce_taps_free (htaps, dest_width);
g_free (in);
vtaps = reduce_taps (sh, dest_height);
for (y = 0; y < dest_height; y++)
{
guchar *row = gdk_pixbuf_get_pixels (dest) + y * gdk_pixbuf_get_rowstride (dest);
for (x = 0; x < dest_width; x++)
{
gfloat v[4] = { 0.f, 0.f, 0.f, 0.f };
guchar *p = row + x * (alpha ? 4 : 3);
for (i = 0; i < vtaps[y].n; i++)
{
gint sy = CLAMP (vtaps[y].first + i, 0, sh - 1);
const gfloat *s = tmp + ((gsize) sy * dest_width + x) * 4;
for (c = 0; c < 4; c++)
v[c] += vtaps[y].w[i] * s[c];
}
v[3] = CLAMP (v[3], 0.f, 1.f);
for (c = 0; c < 3; c++)
{
gfloat u = v[3] > 0.f ? v[c] / v[3] : 0.f;
p[c] = (guchar) CLAMP (u + 0.5f, 0.f, 255.f);
}
if (alpha)
p[3] = (guchar) (v[3] * 255.f + 0.5f);
}
}
reduce_taps_free (vtaps, dest_height);
g_free (tmp);
return dest;
}
/* Scales an icon to @dest_width x @dest_height: smaller with
* icon_reduce().
*
* Strongly upscaling small bitmap icons with bilinear filtering makes
* them blurry. For tiny bitmap icons enlarged two times or more, each
@ -3006,6 +3182,9 @@ icon_scale_pixbuf (GdkPixbuf *source,
GdkPixbuf *blocks, *scaled;
gint k;
if (dest_width < width && dest_height < height)
return icon_reduce (source, dest_width, dest_height);
if (image_size <= 0 || image_size > 64 || scale < 2.0)
return gdk_pixbuf_scale_simple (source, dest_width, dest_height,
GDK_INTERP_BILINEAR);
@ -3113,12 +3292,20 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info,
&icon_info->load_error);
if (stream)
{
icon_info->pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
icon_info->desired_size,
icon_info->desired_size,
TRUE,
NULL,
&icon_info->load_error);
/* As Mac OS X does: drawn at twice the size, then made smaller */
GdkPixbuf *twice = gdk_pixbuf_new_from_stream_at_scale (stream,
2 * icon_info->desired_size,
2 * icon_info->desired_size,
TRUE,
NULL,
&icon_info->load_error);
if (twice)
{
icon_info->pixbuf = icon_reduce (twice,
MAX (1, (gdk_pixbuf_get_width (twice) + 1) / 2),
MAX (1, (gdk_pixbuf_get_height (twice) + 1) / 2));
g_object_unref (twice);
}
_gtk_icon_pixbuf_mark_shared (icon_info->pixbuf);
g_object_unref (stream);
}