summaryrefslogtreecommitdiffstats
path: root/src/db-image-parser.c
blob: dfc7d5dc1fe0a041c856a6fb2997c1316d35eb14 (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
/*
 *  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 "itdb_device.h"
#include "itdb_endianness.h"
#include "db-artwork-parser.h"
#include "db-image-parser.h"
#include <glib/gi18n-lib.h>

static int
image_type_from_format_id (Itdb_Device *device, gint16 format_id)
{
	const Itdb_ArtworkFormat *formats;

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

	formats = itdb_device_get_artwork_formats (device);

	if (formats == NULL) {
		return -1;
	}
	
	while (formats->type != -1) {
		if (formats->format_id == format_id) {
			return formats->type;
		}
		formats++;
	}

	return -1;
}


G_GNUC_INTERNAL const Itdb_ArtworkFormat *
itdb_get_artwork_info_from_type (Itdb_Device *device,
				 ItdbThumbType image_type)
{
	const Itdb_ArtworkFormat *formats;
	
	if (device == NULL) {
		return NULL;
	}

	formats = itdb_device_get_artwork_formats (device);
	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_Thumb *
ipod_image_new_from_mhni (MhniHeader *mhni, Itdb_DB *db)
{
	Itdb_Thumb *img;
	gint16 format_id;
	Itdb_Device *device = NULL;

	img = g_new0 (Itdb_Thumb, 1);
	if (img == NULL) {
		return NULL;
	}
	img->size   = get_guint32_db (db, mhni->image_size);
	img->offset = get_guint32_db (db, mhni->ithmb_offset);
	img->width  = get_gint16_db (db, mhni->image_width);
	img->height = get_gint16_db (db, mhni->image_height);
	img->horizontal_padding  =
	    get_gint16_db (db, mhni->horizontal_padding);
	img->vertical_padding =
	    get_gint16_db (db, mhni->vertical_padding);

	device = db_get_device (db);
	g_return_val_if_fail (device, NULL);

	format_id = get_gint32_db (db, mhni->format_id);
	img->type = image_type_from_format_id (device, format_id);

#if DEBUG_ARTWORK
	printf ("format_id: %d, of: %6d sz: %6d, x: %3d, y: %3d, xpad: %3d, ypad: %3d\n",
		format_id, img->offset, img->size, img->width, img->height, img->horizontal_padding, img->vertical_padding);
#endif

	if (img->type == -1)
	{
	    g_warning (_("Unexpected image type in mhni: size: %ux%u (%d), offset: %d\n"), 
		       img->width, img->height, 
		       format_id, img->offset);
	    g_free (img);
	    return NULL;
	}

	return img;
}