source: trunk/third/bonobo/bonobo/bonobo-ui-icon-modify.c @ 17169

Revision 17169, 3.6 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17168, which included commits to RCS files with non-trunk default branches.
Line 
1/* bonobo-ui-icon-modify.h: Icon modifier for the Bonobo UI engine
2 *
3 * Copyright (C) 2001 Ximian, Inc.
4 *
5 * Author: Federico Mena-Quintero <federico@ximian.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include "bonobo-ui-icon-modify.h"
28
29
30
31/**
32 * bonobo_ui_icon_modify:
33 * @source: A pixbuf.
34 * @saturation: Saturation value for the new image.  1.0 means full saturation and
35 * leaves the original colors untouched.  0.0 means full desaturation and effectively
36 * converts the image to grayscale.
37 * @pixelate: Whether to apply a 50% dark stipple pattern to the destination image.
38 * @pixelation_dark_factor: If @pixelate is TRUE, then the stipple pattern will
39 * be created using the colors of the original image darkened by this factor.
40 * 0.0 is completely black and 1.0 leaves the original colors untouched.
41 *
42 * Modifies a @source pixbuf for use as a menu/toolbar icon by optionally
43 * desaturating and applying a dark stipple pattern to it.
44 *
45 * Return value: The generated, modified image or NULL if it could not be
46 * created.
47 **/
48GdkPixbuf *
49bonobo_ui_icon_modify (GdkPixbuf *source, double saturation,
50                       gboolean pixelate, double pixelation_dark_factor)
51{
52        GdkPixbuf *dest;
53        int width, height, source_rowstride, n_channels;
54        int dest_rowstride;
55        gboolean has_alpha;
56        int x, y;
57        guchar *src_row, *dest_row;
58
59        g_return_val_if_fail (source != NULL, NULL);
60        g_return_val_if_fail (gdk_pixbuf_get_colorspace (source) == GDK_COLORSPACE_RGB, NULL);
61
62        n_channels = gdk_pixbuf_get_n_channels (source);
63        has_alpha = gdk_pixbuf_get_has_alpha (source);
64
65        g_assert ((!has_alpha && n_channels == 3) || (has_alpha && n_channels == 4));
66
67        width = gdk_pixbuf_get_width (source);
68        height = gdk_pixbuf_get_height (source);
69        source_rowstride = gdk_pixbuf_get_rowstride (source);
70
71        dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, 8, width, height);
72        if (!dest)
73                return NULL;
74
75        dest_rowstride = gdk_pixbuf_get_rowstride (dest);
76
77        src_row = gdk_pixbuf_get_pixels (source);
78        dest_row = gdk_pixbuf_get_pixels (dest);
79
80        for (y = 0; y < height; y++) {
81                guchar *s, *d;
82
83                s = src_row;
84                d = dest_row;
85
86                for (x = 0; x < width; x++) {
87                        int r, g, b;
88
89                        r = *s++;
90                        g = *s++;
91                        b = *s++;
92
93                        /* Desaturate */
94
95                        if (saturation != 1.0) {
96                                int intensity;
97
98                                intensity = r * 0.30 + g * 0.59 + b * 0.11 + 0.5;
99
100                                r = intensity + (r - intensity) * saturation + 0.5;
101                                g = intensity + (g - intensity) * saturation + 0.5;
102                                b = intensity + (b - intensity) * saturation + 0.5;
103                        }
104
105                        /* Pixelate if needed */
106
107                        if (pixelate && ((x + y) % 2) == 0) {
108                                r = r * pixelation_dark_factor + 0.5;
109                                g = g * pixelation_dark_factor + 0.5;
110                                b = b * pixelation_dark_factor + 0.5;
111                        }
112
113                        /* Set destination pixel */
114                       
115                        *d++ = CLAMP (r, 0, 255);
116                        *d++ = CLAMP (g, 0, 255);
117                        *d++ = CLAMP (b, 0, 255);
118
119                        if (has_alpha)
120                                *d++ = *s++;
121                }
122
123                src_row += source_rowstride;
124                dest_row += dest_rowstride;
125        }
126
127        return dest;
128}
Note: See TracBrowser for help on using the repository browser.