summaryrefslogtreecommitdiff
path: root/eel/eel-graphic-effects.c
blob: b0f8b5a057ceca6cd80504cb8e438c722536db41 (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
/* Eel - 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, see <http://www.gnu.org/licenses/>.
 */

#include "eel-graphic-effects.h"

GdkTexture *
eel_create_spotlight_texture (GdkTexture *texture)
{
    int width;
    int height;
    cairo_surface_t *surface;
    cairo_t *cr;
    unsigned char *data;
    int stride;
    g_autoptr (GBytes) bytes = NULL;
    GdkTexture *prelit_texture;

    g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);

    width = gdk_texture_get_width (texture);
    height = gdk_texture_get_height (texture);
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    cr = cairo_create (surface);
    data = cairo_image_surface_get_data (surface);
    stride = cairo_image_surface_get_stride (surface);

    gdk_texture_download (texture, data, stride);

    cairo_surface_mark_dirty (surface);

    cairo_set_source_surface (cr, surface, 0, 0);
    cairo_paint (cr);

    cairo_set_operator (cr, CAIRO_OPERATOR_ADD);

    cairo_push_group (cr);

    /* This is *close enough* to the original look.
     * The magic alpha value was selected after visual comparison.
     */
    cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.18);
    cairo_paint (cr);

    cairo_pop_group_to_source (cr);

    cairo_mask_surface (cr, surface, 0.0, 0.0);

    cairo_surface_flush (surface);

    bytes = g_bytes_new (data, height * stride);
    prelit_texture = gdk_memory_texture_new (width, height, GDK_MEMORY_B8G8R8A8_PREMULTIPLIED, bytes, stride);

    cairo_destroy (cr);
    cairo_surface_destroy (surface);

    return prelit_texture;
}