1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
//
// Copyright (C) 2011-2012 Robert Dyer, Rico Tzschichholz
//
// This file is part of Plank.
//
// Plank is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Plank 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
namespace Plank
{
/**
* Utility service for loading icons and working with pixbufs.
*/
public class DrawingService : GLib.Object
{
const string DEFAULT_ICON = "application-default-icon";
const string FILE_ATTRIBUTE_CUSTOM_ICON = "metadata::custom-icon";
const string FILE_ATTRIBUTE_CUSTOM_ICON_NAME = "metadata::custom-icon-name";
// Parameters for average-color calculation
const double SATURATION_WEIGHT = 1.5;
const double WEIGHT_THRESHOLD = 1.0;
const uint8 ALPHA_THRESHOLD = 24;
static Mutex icon_theme_mutex;
static Gtk.IconTheme icon_theme;
DrawingService ()
{
}
public static unowned Gtk.IconTheme get_icon_theme ()
{
icon_theme_mutex.lock ();
if (icon_theme == null)
icon_theme = Gtk.IconTheme.get_for_screen (Gdk.Screen.get_default ());
icon_theme_mutex.unlock ();
return icon_theme;
}
/**
* Gets the icon name from a {@link GLib.File}.
*
* @param file the file to get the icon name for
* @return the icon name for the file, or null if none exists
*/
public static string? get_icon_from_file (File file)
{
try {
var info = file.query_info (FileAttribute.STANDARD_ICON + ","
+ FILE_ATTRIBUTE_CUSTOM_ICON_NAME + "," + FILE_ATTRIBUTE_CUSTOM_ICON + ","
+ FileAttribute.THUMBNAIL_PATH, 0);
// look for a custom icon-name
unowned string custom_icon_name = info.get_attribute_string (FILE_ATTRIBUTE_CUSTOM_ICON_NAME);
if (custom_icon_name != null && custom_icon_name != "")
return custom_icon_name;
// look for a custom icon
unowned string custom_icon = info.get_attribute_string (FILE_ATTRIBUTE_CUSTOM_ICON);
if (custom_icon != null && custom_icon != "") {
if (custom_icon.has_prefix ("file://"))
return custom_icon;
return file.get_child (custom_icon).get_path ();
}
// look for a thumbnail
unowned string thumb_icon = info.get_attribute_byte_string (FileAttribute.THUMBNAIL_PATH);
if (thumb_icon != null && thumb_icon != "")
return thumb_icon;
// otherwise try to get the icon from the fileinfo
return get_icon_from_gicon (info.get_icon ());
} catch {
debug ("Could not get file info for '%s'", file.get_path () ?? "");
}
return null;
}
/**
* Gets an icon from a {@link GLib.Icon}.
*
* @param icon the icon to get the name for
* @return the icon name, or null if none exists
*/
public static string? get_icon_from_gicon (Icon? icon)
{
if (icon is ThemedIcon) {
var icons = string.joinv (";;", ((ThemedIcon) icon).get_names ());
// Remove possible null values which sneaked through joinv, possibly a GTK+ bug?
return icons.replace ("(null);;", "");
}
if (icon is FileIcon)
return ((FileIcon) icon).get_file ().get_path ();
return null;
}
/**
* Loads an icon based on names and the given width/height
*
* @param names a delimited (with ";;") list of icon names, first one found is used
* @param width the requested width of the icon
* @param height the requested height of the icon
* @return the pixbuf representing the requested icon
*/
public static Gdk.Pixbuf load_icon (string names, int width, int height)
{
Gdk.Pixbuf? pbuf = null;
var all_names = names.split (";;");
all_names += DEFAULT_ICON;
foreach (unowned string name in all_names) {
var file = try_get_icon_file (name);
if (file != null) {
pbuf = load_pixbuf_from_file (file, width, height);
if (pbuf != null)
break;
}
pbuf = load_pixbuf (name, int.max (width, height));
if (pbuf != null)
break;
if (name != DEFAULT_ICON)
message ("Could not find icon '%s'", name);
}
// Load internal default icon as last resort
if (pbuf == null)
pbuf = load_pixbuf_from_resource (Plank.G_RESOURCE_PATH + "/img/application-default-icon.svg", width, height);
if (pbuf != null) {
if (width != -1 && height != -1 && (width != pbuf.width || height != pbuf.height))
return ar_scale (pbuf, width, height);
return pbuf;
}
warning ("No icon found, return empty pixbuf");
return get_empty_pixbuf (int.max (1, width), int.max (1, height));
}
static Gdk.Pixbuf get_empty_pixbuf (int width, int height)
{
var pbuf = new Gdk.Pixbuf (Gdk.Colorspace.RGB, true, 8, width, height);
pbuf.fill (0x00000000);
return pbuf;
}
/**
* Try to get a {@link GLib.File} for the given icon name
*
* @param name a string which might represent an existing file
* @return a {@link GLib.File}, or null if it failed
*/
public static File? try_get_icon_file (string name)
{
File? file = null;
var name_down = name.down ();
if (name_down.has_prefix ("resource://"))
file = File.new_for_uri (name);
else if (name_down.has_prefix ("file://"))
file = File.new_for_uri (name);
else if (name.has_prefix ("~/"))
file = File.new_for_path (name.replace ("~", Environment.get_home_dir ()));
else if (name.has_prefix ("/"))
file = File.new_for_path (name);
if (file != null && file.query_exists ())
return file;
return null;
}
static Gdk.Pixbuf? load_pixbuf_from_file (File file, int width, int height)
{
Gdk.Pixbuf? pbuf = null;
try {
var fis = file.read ();
pbuf = new Gdk.Pixbuf.from_stream_at_scale (fis, width, height, true);
} catch { }
return pbuf;
}
static Gdk.Pixbuf? load_pixbuf_from_resource (string resource, int width, int height)
{
Gdk.Pixbuf? pbuf = null;
try {
pbuf = new Gdk.Pixbuf.from_resource_at_scale (resource, width, height, true);
} catch { }
return pbuf;
}
static Gdk.Pixbuf? load_pixbuf (string icon, int size)
{
Gdk.Pixbuf? pbuf = null;
unowned Gtk.IconTheme icon_theme = get_icon_theme ();
icon_theme_mutex.lock ();
try {
pbuf = icon_theme.load_icon (icon, size, 0);
} catch { }
try {
if (pbuf == null && icon.contains (".")) {
var parts = icon.split (".");
pbuf = icon_theme.load_icon (parts [0], size, 0);
}
} catch { }
icon_theme_mutex.unlock ();
return pbuf;
}
/**
* Loads an icon based on names and the given width/height
*
* @param names a delimited (with ";;") list of icon names, first one found is used
* @param width the requested width of the icon
* @param height the requested height of the icon
* @param scale the implicit requested scale of the icon
* @return the {link Cairo.Surface} containing the requested icon, do not alter this surface
*/
public static Cairo.Surface? load_icon_for_scale (string names, int width, int height, int scale)
{
Cairo.Surface? surface = null;
var all_names = names.split (";;");
all_names += DEFAULT_ICON;
foreach (unowned string name in all_names) {
var file = try_get_icon_file (name);
if (file != null) {
var pbuf = load_pixbuf_from_file (file, width, height);
if (pbuf != null) {
surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
var cr = new Cairo.Context (surface);
Gdk.cairo_set_source_pixbuf (cr, pbuf, (width - pbuf.width) / 2, (height - pbuf.height) / 2);
cr.paint ();
surface.set_device_scale (scale, scale);
break;
}
}
surface = load_surface (name, int.max (width, height) / scale, scale);
if (surface != null)
break;
if (name != DEFAULT_ICON)
message ("Could not find icon '%s'", name);
}
// Load internal default icon as last resort
if (surface == null)
surface = load_surface_from_resource_at_scale (Plank.G_RESOURCE_PATH + "/img/application-default-icon.svg", width, height, scale);
return surface;
}
static Cairo.Surface? load_surface_from_resource_at_scale (string resource, int width, int height, int scale)
{
Gdk.Pixbuf? pbuf = null;
Cairo.Surface? surface = null;
try {
pbuf = new Gdk.Pixbuf.from_resource_at_scale (resource, width, height, true);
} catch { }
if (pbuf != null) {
surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
var cr = new Cairo.Context (surface);
Gdk.cairo_set_source_pixbuf (cr, pbuf, (width - pbuf.width) / 2, (height - pbuf.height) / 2);
cr.paint ();
surface.set_device_scale (scale, scale);
}
return surface;
}
static Cairo.Surface? load_surface (string icon, int size, int scale)
{
Cairo.Surface? surface = null;
Gtk.IconInfo? info = null;
unowned Gtk.IconTheme icon_theme = get_icon_theme ();
icon_theme_mutex.lock ();
try {
info = icon_theme.lookup_icon_for_scale (icon, size, scale, Gtk.IconLookupFlags.FORCE_SIZE);
if (info != null)
surface = info.load_surface (null);
} catch { }
try {
if (surface == null && icon.contains (".")) {
var parts = icon.split (".");
info = icon_theme.lookup_icon_for_scale (parts [0], size, scale, Gtk.IconLookupFlags.FORCE_SIZE);
if (info != null)
surface = info.load_surface (null);
}
} catch { }
icon_theme_mutex.unlock ();
return surface;
}
/**
* Scales a {@link Gdk.Pixbuf}, maintaining the original aspect ratio.
*
* @param source the pixbuf to scale
* @param width the width of the scaled pixbuf
* @param height the height of the scaled pixbuf
* @return the scaled pixbuf
*/
public static Gdk.Pixbuf ar_scale (Gdk.Pixbuf source, int width, int height)
{
var source_width = (double) source.width;
var source_height = (double) source.height;
var x_scale = width / source_width;
var y_scale = height / source_height;
var scale = double.min (x_scale, y_scale);
if (scale == 1)
return source;
var scaled_width = int.max (1, (int) (source_width * scale));
var scaled_height = int.max (1, (int) (source_height * scale));
return source.scale_simple (scaled_width, scaled_height, Gdk.InterpType.HYPER);
}
/**
* Computes and returns the average color of a {@link Gdk.Pixbuf}.
* The resulting color is the average of all pixels which aren't
* nearly transparent while saturated pixels are weighted more than
* "grey" ones.
*
* @param source the pixbuf to use
* @return the average color of the pixbuf
*/
public static Color average_color (Gdk.Pixbuf source)
{
uint8 r, g, b, a, min, max;
double delta;
var rTotal = 0.0;
var gTotal = 0.0;
var bTotal = 0.0;
var bTotal2 = 0.0;
var gTotal2 = 0.0;
var rTotal2 = 0.0;
var aTotal2 = 0.0;
uint8* dataPtr = source.get_pixels ();
int n_channels = source.n_channels;
int width = source.width;
int height = source.height;
int rowstride = source.rowstride;
int length = width * height;
int pixels = height * rowstride / n_channels;
double scoreTotal = 0.0;
for (var i = 0; i < pixels; i++) {
r = dataPtr [0];
g = dataPtr [1];
b = dataPtr [2];
a = dataPtr [3];
// skip (nearly) invisible pixels
if (a <= ALPHA_THRESHOLD) {
length--;
dataPtr += n_channels;
continue;
}
min = uint8.min (r, uint8.min (g, b));
max = uint8.max (r, uint8.max (g, b));
delta = max - min;
// prefer colored pixels over shades of grey
var score = SATURATION_WEIGHT * (delta == 0 ? 0.0 : delta / max);
// weighted sums, revert pre-multiplied alpha value
bTotal += score * b / a;
gTotal += score * g / a;
rTotal += score * r / a;
scoreTotal += score;
// not weighted sums
bTotal2 += b;
gTotal2 += g;
rTotal2 += r;
aTotal2 += a;
dataPtr += n_channels;
}
// looks like a fully transparent image
if (length <= 0)
return { 0.0, 0.0, 0.0, 0.0 };
scoreTotal /= length;
bTotal /= length;
gTotal /= length;
rTotal /= length;
if (scoreTotal > 0.0) {
bTotal /= scoreTotal;
gTotal /= scoreTotal;
rTotal /= scoreTotal;
}
bTotal2 /= length * uint8.MAX;
gTotal2 /= length * uint8.MAX;
rTotal2 /= length * uint8.MAX;
aTotal2 /= length * uint8.MAX;
// combine weighted and not weighted sum depending on the average "saturation"
// if saturation isn't reasonable enough
// s = 0.0 -> f = 0.0 ; s = WEIGHT_THRESHOLD -> f = 1.0
if (scoreTotal <= WEIGHT_THRESHOLD) {
var f = 1.0 / WEIGHT_THRESHOLD * scoreTotal;
var rf = 1.0 - f;
bTotal = bTotal * f + bTotal2 * rf;
gTotal = gTotal * f + gTotal2 * rf;
rTotal = rTotal * f + rTotal2 * rf;
}
// there shouldn't be values larger then 1.0
var max_val = double.max (rTotal, double.max (gTotal, bTotal));
if (max_val > 1.0) {
bTotal /= max_val;
gTotal /= max_val;
rTotal /= max_val;
}
return { rTotal, gTotal, bTotal, aTotal2 };
}
}
}
|