summaryrefslogtreecommitdiff
path: root/src/libaudgui/pixbufs.c
blob: 550dce07fdba435f866bcde6a4b541c9d156f02e (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
/*
 * pixbufs.c
 * Copyright 2010-2012 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <gdk-pixbuf/gdk-pixbuf.h>

#include <audacious/debug.h>
#include <audacious/misc.h>
#include <audacious/playlist.h>
#include <libaudcore/audstrings.h>

#include "libaudgui-gtk.h"

static GdkPixbuf * current_pixbuf;

EXPORT GdkPixbuf * audgui_pixbuf_fallback (void)
{
    static GdkPixbuf * fallback = NULL;

    if (! fallback)
    {
        const char * data_dir = aud_get_path (AUD_PATH_DATA_DIR);
        SCONCAT2 (path, data_dir, "/images/album.png");
        fallback = gdk_pixbuf_new_from_file (path, NULL);
    }

    if (fallback)
        g_object_ref ((GObject *) fallback);

    return fallback;
}

void audgui_pixbuf_uncache (void)
{
    if (current_pixbuf)
    {
        g_object_unref ((GObject *) current_pixbuf);
        current_pixbuf = NULL;
    }
}

EXPORT void audgui_pixbuf_scale_within (GdkPixbuf * * pixbuf, int size)
{
    int width = gdk_pixbuf_get_width (* pixbuf);
    int height = gdk_pixbuf_get_height (* pixbuf);

    if (width <= size && height <= size)
        return;

    if (width > height)
    {
        height = size * height / width;
        width = size;
    }
    else
    {
        width = size * width / height;
        height = size;
    }

    if (width < 1)
        width = 1;
    if (height < 1)
        height = 1;

    GdkPixbuf * pixbuf2 = gdk_pixbuf_scale_simple (* pixbuf, width, height, GDK_INTERP_BILINEAR);
    g_object_unref (* pixbuf);
    * pixbuf = pixbuf2;
}

EXPORT GdkPixbuf * audgui_pixbuf_request (const char * filename)
{
    const void * data;
    int64_t size;

    aud_art_request_data (filename, & data, & size);
    if (! data)
        return NULL;

    GdkPixbuf * p = audgui_pixbuf_from_data (data, size);

    aud_art_unref (filename);
    return p;
}

EXPORT GdkPixbuf * audgui_pixbuf_request_current (void)
{
    if (! current_pixbuf)
    {
        int list = aud_playlist_get_playing ();
        int entry = aud_playlist_get_position (list);
        if (entry < 0)
            return NULL;

        char * filename = aud_playlist_entry_get_filename (list, entry);
        current_pixbuf = audgui_pixbuf_request (filename);
        str_unref (filename);
    }

    if (current_pixbuf)
        g_object_ref ((GObject *) current_pixbuf);

    return current_pixbuf;
}

EXPORT GdkPixbuf * audgui_pixbuf_from_data (const void * data, int64_t size)
{
    GdkPixbuf * pixbuf = NULL;
    GdkPixbufLoader * loader = gdk_pixbuf_loader_new ();
    GError * error = NULL;

    if (gdk_pixbuf_loader_write (loader, data, size, & error) &&
     gdk_pixbuf_loader_close (loader, & error))
    {
        if ((pixbuf = gdk_pixbuf_loader_get_pixbuf (loader)))
            g_object_ref (pixbuf);
    }
    else
    {
        AUDDBG ("error while loading pixbuf: %s\n", error->message);
        g_error_free (error);
    }

    g_object_unref (loader);
    return pixbuf;
}