-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathexo-gdk-pixbuf-extensions.c
514 lines (424 loc) · 16.3 KB
/
exo-gdk-pixbuf-extensions.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*-
* Copyright (c) 2004-2006 os-cillation e.K.
*
* Written by Benedikt Meurer <benny@xfce.org>.
*
* 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 3 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 Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
/* Taken from exo v0.10.2 (Debian package libexo-1-0), according to changelog
* commit f455681554ca205ffe49bd616310b19f5f9f8ef1 Dec 27 13:50:21 2012 */
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#if defined(__GNUC__) && defined(__MMX__)
#include <mmintrin.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
/* required for GdkPixbufFormat */
#ifndef GDK_PIXBUF_ENABLE_BACKEND
#define GDK_PIXBUF_ENABLE_BACKEND
#endif
#include "exo-gdk-pixbuf-extensions.h"
#include "exo-private.h"
#define g_open(path, mode, flags) (open ((path), (mode), (flags)))
/* _O_BINARY is required on some platforms */
#ifndef _O_BINARY
#define _O_BINARY 0
#endif
/**
* SECTION: exo-gdk-pixbuf-extensions
* @title: Extensions to gdk-pixbuf
* @short_description: Miscelleanous extensions to the gdk-pixbuf library
* @include: exo/exo.h
*
* This facility includes several functions to extend the basic functionality
* provided by the gdk-pixbuf library.
**/
/**
* exo_gdk_pixbuf_colorize:
* @source : the source #GdkPixbuf.
* @color : the new color.
*
* Creates a new #GdkPixbuf based on @source, which is
* colorized to @color.
*
* The caller is responsible to free the returned object
* using g_object_unref() when no longer needed.
*
* Returns: the colorized #GdkPixbuf.
*
* Since: 0.3.1.3
**/
GdkPixbuf*
exo_gdk_pixbuf_colorize (const GdkPixbuf *source,
const GdkColor *color)
{
GdkPixbuf *dst;
gboolean has_alpha;
gint dst_row_stride;
gint src_row_stride;
gint width;
gint height;
gint i;
/* determine source parameters */
width = gdk_pixbuf_get_width (source);
height = gdk_pixbuf_get_height (source);
has_alpha = gdk_pixbuf_get_has_alpha (source);
/* allocate the destination pixbuf */
dst = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (source), has_alpha, gdk_pixbuf_get_bits_per_sample (source), width, height);
/* determine row strides on src/dst */
dst_row_stride = gdk_pixbuf_get_rowstride (dst);
src_row_stride = gdk_pixbuf_get_rowstride (source);
#if defined(__GNUC__) && defined(__MMX__)
/* check if there's a good reason to use MMX */
if (G_LIKELY (has_alpha && dst_row_stride == width * 4 && src_row_stride == width * 4 && (width * height) % 2 == 0))
{
__m64 *pixdst = (__m64 *) gdk_pixbuf_get_pixels (dst);
__m64 *pixsrc = (__m64 *) gdk_pixbuf_get_pixels (source);
__m64 alpha_mask = _mm_set_pi8 (0xff, 0, 0, 0, 0xff, 0, 0, 0);
__m64 color_factor = _mm_set_pi16 (0, color->blue, color->green, color->red);
__m64 zero = _mm_setzero_si64 ();
__m64 src, alpha, hi, lo;
/* divide color components by 256 */
color_factor = _mm_srli_pi16 (color_factor, 8);
for (i = (width * height) >> 1; i > 0; --i)
{
/* read the source pixel */
src = *pixsrc;
/* remember the two alpha values */
alpha = _mm_and_si64 (alpha_mask, src);
/* extract the hi pixel */
hi = _mm_unpackhi_pi8 (src, zero);
hi = _mm_mullo_pi16 (hi, color_factor);
/* extract the lo pixel */
lo = _mm_unpacklo_pi8 (src, zero);
lo = _mm_mullo_pi16 (lo, color_factor);
/* prefetch the next two pixels */
__builtin_prefetch (++pixsrc, 0, 1);
/* divide by 256 */
hi = _mm_srli_pi16 (hi, 8);
lo = _mm_srli_pi16 (lo, 8);
/* combine the 2 pixels again */
src = _mm_packs_pu16 (lo, hi);
/* write back the calculated color together with the alpha */
*pixdst = _mm_or_si64 (alpha, src);
/* advance the dest pointer */
++pixdst;
}
_mm_empty ();
}
else
#endif
{
guchar *dst_pixels = gdk_pixbuf_get_pixels (dst);
guchar *src_pixels = gdk_pixbuf_get_pixels (source);
guchar *pixdst;
guchar *pixsrc;
gint red_value = color->red / 255.0;
gint green_value = color->green / 255.0;
gint blue_value = color->blue / 255.0;
gint j;
for (i = height; --i >= 0; )
{
pixdst = dst_pixels + i * dst_row_stride;
pixsrc = src_pixels + i * src_row_stride;
for (j = width; j > 0; --j)
{
*pixdst++ = (*pixsrc++ * red_value) >> 8;
*pixdst++ = (*pixsrc++ * green_value) >> 8;
*pixdst++ = (*pixsrc++ * blue_value) >> 8;
if (has_alpha)
*pixdst++ = *pixsrc++;
}
}
}
return dst;
}
static inline void
draw_frame_row (const GdkPixbuf *frame_image,
gint target_width,
gint source_width,
gint source_v_position,
gint dest_v_position,
GdkPixbuf *result_pixbuf,
gint left_offset,
gint height)
{
gint remaining_width;
gint slab_width;
gint h_offset;
for (h_offset = 0, remaining_width = target_width; remaining_width > 0; h_offset += slab_width, remaining_width -= slab_width)
{
slab_width = (remaining_width > source_width) ? source_width : remaining_width;
gdk_pixbuf_copy_area (frame_image, left_offset, source_v_position, slab_width, height, result_pixbuf, left_offset + h_offset, dest_v_position);
}
}
static inline void
draw_frame_column (const GdkPixbuf *frame_image,
gint target_height,
gint source_height,
gint source_h_position,
gint dest_h_position,
GdkPixbuf *result_pixbuf,
gint top_offset,
gint width)
{
gint remaining_height;
gint slab_height;
gint v_offset;
for (v_offset = 0, remaining_height = target_height; remaining_height > 0; v_offset += slab_height, remaining_height -= slab_height)
{
slab_height = (remaining_height > source_height) ? source_height : remaining_height;
gdk_pixbuf_copy_area (frame_image, source_h_position, top_offset, width, slab_height, result_pixbuf, dest_h_position, top_offset + v_offset);
}
}
/**
* exo_gdk_pixbuf_frame:
* @source : the source #GdkPixbuf.
* @frame : the frame #GdkPixbuf.
* @left_offset : the left frame offset.
* @top_offset : the top frame offset.
* @right_offset : the right frame offset.
* @bottom_offset : the bottom frame offset.
*
* Embeds @source in @frame and returns the result as new #GdkPixbuf.
*
* The caller is responsible to free the returned #GdkPixbuf using g_object_unref().
*
* Returns: the framed version of @source.
*
* Since: 0.3.1.9
**/
GdkPixbuf*
exo_gdk_pixbuf_frame (const GdkPixbuf *source,
const GdkPixbuf *frame,
gint left_offset,
gint top_offset,
gint right_offset,
gint bottom_offset)
{
GdkPixbuf *dst;
gint dst_width;
gint dst_height;
gint frame_width;
gint frame_height;
gint src_width;
gint src_height;
g_return_val_if_fail (GDK_IS_PIXBUF (frame), NULL);
g_return_val_if_fail (GDK_IS_PIXBUF (source), NULL);
src_width = gdk_pixbuf_get_width (source);
src_height = gdk_pixbuf_get_height (source);
frame_width = gdk_pixbuf_get_width (frame);
frame_height = gdk_pixbuf_get_height (frame);
dst_width = src_width + left_offset + right_offset;
dst_height = src_height + top_offset + bottom_offset;
/* allocate the resulting pixbuf */
dst = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, dst_width, dst_height);
/* fill the destination if the source has an alpha channel */
if (G_UNLIKELY (gdk_pixbuf_get_has_alpha (source)))
gdk_pixbuf_fill (dst, 0xffffffff);
/* draw the left top corner and top row */
gdk_pixbuf_copy_area (frame, 0, 0, left_offset, top_offset, dst, 0, 0);
draw_frame_row (frame, src_width, frame_width - left_offset - right_offset, 0, 0, dst, left_offset, top_offset);
/* draw the right top corner and left column */
gdk_pixbuf_copy_area (frame, frame_width - right_offset, 0, right_offset, top_offset, dst, dst_width - right_offset, 0);
draw_frame_column (frame, src_height, frame_height - top_offset - bottom_offset, 0, 0, dst, top_offset, left_offset);
/* draw the bottom right corner and bottom row */
gdk_pixbuf_copy_area (frame, frame_width - right_offset, frame_height - bottom_offset, right_offset,
bottom_offset, dst, dst_width - right_offset, dst_height - bottom_offset);
draw_frame_row (frame, src_width, frame_width - left_offset - right_offset, frame_height - bottom_offset,
dst_height - bottom_offset, dst, left_offset, bottom_offset);
/* draw the bottom left corner and the right column */
gdk_pixbuf_copy_area (frame, 0, frame_height - bottom_offset, left_offset, bottom_offset, dst, 0, dst_height - bottom_offset);
draw_frame_column (frame, src_height, frame_height - top_offset - bottom_offset, frame_width - right_offset,
dst_width - right_offset, dst, top_offset, right_offset);
/* copy the source pixbuf into the framed area */
gdk_pixbuf_copy_area (source, 0, 0, src_width, src_height, dst, left_offset, top_offset);
return dst;
}
static inline guchar
lighten_channel (guchar cur_value)
{
gint new_value = cur_value;
new_value += 24 + (new_value >> 3);
if (G_UNLIKELY (new_value > 255))
new_value = 255;
return (guchar) new_value;
}
/**
* exo_gdk_pixbuf_spotlight:
* @source : the source #GdkPixbuf.
*
* Creates a lightened version of @source, suitable for
* prelit state display of icons.
*
* The caller is responsible to free the returned
* pixbuf using #g_object_unref().
*
* Returns: the lightened version of @source.
*
* Since: 0.3.1.3
**/
GdkPixbuf*
exo_gdk_pixbuf_spotlight (const GdkPixbuf *source)
{
GdkPixbuf *dst;
gboolean has_alpha;
gint dst_row_stride;
gint src_row_stride;
gint width;
gint height;
gint i;
/* determine source parameters */
width = gdk_pixbuf_get_width (source);
height = gdk_pixbuf_get_height (source);
has_alpha = gdk_pixbuf_get_has_alpha (source);
/* allocate the destination pixbuf */
dst = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (source), has_alpha, gdk_pixbuf_get_bits_per_sample (source), width, height);
/* determine src/dst row strides */
dst_row_stride = gdk_pixbuf_get_rowstride (dst);
src_row_stride = gdk_pixbuf_get_rowstride (source);
#if defined(__GNUC__) && defined(__MMX__)
/* check if there's a good reason to use MMX */
if (G_LIKELY (has_alpha && dst_row_stride == width * 4 && src_row_stride == width * 4 && (width * height) % 2 == 0))
{
__m64 *pixdst = (__m64 *) gdk_pixbuf_get_pixels (dst);
__m64 *pixsrc = (__m64 *) gdk_pixbuf_get_pixels (source);
__m64 alpha_mask = _mm_set_pi8 (0xff, 0, 0, 0, 0xff, 0, 0, 0);
__m64 twentyfour = _mm_set_pi8 (0, 24, 24, 24, 0, 24, 24, 24);
__m64 zero = _mm_setzero_si64 ();
for (i = (width * height) >> 1; i > 0; --i)
{
/* read the source pixel */
__m64 src = *pixsrc;
/* remember the two alpha values */
__m64 alpha = _mm_and_si64 (alpha_mask, src);
/* extract the hi pixel */
__m64 hi = _mm_unpackhi_pi8 (src, zero);
/* extract the lo pixel */
__m64 lo = _mm_unpacklo_pi8 (src, zero);
/* add (x >> 3) to x */
hi = _mm_adds_pu16 (hi, _mm_srli_pi16 (hi, 3));
lo = _mm_adds_pu16 (lo, _mm_srli_pi16 (lo, 3));
/* prefetch next value */
__builtin_prefetch (++pixsrc, 0, 1);
/* combine the two pixels again */
src = _mm_packs_pu16 (lo, hi);
/* add 24 (with saturation) */
src = _mm_adds_pu8 (src, twentyfour);
/* drop the alpha channel from the temp color */
src = _mm_andnot_si64 (alpha_mask, src);
/* write back the calculated color */
*pixdst = _mm_or_si64 (alpha, src);
/* advance the dest pointer */
++pixdst;
}
_mm_empty ();
}
else
#endif
{
guchar *dst_pixels = gdk_pixbuf_get_pixels (dst);
guchar *src_pixels = gdk_pixbuf_get_pixels (source);
guchar *pixdst;
guchar *pixsrc;
gint j;
for (i = height; --i >= 0; )
{
pixdst = dst_pixels + i * dst_row_stride;
pixsrc = src_pixels + i * src_row_stride;
for (j = width; j > 0; --j)
{
*pixdst++ = lighten_channel (*pixsrc++);
*pixdst++ = lighten_channel (*pixsrc++);
*pixdst++ = lighten_channel (*pixsrc++);
if (G_LIKELY (has_alpha))
*pixdst++ = *pixsrc++;
}
}
}
return dst;
}
/**
* exo_gdk_pixbuf_scale_down:
* @source : the source #GdkPixbuf.
* @preserve_aspect_ratio : %TRUE to preserve aspect ratio.
* @dest_width : the max width for the result.
* @dest_height : the max height for the result.
*
* Scales down the @source to fit into the given @width and
* @height. If @aspect_ratio is %TRUE then the aspect ratio
* of @source will be preserved.
*
* If @width is larger than the width of @source and @height
* is larger than the height of @source, a reference to
* @source will be returned, as it's unneccesary then to
* scale down.
*
* The caller is responsible to free the returned #GdkPixbuf
* using g_object_unref() when no longer needed.
*
* Returns: the resulting #GdkPixbuf.
*
* Since: 0.3.1.1
**/
GdkPixbuf*
exo_gdk_pixbuf_scale_down (GdkPixbuf *source,
gboolean preserve_aspect_ratio,
gint dest_width,
gint dest_height)
{
gdouble wratio;
gdouble hratio;
gint source_width;
gint source_height;
g_return_val_if_fail (GDK_IS_PIXBUF (source), NULL);
g_return_val_if_fail (dest_width > 0, NULL);
g_return_val_if_fail (dest_height > 0, NULL);
source_width = gdk_pixbuf_get_width (source);
source_height = gdk_pixbuf_get_height (source);
/* check if we need to scale */
if (G_UNLIKELY (source_width <= dest_width && source_height <= dest_height))
return g_object_ref (G_OBJECT (source));
/* check if aspect ratio should be preserved */
if (G_LIKELY (preserve_aspect_ratio))
{
/* calculate the new dimensions */
wratio = (gdouble) source_width / (gdouble) dest_width;
hratio = (gdouble) source_height / (gdouble) dest_height;
if (hratio > wratio)
dest_width = rint (source_width / hratio);
else
dest_height = rint (source_height / wratio);
}
return gdk_pixbuf_scale_simple (source, MAX (dest_width, 1), MAX (dest_height, 1), GDK_INTERP_BILINEAR);
}
#define __EXO_GDK_PIXBUF_EXTENSIONS_C__