File: bonobo-ui-icon-modify.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (128 lines) | stat: -rw-r--r-- 3,697 bytes parent folder | download | duplicates (2)
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
/* bonobo-ui-icon-modify.h: Icon modifier for the Bonobo UI engine
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "bonobo-ui-icon-modify.h"



/**
 * bonobo_ui_icon_modify:
 * @source: A pixbuf.
 * @saturation: Saturation value for the new image.  1.0 means full saturation and
 * leaves the original colors untouched.  0.0 means full desaturation and effectively
 * converts the image to grayscale.
 * @pixelate: Whether to apply a 50% dark stipple pattern to the destination image.
 * @pixelation_dark_factor: If @pixelate is TRUE, then the stipple pattern will
 * be created using the colors of the original image darkened by this factor.
 * 0.0 is completely black and 1.0 leaves the original colors untouched.
 * 
 * Modifies a @source pixbuf for use as a menu/toolbar icon by optionally
 * desaturating and applying a dark stipple pattern to it.
 * 
 * Return value: The generated, modified image or NULL if it could not be
 * created.
 **/
GdkPixbuf *
bonobo_ui_icon_modify (GdkPixbuf *source, double saturation,
		       gboolean pixelate, double pixelation_dark_factor)
{
	GdkPixbuf *dest;
	int width, height, source_rowstride, n_channels;
	int dest_rowstride;
	gboolean has_alpha;
	int x, y;
	guchar *src_row, *dest_row;

	g_return_val_if_fail (source != NULL, NULL);
	g_return_val_if_fail (gdk_pixbuf_get_colorspace (source) == GDK_COLORSPACE_RGB, NULL);

	n_channels = gdk_pixbuf_get_n_channels (source);
	has_alpha = gdk_pixbuf_get_has_alpha (source);

	g_assert ((!has_alpha && n_channels == 3) || (has_alpha && n_channels == 4));

	width = gdk_pixbuf_get_width (source);
	height = gdk_pixbuf_get_height (source);
	source_rowstride = gdk_pixbuf_get_rowstride (source);

	dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, 8, width, height);
	if (!dest)
		return NULL;

	dest_rowstride = gdk_pixbuf_get_rowstride (dest);

	src_row = gdk_pixbuf_get_pixels (source);
	dest_row = gdk_pixbuf_get_pixels (dest);

	for (y = 0; y < height; y++) {
		guchar *s, *d;

		s = src_row;
		d = dest_row;

		for (x = 0; x < width; x++) {
			int r, g, b;

			r = *s++;
			g = *s++;
			b = *s++;

			/* Desaturate */

			if (saturation != 1.0) {
				int intensity;

				intensity = r * 0.30 + g * 0.59 + b * 0.11 + 0.5;

				r = intensity + (r - intensity) * saturation + 0.5;
				g = intensity + (g - intensity) * saturation + 0.5;
				b = intensity + (b - intensity) * saturation + 0.5;
			}

			/* Pixelate if needed */

			if (pixelate && ((x + y) % 2) == 0) {
				r = r * pixelation_dark_factor + 0.5;
				g = g * pixelation_dark_factor + 0.5;
				b = b * pixelation_dark_factor + 0.5;
			}

			/* Set destination pixel */
			
			*d++ = CLAMP (r, 0, 255);
			*d++ = CLAMP (g, 0, 255);
			*d++ = CLAMP (b, 0, 255);

			if (has_alpha)
				*d++ = *s++;
		}

		src_row += source_rowstride;
		dest_row += dest_rowstride;
	}

	return dest;
}