Linus Walleij | 3d78c4c | 2007-02-15 12:18:12 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file thumb.c |
| 3 | * Example program to send and associate album art to an entity |
| 4 | * on a device. |
| 5 | * |
| 6 | * Copyright (C) 2006 Robert Reardon <rreardon@monkshatch.vispa.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the |
| 20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | * Boston, MA 02111-1307, USA. |
| 22 | */ |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 23 | #include "common.h" |
| 24 | #include "string.h" |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | static void usage(void) { |
| 30 | printf("Usage: thumb -i <fileid/trackid> <imagefile>\n"); |
| 31 | exit(0); |
| 32 | } |
| 33 | |
| 34 | int main (int argc, char **argv) { |
| 35 | int opt; |
| 36 | extern int optind; |
| 37 | extern char *optarg; |
| 38 | LIBMTP_mtpdevice_t *device = NULL; |
| 39 | int fd; |
| 40 | uint32_t id = 0; |
| 41 | uint64_t filesize; |
| 42 | uint8_t *imagedata = NULL; |
| 43 | char *path = NULL; |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame^] | 44 | char *rest; |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 45 | struct stat statbuff; |
| 46 | int ret; |
| 47 | |
tedbullock | cd9f499 | 2007-03-29 06:00:40 +0000 | [diff] [blame] | 48 | fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n"); |
| 49 | |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 50 | while ( (opt = getopt(argc, argv, "hi:")) != -1 ) { |
| 51 | switch (opt) { |
| 52 | case 'h': |
| 53 | usage(); |
| 54 | case 'i': |
Linus Walleij | 20c3b67 | 2007-11-09 19:02:19 +0000 | [diff] [blame^] | 55 | id = strtoul(optarg, &rest, 0); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 56 | break; |
| 57 | default: |
| 58 | usage(); |
| 59 | } |
| 60 | } |
| 61 | argc -= optind; |
| 62 | argv += optind; |
| 63 | |
| 64 | if ( argc != 1 ) { |
| 65 | printf("You need to pass a filename.\n"); |
| 66 | usage(); |
| 67 | } |
| 68 | |
| 69 | path = argv[0]; |
| 70 | |
| 71 | if ( stat(path, &statbuff) == -1 ) { |
| 72 | fprintf(stderr, "%s: ", path); |
| 73 | perror("stat"); |
| 74 | exit(1); |
| 75 | } |
| 76 | filesize = (uint64_t) statbuff.st_size; |
| 77 | imagedata = malloc(filesize * sizeof(uint16_t)); |
| 78 | |
| 79 | #ifdef __WIN32__ |
| 80 | if ( (fd = open(path, O_RDONLY|O_BINARY) == -1 ) { |
| 81 | #else |
| 82 | if ( (fd = open(path, O_RDONLY)) == -1) { |
| 83 | #endif |
| 84 | printf("Couldn't open image file %s (%s)\n",path,strerror(errno)); |
| 85 | return 1; |
| 86 | } |
| 87 | else { |
| 88 | read(fd, imagedata, filesize); |
| 89 | close(fd); |
| 90 | } |
| 91 | |
| 92 | LIBMTP_Init(); |
| 93 | device = LIBMTP_Get_First_Device(); |
| 94 | if (device == NULL) { |
| 95 | printf("No devices.\n"); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | LIBMTP_filesampledata_t *thumb = LIBMTP_new_filesampledata_t(); |
| 100 | |
| 101 | int i; |
| 102 | thumb->data = malloc(sizeof(uint16_t) * filesize); |
| 103 | for (i = 0; i < filesize; i++) { |
| 104 | thumb->data[i] = imagedata[i]; |
| 105 | } |
| 106 | |
| 107 | thumb->size = filesize; |
| 108 | thumb->filetype = LIBMTP_FILETYPE_JPEG; |
| 109 | |
| 110 | ret = LIBMTP_Send_Representative_Sample(device,id,thumb); |
| 111 | if (ret != 0) { |
| 112 | printf("Couldn't send thumbnail\n"); |
Linus Walleij | 070e9b4 | 2007-01-22 08:49:28 +0000 | [diff] [blame] | 113 | LIBMTP_Dump_Errorstack(device); |
| 114 | LIBMTP_Clear_Errorstack(device); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | free(imagedata); |
Linus Walleij | f1b02f2 | 2006-12-06 09:03:23 +0000 | [diff] [blame] | 118 | LIBMTP_destroy_filesampledata_t(thumb); |
rreardon | 5332f9c | 2006-12-05 10:18:23 +0000 | [diff] [blame] | 119 | |
| 120 | LIBMTP_Release_Device(device); |
| 121 | printf("OK.\n"); |
| 122 | return 0; |
| 123 | } |