static void set_album_image (RBMtpThread *thread, RBMtpThreadTask *task) { LIBMTP_filesampledata_t *albumart; LIBMTP_album_t *album; GError *error = NULL; char *image_data; gsize image_size; int ret; album = g_hash_table_lookup (thread->albums, task->album); if (album == NULL) { rb_debug ("Couldn't find an album for %s", task->album); return; } /* probably should scale the image down, since some devices have a size limit and they all have * tiny displays anyway. */ if (gdk_pixbuf_save_to_buffer (task->image, &image_data, &image_size, "jpeg", &error, NULL) == FALSE) { rb_debug ("unable to convert album art image to a JPEG buffer: %s", error->message); g_error_free (error); return; } albumart = LIBMTP_new_filesampledata_t (); albumart->filetype = LIBMTP_FILETYPE_JPEG; albumart->data = image_data; albumart->size = image_size; ret = LIBMTP_Send_Representative_Sample (thread->device, album->album_id, albumart); if (ret != 0) { rb_mtp_thread_report_errors (thread, TRUE); } else { rb_debug ("successfully set album art for %s (%" G_GSIZE_FORMAT " bytes)", task->album, image_size); } /* libmtp will try to free this if we don't clear the pointer */ albumart->data = NULL; LIBMTP_destroy_filesampledata_t (albumart); }
int main (int argc, char **argv) { int opt; extern int optind; extern char *optarg; LIBMTP_mtpdevice_t *device = NULL; int fd; uint32_t id = 0; uint64_t filesize; uint8_t *imagedata = NULL; char *path = NULL; char *rest; struct stat statbuff; int ret; fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); while ( (opt = getopt(argc, argv, "hi:")) != -1 ) { switch (opt) { case 'h': usage(); case 'i': id = strtoul(optarg, &rest, 0); break; default: usage(); } } argc -= optind; argv += optind; if ( argc != 1 ) { printf("You need to pass a filename.\n"); usage(); } path = argv[0]; if ( stat(path, &statbuff) == -1 ) { fprintf(stderr, "%s: ", path); perror("stat"); exit(1); } filesize = (uint64_t) statbuff.st_size; imagedata = malloc(filesize * sizeof(uint16_t)); #ifdef __WIN32__ if ( (fd = open(path, O_RDONLY|O_BINARY) == -1) ) { #else if ( (fd = open(path, O_RDONLY)) == -1) { #endif printf("Couldn't open image file %s (%s)\n",path,strerror(errno)); return 1; } else { read(fd, imagedata, filesize); close(fd); } LIBMTP_Init(); device = LIBMTP_Get_First_Device(); if (device == NULL) { printf("No devices.\n"); return 0; } LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t(); int i; thumb->data = malloc(sizeof(uint16_t) * filesize); for (i = 0; i < filesize; i++) { thumb->data[i] = imagedata[i]; } thumb->size = filesize; thumb->filetype = LIBMTP_FILETYPE_JPEG; ret = LIBMTP_Send_Representative_Sample(device,id,thumb); if (ret != 0) { printf("Couldn't send thumbnail\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } free(imagedata); LIBMTP_destroy_filesampledata_t(thumb); LIBMTP_Release_Device(device); printf("OK.\n"); return 0; }
static void artwork_notify_cb (RhythmDB *db, RhythmDBEntry *entry, const char *property_name, const GValue *metadata, RBMtpSource *source) { RBMtpSourcePrivate *priv = MTP_SOURCE_GET_PRIVATE (source); GdkPixbuf *pixbuf; LIBMTP_album_t *album; LIBMTP_filesampledata_t *albumart; GError *error = NULL; const char *album_name; char *image_data; gsize image_size; int ret; album_name = rhythmdb_entry_get_string (entry, RHYTHMDB_PROP_ALBUM); /* check if we're looking for art for this entry, and if we actually got some */ if (g_hash_table_remove (priv->artwork_request_map, album_name) == FALSE) return; if (G_VALUE_HOLDS (metadata, GDK_TYPE_PIXBUF) == FALSE) return; pixbuf = GDK_PIXBUF (g_value_get_object (metadata)); /* we should already have created an album object */ album = g_hash_table_lookup (priv->album_map, album_name); if (album == NULL) { rb_debug ("couldn't find an album for %s", album_name); return; } /* probably should scale the image down, since some devices have a size limit and they all have * tiny displays anyway. */ if (gdk_pixbuf_save_to_buffer (pixbuf, &image_data, &image_size, "jpeg", &error, NULL) == FALSE) { rb_debug ("unable to convert album art image to a JPEG buffer: %s", error->message); g_error_free (error); return; } albumart = LIBMTP_new_filesampledata_t (); albumart->filetype = LIBMTP_FILETYPE_JPEG; albumart->data = image_data; albumart->size = image_size; ret = LIBMTP_Send_Representative_Sample (priv->device, album->album_id, albumart); if (ret != 0) { report_libmtp_errors (priv->device, TRUE); } else { rb_debug ("successfully set album art for %s (%" G_GSIZE_FORMAT " bytes)", album_name, image_size); } /* libmtp will try to free this if we don't clear the pointer */ albumart->data = NULL; LIBMTP_destroy_filesampledata_t (albumart); g_free (image_data); }