summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-graphic-effects.c
blob: 8ea37a7cea8d34cbfd306e5968d15ebfdd6b6ea7 (plain)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* Nautilus - pixbuf manipulation routines for graphical effects.
 *
 * Copyright (C) 2000 Eazel, Inc
 *
 * Author: Andy Hertzfeld <andy@eazel.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.
 */

/* This file contains pixbuf manipulation routines used for graphical effects like pre-lighting
   and selection hilighting */

#include <config.h>
#include "nautilus-graphic-effects.h"

/* shared utility to create a new pixbuf from the passed-in one */

static GdkPixbuf *
create_new_pixbuf (GdkPixbuf *src)
{
	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);

	return gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
			       gdk_pixbuf_get_has_alpha (src),
			       gdk_pixbuf_get_bits_per_sample (src),
			       gdk_pixbuf_get_width (src),
			       gdk_pixbuf_get_height (src));
}

static GdkPixbuf *
create_new_pixbuf_with_alpha (GdkPixbuf *src)
{
	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);

	return gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
			       TRUE,
			       gdk_pixbuf_get_bits_per_sample (src),
			       gdk_pixbuf_get_width (src),
			       gdk_pixbuf_get_height (src));
}

/* utility routine to bump the level of a color component with pinning */

static guchar
lighten_component (guchar cur_value)
{
	int new_value = cur_value;
	new_value += 24 + (new_value >> 3);
	if (new_value > 255) {
		new_value = 255;
	}
	return (guchar) new_value;
}

GdkPixbuf *
nautilus_create_spotlight_pixbuf (GdkPixbuf* src)
{
	GdkPixbuf *dest;
	int i, j;
	int width, height, has_alpha, src_row_stride, dst_row_stride;
	guchar *target_pixels, *original_pixels;
	guchar *pixsrc, *pixdest;

	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);
	g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);

	dest = create_new_pixbuf (src);
	
	has_alpha = gdk_pixbuf_get_has_alpha (src);
	width = gdk_pixbuf_get_width (src);
	height = gdk_pixbuf_get_height (src);
	dst_row_stride = gdk_pixbuf_get_rowstride (dest);
	src_row_stride = gdk_pixbuf_get_rowstride (src);
	target_pixels = gdk_pixbuf_get_pixels (dest);
	original_pixels = gdk_pixbuf_get_pixels (src);

	for (i = 0; i < height; i++) {
		pixdest = target_pixels + i * dst_row_stride;
		pixsrc = original_pixels + i * src_row_stride;
		for (j = 0; j < width; j++) {		
			*pixdest++ = lighten_component (*pixsrc++);
			*pixdest++ = lighten_component (*pixsrc++);
			*pixdest++ = lighten_component (*pixsrc++);
			if (has_alpha) {
				*pixdest++ = *pixsrc++;
			}
		}
	}
	return dest;
}


/* the following routine was stolen from the panel to darken a pixbuf, by manipulating the saturation */

/* saturation is 0-255, darken is 0-255 */

GdkPixbuf *
nautilus_create_darkened_pixbuf (GdkPixbuf *src, int saturation, int darken)
{
	gint i, j;
	gint width, height, src_row_stride, dest_row_stride;
	gboolean has_alpha;
	guchar *target_pixels, *original_pixels;
	guchar *pixsrc, *pixdest;
	guchar intensity;
	guchar alpha;
	guchar negalpha;
	guchar r, g, b;
	GdkPixbuf *dest;

	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);
	g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);

	dest = create_new_pixbuf (src);

	has_alpha = gdk_pixbuf_get_has_alpha (src);
	width = gdk_pixbuf_get_width (src);
	height = gdk_pixbuf_get_height (src);
	dest_row_stride = gdk_pixbuf_get_rowstride (dest);
	src_row_stride = gdk_pixbuf_get_rowstride (src);
	target_pixels = gdk_pixbuf_get_pixels (dest);
	original_pixels = gdk_pixbuf_get_pixels (src);

	for (i = 0; i < height; i++) {
		pixdest = target_pixels + i * dest_row_stride;
		pixsrc = original_pixels + i * src_row_stride;
		for (j = 0; j < width; j++) {
			r = *pixsrc++;
			g = *pixsrc++;
			b = *pixsrc++;
			intensity = (r * 77 + g * 150 + b * 28) >> 8;
			negalpha = ((255 - saturation) * darken) >> 8;
			alpha = (saturation * darken) >> 8;
			*pixdest++ = (negalpha * intensity + alpha * r) >> 8;
			*pixdest++ = (negalpha * intensity + alpha * g) >> 8;
			*pixdest++ = (negalpha * intensity + alpha * b) >> 8;
			if (has_alpha) {
				*pixdest++ = *pixsrc++;
			}
		}
	}
	return dest;
}

/* this routine colorizes the passed-in pixbuf by multiplying each pixel with the passed in color */

GdkPixbuf *
nautilus_create_colorized_pixbuf (GdkPixbuf *src,
				  int red_value,
				  int green_value,
				  int blue_value)
{
	int i, j;
	int width, height, has_alpha, src_row_stride, dst_row_stride;
	guchar *target_pixels;
	guchar *original_pixels;
	guchar *pixsrc;
	guchar *pixdest;
	GdkPixbuf *dest;
	
	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);
	g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);

	dest = create_new_pixbuf (src);
	
	has_alpha = gdk_pixbuf_get_has_alpha (src);
	width = gdk_pixbuf_get_width (src);
	height = gdk_pixbuf_get_height (src);
	src_row_stride = gdk_pixbuf_get_rowstride (src);
	dst_row_stride = gdk_pixbuf_get_rowstride (dest);
	target_pixels = gdk_pixbuf_get_pixels (dest);
	original_pixels = gdk_pixbuf_get_pixels (src);

	for (i = 0; i < height; i++) {
		pixdest = target_pixels + i*dst_row_stride;
		pixsrc = original_pixels + i*src_row_stride;
		for (j = 0; j < width; j++) {		
			*pixdest++ = (*pixsrc++ * red_value) >> 8;
			*pixdest++ = (*pixsrc++ * green_value) >> 8;
			*pixdest++ = (*pixsrc++ * blue_value) >> 8;
			if (has_alpha) {
				*pixdest++ = *pixsrc++;
			}
		}
	}
	return dest;
}

/* draw a frame with a drop shadow into the passed in pixbuf */

void
nautilus_draw_frame (GdkPixbuf *frame_pixbuf)
{
	int index, h_index, last_index, pin_width;
	int width, height, depth, rowstride, fill_value;
  	guchar *pixels, *temp_pixels;
	
	width = gdk_pixbuf_get_width (frame_pixbuf);
	height = gdk_pixbuf_get_height (frame_pixbuf);
	depth = gdk_pixbuf_get_bits_per_sample (frame_pixbuf);
	pixels = gdk_pixbuf_get_pixels (frame_pixbuf);
	rowstride = gdk_pixbuf_get_rowstride (frame_pixbuf);

	/* loop through the pixbuf a scaleline at a time, drawing the frame */
	for (index = 0; index < height; index++) {
		/* special case the first and last few lines to make them dark */
		fill_value = 239;
		pin_width = width;
		last_index = height - index;
		if (index == 0)
			fill_value = 0;
		else if (index > (height - 6)) {
			fill_value = (index - (height - 5)) * 50;
			pin_width = width - (height - index);
		}	
		/* memset(pixels, fill_value, rowstride); */
		temp_pixels = pixels;
		for (h_index = 0; h_index < pin_width; h_index++) {
			*temp_pixels++ = fill_value;
			*temp_pixels++ = fill_value;
			*temp_pixels++ = fill_value;
			*temp_pixels++ = ((last_index < 6) && ((4 - last_index) > h_index)) ? 0 : 255;
		}
		
		/* draw the frame at the edge for each scanline */
		if (last_index > 5) {
			temp_pixels = pixels;
			*temp_pixels++ = 0;
			*temp_pixels++ = 0;
			*temp_pixels++ = 0;
			*temp_pixels++ = 255;
		}
		
		pixels += rowstride;
		
		/* handle the last 5 pixels specially for the drop shadow */
		
		temp_pixels = pixels - 5*4;

		if (last_index > 4) {
			*temp_pixels++ = 0;
			*temp_pixels++ = 0;
			*temp_pixels++ = 0;		
			*temp_pixels++ = 255;
		} else temp_pixels += 4;
		
		if (last_index > 3) {			
			*temp_pixels++ = 50;
			*temp_pixels++ = 50;
			*temp_pixels++ = 50;		
			*temp_pixels++ = index < 1 ? 0 : 255;
		}  else temp_pixels += 4;
		
		if (last_index > 2) {
			*temp_pixels++ = 100;
			*temp_pixels++ = 100;
			*temp_pixels++ = 100;		
			*temp_pixels++ = index < 2 ? 0 : 255;
		}  else temp_pixels += 4;
		
		if (last_index > 1) {
			*temp_pixels++ = 150;
			*temp_pixels++ = 150;
			*temp_pixels++ = 150;		
			*temp_pixels++ = index < 3 ? 0 : 255;
		}  else temp_pixels += 4;
		
		if (last_index > 0) {
			*temp_pixels++ = 200;
			*temp_pixels++ = 200;
			*temp_pixels++ = 200;		
			*temp_pixels++ = index < 4 ? 0 : 255;
		}
	}
}


/* this routine takes the source pixbuf and returns a new one that's semi-transparent, by
   clearing every other pixel's alpha value in a checkerboard grip.  We have to do the
   checkerboard instead of reducing the alpha since it will be turned into an alpha-less
   gdkpixmap and mask for the actual dragging */

GdkPixbuf *
nautilus_make_semi_transparent (GdkPixbuf *src)
{
	gint i, j, temp_alpha;
	gint width, height, has_alpha, src_row_stride, dst_row_stride;
	guchar *target_pixels, *original_pixels;
	guchar *pixsrc, *pixdest;
	guchar alpha_value;
	GdkPixbuf *dest_pixbuf;
	guchar start_alpha_value;
	
	g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
	g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
			       && gdk_pixbuf_get_n_channels (src) == 3)
			      || (gdk_pixbuf_get_has_alpha (src)
				  && gdk_pixbuf_get_n_channels (src) == 4), NULL);
	g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);

	dest_pixbuf = create_new_pixbuf_with_alpha (src);

	has_alpha = gdk_pixbuf_get_has_alpha (src);
	width = gdk_pixbuf_get_width (src);
	height = gdk_pixbuf_get_height (src);
	src_row_stride = gdk_pixbuf_get_rowstride (src);
	dst_row_stride = gdk_pixbuf_get_rowstride (dest_pixbuf);
	
	/* set up pointers to the actual pixels */
	target_pixels = gdk_pixbuf_get_pixels (dest_pixbuf);
	original_pixels = gdk_pixbuf_get_pixels (src);

	/* loop through the pixels to do the actual work, copying from the source to the destination */
	start_alpha_value = ~0;
	for (i = 0; i < height; i++) {
		pixdest = target_pixels + i * dst_row_stride;
		pixsrc = original_pixels + i * src_row_stride;
		alpha_value = start_alpha_value;
		for (j = 0; j < width; j++) {
			*pixdest++ = *pixsrc++; /* red */
			*pixdest++ = *pixsrc++; /* green */
			*pixdest++ = *pixsrc++; /* blue */
			
			if (has_alpha) {
				temp_alpha = *pixsrc++;
			} else {
				temp_alpha = ~0;
			}
			*pixdest++ = temp_alpha & alpha_value;
			
			alpha_value = ~alpha_value;
		}
		
		start_alpha_value = ~start_alpha_value;
	}
	
	return dest_pixbuf;
}