summaryrefslogtreecommitdiffstats
path: root/src/db-image-parser.c
blob: 372013c302e080e41307c87b33bb90ebd8c5e574 (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
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
/*
 *  Copyright (C) 2005 Christophe Fergeau
 *
 * 
 *  The code contained in this file 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 file 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 code; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  iTunes and iPod are trademarks of Apple
 * 
 *  This product is not supported/written/published by Apple!
 *
 */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#include <glib.h>
#include <glib-object.h>

#include "db-artwork-parser.h"
#include "db-image-parser.h"
#if HAVE_GDKPIXBUF
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif
#include <glib/gi18n-lib.h>

static unsigned char *
unpack_RGB_565 (gushort *pixels, unsigned int bytes_len)
{
	unsigned char *result;
	unsigned int i;

	result = g_malloc ((bytes_len/2) * 3);
	if (result == NULL) {
		return NULL;
	}
	for (i = 0; i < bytes_len/2; i++) {
		gushort cur_pixel;

		cur_pixel = GINT16_FROM_LE (pixels[i]);
		/* Unpack pixels */
		result[3*i] = (cur_pixel & RED_MASK) >> RED_SHIFT;
		result[3*i+1] = (cur_pixel & GREEN_MASK) >> GREEN_SHIFT;
		result[3*i+2] = (cur_pixel & BLUE_MASK) >> BLUE_SHIFT;
		
		/* Normalize color values so that they use a [0..255] range */
		result[3*i] <<= (8 - RED_BITS);
		result[3*i+1] <<= (8 - GREEN_BITS);
		result[3*i+2] <<= (8 - BLUE_BITS);
	}

	return result;
}


static unsigned char *
get_pixel_data (Itdb_Image *image)
{
	unsigned char *result;
	FILE *f;
	int res;

	f = NULL;
	result = g_malloc (image->size);
	if (result == NULL) {
		return NULL;
	}

	f = fopen (image->filename, "r");
	if (f == NULL) {
		g_print ("Failed to open %s: %s\n", 
			 image->filename, strerror (errno));
		goto end;
	}

	res = fseek (f, image->offset, SEEK_SET);
	if (res != 0) {
		g_print ("Seek to %d failed on %s: %s\n",
			 image->offset, image->filename, strerror (errno));
		goto end;
	}

	res = fread (result, image->size, 1, f);
	if (res != 1) {
		g_print ("Failed to read %u bytes from %s: %s\n", 
			 image->size, image->filename, strerror (errno));
		goto end;
	}
	fclose (f);

	return result;

 end:
	if (f != NULL) {
		fclose (f);
	}
	g_free (result);

	return NULL;
}

unsigned char *
itdb_image_get_rgb_data (Itdb_Image *image)
{
	void *pixels565;
	void *pixels;

	pixels565 = get_pixel_data (image);
	if (pixels565 == NULL) {
		return NULL;
	}
	
	pixels = unpack_RGB_565 (pixels565, image->size);
	g_free (pixels565);

	return pixels;

}

/* Convert the pixeldata in @image to a GdkPixbuf.
   Since we want to have gdk-pixbuf dependency optional, a generic
   gpointer is returned which you have to cast to (GdkPixbuf *)
   yourself. If gdk-pixbuf is not installed the NULL pointer is
   returned.
   The returned GdkPixbuf must be freed with gdk_pixbuf_unref() after
   use. */
gpointer
itdb_image_get_gdk_pixbuf (Itdb_iTunesDB *itdb, Itdb_Image *image)
{
#if HAVE_GDKPIXBUF
    GdkPixbuf *result;
    guchar *pixels;
    const IpodArtworkFormat *img_info;

    g_return_val_if_fail (itdb, NULL);
    g_return_val_if_fail (image, NULL);

    pixels = itdb_image_get_rgb_data (image);
    if (pixels == NULL)
    {
	return NULL;
    }

    img_info = ipod_get_artwork_info_from_type (itdb->device,
						image->type);

    if (img_info == NULL)
    {
	g_print (_("Unable to obtain image info on image (type: %d, filename: '%s'\n)"), image->type, image->filename);
	g_free (pixels);
	return NULL;
    }

    result = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE,
				       8, image->width, image->height,
				       img_info->width*3,
				       (GdkPixbufDestroyNotify)g_free,
				       NULL);

    /* !! do not g_free(pixels) here: it will be freed when doing a
     * gdk_pixbuf_unref() on the GdkPixbuf !! */

    return result;
#else
    return NULL;
#endif
}



static int
image_type_from_corr_id (IpodDevice *ipod, int corr_id)
{
	const IpodArtworkFormat *formats;

	if (ipod == NULL) {
		return -1;
	}

	g_object_get (G_OBJECT (ipod), "artwork-formats", &formats, NULL);
	if (formats == NULL) {
		return -1;
	}
	
	while (formats->type != -1) {
		if (formats->correlation_id == corr_id) {
			return formats->type;
		}
		formats++;
	}

	return -1;
}


G_GNUC_INTERNAL const IpodArtworkFormat *
ipod_get_artwork_info_from_type (IpodDevice *ipod, int image_type)
{
	const IpodArtworkFormat *formats;
	
	if (ipod == NULL) {
		return NULL;
	}

	g_object_get (G_OBJECT (ipod), "artwork-formats", &formats, NULL);
	if (formats == NULL) {
		return NULL;
	}
	
	while ((formats->type != -1) && (formats->type != image_type)) {
		formats++;
	}

	if (formats->type == -1) {
		return NULL;
	}

	return formats;
}

G_GNUC_INTERNAL Itdb_Image *
ipod_image_new_from_mhni (MhniHeader *mhni, Itdb_iTunesDB *db)
{

	Itdb_Image *img;
	img = g_new0 (Itdb_Image, 1);
	if (img == NULL) {
		return NULL;
	}
	img->size   = GUINT32_FROM_LE (mhni->image_size);
	img->offset = GUINT32_FROM_LE (mhni->ithmb_offset);
	img->width  = GINT16_FROM_LE (mhni->image_width);
	img->height = GINT16_FROM_LE (mhni->image_height);

	img->type = image_type_from_corr_id (db->device, mhni->correlation_id);
	if ((img->type != IPOD_COVER_SMALL) && (img->type != IPOD_COVER_LARGE)) {
		g_warning ("Unexpected cover type in mhni: %ux%u (%d)\n", 
			   img->width, img->height, mhni->correlation_id);
		g_free (img);
		return NULL;
	}

	return img;
}