summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog17
-rw-r--r--src/itdb.h24
-rw-r--r--src/itdb_artwork.c86
-rw-r--r--src/itdb_photoalbum.c54
-rw-r--r--src/itdb_track.c104
-rw-r--r--src/ithumb-writer.c73
6 files changed, 298 insertions, 60 deletions
diff --git a/ChangeLog b/ChangeLog
index 6052c74..197a8c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2006-09-18 Jorg Schuler <jcsjcs at users.sourceforge.net>
+
+ * src/itdb.h
+ src/itdb_artwork.c: added itdb_artwork_add_thumbnail_from_data().
+
+ * src/itdb.h
+ src/itdb_track.c: added itdb_track_set_thumbnails_from_data().
+
+ * src/itdb.h
+ src/itdb_photoalbum.c: added itdb_photodb_add_photo_from_data().
+
+ * src/itdb_artwork.c (itdb_thumb_get_gdk_pixbuf): added support to
+ retreive thumbnail from raw data instead of file.
+
+ * src/ithumb-writer.c (ithumb_writer_write_thumbnail): added
+ support to write thumbnail from raw data instead of file.
+
2006-09-17 Jorg Schuler <jcsjcs at users.sourceforge.net>
* src/itdb.h
diff --git a/src/itdb.h b/src/itdb.h
index 6e1c981..1da2f04 100644
--- a/src/itdb.h
+++ b/src/itdb.h
@@ -1,4 +1,4 @@
-/* Time-stamp: <2006-09-17 13:45:03 jcs>
+/* Time-stamp: <2006-09-18 01:33:30 jcs>
|
| Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
| Part of the gtkpod project.
@@ -88,7 +88,7 @@ typedef enum {
/* The Itdb_Thumb structure can represent two slightly different
thumbnails:
- - a thumbnail before it's transferred to the iPod.
+ a) a thumbnail before it's transferred to the iPod.
offset and size are 0
@@ -98,15 +98,20 @@ typedef enum {
type is set according to the type this thumbnail represents
- filename point to a 'real' image file.
+ filename point to a 'real' image file OR image_data and
+ image_data_len are set.
- - a thumbnail (big or small) stored on a database in the iPod. In
+ b) a thumbnail (big or small) stored on a database in the iPod. In
these cases, id corresponds to the ID originally used in the
database, filename points to a .ithmb file on the iPod
*/
struct _Itdb_Thumb {
ItdbThumbType type;
gchar *filename;
+ guchar *image_data; /* holds the thumbnail data of
+ non-transfered thumbnails when
+ filename == NULL */
+ gsize image_data_len; /* length of data */
guint32 offset;
guint32 size;
gint16 width;
@@ -938,6 +943,9 @@ void itdb_spl_update_live (Itdb_iTunesDB *itdb);
/* itdb_track_... */
gboolean itdb_track_set_thumbnails (Itdb_Track *track,
const gchar *filename);
+gboolean itdb_track_set_thumbnails_from_data (Itdb_Track *track,
+ const guchar *image_data,
+ gsize image_data_len);
void itdb_track_remove_thumbnails (Itdb_Track *track);
/* photoalbum functions */
@@ -945,6 +953,10 @@ Itdb_PhotoDB *itdb_photodb_parse (const gchar *mp, GError **error);
gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
const gchar *albumname,
const gchar *filename);
+gboolean itdb_photodb_add_photo_from_data (Itdb_PhotoDB *db,
+ const gchar *albumname,
+ const guchar *image_data,
+ gsize image_data_len);
Itdb_PhotoAlbum *itdb_photodb_photoalbum_new (Itdb_PhotoDB *db,
const gchar *album_name);
Itdb_PhotoDB *itdb_photodb_new (void);
@@ -963,6 +975,10 @@ Itdb_Thumb *itdb_artwork_get_thumb_by_type (Itdb_Artwork *artwork,
gboolean itdb_artwork_add_thumbnail (Itdb_Artwork *artwork,
ItdbThumbType type,
const gchar *filename);
+gboolean itdb_artwork_add_thumbnail_from_data (Itdb_Artwork *artwork,
+ ItdbThumbType type,
+ const guchar *image_data,
+ gsize image_data_len);
void itdb_artwork_remove_thumbnail (Itdb_Artwork *artwork,
Itdb_Thumb *thumb);
void itdb_artwork_remove_thumbnails (Itdb_Artwork *artwork);
diff --git a/src/itdb_artwork.c b/src/itdb_artwork.c
index 99b027f..a203867 100644
--- a/src/itdb_artwork.c
+++ b/src/itdb_artwork.c
@@ -1,4 +1,4 @@
-/* Time-stamp: <2006-06-04 17:30:39 jcs>
+/* Time-stamp: <2006-09-18 02:13:08 jcs>
|
| Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
| Part of the gtkpod project.
@@ -173,7 +173,7 @@ itdb_artwork_remove_thumbnails (Itdb_Artwork *artwork)
gboolean
itdb_artwork_add_thumbnail (Itdb_Artwork *artwork,
ItdbThumbType type,
- const char *filename)
+ const gchar *filename)
{
#ifdef HAVE_GDKPIXBUF
/* This operation doesn't make sense when we can't save thumbnail files */
@@ -203,6 +203,55 @@ itdb_artwork_add_thumbnail (Itdb_Artwork *artwork,
/**
+ * itdb_artwork_add_thumbnail_from_data
+ * @artwork: an #Itdb_Thumbnail
+ * @type: thumbnail size
+ * @image_data: data used to create the thumbnail (the raw contents of
+ * an image file)
+ * @image_data_len: length of above data block
+ *
+ * Appends a thumbnail of type @type to existing thumbnails in
+ * @artwork. No data is processed yet. This will be done when @artwork
+ * is saved to disk.
+ *
+ * Return value: TRUE if the thumbnail could be successfully added, FALSE
+ * otherwise
+ **/
+gboolean
+itdb_artwork_add_thumbnail_from_data (Itdb_Artwork *artwork,
+ ItdbThumbType type,
+ const guchar *image_data,
+ gsize image_data_len)
+{
+#ifdef HAVE_GDKPIXBUF
+/* This operation doesn't make sense when we can't save thumbnail files */
+ Itdb_Thumb *thumb;
+ GTimeVal time;
+
+ g_return_val_if_fail (artwork, FALSE);
+ g_return_val_if_fail (image_data, FALSE);
+
+ g_get_current_time (&time);
+
+ artwork->artwork_size = image_data_len;
+ artwork->creation_date = time.tv_sec;
+
+ thumb = itdb_thumb_new ();
+ thumb->image_data = g_malloc (image_data_len);
+ thumb->image_data_len = image_data_len;
+ memcpy (thumb->image_data, image_data, image_data_len);
+
+ thumb->type = type;
+ artwork->thumbnails = g_list_append (artwork->thumbnails, thumb);
+
+ return TRUE;
+#else
+ return FALSE;
+#endif
+}
+
+
+/**
* itdb_artwork_get_thumb_by_type:
* @artwork: an #Itdb_Artwork
* @type: type of the #Itdb_Thumb to retrieve
@@ -484,9 +533,28 @@ itdb_thumb_get_gdk_pixbuf (Itdb_Device *device, Itdb_Thumb *thumb)
}
}
- pixbuf = gdk_pixbuf_new_from_file_at_size (thumb->filename,
- width, height,
- NULL);
+ if (thumb->filename)
+ { /* read data from filename */
+ pixbuf = gdk_pixbuf_new_from_file_at_size (thumb->filename,
+ width, height,
+ NULL);
+ }
+ else if (thumb->image_data)
+ { /* use data stored in image_data */
+ GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
+ g_return_val_if_fail (loader, FALSE);
+ gdk_pixbuf_loader_write (loader,
+ thumb->image_data,
+ thumb->image_data_len,
+ NULL);
+ gdk_pixbuf_loader_close (loader, NULL);
+ gdk_pixbuf_loader_set_size (loader,
+ width, height);
+ pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+ g_object_ref (pixbuf);
+ g_object_unref (loader);
+ }
+
if (!pixbuf)
return NULL;
@@ -599,6 +667,7 @@ void itdb_thumb_free (Itdb_Thumb *thumb)
{
g_return_if_fail (thumb);
+ g_free (thumb->image_data);
g_free (thumb->filename);
g_free (thumb);
}
@@ -623,5 +692,12 @@ Itdb_Thumb *itdb_thumb_duplicate (Itdb_Thumb *thumb)
memcpy (new_thumb, thumb, sizeof (Itdb_Thumb));
new_thumb->filename = g_strdup (thumb->filename);
+ if (thumb->image_data)
+ {
+ /* image_data_len already copied with the memcpy above */
+ new_thumb->image_data = g_malloc (thumb->image_data_len);
+ memcpy (new_thumb->image_data, thumb->image_data,
+ new_thumb->image_data_len);
+ }
return new_thumb;
}
diff --git a/src/itdb_photoalbum.c b/src/itdb_photoalbum.c
index 0c49baf..003222d 100644
--- a/src/itdb_photoalbum.c
+++ b/src/itdb_photoalbum.c
@@ -254,9 +254,11 @@ void itdb_photodb_photoalbum_free (Itdb_PhotoAlbum *pa)
}
}
-gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
- const gchar *albumname,
- const gchar *filename)
+static gboolean itdb_photodb_add_photo_internal (Itdb_PhotoDB *db,
+ const gchar *albumname,
+ const gchar *filename,
+ const guchar *image_data,
+ gsize image_data_len)
{
gboolean result;
Itdb_Artwork *artwork;
@@ -265,20 +267,32 @@ gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
gint photo_id;
g_return_val_if_fail (db, FALSE);
- g_return_val_if_fail (filename, FALSE);
+
artwork = itdb_artwork_new ();
photo_id = itdb_get_free_photo_id( db );
artwork->id = photo_id;
+
/* Add a thumbnail for every supported format */
format = itdb_device_get_artwork_formats(db->device);
- for( result = TRUE; format->type != -1 && result == TRUE; format++) {
+ for( result = TRUE; format->type != -1 && result == TRUE; format++)
+ {
if((format->type == ITDB_THUMB_COVER_SMALL) ||
(format->type == ITDB_THUMB_COVER_LARGE))
continue;
+ if (filename)
+ {
result = itdb_artwork_add_thumbnail (artwork,
- format->type,
- filename);
+ format->type,
+ filename);
+ }
+ if (image_data)
+ {
+ result = itdb_artwork_add_thumbnail_from_data (artwork,
+ format->type,
+ image_data,
+ image_data_len);
+ }
}
if (result != TRUE)
@@ -297,6 +311,32 @@ gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
return result;
}
+
+gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
+ const gchar *albumname,
+ const gchar *filename)
+{
+ g_return_val_if_fail (db, FALSE);
+ g_return_val_if_fail (filename, FALSE);
+
+ return itdb_photodb_add_photo_internal (db, albumname, filename,
+ NULL, 0);
+}
+
+
+gboolean itdb_photodb_add_photo_from_data (Itdb_PhotoDB *db,
+ const gchar *albumname,
+ const guchar *image_data,
+ gsize image_data_len)
+{
+ g_return_val_if_fail (db, FALSE);
+ g_return_val_if_fail (image_data, FALSE);
+
+ return itdb_photodb_add_photo_internal (db, albumname, NULL,
+ image_data, image_data_len);
+}
+
+
gboolean itdb_photodb_remove_photo (Itdb_PhotoDB *db,
const gint photo_id )
{
diff --git a/src/itdb_track.c b/src/itdb_track.c
index 119785c..e304e30 100644
--- a/src/itdb_track.c
+++ b/src/itdb_track.c
@@ -1,4 +1,4 @@
-/* Time-stamp: <2006-06-04 18:08:42 jcs>
+/* Time-stamp: <2006-09-18 01:33:31 jcs>
|
| Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
| Part of the gtkpod project.
@@ -366,34 +366,41 @@ Itdb_Track *itdb_track_duplicate (Itdb_Track *tr)
}
-/**
- * itdb_track_set_thumbnails:
- * @track: an #Itdb_Track
- * @filename: image file to use as a thumbnail
- *
- * Uses the image contained in @filename to generate iPod thumbnails. The image
- * can be in any format supported by gdk-pixbuf. To save memory, the thumbnails
- * will only be generated when necessary, ie when itdb_save() or a similar
- * function is called.
- *
- * Return value: TRUE if the thumbnail could be added, FALSE otherwise.
- **/
-gboolean itdb_track_set_thumbnails (Itdb_Track *track,
- const gchar *filename)
-{
- gboolean result;
+/* called by itdb_track_set_thumbnails() and
+ itdb_track_set_thumbnails_from_data() */
+static gboolean itdb_track_set_thumbnails_internal (Itdb_Track *track,
+ const gchar *filename,
+ const guchar *image_data,
+ gsize image_data_len)
+{
+ gboolean result = FALSE;
g_return_val_if_fail (track, FALSE);
- g_return_val_if_fail (filename, FALSE);
itdb_artwork_remove_thumbnails (track->artwork);
- result = itdb_artwork_add_thumbnail (track->artwork,
- ITDB_THUMB_COVER_SMALL,
- filename);
- if (result == TRUE)
+
+ if (filename)
+ {
result = itdb_artwork_add_thumbnail (track->artwork,
- ITDB_THUMB_COVER_LARGE,
+ ITDB_THUMB_COVER_SMALL,
filename);
+ if (result == TRUE)
+ result = itdb_artwork_add_thumbnail (track->artwork,
+ ITDB_THUMB_COVER_LARGE,
+ filename);
+ }
+ if (image_data)
+ {
+ result = itdb_artwork_add_thumbnail_from_data (track->artwork,
+ ITDB_THUMB_COVER_SMALL,
+ image_data,
+ image_data_len);
+ if (result == TRUE)
+ result = itdb_artwork_add_thumbnail_from_data (track->artwork,
+ ITDB_THUMB_COVER_LARGE,
+ image_data,
+ image_data_len);
+ }
if (result == TRUE)
{
@@ -418,6 +425,57 @@ gboolean itdb_track_set_thumbnails (Itdb_Track *track,
return result;
}
+
+/**
+ * itdb_track_set_thumbnails:
+ * @track: an #Itdb_Track
+ * @filename: image file to use as a thumbnail
+ *
+ * Uses the image contained in @filename to generate iPod thumbnails. The image
+ * can be in any format supported by gdk-pixbuf. To save memory, the thumbnails
+ * will only be generated when necessary, ie when itdb_save() or a similar
+ * function is called.
+ *
+ * Return value: TRUE if the thumbnail could be added, FALSE otherwise.
+ **/
+gboolean itdb_track_set_thumbnails (Itdb_Track *track,
+ const gchar *filename)
+{
+ g_return_val_if_fail (track, FALSE);
+ g_return_val_if_fail (filename, FALSE);
+
+ return itdb_track_set_thumbnails_internal (track, filename, NULL, 0);
+}
+
+
+
+
+/**
+ * itdb_track_set_thumbnails_from_data:
+ * @track: an #Itdb_Track
+ * @image_data: data used to create the thumbnail (the raw contents of
+ * an image file)
+ * @image_data_len: length of above data block
+ *
+ * Uses @image_data to generate iPod thumbnails. The image can be in
+ * any format supported by gdk-pixbuf. To save memory, the thumbnails
+ * will only be generated when necessary, ie when itdb_save() or a
+ * similar function is called.
+ *
+ * Return value: TRUE if the thumbnail could be added, FALSE otherwise.
+ **/
+gboolean itdb_track_set_thumbnails_from_data (Itdb_Track *track,
+ const guchar *image_data,
+ gsize image_data_len)
+{
+ g_return_val_if_fail (track, FALSE);
+ g_return_val_if_fail (image_data, FALSE);
+
+ return itdb_track_set_thumbnails_internal (track, NULL,
+ image_data, image_data_len);
+}
+
+
/**
* itdb_track_remove_thumbnails:
* @track: an #Itdb_Track
diff --git a/src/ithumb-writer.c b/src/ithumb-writer.c
index 2aad604..98f350c 100644
--- a/src/ithumb-writer.c
+++ b/src/ithumb-writer.c
@@ -195,9 +195,9 @@ static gboolean
ithumb_writer_write_thumbnail (iThumbWriter *writer,
Itdb_Thumb *thumb)
{
- GdkPixbuf *pixbuf;
+ GdkPixbuf *pixbuf = NULL;
guint16 *pixels;
- gchar *filename;
+ gchar *filename = NULL;
gint width, height;
Itdb_Thumb *old_thumb;
@@ -205,24 +205,48 @@ ithumb_writer_write_thumbnail (iThumbWriter *writer,
g_return_val_if_fail (writer, FALSE);
g_return_val_if_fail (thumb, FALSE);
- /* If the same filename was written before, just use the old
- thumbnail to save space on the iPod */
- old_thumb = g_hash_table_lookup (writer->cache, thumb->filename);
- if (old_thumb != NULL)
- {
- g_free (thumb->filename);
- memcpy (thumb, old_thumb, sizeof (Itdb_Thumb));
- thumb->filename = g_strdup (old_thumb->filename);
- return TRUE;
- }
+ if (thumb->filename)
+ { /* read image from filename */
+ /* If the same filename was written before, just use the old
+ thumbnail to save space on the iPod */
+ old_thumb = g_hash_table_lookup (writer->cache, thumb->filename);
+ if (old_thumb != NULL)
+ {
+ g_free (thumb->filename);
+ memcpy (thumb, old_thumb, sizeof (Itdb_Thumb));
+ thumb->filename = g_strdup (old_thumb->filename);
+ return TRUE;
+ }
- filename = g_strdup (thumb->filename);
+ pixbuf = gdk_pixbuf_new_from_file_at_size (thumb->filename,
+ writer->img_info->width,
+ writer->img_info->height,
+ NULL);
- pixbuf = gdk_pixbuf_new_from_file_at_size (filename,
- writer->img_info->width,
- writer->img_info->height,
- NULL);
- if (pixbuf == NULL) {
+ }
+ else if (thumb->image_data)
+ { /* image data is stored in image_data and image_data_len */
+ GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
+ g_return_val_if_fail (loader, FALSE);
+ gdk_pixbuf_loader_write (loader,
+ thumb->image_data,
+ thumb->image_data_len,
+ NULL);
+ gdk_pixbuf_loader_close (loader, NULL);
+ gdk_pixbuf_loader_set_size (loader,
+ writer->img_info->width,
+ writer->img_info->height);
+ pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+ g_object_ref (pixbuf);
+ g_object_unref (loader);
+
+ g_free (thumb->image_data);
+ thumb->image_data = NULL;
+ thumb->image_data_len = 0;
+ }
+
+ if (pixbuf == NULL)
+ {
return FALSE;
}
@@ -234,6 +258,9 @@ ithumb_writer_write_thumbnail (iThumbWriter *writer,
"height", &height,
NULL);
+ filename = thumb->filename;
+ thumb->filename = NULL;
+
/* FIXME: under certain conditions (probably related to
* writer->offset getting too big), this should be :F%04u_2.ithmb
* and so on
@@ -291,10 +318,14 @@ ithumb_writer_write_thumbnail (iThumbWriter *writer,
}
g_free (pixels);
writer->cur_offset += thumb->size;
- g_hash_table_insert (writer->cache, filename, thumb);
- /* !! filename is g_free()d when destroying the hash table. Do not
- do it here */
+ if (filename)
+ {
+ g_hash_table_insert (writer->cache, filename, thumb);
+
+ /* !! filename is g_free()d when destroying the hash table. Do not
+ do it here */
+ }
return TRUE;
}