summaryrefslogtreecommitdiffstats
path: root/gtk/src/icon_tools.c
blob: 763c0f8a021ddf1e8a56e229310d6c24cf9469bb (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * icon_tools.c
 * Copyright (C) John Stebbins 2008-2013 <stebbins@stebbins>
 * 
 * icon_tools.c is free software.
 * 
 * You may redistribute it and/or modify it under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 */
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include "icon_tools.h"

GdkPixbuf*
icon_deserialize(const guint8 *sd, guint len)
{
    GdkPixdata pd;
    GdkPixbuf *pb;
    GError *err = NULL;

    gdk_pixdata_deserialize(&pd, len, sd, &err);
    pb = gdk_pixbuf_from_pixdata(&pd, TRUE, &err);
    return pb;
}

guint8*
icon_serialize(const GdkPixbuf *pixbuf, guint *len)
{
    GdkPixdata pd;
    guint8 *sd;

    gdk_pixdata_from_pixbuf(&pd, pixbuf, FALSE);
    sd = gdk_pixdata_serialize(&pd, len);
    return sd;
}

guint8*
icon_file_serialize(const gchar *filename, guint *len)
{
    GdkPixbuf *pb;
    GError *err = NULL;

    pb = gdk_pixbuf_new_from_file(filename, &err);
    if (pb == NULL)
    {
        g_warning("Failed to open icon file %s: %s", filename, err->message);
        return NULL;
    }
    return icon_serialize(pb, len);
}

GdkPixbuf*
base64_to_icon(const gchar *bd)
{
    guchar *sd;
    gsize len;
    GdkPixdata pd;
    GdkPixbuf *pb;
    GError *err = NULL;

    sd = g_base64_decode(bd, &len);
    gdk_pixdata_deserialize(&pd, len, sd, &err);
    pb = gdk_pixbuf_from_pixdata(&pd, TRUE, &err);
    g_free(sd);
    return pb;
}

gchar*
icon_to_base64(const GdkPixbuf *pixbuf)
{
    GdkPixdata pd;
    guint len;
    guint8 *sd;
    gchar *bd;

    gdk_pixdata_from_pixbuf(&pd, pixbuf, FALSE);
    sd = gdk_pixdata_serialize(&pd, &len);
    bd = g_base64_encode(sd, len);
    g_free(sd);
    return bd;
}

gchar*
icon_file_to_base64(const gchar *filename)
{
    GdkPixbuf *pb;
    GError *err = NULL;

    pb = gdk_pixbuf_new_from_file(filename, &err);
    if (pb == NULL)
    {
        g_warning("Failed to open icon file %s: %s", filename, err->message);
        return NULL;
    }
    return icon_to_base64(pb);
}