Update of /cvsroot/gtkpod/libgpod/src
In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv26715/src

Modified Files:
        itdb.h itdb_artwork.c itdb_photoalbum.c itdb_track.c 
        ithumb-writer.c 
Log Message:
        * 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.



Index: itdb.h
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/src/itdb.h,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -d -r1.40 -r1.41
--- itdb.h      17 Sep 2006 04:48:30 -0000      1.40
+++ itdb.h      17 Sep 2006 17:14:21 -0000      1.41
@@ -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 @@
 /* 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 @@
 
      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 @@
 /* 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 @@
 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 @@
 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);

Index: itdb_artwork.c
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/src/itdb_artwork.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- itdb_artwork.c      4 Jun 2006 16:24:44 -0000       1.13
+++ itdb_artwork.c      17 Sep 2006 17:14:21 -0000      1.14
@@ -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 @@
 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_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 @@
            }
        }
 
-       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 @@
 {
     g_return_if_fail (thumb);
 
+    g_free (thumb->image_data);
     g_free (thumb->filename);
     g_free (thumb);
 }
@@ -623,5 +692,12 @@
     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;
 }

Index: itdb_photoalbum.c
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/src/itdb_photoalbum.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- itdb_photoalbum.c   7 Jun 2006 15:41:50 -0000       1.4
+++ itdb_photoalbum.c   17 Sep 2006 17:14:21 -0000      1.5
@@ -254,9 +254,11 @@
     }
 }
 
-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 @@
        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 @@
        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 )
 {

Index: itdb_track.c
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/src/itdb_track.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- itdb_track.c        4 Jun 2006 16:24:45 -0000       1.20
+++ itdb_track.c        17 Sep 2006 17:14:21 -0000      1.21
@@ -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_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 @@
     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

Index: ithumb-writer.c
===================================================================
RCS file: /cvsroot/gtkpod/libgpod/src/ithumb-writer.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- ithumb-writer.c     4 Jun 2006 16:24:45 -0000       1.18
+++ ithumb-writer.c     17 Sep 2006 17:14:21 -0000      1.19
@@ -195,9 +195,9 @@
 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 @@
     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;
+       }
+
+       pixbuf = gdk_pixbuf_new_from_file_at_size (thumb->filename, 
+                                                  writer->img_info->width, 
+                                                  writer->img_info->height,
+                                                  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);
 
-    filename = g_strdup (thumb->filename);
+       g_free (thumb->image_data);
+       thumb->image_data = NULL;
+       thumb->image_data_len = 0;
+    }
 
-    pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 
-                                              writer->img_info->width, 
-                                              writer->img_info->height,
-                                              NULL);
-    if (pixbuf == NULL) {
+    if (pixbuf == NULL)
+    {
        return FALSE;
     }
 
@@ -234,6 +258,9 @@
                  "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 @@
     }
     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;
 }


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to