summaryrefslogtreecommitdiff
path: root/libmediaart/extractpixbuf.c
blob: 1a1219563afea9eb24ca3da80703912f217ceae4 (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
/*
 * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser 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.
 *
 * Authors:
 * Philip Van Hoof <philip@codeminded.be>
 */

#include "config.h"

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

#include "extractgeneric.h"

static gint max_width_in_bytes = 0;

void
media_art_plugin_init (gint max_width)
{
	g_return_if_fail (max_width >= 0);

	max_width_in_bytes = max_width;
}

void
media_art_plugin_shutdown (void)
{
}

gboolean
media_art_file_to_jpeg (const gchar  *filename,
                        const gchar  *target,
                        GError      **error)
{
	GdkPixbuf *pixbuf;
	GError *local_error = NULL;

	/* TODO: Add resizing support */

	pixbuf = gdk_pixbuf_new_from_file (filename, &local_error);

	if (local_error) {
		g_propagate_error (error, local_error);
		return FALSE;
	}

	gdk_pixbuf_save (pixbuf, target, "jpeg", &local_error, NULL);
	g_object_unref (pixbuf);

	if (local_error) {
		g_propagate_error (error, local_error);
		return FALSE;
	}

	return TRUE;
}

static void
size_prepared_cb (GdkPixbufLoader *loader,
                  gint             width,
                  gint             height,
                  gpointer         user_data)
{
	gfloat scale;

	if (max_width_in_bytes < 1 || width <= max_width_in_bytes) {
		return;
	}

	g_debug ("Resizing media art to %d width", max_width_in_bytes);

	scale = width / (gfloat) max_width_in_bytes;

	gdk_pixbuf_loader_set_size (loader, (gint) (width / scale), (gint) (height / scale));
}

gboolean
media_art_buffer_to_jpeg (const unsigned char  *buffer,
                          size_t                len,
                          const gchar          *buffer_mime,
                          const gchar          *target,
                          GError              **error)
{
	GError *local_error = NULL;

	if (max_width_in_bytes < 0) {
		g_debug ("Not saving album art from buffer, disabled in config");
		return TRUE;
	}

	/* FF D8 FF are the three first bytes of JPeg images */
	if (max_width_in_bytes == 0 &&
	    (g_strcmp0 (buffer_mime, "image/jpeg") == 0 ||
	     g_strcmp0 (buffer_mime, "JPG") == 0) &&
	    (buffer && len > 2 && buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff)) {
		g_debug ("Saving album art using raw data as uri:'%s'", target);
		if (!g_file_set_contents (target, (const gchar *) buffer, (gssize) len, error)) {
			return FALSE;
		}
	} else {
		GdkPixbuf *pixbuf;
		GdkPixbufLoader *loader;

		g_debug ("Saving album art using GdkPixbufLoader for uri:'%s' (max width:%d)",
		         target,
		         max_width_in_bytes);

		loader = gdk_pixbuf_loader_new ();
		if (max_width_in_bytes > 0) {
			g_signal_connect (loader,
			                  "size-prepared",
			                  G_CALLBACK (size_prepared_cb),
			                  NULL);
		}

		if (!gdk_pixbuf_loader_write (loader, buffer, len, &local_error)) {
			g_warning ("Could not write with GdkPixbufLoader when setting media art, %s",
			           local_error ? local_error->message : "no error given");

			g_propagate_error (error, local_error);
			gdk_pixbuf_loader_close (loader, NULL);
			g_object_unref (loader);

			return FALSE;
		}

		pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);

		if (pixbuf == NULL) {
			g_warning ("Could not get pixbuf from GdkPixbufLoader when setting media art");

			/* FIXME: Set error here */

			gdk_pixbuf_loader_close (loader, NULL);
			g_object_unref (loader);

			return FALSE;
		}

		if (!gdk_pixbuf_save (pixbuf, target, "jpeg", &local_error, NULL)) {
			g_warning ("Could not save GdkPixbuf when setting media art, %s",
			           local_error ? local_error->message : "no error given");

			g_propagate_error (error, local_error);
			gdk_pixbuf_loader_close (loader, NULL);
			g_object_unref (loader);

			return FALSE;
		}

		if (!gdk_pixbuf_loader_close (loader, &local_error)) {
			g_warning ("Could not close GdkPixbufLoader when setting media art, %s",
			           local_error ? local_error->message : "no error given");

			g_propagate_error (error, local_error);

			return FALSE;
		}

		g_object_unref (loader);
	}

	return TRUE;
}