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
|
/*
* Copyright (C) 2007, Benjamin Berg
*
* 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "sugar-utils.h"
GdkPixbuf*
sugar_pixbuf_scale_or_ref (GdkPixbuf *pixbuf,
gint width,
gint height)
{
gint pixbuf_width = gdk_pixbuf_get_width (pixbuf);
gint pixbuf_height = gdk_pixbuf_get_height (pixbuf);
if ((pixbuf_width != width) || (pixbuf_height != height)) {
return gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
} else {
g_object_ref (pixbuf);
return pixbuf;
}
}
#define RED 0
#define GREEN 1
#define BLUE 2
#define ALPHA 3
GdkPixbuf*
sugar_get_insensitive_icon (GdkPixbuf *icon,
guint range,
guint base)
{
GdkPixbuf *result = gdk_pixbuf_copy (icon);
guint x, y, rowstride, height, width;
guint n_channels;
guint8 *pixel, *pixels;
guint min_color_value = G_MAXUINT8;
guint max_color_value = 0;
guint mult;
gboolean has_alpha;
width = gdk_pixbuf_get_width (result);
height = gdk_pixbuf_get_height (result);
rowstride = gdk_pixbuf_get_rowstride (result);
n_channels = gdk_pixbuf_get_n_channels (result);
pixels = gdk_pixbuf_get_pixels (result);
for (y = 0; y < height; y++) {
pixel = pixels + y*rowstride;
for (x = 0; x < width; x++) {
if (n_channels == 4 && pixel[ALPHA] == 0) {
pixel += n_channels;
continue;
}
min_color_value = MIN (min_color_value, pixel[RED]);
max_color_value = MAX (max_color_value, pixel[RED]);
min_color_value = MIN (min_color_value, pixel[GREEN]);
max_color_value = MAX (max_color_value, pixel[GREEN]);
min_color_value = MIN (min_color_value, pixel[BLUE]);
max_color_value = MAX (max_color_value, pixel[BLUE]);
pixel += n_channels;
}
}
/* Shift the base value to the lower side of the range */
if (max_color_value - min_color_value > 0) {
base = base - range / 2;
mult = (range << 8) / (max_color_value - min_color_value);
} else {
/* There is only one color, just fill everything with base.
* Se setting mult to 0 means that the old pixel value will just
* be discarted. */
mult = 0;
}
for (y = 0; y < height; y++) {
pixel = pixels + y*rowstride;
for (x = 0; x < width; x++) {
pixel[RED] = ((((guint) (pixel[RED] - min_color_value)) * mult) >> 8) + base;
pixel[GREEN] = ((((guint) (pixel[GREEN] - min_color_value)) * mult) >> 8) + base;
pixel[BLUE] = ((((guint) (pixel[BLUE] - min_color_value)) * mult) >> 8) + base;
pixel += n_channels;
}
}
return result;
}
#undef RED
#undef GREEN
#undef BLUE
#undef ALPHA
|